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



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

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

#branchObject



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

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

#commits_until(sha) ⇒ Object



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

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
24
# 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)]
                    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_shaObject



68
69
70
# File 'lib/pronto/git/repository.rb', line 68

def head_commit_sha
  head.oid
end

#head_detached?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/pronto/git/repository.rb', line 72

def head_detached?
  @repo.head_detached?
end

#pathObject



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

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

#remote_urlsObject



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

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

#show_commit(sha) ⇒ Object



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

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