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



64
65
66
67
68
69
70
# 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

#branchObject



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

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

#commits_until(sha) ⇒ Object



51
52
53
54
55
56
57
58
# 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) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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_shaObject



80
81
82
# File 'lib/pronto/git/repository.rb', line 80

def head_commit_sha
  head.oid
end

#head_detached?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/pronto/git/repository.rb', line 84

def head_detached?
  @repo.head_detached?
end

#pathObject



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

def path
  Pathname.new(@repo.workdir).cleanpath
end

#remote_urlsObject



76
77
78
# File 'lib/pronto/git/repository.rb', line 76

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

#show_commit(sha) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# 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