Module: Covet::VCS::Git

Defined in:
lib/covet/vcs/git.rb

Class Method Summary collapse

Class Method Details

.changes_since(revision = :last_commit) ⇒ Object

Find lines in git-indexed files that were changed (added/deleted/modified) in the codebase since ‘revision`.

Raises:

  • Rugged::Error, Rugged::InvalidError, TypeError



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
37
38
39
40
# File 'lib/covet/vcs/git.rb', line 11

def self.changes_since(revision = :last_commit)
  repo = Rugged::Repository.new(find_git_repo_path!) # raises if can't find git repo
  lines_to_run = Set.new
  diff = case revision.to_s
  when 'last_commit', 'HEAD'
    repo.index.diff
  else
    # raises Rugged::Error or TypeError if `revision` is invalid Git object id
    # (tag name or sha1, etc.)
    commit = Rugged::Commit.new(repo, revision)
    repo.index.diff(commit, {}) # NOTE: for some reason, this call doesn't work properly if the second argument isn't given. Bug in rugged?
  end
  diff.each_patch { |patch|
    file = patch.delta.old_file[:path] # NOTE: old file is the index's version

    patch.each_hunk { |hunk|
      hunk.each_line { |line|
        case line.line_origin
        when :addition
          lines_to_run << [file, line.new_lineno]
        when :deletion
          lines_to_run << [file, line.old_lineno]
        when :context
          lines_to_run << [file, line.new_lineno]
        end
      }
    }
  }
  lines_to_run
end

.find_git_repo_path!Object

find git repository path at or below ‘Dir.pwd`



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/covet/vcs/git.rb', line 43

def self.find_git_repo_path!
  dir = orig_dir = Dir.pwd
  found = Dir.exist?('.git') && dir
  while !found && dir && Dir.exist?(dir)
    old_dir = Dir.pwd
    Dir.chdir('..')
    dir = Dir.pwd
    return if old_dir == dir # at root directory
    if dir && Dir.exist?('.git')
      found = dir
    end
  end
  found
ensure
  Dir.chdir(orig_dir) if Dir.pwd != orig_dir
end