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

def initialize(path)
  @git = Git.open(path)
end

Instance Method Details

#changesObject



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
37
38
39
40
41
42
43
# File 'rake_modules/git.rb', line 10

def changes
  # Set of files that have changed between the current revision and the
  # first parent in the git tree.
  # Returns a hash with :old and :new holding lists of files
  # changed between the old revision and the new one.
  changed = {old: [], new: []}
  old = changed[:old]
  new = changed[:new]
  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?
    name_status = diffs.name_status[diff.path]
    case name_status
    when 'A'
      new << diff.path
    when 'C', 'M'
      new << diff.path
      old << diff.path
    when 'D'
      old << diff.path
    when /R\d+/
      old << diff.path
      regex = Regexp.new "^diff --git a/#{Regexp.escape(diff.path)} b/(.+)"
      if diff.patch =~ regex
        new << Regexp.last_match[1]
      end
    end
  end
  changed
end

#changes_in_headObject



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

def changes_in_head
  # Files modified in the current revision, as an array. Includes renames.
  changes[:new]
end

#exec_in_rewindObject



55
56
57
58
59
60
61
62
63
64
65
# File 'rake_modules/git.rb', line 55

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

#uncommitted_changes?Boolean

Returns:

  • (Boolean)


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

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