Class: AppMap::Depends::GitDiff

Inherits:
Object
  • Object
show all
Defined in:
lib/appmap/depends/git_diff.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_branches: [], base: nil, head: nil) ⇒ GitDiff

Returns a new instance of GitDiff.



6
7
8
9
10
# File 'lib/appmap/depends/git_diff.rb', line 6

def initialize(base_branches: [], base: nil, head: nil)
  @base_branches = base_branches
  @base = base
  @head = head
end

Instance Attribute Details

#base_branchesObject (readonly)

Returns the value of attribute base_branches.



4
5
6
# File 'lib/appmap/depends/git_diff.rb', line 4

def base_branches
  @base_branches
end

#headObject (readonly)

Returns the value of attribute head.



4
5
6
# File 'lib/appmap/depends/git_diff.rb', line 4

def head
  @head
end

Instance Method Details

#baseObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/appmap/depends/git_diff.rb', line 16

def base
  return @base if @base

  git_exists = -> { system('git --version 2>&1 > /dev/null') }
  detect_branch = ->(branch) { `git branch -a`.split("\n").map(&:strip).member?(branch) }
  detect_base = lambda do
    return nil unless git_exists.()

    @base_branches.find(&detect_branch)
  end
  @base = detect_base.()
  raise "Unable to detect base branch. Specify it explicitly as a task argument." unless @base
  @base
end

#base=(base) ⇒ Object



12
13
14
# File 'lib/appmap/depends/git_diff.rb', line 12

def base=(base)
  @base = base
end

#modified_filesObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/appmap/depends/git_diff.rb', line 31

def modified_files
  warn "Using base #{base.inspect}" if Depends.verbose
  warn "Using head #{head.inspect}" if head && Depends.verbose

  branches = [ head, base ].compact
  diff_cmd = [ 'git', 'diff', '--name-only', branches.join('..') ]

  if Depends.verbose
    warn diff_cmd.join(' ')
    warn "Files modified #{head ? 'in ' + head : 'locally'} compared to #{base}:"
  end

  stdout, stderr, status = Open3.capture3(*diff_cmd)
  if status.exitstatus != 0
    warn stdout
    warn stderr
    raise CommandError.new(diff_cmd)
  end
  stdout.split("\n")
end