Class: RJGit::Repo

Inherits:
Object
  • Object
show all
Defined in:
lib/repo.rb

Constant Summary collapse

PACK_LIST =
'objects/info/packs'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ Repo

Returns a new instance of Repo.


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/repo.rb', line 43

def initialize(path, options = {})
  epath = File.expand_path(path)
  if options[:git_dir]
    if (Pathname.new options[:git_dir]).absolute?
      gitpath = options[:git_dir]
    else
      gitpath = File.expand_path(options[:git_dir])
    end
  else
    gitpath = File.join(epath, '.git')
  end
  
  # Default value for bare
  bare = false
  # If the repo path is new
  unless File.exists?(epath) 
    # take user setting if defined
    bare = !! options[:is_bare] unless options[:is_bare].nil?
  # If the repo path exists
  else
    # scan the directory for a .git directory
    bare = File.exists?(gitpath) ? false : true
    # but allow overriding user setting
    bare = !! options[:is_bare] unless options[:is_bare].nil? 
  end
  
  @path = bare ? epath : gitpath
  @config = RJGit::Configuration.new(File.join(@path, 'config'))
  repo_path = java.io.File.new(@path)
  @jrepo = bare ? RepositoryBuilder.new().set_bare.set_git_dir(repo_path).build() : RepositoryBuilder.new().set_git_dir(repo_path).set_work_tree(java.io.File.new(epath)).build()
  @jrepo.create(bare) if options[:create]
  @git = RubyGit.new(@jrepo)
end

Instance Attribute Details

#gitObject

Returns the value of attribute git


35
36
37
# File 'lib/repo.rb', line 35

def git
  @git
end

#jrepoObject

Returns the value of attribute jrepo


36
37
38
# File 'lib/repo.rb', line 36

def jrepo
  @jrepo
end

#pathObject

Returns the value of attribute path


37
38
39
# File 'lib/repo.rb', line 37

def path
  @path
end

Class Method Details

.create(path, options = {:is_bare => false}) ⇒ Object


82
83
84
85
# File 'lib/repo.rb', line 82

def self.create(path, options = {:is_bare => false})
  options[:create] = true
  Repo.new(path, options)
end

.new_from_jgit_repo(jrepo) ⇒ Object


91
92
93
94
# File 'lib/repo.rb', line 91

def self.new_from_jgit_repo(jrepo)
  path = File.dirname(jrepo.get_directory.get_path)
  Repo.new(path, {:is_bare => jrepo.is_bare})
end

Instance Method Details

#add(file_pattern) ⇒ Object


158
159
160
# File 'lib/repo.rb', line 158

def add(file_pattern)
  @git.add(file_pattern)
end

#bare?Boolean Also known as: bare

Returns:

  • (Boolean)

77
78
79
# File 'lib/repo.rb', line 77

def bare?
  @jrepo.is_bare
end

#blob(file_path, revstring = Constants::HEAD) ⇒ Object

Convenience method to retrieve a Blob by name


211
212
213
# File 'lib/repo.rb', line 211

def blob(file_path, revstring=Constants::HEAD)
  Blob.find_blob(@jrepo, file_path, revstring)
end

#branchObject


113
114
115
# File 'lib/repo.rb', line 113

def branch
  @jrepo.get_full_branch
end

#branchesObject


117
118
119
# File 'lib/repo.rb', line 117

def branches
  return @git.branch_list
end

#checkout(branch_name, options = {}) ⇒ Object Also known as: switch


133
134
135
# File 'lib/repo.rb', line 133

def checkout(branch_name, options = {})
  @git.checkout(branch_name, options)
end

#clean(options = {}) ⇒ Object


170
171
172
# File 'lib/repo.rb', line 170

def clean(options = {})
  @git.clean(options)
end

#commit(message, options = {}) ⇒ Object


166
167
168
# File 'lib/repo.rb', line 166

def commit(message, options = {})
  @git.commit(message, options)
end

#commits(ref = "master", limit = 100) ⇒ Object


96
97
98
99
# File 'lib/repo.rb', line 96

def commits(ref="master", limit=100)
  options = { :limit => limit }
  Commit.find_all(@jrepo, ref, options)
end

#configObject


109
110
111
# File 'lib/repo.rb', line 109

def config
  @config.load
end

#create!Object


87
88
89
# File 'lib/repo.rb', line 87

def create!
  @jrepo.create(self.bare?)
end

#create_branch(name) ⇒ Object


121
122
123
# File 'lib/repo.rb', line 121

def create_branch(name)
  @git.create_branch(name)
end

#delete_branch(name) ⇒ Object


125
126
127
# File 'lib/repo.rb', line 125

def delete_branch(name)
  @git.delete_branch(name)
end

#find(sha, type = :any) ⇒ Object


174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/repo.rb', line 174

def find(sha, type = :any)
    begin
      oi = ObjectId.from_string(sha)
      walk = RevWalk.new(@jrepo)
      rev_object = case type
      when :any
        walk.parse_any(oi)
      when :tree
        walk.parse_tree(oi)
      when :blob
        walk.parse_any(oi)
      when :tag
        walk.parse_tag(oi)
      when :commit
        walk.parse_commit(oi)
      else nil
      end
    rescue Java::OrgEclipseJgitErrors::MissingObjectException, Java::JavaLang::IllegalArgumentException, Java::JavaLang::NullPointerException
      return nil
    end
  return nil if rev_object.nil?
  object_type = (type == :any || type == :blob) ? RJGit.sym_for_type(rev_object.get_type) : type
  return nil if type == :blob && object_type != :blob # Blobs need to be found with parse_any, so make sure that the result of this is actually a blob.
    return case object_type
    when :tree
      Tree.new(@jrepo, nil, nil, rev_object)
    when :blob
      mode = RJGit.get_file_mode(@jrepo, rev_object)
      Blob.new(@jrepo, mode, nil, rev_object)
    when :tag
      Tag.new(rev_object)
    when :commit
      Commit.new(jrepo, rev_object)
    end
end

#headObject


101
102
103
# File 'lib/repo.rb', line 101

def head
  Commit.find_head(@jrepo)
end

#remove(file_pattern) ⇒ Object


162
163
164
# File 'lib/repo.rb', line 162

def remove(file_pattern)
  @git.remove(file_pattern)
end

#rename_branch(old_name, new_name) ⇒ Object


129
130
131
# File 'lib/repo.rb', line 129

def rename_branch(old_name, new_name)
  @git.rename_branch(old_name, new_name)
end

#tags(lightweight = false) ⇒ Object


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/repo.rb', line 139

def tags(lightweight = false)
  jtags = @jrepo.get_tags.to_hash
  if lightweight
    jtags.each_with_object( Hash.new ) do |(key, value), hash| 
      hash[key] = ObjectId.to_string(value.get_object_id)
    end
  else
    tags = Hash.new
    jtags.each do |key, value|
      jtag = @git.resolve_tag(value)
      if jtag
        tag = Tag.new(jtag)
        tags[key] = tag 
      end
    end
    tags
  end
end

#tree(file_path, revstring = Constants::HEAD) ⇒ Object

Convenience method to retrieve a Tree by name


216
217
218
# File 'lib/repo.rb', line 216

def tree(file_path, revstring=Constants::HEAD)
  Tree.find_tree(@jrepo, file_path, revstring)
end

#update_ref(commit, force = false, ref = "refs/heads/#{Constants::MASTER}") ⇒ Object


220
221
222
223
224
225
# File 'lib/repo.rb', line 220

def update_ref(commit, force = false, ref = "refs/heads/#{Constants::MASTER}")
  ref_updater = @jrepo.updateRef(ref)
  ref_updater.setForceUpdate(force)
  ref_updater.setNewObjectId(RJGit.commit_type(commit))
  ref_updater.update.to_string
end

#update_server_infoObject

Update the info files required for fetching files over the dumb-HTTP protocol


228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/repo.rb', line 228

def update_server_info
  # First update the $GIT_DIR/refs/info file
  refs = @jrepo.get_all_refs # Note: JGit will include directories under $GIT_DIR/refs/heads that start with a '.' in its search for refs. Filter these out in LocalRefWriter?
  writer = LocalRefWriter.new(refs, @path)
  writer.write_info_refs
  
  # Now update the $GIT_OBJECT_DIRECTORY/info/packs file
  f = File.new(File.join(@path, PACK_LIST), "w")
  @jrepo.get_object_database.get_packs.each do |pack|
    f.write "P " + pack.get_pack_file.get_name + "\n"
  end
  f.write "\n"
  f.close
  
  return true
end

#valid?Boolean

Returns:

  • (Boolean)

105
106
107
# File 'lib/repo.rb', line 105

def valid?
  @jrepo.get_object_database.exists
end