Class: MetaCommit::Git::Repo
- Inherits:
-
Object
- Object
- MetaCommit::Git::Repo
- 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
-
#repo ⇒ Rugged::Repository
The current value of repo.
Instance Method Summary collapse
- #commit_of_tag(search_tag) ⇒ Rugged::Commit?
-
#diff(left, right, options) ⇒ Rugged::Diff::Delta
Proxy to Rugged::Repository#diff.
-
#diff_with_optimized_lines(left, right) {|old_file_path, new_file_path, patch, line| ... } ⇒ Object
Iterates over optimized lines in diff.
-
#dir ⇒ String
Directory of repository.
- #get_blob_at(revision, path, default = nil) ⇒ Object
-
#get_content_of(rel_repo_file_path, default = nil) ⇒ String
File content or default value.
-
#index_diff(options) ⇒ Rugged::Diff::Delta
Proxy to Rugged::Index#diff.
-
#index_diff_with_optimized_lines {|old_file_path, new_file_path, patch, line| ... } ⇒ Object
Iterates over optimized lines in index diff.
-
#initialize(repo_path) ⇒ Repo
constructor
A new instance of Repo.
-
#last_commit_oid ⇒ String
Last commit oid.
-
#path ⇒ String
Path to .git folder of repository.
-
#walk_by_commits {|previous_commit, current_commit| ... } ⇒ Object
Starts commit walker and yields following commits for easier iteration.
-
#walker ⇒ Rugged::Walker
Commit iterator.
Constructor Details
#initialize(repo_path) ⇒ Repo
Returns a new instance of Repo.
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
#repo ⇒ Rugged::Repository
Returns 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?
127 128 129 130 131 |
# File 'lib/meta_commit/git/repo.rb', line 127 def commit_of_tag(search_tag) @repo..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
54 55 56 |
# File 'lib/meta_commit/git/repo.rb', line 54 def diff(left, right, ) @repo.diff(left, right, ) end |
#diff_with_optimized_lines(left, right) {|old_file_path, new_file_path, patch, line| ... } ⇒ Object
Iterates over optimized lines in diff
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 |
#dir ⇒ String
Returns 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
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.
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
89 90 91 |
# File 'lib/meta_commit/git/repo.rb', line 89 def index_diff() @repo.index.diff(@repo.head.target.tree, ) end |
#index_diff_with_optimized_lines {|old_file_path, new_file_path, patch, line| ... } ⇒ Object
Iterates over optimized lines in index diff
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_oid ⇒ String
Returns last commit oid.
134 135 136 |
# File 'lib/meta_commit/git/repo.rb', line 134 def last_commit_oid @repo.last_commit.oid end |
#path ⇒ String
Returns 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
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 |
#walker ⇒ Rugged::Walker
Returns 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 |