Class: Pronto::Git::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/pronto/git/repository.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Repository

Returns a new instance of Repository.



6
7
8
# File 'lib/pronto/git/repository.rb', line 6

def initialize(path)
  @repo = Rugged::Repository.new(path)
end

Instance Method Details

#blame(path, lineno) ⇒ Object



51
52
53
54
55
# File 'lib/pronto/git/repository.rb', line 51

def blame(path, lineno)
  Rugged::Blame.new(@repo, path, min_line: lineno, max_line: lineno,
                                 track_copies_same_file: true,
                                 track_copies_any_commit_copies: true)[0]
end

#branchObject



57
58
59
# File 'lib/pronto/git/repository.rb', line 57

def branch
  @repo.head.name.sub('refs/heads/', '') if @repo.head.branch?
end

#commits_until(sha) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/pronto/git/repository.rb', line 38

def commits_until(sha)
  result = []
  @repo.walk(head, Rugged::SORT_TOPO).take_while do |commit|
    result << commit.oid
    !commit.oid.start_with?(sha)
  end
  result
end

#diff(commit, options = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/pronto/git/repository.rb', line 10

def diff(commit, options = nil)
  target, patches = case commit
                    when :unstaged, :index
                      [head, @repo.index.diff(options)]
                    when :staged
                      [head, @repo.head.target.diff(@repo.index, options)]
                    else
                      merge_base = merge_base(commit)
                      patches = @repo.diff(merge_base, head, options)
                      [merge_base, patches]
                    end

  Patches.new(self, target, patches)
end

#head_commit_shaObject



65
66
67
# File 'lib/pronto/git/repository.rb', line 65

def head_commit_sha
  head.oid
end

#pathObject



47
48
49
# File 'lib/pronto/git/repository.rb', line 47

def path
  Pathname.new(@repo.path).parent
end

#remote_urlsObject



61
62
63
# File 'lib/pronto/git/repository.rb', line 61

def remote_urls
  @repo.remotes.map(&:url)
end

#show_commit(sha) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pronto/git/repository.rb', line 25

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