class Pronto::Git::Repository

Public Class Methods

new(path) click to toggle source
# File lib/pronto/git/repository.rb, line 6
def initialize(path)
  @repo = Rugged::Repository.new(path)
end

Public Instance Methods

blame(path, lineno) click to toggle source
# File lib/pronto/git/repository.rb, line 64
def blame(path, lineno)
  return if new_file?(path)

  Rugged::Blame.new(@repo, path, min_line: lineno, max_line: lineno,
                                 track_copies_same_file: true,
                                 track_copies_any_commit_copies: true)[0]
end
branch() click to toggle source
# File lib/pronto/git/repository.rb, line 72
def branch
  @repo.head.name.sub('refs/heads/', '') if @repo.head.branch?
end
commits_until(sha) click to toggle source
# File lib/pronto/git/repository.rb, line 51
def commits_until(sha)
  result = []
  @repo.walk(head_commit_sha, Rugged::SORT_TOPO).take_while do |commit|
    result << commit.oid
    !commit.oid.start_with?(sha)
  end
  result
end
diff(commit, options = nil) click to toggle source
# File lib/pronto/git/repository.rb, line 10
def diff(commit, options = nil)
  target, patches = case commit
                    when :unstaged, :index
                      [head_commit_sha, @repo.index.diff(options)]
                    when :staged
                      [head_commit_sha, head.diff(@repo.index, options)]
                    when :workdir
                      [
                        head_commit_sha,
                        @repo.diff_workdir(
                          head,
                          {
                            include_untracked: true,
                            include_untracked_content: true,
                            recurse_untracked_dirs: true
                          }.merge(options || {})
                        )
                      ]
                    else
                      merge_base = merge_base(commit)
                      patches = @repo.diff(merge_base, head, options)
                      [merge_base, patches]
                    end

  patches.find_similar!(renames: true)
  Patches.new(self, target, patches)
end
head_commit_sha() click to toggle source
# File lib/pronto/git/repository.rb, line 80
def head_commit_sha
  head.oid
end
head_detached?() click to toggle source
# File lib/pronto/git/repository.rb, line 84
def head_detached?
  @repo.head_detached?
end
path() click to toggle source
# File lib/pronto/git/repository.rb, line 60
def path
  Pathname.new(@repo.workdir).cleanpath
end
remote_urls() click to toggle source
# File lib/pronto/git/repository.rb, line 76
def remote_urls
  @repo.remotes.map(&:url)
end
show_commit(sha) click to toggle source
# File lib/pronto/git/repository.rb, line 38
def show_commit(sha)
  return empty_patches(sha) unless sha

  commit = @repo.lookup(sha)
  return empty_patches(sha) if commit.parents.count != 1

  # TODO: Rugged does not seem to support diffing against multiple parents
  diff = commit.diff(reverse: true)
  return empty_patches(sha) if diff.nil?

  Patches.new(self, sha, diff.patches)
end

Private Instance Methods

empty_patches(sha) click to toggle source
# File lib/pronto/git/repository.rb, line 94
def empty_patches(sha)
  Patches.new(self, sha, [])
end
head() click to toggle source
# File lib/pronto/git/repository.rb, line 102
def head
  @repo.head.target
end
merge_base(commit) click to toggle source
# File lib/pronto/git/repository.rb, line 98
def merge_base(commit)
  @repo.merge_base(commit, head)
end
new_file?(path) click to toggle source
# File lib/pronto/git/repository.rb, line 90
def new_file?(path)
  @repo.status(path).include?(:index_new)
end