public
Description: A set of git tools
Homepage:
Clone URL: git://github.com/evanphx/gx.git
gx / git-update.rb
100644 351 lines (281 sloc) 9.82 kb
1
2
3
4
5
6
7
8
9
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/env ruby
 
# TODO:
# * Handle a conflict in a remote commit that is in a renamed file.
 
$:.unshift File.dirname(__FILE__)
 
require 'enhance'
require 'optparse'
require 'ostruct'
require 'fileutils'
require 'readline'
 
opts = OpenStruct.new
 
op = OptionParser.new do |o|
  o.on "-z", "--analyze", "Output information on what update would do" do
    opts.analyze = true
  end
 
  o.on "-v", "--verbose", "Be verbose" do
    opts.verbose = true
  end
 
  o.on "--debug", "Show all git commands run" do
    Grit.debug = true
  end
 
  o.on "-q", "--quiet", "Show the minimal output" do
    opts.quiet = true
  end
end
 
op.parse!(ARGV)
 
STDOUT.sync = true
 
class Update
    HELP = <<-TXT
You're currently inside the conflict resolver. The following commands
are available to help you.
 
When the conflict resolver is first started, the contents of the file
will contain the file populated with conflict markers for you to edit.
 
[D]iff View the diffs between the (original version and local version)
and (original version and remote version).
[E]dit Launch your editor to edit the file.
[T]ool Run git-mergetool on the file.
[O]riginal Set the contents of the file to the original version. This is
version from the common ancestor of your commit and the remote
commit.
[M]ine Set the contents of the file to be your version.
[R]emote Set the contents of the file to be the remote version.
co[N]flict Set the contents of the file to contain the merged between the
local version and remote version, with conflict markers.
[P]rompt Launch a subshell to deal with the conflict. Simply exit
from the shell to continue with conflict resolution.
[I]nfo View information about the commit and the current file.
[A]bort Cancel the update all together, restore everything to before
the update was started.
[C]ontinue You're done dealing with this conflict, move on to the next one.
[H]elp Detail all available options, you're looking at it now.
TXT
 
 
  def initialize(opts)
    @opts = opts
    @repo = Grit::Repo.current
 
    @current = @repo.resolve_rev "HEAD"
    @branch = @repo.git.symbolic_ref({:q => true}, "HEAD").strip
 
    @branch_name = @branch.gsub %r!^refs/heads/!, ""
 
    @origin_ref = @repo.merge_ref @branch_name
 
    unless @origin_ref
      puts "Sorry, it appears you're current branch is not setup with merge info."
      puts "Please set 'branch.#{@branch_name}.remote' and 'branch.#{@branch_name}.merge'"
      puts "and try again."
      exit 1
    end
  end
 
  def fetch
    print "Fetching new commits: "
    out = @repo.git.fetch :timeout => false
    puts "done."
 
    # TODO parse +out+ for details to show the user.
  end
 
  def includes_conflict_markers?(path)
/^<<<<<<< HEAD/.match(File.read(path))
  end
 
  def cat_file(ref, file)
    File.open(file, "w") do |f|
      f << @repo.git.cat_file({}, ref)
    end
  end
 
  def handle_unmerged(patch_info, files)
    files.each do |name, info|
      system "cp #{name} .git/with_markers"
 
      puts
      puts "Conflict discovered in '#{name}'"
 
      loop do
 
        # If there are conflict markers, default is edit.
        if includes_conflict_markers?(name)
          default = "E"
 
          # otherwise it's continue.
        else
          default = "C"
        end
 
        ans = Readline.readline "Select: [D]iff, [E]dit, [C]ontinue, [H]elp: [#{default}] "
        ans = default if ans.empty?
        want = ans.downcase[0]
        case want
        when ?d
          orig = ".git/diff/original/#{name}"
          FileUtils.mkdir_p File.dirname(orig)
          cat_file info.original, orig
 
          mine = ".git/diff/mine/#{name}"
          FileUtils.mkdir_p File.dirname(mine)
          cat_file info.mine, mine
 
          remote = ".git/diff/remote/#{name}"
          FileUtils.mkdir_p File.dirname(remote)
          cat_file info.yours, remote
 
          system "cd .git/diff; diff -u original/#{name} mine/#{name}"
          system "cd .git/diff; diff -u original/#{name} remote/#{name}"
          system "rm -rf .git/diff"
        when ?e
          system "#{ENV['EDITOR']} #{name}"
        when ?t
          system "git mergetool #{name}"
        when ?o
          cat_file info.original, name
        when ?m
          cat_file info.mine, name
        when ?r
          cat_file info.yours, name
        when ?n
          system "cp .git/with_markers #{name}"
        when ?p
          puts "Starting a sub-shell to handle conflicts for #{name}."
          puts "Exit the shell to continue resolution."
          system "$SHELL"
        when ?i
          puts "Current file: #{name}"
          puts "Current commit:"
          puts " Subject: #{patch_info[:subject]}"
          puts " Date: #{patch_info[:date]}"
          puts " Author: #{patch_info[:author]} (#{patch_info[:email]})"
        when ?a
          raise "abort!"
        when ?h
          puts HELP
        when ?c
          if includes_conflict_markers?(name)
            puts
            puts "It looks like this file still contains conflict markers."
            a = Readline.readline "Are you sure that you want to commit it? [Y/N]: "
            break if a.downcase[0] == ?y
          else
            break
          end
        else
          puts "Unknown option. Try again."
        end
      end
 
      File.unlink ".git/with_markers" rescue nil
      @repo.git.add({}, name)
    end
  end
 
  def analyze
    puts "Automatically merging in refs from: #{@origin_ref} / #{@origin[0,7]}"
    puts "Closest ancestor between HEAD and origin: #{@common[0,7]}"
    puts
 
    if @to_receive.empty?
      puts "Current history is up to date."
      exit 0
    end
 
    puts "#{@to_receive.size} new commits."
    if @opts.verbose
      system "git log --pretty=oneline #{@common}..#{@origin_ref}"
      puts
    end
 
    puts "#{@to_replay.size} commits to adapt."
    if @opts.verbose
      system "git log --pretty=oneline #{@common}..HEAD"
      puts
    end
  end
 
  def run
 
    fetch
 
    @origin = @repo.resolve_rev @origin_ref
 
    @common = @repo.find_ancestor(@origin, @current)
 
    @to_replay = @repo.revs_between(@common, @current)
    @to_receive = @repo.revs_between(@common, @origin)
 
    if @opts.analyze
      analyze
      exit 0
    end
 
    if @to_receive.empty?
      puts "Up to date."
      exit 0
    end
 
    if @opts.verbose
      puts "Extracting commits between #{@common[0,7]} and HEAD..."
    end
 
    # DANGER. Before here, we can abort anytime, after here, we're making
    # changes, so we need to be able to recover.
    #
    begin
      port_changes
    rescue Exception => e
      puts "Error detected, aborting update: #{e.message} (#{e.class})"
      puts e.backtrace
      recover
      exit 1
    end
  end
 
  def recover
    @repo.git.reset({:hard => true}, @current)
    @repo.git.checkout({}, @branch.gsub(%r!^refs/heads/!, ""))
 
    if @used_wip
      @repo.git.reset({:mixed => true}, "HEAD^")
    end
 
    system "rm -rf #{Grit.rebase_dir}" rescue nil
  end
 
  def sh(cmd)
    Grit.log cmd if Grit.debug
    out = `#{cmd}`
    Grit.log out if Grit.debug
  end
 
  def port_changes
    # Switch back in time so we can re-apply commits. checkout
    # will return non-zero if there it can't be done. In that case
    # we perform a WIP commit, and unwind that WIP commit later,
    # leaving the working copy the same way it was.
 
    @used_wip = false
 
    list = @repo.git.ls_files(:m => true).split("\n")
    if list.size > 0
      @repo.git.commit({:m => "++WIP++", :a => true})
      @used_wip = true
 
      # Because we've introduced a new commit, we need to repoint current.
      @current = @repo.resolve_rev "HEAD"
 
      # And the list of commits to replay.
      @to_replay = @repo.revs_between(@common, @current)
 
      # Ok, try again.
      error = @repo.git.checkout({:q => true}, @origin)
      if $?.exitstatus != 0
        # Ok, give up.
        recover
 
        # Now tell the user what happened.
        puts "ERROR: Sorry, 'git checkout' can't figure out how to properly switch"
        puts "the working copy. Please fix this and run 'git update' again."
        puts "Here is the error that 'git checkout' reported:"
        puts error
        exit 1
      end
    else
      @repo.git.checkout({:q => true}, @origin)
    end
 
    sh "git format-patch --full-index --stdout #{@common}..#{@current} > .git/update-patch"
    out = sh "git am --rebasing < .git/update-patch 2> /dev/null"
    while $?.exitstatus != 0
      info = @repo.am_info
      if @opts.verbose
        if info[:subject] == "++WIP++"
          puts "Conflict detected in working copy."
        else
          puts "Conflict detected applying: #{info[:subject]}"
        end
      end
 
      unmerged = @repo.unmerged_files
      handle_unmerged info, unmerged
 
      if @repo.to_be_committed.empty?
        out = @repo.git.am({:skip => true, "3" => true})
      else
        out = @repo.git.am({:resolved => true, "3" => true})
      end
    end
 
    # Remove the patch we created contain all the rebased commits
    File.unlink ".git/update-patch" rescue nil
 
    rev = @repo.resolve_rev "HEAD"
 
    # Update the branch ref to point to our new commit
 
    @repo.git.update_ref({:m => "updated"}, @branch, rev, @current)
    @repo.git.symbolic_ref({}, "HEAD", @branch)
 
    # If we inserted a WIP commit on the top, remove the commit, but leave
    # the work.
    if @used_wip
      @repo.git.reset({:mixed => true}, "HEAD^")
    end
 
    puts
    puts "Updated. Imported #{@to_receive.size} commits, HEAD now pointed to #{rev[0,7]}."
    puts
 
    unless @opts.quiet
      system "git diff --stat #{@common}..#{@origin}"
    end
 
  end
end
 
Update.new(opts).run