Class: Rosette::Core::Repo

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

Overview

Wraps a git repository and can perform operations on it via jgit. NOTE: This class is NOT thread safe.

Examples:

repo = Repo.from_path('/path/to/my_repo/.git')
repo.get_rev_commit('master')  # => <RevCommit #5234ab6>

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jgit_repo) ⇒ Repo

Creates a new repo instance from the given jgit repo object.

Parameters:

  • jgit_repo (Java::OrgEclipseJgitStorageFile::FileRepository)

    The jgit repo object to wrap.


47
48
49
# File 'lib/rosette/core/git/repo.rb', line 47

def initialize(jgit_repo)
  @jgit_repo = jgit_repo
end

Instance Attribute Details

#jgit_repoJava::OrgEclipseJgitStorageFile::FileRepository (readonly)

Returns The jgit repository object.

Returns:

  • (Java::OrgEclipseJgitStorageFile::FileRepository)

    The jgit repository object.


31
32
33
34
35
36
37
38
39
40
41
42
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/rosette/core/git/repo.rb', line 31

class Repo
  attr_reader :jgit_repo

  # Creates a repo instance from the given path.
  #
  # @param [String] path The path to the .git directory of a git
  #   repository.
  # @return [Repo] The new repo instance.
  def self.from_path(path)
    new(FileRepository.new(path))
  end

  # Creates a new repo instance from the given jgit repo object.
  #
  # @param [Java::OrgEclipseJgitStorageFile::FileRepository] jgit_repo
  #   The jgit repo object to wrap.
  def initialize(jgit_repo)
    @jgit_repo = jgit_repo
  end

  # Retrieves a jgit commit object for the given ref or commit id. If
  # +ref_or_commit_id+ cannot be directly looked up as a commit id or ref,
  # heads are searched as a fallback. If no head ref can be found, remotes
  # are searched as a secondary fallback. If no remote can be found, an
  # error is raised.
  #
  # @param [String] ref_or_commit_id The git ref (i.e. a branch name) or
  #   git commit id of the commit to retrieve.
  # @param [Java::OrgEclipseJgitRevwalk::RevWalk] walker The +RevWalk+ to
  #   use. Since +RevCommits+ returned from different +RevWalk+s aren't
  #   equivalent, callers may want to pass in an instance of their own.
  #   By default, an internally created +RevWalk+ is used.
  # @return [Java::OrgEclipseJgitRevwalk::RevCommit] The identified
  #   +RevCommit+.
  def get_rev_commit(ref_or_commit_id, walker = rev_walker)
    walker.parseCommit(
      ObjectId.fromString(ref_or_commit_id)
    )
  rescue Java::OrgEclipseJgitErrors::MissingObjectException, Java::JavaLang::IllegalArgumentException => e
    found_ref = get_ref(ref_or_commit_id)

    unless found_ref
      [Constants::R_HEADS, Constants::R_REMOTES + 'origin/'].each do |prefix|
        ref_candidates = jgit_repo.getRefDatabase.getRefs(prefix)

        found_ref_name, found_ref = ref_candidates.find do |ref_name, ref|
          ref_name == ref_or_commit_id
        end

        break if found_ref
      end
    end

    if found_ref
      walker.parseCommit(found_ref.getObjectId)
    else
      raise e
    end
  end

  # Calculates a diff between two git refs or commit ids. Returns a hash of
  # commit ids to +DiffEntry+ objects.
  #
  # @see DiffFinder
  # @param [String] ref_parent The parent git ref or commit id.
  # @param [String] ref_child The child git ref or commit id.
  # @param [Array<String>] paths The paths to include in the diff. If an
  #   empty array is given, returns a diff for all paths.
  # @return [Hash<String, Java::OrgEclipseJgitDiff::DiffEntry>]
  def diff(ref_parents, ref_child, paths = [], finder = diff_finder)
    rev_child = get_rev_commit(ref_child, finder.rev_walker)

    rev_parents = Array(ref_parents).map do |ref_parent|
      get_rev_commit(ref_parent, finder.rev_walker)
    end

    finder.diff(rev_parents, rev_child, paths)
  end

  # Calculates a diff for the given ref against its parent.
  #
  # @param [String] ref The ref to diff with.
  # @param [DiffFinder] finder The diff finder to use when calculating
  #   diffs in a multi-threaded environment
  # @return [Array<Java::OrgEclipseJgitDiff::DiffEntry>]
  def ref_diff_with_parents(ref, finder = diff_finder)
    rev_diff_with_parents(get_rev_commit(ref), finder)
  end

  # Calculates a diff for the given rev against its parent.
  #
  # @param [Java::OrgEclipseJgitRevwalk::RevCommit] rev The rev to diff with.
  # @param [DiffFinder] finder The diff finder to use when calculating
  #   diffs in a multi-threaded environment.
  # @return [Array<Java::OrgEclipseJgitDiff::DiffEntry>]
  def rev_diff_with_parents(rev, finder = diff_finder)
    finder.diff_with_parents(rev)
  end

  # Retrieves the parent commits for the given rev.
  #
  # @param [Java::OrgEclipseJgitRevwalk::RevCommit] rev The rev to get
  #   parents for.
  # @return [Array<Java::OrgEclipseJgitRevwalk::RevCommit>] A list of
  #   parent commits.
  def parents_of(rev)
    rev.getParentCount.times.map do |i|
      rev.getParent(i)
    end
  end

  # Retrieves the parent commit ids for the given rev.
  #
  # @param [Java::OrgEclipseJgitRevwalk::RevCommit] rev The rev to get
  #   parent ids for.
  # @return [Array<Java::OrgEclipseJgitLib::ObjectId>] An array of object
  #   id instances (the commit ids for the parents of +rev+).
  def parent_ids_of(rev)
    parents_of(rev).map { |parent| parent.getName }
  end

  # Retrieves the path for the repository's working directory.
  #
  # @return [String] The path to the working directory.
  def path
    jgit_repo.workTree.path
  end

  # Reads the git entry for the given object id and returns the bytes.
  #
  # @param [Java::OrgEclipseJgitLib::ObjectId] object_id The object id
  #   to retrieve bytes for.
  # @return [Array<Fixnum>] An array of bytes.
  def read_object_bytes(object_id)
    object_reader.open(object_id).getBytes
  end

  # Iterates over and yields each commit in the repo.
  #
  # @return [void, Enumerator] If no block is given, returns an
  #   +Enumerator+.
  # @yield [rev]
  # @yieldparam rev [Java::OrgEclipseJgitRevwalk::RevCommit]
  def each_commit
    if block_given?
      commit_walker = RevWalk.new(jgit_repo).tap do |walker|
        walker.markStart(all_heads(walker))
        walker.sort(RevSort::REVERSE)
      end

      commit_walker.each { |cur_rev| yield cur_rev }
      commit_walker.dispose
    else
      to_enum(__method__)
    end
  end

  # Iterates over and yields each commit, starting at the given git ref
  # or commit id.
  #
  # @param [String] start_ref The ref to start at.
  # @return [void, Enumerator] If no block is given, returns an
  #   +Enumerator+.
  def each_commit_starting_at(start_ref)
    if block_given?
      commit_walker = RevWalk.new(jgit_repo).tap do |walker|
        walker.markStart(get_rev_commit(start_ref, walker))
      end

      commit_walker.each { |cur_rev| yield cur_rev }
      commit_walker.dispose
    else
      to_enum(__method__, start_ref)
    end
  end

  # Iterates over and yields each commit, starting at the given git ref
  # or commit id and ending at the other.
  #
  # @param [String] start_ref The beginning of the commit range.
  # @param [String] end_ref The end of the commit range (inclusive).
  # @return [void, Enumerator] If no block is given, returns an
  #   +Enumerator+.
  def each_commit_in_range(start_ref, end_ref)
    if block_given?
      commit_walker = RevWalk.new(jgit_repo).tap do |walker|
        walker.markStart(get_rev_commit(start_ref, walker))
      end

      end_rev = get_rev_commit(end_ref, commit_walker)

      commit_walker.each do |cur_rev|
        yield cur_rev
        break if cur_rev.getId.name == end_rev.getId.name
      end

      commit_walker.dispose
    else
      to_enum(__method__, start_ref, end_ref)
    end
  end

  # Counts the number of commits in the repo.
  #
  # @return [Fixnum] The number of commits in the repo.
  def commit_count
    commit_walker = RevWalk.new(jgit_repo).tap do |walker|
      walker.markStart(all_heads(walker))
    end

    count = commit_walker.count
    commit_walker.dispose
    count
  end

  # Fetches the repository.
  #
  # @param [String] remote The remote to fetch from.
  # @return [void]
  def fetch(remote = 'origin')
    git.fetch.setRemote(remote).call
  end

  # Clones a repository
  #
  # @param [String] repo_uri The URI of the repo to be cloned.
  # @param [String] repo_dir The directory to store the local copy.
  # @return [void]
  def self.clone(repo_uri, repo_dir)
    CloneCommand.new
      .setDirectory(Java::JavaIo::File.new(repo_dir))
      .setURI(repo_uri)
      .call
  end

  # Retrieves the first non-merge parent of the given ref or commit id.
  #
  # @param [String] ref The git ref or commit id.
  # @return [Java::OrgEclipseJgitRevwalk::RevCommit] The first non-merge
  #   parent of +ref+.
  def find_first_non_merge_parent(ref)
    each_commit_starting_at(ref).with_index do |prev_rev, idx|
      next if idx == 0
      break prev_rev if prev_rev.getParentCount <= 1
    end
  end

  # Finds git authors per source line for the given file and commit.
  #
  # @param [String] path The file path.
  # @param [String] commit_id The commit id.
  # @return [Hash<Fixnum, Java::OrgEclipseJgitLib::PersonIdent>]
  #   A hash of line numbers to git authors.
  def blame(path, commit_id)
    blame_result = BlameCommand.new(jgit_repo)
      .setFilePath(path)
      .setFollowFileRenames(true)
      .setTextComparator(RawTextComparator::WS_IGNORE_ALL)
      .setStartCommit(ObjectId.fromString(commit_id))
      .call

    lines_to_authors = {}
    line_number = 0

    loop do
      begin
        lines_to_authors[line_number] = blame_result.getSourceAuthor(line_number)
        line_number += 1
      rescue Java::JavaLang::ArrayIndexOutOfBoundsException
        break
      end
    end

    lines_to_authors
  end

  # Gets a reference to the given git ref.
  #
  # @param [String] ref_str The ref to get.
  # @return [Java::OrgEclipseJgitLib::Ref] A reference to the commit found
  #   for +ref_str+.
  def get_ref(ref_str)
    jgit_repo.getRef(ref_str)
  end

  # Get all refs in the repo.
  #
  # @return [Hash<String, Java::OrgEclipseJgitLib::Ref>] A hash of ref
  #   strings to jgit ref objects.
  def all_refs
    jgit_repo.all_refs
  end

  # Get all head refs in the repo.
  #
  # @return [Array<String>] A list of all head refs.
  def all_head_refs
    all_refs = jgit_repo.refDatabase.getRefs(RefDatabase::ALL).keys
    all_refs.select do |ref|
      ref =~ /\Arefs\/(?:heads|remotes)/
    end
  end

  # Get a list of commits for all the heads in the repo.
  #
  # @param [Java::OrgEclipseJgitRevwalk::RevWalk] walker The walker to use.
  # @return [Array<Java::OrgEclipseJgitRevwalk::RevCommit>] A list of
  #   commits, one for each of the heads in the repo.
  def all_heads(walker = rev_walker)
    all_head_refs.map { |ref| get_rev_commit(ref, walker) }
  end

  # Get a list of refs (i.e. branches) that contain the given commit id.
  #
  # @param [String] commit_id_or_ref The git commit id or ref to get refs
  #   for. This method returns all the refs that contain this commit.
  # @param [Java::OrgEclipseJgitRevwalk::RevWalk] walker The walker to use.
  # @return [Array<Java::OrgEclipseJgitLib::Ref>] The list of refs that
  #   contain +commit_id_or_ref+.
  def refs_containing(commit_id_or_ref, walker = rev_walker, refs = nil)
    refs ||= jgit_repo.refDatabase.getRefs(RefDatabase::ALL).values
    commit = get_rev_commit(commit_id_or_ref, walker)
    RevWalkUtils.findBranchesReachableFrom(commit, walker, refs)
  rescue Java::OrgEclipseJgitErrors::MissingObjectException
    []
  end

  private

  def git
    @git ||= Git.new(jgit_repo)
  end

  def diff_finder
    @diff_finder ||= DiffFinder.new(jgit_repo, rev_walker)
  end

  def rev_walker
    @rev_walker ||= RevWalk.new(jgit_repo)
  end

  def object_reader
    @object_reader ||= jgit_repo.newObjectReader
  end
end

Class Method Details

.clone(repo_uri, repo_dir) ⇒ void

This method returns an undefined value.

Clones a repository

Parameters:

  • repo_uri (String)

    The URI of the repo to be cloned.

  • repo_dir (String)

    The directory to store the local copy.


259
260
261
262
263
264
# File 'lib/rosette/core/git/repo.rb', line 259

def self.clone(repo_uri, repo_dir)
  CloneCommand.new
    .setDirectory(Java::JavaIo::File.new(repo_dir))
    .setURI(repo_uri)
    .call
end

.from_path(path) ⇒ Repo

Creates a repo instance from the given path.

Parameters:

  • path (String)

    The path to the .git directory of a git repository.

Returns:

  • (Repo)

    The new repo instance.


39
40
41
# File 'lib/rosette/core/git/repo.rb', line 39

def self.from_path(path)
  new(FileRepository.new(path))
end

Instance Method Details

#all_head_refsArray<String>

Get all head refs in the repo.

Returns:

  • (Array<String>)

    A list of all head refs.


327
328
329
330
331
332
# File 'lib/rosette/core/git/repo.rb', line 327

def all_head_refs
  all_refs = jgit_repo.refDatabase.getRefs(RefDatabase::ALL).keys
  all_refs.select do |ref|
    ref =~ /\Arefs\/(?:heads|remotes)/
  end
end

#all_heads(walker = rev_walker) ⇒ Array<Java::OrgEclipseJgitRevwalk::RevCommit>

Get a list of commits for all the heads in the repo.

Parameters:

  • walker (Java::OrgEclipseJgitRevwalk::RevWalk) (defaults to: rev_walker)

    The walker to use.

Returns:

  • (Array<Java::OrgEclipseJgitRevwalk::RevCommit>)

    A list of commits, one for each of the heads in the repo.


339
340
341
# File 'lib/rosette/core/git/repo.rb', line 339

def all_heads(walker = rev_walker)
  all_head_refs.map { |ref| get_rev_commit(ref, walker) }
end

#all_refsHash<String, Java::OrgEclipseJgitLib::Ref>

Get all refs in the repo.

Returns:

  • (Hash<String, Java::OrgEclipseJgitLib::Ref>)

    A hash of ref strings to jgit ref objects.


320
321
322
# File 'lib/rosette/core/git/repo.rb', line 320

def all_refs
  jgit_repo.all_refs
end

#blame(path, commit_id) ⇒ Hash<Fixnum, Java::OrgEclipseJgitLib::PersonIdent>

Finds git authors per source line for the given file and commit.

Parameters:

  • path (String)

    The file path.

  • commit_id (String)

    The commit id.

Returns:

  • (Hash<Fixnum, Java::OrgEclipseJgitLib::PersonIdent>)

    A hash of line numbers to git authors.


284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/rosette/core/git/repo.rb', line 284

def blame(path, commit_id)
  blame_result = BlameCommand.new(jgit_repo)
    .setFilePath(path)
    .setFollowFileRenames(true)
    .setTextComparator(RawTextComparator::WS_IGNORE_ALL)
    .setStartCommit(ObjectId.fromString(commit_id))
    .call

  lines_to_authors = {}
  line_number = 0

  loop do
    begin
      lines_to_authors[line_number] = blame_result.getSourceAuthor(line_number)
      line_number += 1
    rescue Java::JavaLang::ArrayIndexOutOfBoundsException
      break
    end
  end

  lines_to_authors
end

#commit_countFixnum

Counts the number of commits in the repo.

Returns:

  • (Fixnum)

    The number of commits in the repo.


236
237
238
239
240
241
242
243
244
# File 'lib/rosette/core/git/repo.rb', line 236

def commit_count
  commit_walker = RevWalk.new(jgit_repo).tap do |walker|
    walker.markStart(all_heads(walker))
  end

  count = commit_walker.count
  commit_walker.dispose
  count
end

#diff(ref_parents, ref_child, paths = [], finder = diff_finder) ⇒ Hash<String, Java::OrgEclipseJgitDiff::DiffEntry>

Calculates a diff between two git refs or commit ids. Returns a hash of commit ids to DiffEntry objects.

Parameters:

  • ref_parent (String)

    The parent git ref or commit id.

  • ref_child (String)

    The child git ref or commit id.

  • paths (Array<String>) (defaults to: [])

    The paths to include in the diff. If an empty array is given, returns a diff for all paths.

Returns:

  • (Hash<String, Java::OrgEclipseJgitDiff::DiffEntry>)

See Also:


100
101
102
103
104
105
106
107
108
# File 'lib/rosette/core/git/repo.rb', line 100

def diff(ref_parents, ref_child, paths = [], finder = diff_finder)
  rev_child = get_rev_commit(ref_child, finder.rev_walker)

  rev_parents = Array(ref_parents).map do |ref_parent|
    get_rev_commit(ref_parent, finder.rev_walker)
  end

  finder.diff(rev_parents, rev_child, paths)
end

#each_commit {|rev| ... } ⇒ void, Enumerator

Iterates over and yields each commit in the repo.

Yields:

  • (rev)

Yield Parameters:

  • rev (Java::OrgEclipseJgitRevwalk::RevCommit)

Returns:

  • (void, Enumerator)

    If no block is given, returns an Enumerator.


174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/rosette/core/git/repo.rb', line 174

def each_commit
  if block_given?
    commit_walker = RevWalk.new(jgit_repo).tap do |walker|
      walker.markStart(all_heads(walker))
      walker.sort(RevSort::REVERSE)
    end

    commit_walker.each { |cur_rev| yield cur_rev }
    commit_walker.dispose
  else
    to_enum(__method__)
  end
end

#each_commit_in_range(start_ref, end_ref) ⇒ void, Enumerator

Iterates over and yields each commit, starting at the given git ref or commit id and ending at the other.

Parameters:

  • start_ref (String)

    The beginning of the commit range.

  • end_ref (String)

    The end of the commit range (inclusive).

Returns:

  • (void, Enumerator)

    If no block is given, returns an Enumerator.


214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/rosette/core/git/repo.rb', line 214

def each_commit_in_range(start_ref, end_ref)
  if block_given?
    commit_walker = RevWalk.new(jgit_repo).tap do |walker|
      walker.markStart(get_rev_commit(start_ref, walker))
    end

    end_rev = get_rev_commit(end_ref, commit_walker)

    commit_walker.each do |cur_rev|
      yield cur_rev
      break if cur_rev.getId.name == end_rev.getId.name
    end

    commit_walker.dispose
  else
    to_enum(__method__, start_ref, end_ref)
  end
end

#each_commit_starting_at(start_ref) ⇒ void, Enumerator

Iterates over and yields each commit, starting at the given git ref or commit id.

Parameters:

  • start_ref (String)

    The ref to start at.

Returns:

  • (void, Enumerator)

    If no block is given, returns an Enumerator.


194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/rosette/core/git/repo.rb', line 194

def each_commit_starting_at(start_ref)
  if block_given?
    commit_walker = RevWalk.new(jgit_repo).tap do |walker|
      walker.markStart(get_rev_commit(start_ref, walker))
    end

    commit_walker.each { |cur_rev| yield cur_rev }
    commit_walker.dispose
  else
    to_enum(__method__, start_ref)
  end
end

#fetch(remote = 'origin') ⇒ void

This method returns an undefined value.

Fetches the repository.

Parameters:

  • remote (String) (defaults to: 'origin')

    The remote to fetch from.


250
251
252
# File 'lib/rosette/core/git/repo.rb', line 250

def fetch(remote = 'origin')
  git.fetch.setRemote(remote).call
end

#find_first_non_merge_parent(ref) ⇒ Java::OrgEclipseJgitRevwalk::RevCommit

Retrieves the first non-merge parent of the given ref or commit id.

Parameters:

  • ref (String)

    The git ref or commit id.

Returns:

  • (Java::OrgEclipseJgitRevwalk::RevCommit)

    The first non-merge parent of ref.


271
272
273
274
275
276
# File 'lib/rosette/core/git/repo.rb', line 271

def find_first_non_merge_parent(ref)
  each_commit_starting_at(ref).with_index do |prev_rev, idx|
    next if idx == 0
    break prev_rev if prev_rev.getParentCount <= 1
  end
end

#get_ref(ref_str) ⇒ Java::OrgEclipseJgitLib::Ref

Gets a reference to the given git ref.

Parameters:

  • ref_str (String)

    The ref to get.

Returns:

  • (Java::OrgEclipseJgitLib::Ref)

    A reference to the commit found for ref_str.


312
313
314
# File 'lib/rosette/core/git/repo.rb', line 312

def get_ref(ref_str)
  jgit_repo.getRef(ref_str)
end

#get_rev_commit(ref_or_commit_id, walker = rev_walker) ⇒ Java::OrgEclipseJgitRevwalk::RevCommit

Retrieves a jgit commit object for the given ref or commit id. If ref_or_commit_id cannot be directly looked up as a commit id or ref, heads are searched as a fallback. If no head ref can be found, remotes are searched as a secondary fallback. If no remote can be found, an error is raised.

Parameters:

  • ref_or_commit_id (String)

    The git ref (i.e. a branch name) or git commit id of the commit to retrieve.

  • walker (Java::OrgEclipseJgitRevwalk::RevWalk) (defaults to: rev_walker)

    The RevWalk to use. Since RevCommits returned from different RevWalks aren't equivalent, callers may want to pass in an instance of their own. By default, an internally created RevWalk is used.

Returns:

  • (Java::OrgEclipseJgitRevwalk::RevCommit)

    The identified RevCommit.


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rosette/core/git/repo.rb', line 65

def get_rev_commit(ref_or_commit_id, walker = rev_walker)
  walker.parseCommit(
    ObjectId.fromString(ref_or_commit_id)
  )
rescue Java::OrgEclipseJgitErrors::MissingObjectException, Java::JavaLang::IllegalArgumentException => e
  found_ref = get_ref(ref_or_commit_id)

  unless found_ref
    [Constants::R_HEADS, Constants::R_REMOTES + 'origin/'].each do |prefix|
      ref_candidates = jgit_repo.getRefDatabase.getRefs(prefix)

      found_ref_name, found_ref = ref_candidates.find do |ref_name, ref|
        ref_name == ref_or_commit_id
      end

      break if found_ref
    end
  end

  if found_ref
    walker.parseCommit(found_ref.getObjectId)
  else
    raise e
  end
end

#parent_ids_of(rev) ⇒ Array<Java::OrgEclipseJgitLib::ObjectId>

Retrieves the parent commit ids for the given rev.

Parameters:

  • rev (Java::OrgEclipseJgitRevwalk::RevCommit)

    The rev to get parent ids for.

Returns:

  • (Array<Java::OrgEclipseJgitLib::ObjectId>)

    An array of object id instances (the commit ids for the parents of rev).


148
149
150
# File 'lib/rosette/core/git/repo.rb', line 148

def parent_ids_of(rev)
  parents_of(rev).map { |parent| parent.getName }
end

#parents_of(rev) ⇒ Array<Java::OrgEclipseJgitRevwalk::RevCommit>

Retrieves the parent commits for the given rev.

Parameters:

  • rev (Java::OrgEclipseJgitRevwalk::RevCommit)

    The rev to get parents for.

Returns:

  • (Array<Java::OrgEclipseJgitRevwalk::RevCommit>)

    A list of parent commits.


136
137
138
139
140
# File 'lib/rosette/core/git/repo.rb', line 136

def parents_of(rev)
  rev.getParentCount.times.map do |i|
    rev.getParent(i)
  end
end

#pathString

Retrieves the path for the repository's working directory.

Returns:

  • (String)

    The path to the working directory.


155
156
157
# File 'lib/rosette/core/git/repo.rb', line 155

def path
  jgit_repo.workTree.path
end

#read_object_bytes(object_id) ⇒ Array<Fixnum>

Reads the git entry for the given object id and returns the bytes.

Parameters:

  • object_id (Java::OrgEclipseJgitLib::ObjectId)

    The object id to retrieve bytes for.

Returns:

  • (Array<Fixnum>)

    An array of bytes.


164
165
166
# File 'lib/rosette/core/git/repo.rb', line 164

def read_object_bytes(object_id)
  object_reader.open(object_id).getBytes
end

#ref_diff_with_parents(ref, finder = diff_finder) ⇒ Array<Java::OrgEclipseJgitDiff::DiffEntry>

Calculates a diff for the given ref against its parent.

Parameters:

  • ref (String)

    The ref to diff with.

  • finder (DiffFinder) (defaults to: diff_finder)

    The diff finder to use when calculating diffs in a multi-threaded environment

Returns:

  • (Array<Java::OrgEclipseJgitDiff::DiffEntry>)

116
117
118
# File 'lib/rosette/core/git/repo.rb', line 116

def ref_diff_with_parents(ref, finder = diff_finder)
  rev_diff_with_parents(get_rev_commit(ref), finder)
end

#refs_containing(commit_id_or_ref, walker = rev_walker, refs = nil) ⇒ Array<Java::OrgEclipseJgitLib::Ref>

Get a list of refs (i.e. branches) that contain the given commit id.

Parameters:

  • commit_id_or_ref (String)

    The git commit id or ref to get refs for. This method returns all the refs that contain this commit.

  • walker (Java::OrgEclipseJgitRevwalk::RevWalk) (defaults to: rev_walker)

    The walker to use.

Returns:

  • (Array<Java::OrgEclipseJgitLib::Ref>)

    The list of refs that contain commit_id_or_ref.


350
351
352
353
354
355
356
# File 'lib/rosette/core/git/repo.rb', line 350

def refs_containing(commit_id_or_ref, walker = rev_walker, refs = nil)
  refs ||= jgit_repo.refDatabase.getRefs(RefDatabase::ALL).values
  commit = get_rev_commit(commit_id_or_ref, walker)
  RevWalkUtils.findBranchesReachableFrom(commit, walker, refs)
rescue Java::OrgEclipseJgitErrors::MissingObjectException
  []
end

#rev_diff_with_parents(rev, finder = diff_finder) ⇒ Array<Java::OrgEclipseJgitDiff::DiffEntry>

Calculates a diff for the given rev against its parent.

Parameters:

  • rev (Java::OrgEclipseJgitRevwalk::RevCommit)

    The rev to diff with.

  • finder (DiffFinder) (defaults to: diff_finder)

    The diff finder to use when calculating diffs in a multi-threaded environment.

Returns:

  • (Array<Java::OrgEclipseJgitDiff::DiffEntry>)

126
127
128
# File 'lib/rosette/core/git/repo.rb', line 126

def rev_diff_with_parents(rev, finder = diff_finder)
  finder.diff_with_parents(rev)
end