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



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

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



56
57
58
59
# File 'rake_modules/git.rb', line 56

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



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

def changes
  # Geter for changed hash
  @changed
end

#changes_in_headObject



51
52
53
54
# File 'rake_modules/git.rb', line 51

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



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

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



46
47
48
49
# File 'rake_modules/git.rb', line 46

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

#processObject



17
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
# File 'rake_modules/git.rb', line 17

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/')
    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)


61
62
63
64
# File 'rake_modules/git.rb', line 61

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