Class: MetaCommit::Git::Repo

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

Overview

Rugged::Repository wrapper

Constant Summary collapse

DIFF_OPTIONS =
{:context_lines => 0, :ignore_whitespace => true}
INDEX_DIFF_OPTIONS =
{:context_lines => 0, :ignore_whitespace => true, :reverse => true}
FILE_NOT_EXISTS_OID =
'0000000000000000000000000000000000000000'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo_path) ⇒ Repo

Returns a new instance of Repo.

Parameters:

  • repo_path (String)


15
16
17
18
19
20
21
22
23
# File 'lib/meta_commit/git/repo.rb', line 15

def initialize(repo_path)
  begin
    @repo = Rugged::Repository.new(repo_path)
  rescue Rugged::OSError
    raise MetaCommit::Errors::MissingRepoError
  rescue Rugged::RepositoryError
    raise MetaCommit::Errors::MissingRepoError
  end
end

Instance Attribute Details

#repoRugged::Repository

Returns the current value of repo.

Returns:

  • (Rugged::Repository)

    the current value of repo



6
7
8
# File 'lib/meta_commit/git/repo.rb', line 6

def repo
  @repo
end

Instance Method Details

#commit_of_tag(search_tag) ⇒ Rugged::Commit?

Parameters:

  • search_tag (String)

Returns:

  • (Rugged::Commit, nil)


127
128
129
130
131
# File 'lib/meta_commit/git/repo.rb', line 127

def commit_of_tag(search_tag)
  @repo.tags.each do |tag|
    return tag.target if tag.name == search_tag
  end
end

#diff(left, right, options) ⇒ Rugged::Diff::Delta

Proxy to Rugged::Repository#diff

Parameters:

  • left (Object)
  • right (Object)
  • options (Hash)

Returns:

  • (Rugged::Diff::Delta)


54
55
56
# File 'lib/meta_commit/git/repo.rb', line 54

def diff(left, right, options)
  @repo.diff(left, right, options)
end

#diff_with_optimized_lines(left, right) {|old_file_path, new_file_path, patch, line| ... } ⇒ Object

Iterates over optimized lines in diff

Parameters:

  • left (Object)
  • right (Object)

Yields:

  • (old_file_path, new_file_path, patch, line)

Returns:

  • (Object)


63
64
65
66
67
68
69
70
71
# File 'lib/meta_commit/git/repo.rb', line 63

def diff_with_optimized_lines(left, right)
  diff = @repo.diff(left, right, DIFF_OPTIONS)
  diff.deltas.zip(diff.patches).each do |delta, patch|
    lines = organize_lines(delta, patch)
    lines.each do |line|
      yield(delta.old_file[:path], delta.new_file[:path], patch, line)
    end
  end
end

#dirString

Returns directory of repository.

Returns:

  • (String)

    directory of repository



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

def dir
  @repo.path.reverse.sub('/.git'.reverse, '').reverse
end

#get_blob_at(revision, path, default = nil) ⇒ Object

Parameters:

  • revision (String)

    commit hash

  • path (String)
  • default (String) (defaults to: nil)

    value to be returned if error appears



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

def get_blob_at(revision, path, default = nil)
  blob = @repo.blob_at(revision, path)
  return blob.content unless blob.nil?
  default
end

#get_content_of(rel_repo_file_path, default = nil) ⇒ String

Returns file content or default value.

Parameters:

  • rel_repo_file_path (String)

    file path relative to repository root

  • default (String) (defaults to: nil)

    value to be returned if error appears

Returns:

  • (String)

    file content or default value



105
106
107
108
109
110
111
112
113
# File 'lib/meta_commit/git/repo.rb', line 105

def get_content_of(rel_repo_file_path, default = nil)
  absolute_file_path = dir + rel_repo_file_path
  begin
    content = open(absolute_file_path).read
  rescue Errno::ENOENT
    content = default
  end
  content
end

#index_diff(options) ⇒ Rugged::Diff::Delta

Proxy to Rugged::Index#diff

Parameters:

  • options (Hash)

Returns:

  • (Rugged::Diff::Delta)


89
90
91
# File 'lib/meta_commit/git/repo.rb', line 89

def index_diff(options)
  @repo.index.diff(@repo.head.target.tree, options)
end

#index_diff_with_optimized_lines {|old_file_path, new_file_path, patch, line| ... } ⇒ Object

Iterates over optimized lines in index diff

Yields:

  • (old_file_path, new_file_path, patch, line)

Returns:

  • (Object)


76
77
78
79
80
81
82
83
84
# File 'lib/meta_commit/git/repo.rb', line 76

def index_diff_with_optimized_lines
  diff = index_diff(INDEX_DIFF_OPTIONS)
  diff.deltas.zip(diff.patches).each do |delta, patch|
    lines = organize_lines(delta, patch)
    lines.each do |line|
      yield(delta.old_file[:path], delta.new_file[:path], patch, line)
    end
  end
end

#last_commit_oidString

Returns last commit oid.

Returns:

  • (String)

    last commit oid



134
135
136
# File 'lib/meta_commit/git/repo.rb', line 134

def last_commit_oid
  @repo.last_commit.oid
end

#pathString

Returns path to .git folder of repository.

Returns:

  • (String)

    path to .git folder of repository



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

def path
  @repo.path
end

#walk_by_commits {|previous_commit, current_commit| ... } ⇒ Object

Starts commit walker and yields following commits for easier iteration

Yields:

  • (previous_commit, current_commit)


35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/meta_commit/git/repo.rb', line 35

def walk_by_commits
  walker = Rugged::Walker.new(@repo)
  walker.sorting(Rugged::SORT_REVERSE)
  walker.push(@repo.last_commit.oid)
  previous_commit = nil
  walker.each do |current_commit|
    # skip first commit
    previous_commit = current_commit if previous_commit.nil?
    next if previous_commit == current_commit
    yield(previous_commit, current_commit)
    previous_commit = current_commit
  end
end

#walkerRugged::Walker

Returns commit iterator.

Returns:

  • (Rugged::Walker)

    commit iterator



26
27
28
29
30
31
# File 'lib/meta_commit/git/repo.rb', line 26

def walker
  walker = Rugged::Walker.new(@repo)
  walker.sorting(Rugged::SORT_REVERSE)
  walker.push(@repo.last_commit.oid)
  walker
end