Class: GitOps

Inherits:
Object show all
Defined in:
rake_modules/git.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ GitOps

Helper class to perform the git operations we use in the rakefile



7
8
9
10
11
# File 'rake_modules/git.rb', line 7

def initialize(path)
  @git = Git.open(path)
  @changed = {deleted_files: [], changed_files: [], new_files: [], rename_new: [], rename_old: []}
  process
end

Instance Method Details

#changed_files_in_lastObject



58
59
60
61
# File 'rake_modules/git.rb', line 58

def changed_files_in_last
  # Produce a list of changed files that also exists in HEAD~1
  @changed[:changed_files] + @changed[:deleted_files] + @changed[:rename_old]
end

#changesObject



13
14
15
16
# File 'rake_modules/git.rb', line 13

def changes
  # Geter for changed hash
  @changed
end

#changes_in_headObject



53
54
55
56
# File 'rake_modules/git.rb', line 53

def changes_in_head
  # Files modified in the current revision, as an array. Includes renames.
  @changed[:changed_files] + @changed[:new_files] + @changed[:rename_new]
end

#exec_in_rewindObject



68
69
70
71
72
73
74
75
76
77
78
# File 'rake_modules/git.rb', line 68

def exec_in_rewind
  # Execs one block of code in the previous revision in git history (as defined by HEAD^)
  # And then rewinds back to the original position
  if uncommitted_changes?
    raise RunTimeError "You have local changes to the repository that would be overwritten by rewinding to a previous revision"
  end
  raise LocalJumpError "will not rollback to a previous commit with nothing to do" unless block_given?
  @git.reset_hard('HEAD^')
  yield
  @git.reset_hard('HEAD@{1}')
end

#new_files_in_headObject



48
49
50
51
# File 'rake_modules/git.rb', line 48

def new_files_in_head
  # Files added in the current revision, as an array. Includes renames.
  @changed[:new_files]
end

#processObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'rake_modules/git.rb', line 18

def process
  # Process the current diff and populate @changed
  diffs = @git.diff('HEAD^')
  # Support fully ignoring paths
  data = YAML.safe_load(File.open("#{@git.dir.path}/.ignored.yaml"))
  ignored_modules = data["ignored_modules"]
  diffs.each do |diff|
    # Ignore upstream modules
    next unless ignored_modules.select { |m| %r'^modules/#{m}/' =~ diff.path }.empty?
    next if diff.path.start_with?('vendor_modules/')
    next if diff.path.start_with?('core_modules/')
    name_status = diffs.name_status[diff.path]
    case name_status
    when 'A'
      @changed[:new_files] << diff.path
    when 'C', 'M'
      @changed[:changed_files] << diff.path
    when 'D'
      @changed[:deleted_files] << diff.path
    when /R\d+/
      # This is a rename but i think its fine to also consider it a delete
      @changed[:rename_old] << diff.path
      regex = Regexp.new "^diff --git a/#{Regexp.escape(diff.path)} b/(.+)"
      if diff.patch =~ regex
        @changed[:rename_new] << Regexp.last_match[1]
      end
    end
  end
end

#uncommitted_changes?Boolean

Returns:

  • (Boolean)


63
64
65
66
# File 'rake_modules/git.rb', line 63

def uncommitted_changes?
  # Checks if there is any uncommitted change
  @git.diff('HEAD').size > 0 # rubocop:disable Style/NumericPredicate
end