Class: FlashFlow::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/flash_flow/git.rb

Direct Known Subclasses

ShadowGit

Constant Summary collapse

ATTRIBUTES =
[:merge_remote, :merge_branch, :master_branch, :release_branch, :use_rerere]
UNMERGED_STATUSES =
%w{DD AU UD UA DU AA UU}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, logger = nil) ⇒ Git

Returns a new instance of Git.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/flash_flow/git.rb', line 12

def initialize(config, logger=nil)
  @cmd_runner = CmdRunner.new(logger: logger)
  config['release_branch'] ||= config['master_branch']
  ATTRIBUTES.each do |attr|
    unless config.has_key?(attr.to_s)
      raise RuntimeError.new("git configuration missing. Required config parameters: #{ATTRIBUTES}")
    end

    instance_variable_set("@#{attr}", config[attr.to_s])
  end

  @working_branch = current_branch
end

Instance Attribute Details

#working_branchObject (readonly)

Returns the value of attribute working_branch.



8
9
10
# File 'lib/flash_flow/git.rb', line 8

def working_branch
  @working_branch
end

Instance Method Details

#add_and_commit(files, message, opts = {}) ⇒ Object



48
49
50
51
52
# File 'lib/flash_flow/git.rb', line 48

def add_and_commit(files, message, opts={})
  files = [files].flatten
  run("add #{'-f ' if opts[:add] && opts[:add][:force]}#{files.join(' ')}")
  run("commit -m '#{message}'")
end

#commit_rerere(current_rereres) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/flash_flow/git.rb', line 87

def commit_rerere(current_rereres)
  return unless use_rerere
  @cmd_runner.run('mkdir rr-cache')
  @cmd_runner.run('rm -rf rr-cache/*')
  current_rereres.each do |rerere|
    @cmd_runner.run("cp -R .git/rr-cache/#{rerere} rr-cache/")
  end

  run('add rr-cache/')
  run("commit -m 'Update rr-cache'")
end

#conflicted_filesObject



173
174
175
176
# File 'lib/flash_flow/git.rb', line 173

def conflicted_files
  run("diff --name-only --diff-filter=U")
  last_stdout.split("\n")
end

#copy_temp_to_branch(branch, squash_message = nil) ⇒ Object



200
201
202
203
204
205
206
207
208
# File 'lib/flash_flow/git.rb', line 200

def copy_temp_to_branch(branch, squash_message = nil)
  run("checkout #{temp_merge_branch}")
  run("merge --strategy=ours --no-edit #{branch}")
  run("checkout #{branch}")
  run("merge #{temp_merge_branch}")


  squash_commits(branch, squash_message) if squash_message
end

#current_branchObject



178
179
180
181
# File 'lib/flash_flow/git.rb', line 178

def current_branch
  run("rev-parse --abbrev-ref HEAD")
  last_stdout.strip
end

#delete_temp_merge_branchObject



210
211
212
213
214
# File 'lib/flash_flow/git.rb', line 210

def delete_temp_merge_branch
  in_branch(master_branch) do
    run("branch -d #{temp_merge_branch}")
  end
end

#fetch(remote) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/flash_flow/git.rb', line 58

def fetch(remote)
  @fetched_remotes ||= {}
  unless @fetched_remotes[remote]
    run("fetch #{remote}")
    @fetched_remotes[remote] = true
  end
end

#fetch_remote_for_url(url) ⇒ Object



163
164
165
166
# File 'lib/flash_flow/git.rb', line 163

def fetch_remote_for_url(url)
  fetch_remotes = remotes.grep(Regexp.new(url)).grep(/ \(fetch\)/)
  fetch_remotes.map { |remote| remote.to_s.split("\t").first }.first
end

#in_branch(branch) ⇒ Object



224
225
226
227
228
229
230
231
232
233
# File 'lib/flash_flow/git.rb', line 224

def in_branch(branch)
  begin
    starting_branch = current_branch
    run("checkout #{branch}")

    yield
  ensure
    run("checkout #{starting_branch}")
  end
end

#in_dirObject



26
27
28
29
30
# File 'lib/flash_flow/git.rb', line 26

def in_dir
  Dir.chdir(@cmd_runner.dir) do
    yield
  end
end

#in_merge_branch(&block) ⇒ Object



220
221
222
# File 'lib/flash_flow/git.rb', line 220

def in_merge_branch(&block)
  in_branch(merge_branch, &block)
end

#in_original_merge_branchObject



71
72
73
# File 'lib/flash_flow/git.rb', line 71

def in_original_merge_branch
  in_branch("#{merge_remote}/#{merge_branch}") { yield }
end

#in_temp_merge_branch(&block) ⇒ Object



216
217
218
# File 'lib/flash_flow/git.rb', line 216

def in_temp_merge_branch(&block)
  in_branch(temp_merge_branch, &block)
end

#initialize_rerereObject



80
81
82
83
84
85
# File 'lib/flash_flow/git.rb', line 80

def initialize_rerere
  return unless use_rerere

  @cmd_runner.run('mkdir .git/rr-cache')
  @cmd_runner.run('cp -R rr-cache/* .git/rr-cache/')
end

#last_commandObject



36
37
38
# File 'lib/flash_flow/git.rb', line 36

def last_command
  @cmd_runner.last_command
end

#last_stdoutObject



32
33
34
# File 'lib/flash_flow/git.rb', line 32

def last_stdout
  @cmd_runner.last_stdout
end

#last_success?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/flash_flow/git.rb', line 40

def last_success?
  @cmd_runner.last_success?
end

#master_branch_contains?(ref) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
# File 'lib/flash_flow/git.rb', line 66

def master_branch_contains?(ref)
  run("branch --contains #{ref}", log: CmdRunner::LOG_CMD)
  last_stdout.split("\n").detect { |str| str[2..-1] == master_branch }
end

#merge(branch) ⇒ Object



54
55
56
# File 'lib/flash_flow/git.rb', line 54

def merge(branch)
  run("merge #{branch}")
end

#most_recent_commitObject



183
184
185
# File 'lib/flash_flow/git.rb', line 183

def most_recent_commit
  run("show -s --format=%cd head")
end

#push(branch, force = false) ⇒ Object



196
197
198
# File 'lib/flash_flow/git.rb', line 196

def push(branch, force=false)
  run("push #{'-f' if force} #{merge_remote} #{branch}")
end

#read_file_from_merge_branch(filename) ⇒ Object



75
76
77
78
# File 'lib/flash_flow/git.rb', line 75

def read_file_from_merge_branch(filename)
  run("show #{merge_remote}/#{merge_branch}:#{filename}", log: CmdRunner::LOG_CMD)
  last_stdout
end

#remotesObject



146
147
148
149
# File 'lib/flash_flow/git.rb', line 146

def remotes
  run('remote -v')
  last_stdout.split("\n")
end

#remotes_hashObject



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/flash_flow/git.rb', line 151

def remotes_hash
  return @remotes_hash if @remotes_hash

  @remotes_hash = {}
  remotes.each do |r|
    name = r.split[0]
    url = r.split[1]
    @remotes_hash[name] ||= url
  end
  @remotes_hash
end

#rerere_resolve!Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/flash_flow/git.rb', line 99

def rerere_resolve!
  return false unless use_rerere

  if unresolved_conflicts.empty?
    merging_files = staged_and_working_dir_files.select { |s| UNMERGED_STATUSES.include?(s[0..1]) }.map { |s| s[3..-1] }
    conflicts = conflicted_files

    run("add #{merging_files.join(" ")}")
    run('commit --no-edit')

    resolutions(conflicts)
  else
    false
  end
end

#reset_temp_merge_branchObject



187
188
189
190
191
192
193
194
# File 'lib/flash_flow/git.rb', line 187

def reset_temp_merge_branch
  in_branch(master_branch) do
    run("fetch #{merge_remote}")
    run("branch -D #{temp_merge_branch}")
    run("checkout -b #{temp_merge_branch}")
    run("reset --hard #{merge_remote}/#{master_branch}")
  end
end

#resolution_candidates(file) ⇒ Object

git rerere doesn’t give you a deterministic way to determine which resolution was used



132
133
134
135
136
137
138
139
140
# File 'lib/flash_flow/git.rb', line 132

def resolution_candidates(file)
  @cmd_runner.run("diff -q --from-file #{file} .git/rr-cache/*/postimage", log: CmdRunner::LOG_CMD)
  different_files = split_diff_lines(@cmd_runner.last_stdout)

  @cmd_runner.run('ls -la .git/rr-cache/*/postimage', log: CmdRunner::LOG_CMD)
  all_files = split_diff_lines(@cmd_runner.last_stdout)

  all_files - different_files
end

#resolutions(files) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/flash_flow/git.rb', line 123

def resolutions(files)
  {}.tap do |hash|
    files.map do |file|
      hash[file] = resolution_candidates(file)
    end.flatten
  end
end

#run(cmd, opts = {}) ⇒ Object



44
45
46
# File 'lib/flash_flow/git.rb', line 44

def run(cmd, opts={})
  @cmd_runner.run("git #{cmd}", opts)
end

#split_diff_lines(arr) ⇒ Object



142
143
144
# File 'lib/flash_flow/git.rb', line 142

def split_diff_lines(arr)
  arr.split("\n").map { |s| s.split(".git/rr-cache/").last.split("/postimage").first }
end

#staged_and_working_dir_filesObject



168
169
170
171
# File 'lib/flash_flow/git.rb', line 168

def staged_and_working_dir_files
  run("status --porcelain")
  last_stdout.split("\n").reject { |line| line[0..1] == '??' }
end

#unresolved_conflictsObject



115
116
117
118
119
120
121
# File 'lib/flash_flow/git.rb', line 115

def unresolved_conflicts
  in_dir do
    conflicted_files.map do |file|
      File.open(file) { |f| f.grep(/>>>>/) }.empty? ? nil : file
    end.compact
  end
end