Skip to content

Commit 8185e20

Browse files
author
scott Chacon
committed
applied a patch by mateusz jedruch <mateusz.jedruch@gmail.com> for iterating through merge conflicts
1 parent c6db2b7 commit 8185e20

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

lib/git/base.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,13 @@ def merge(branch, message = 'merge')
299299
self.lib.merge(branch, message)
300300
end
301301

302+
# iterates over the files which are unmerged
303+
#
304+
# yields file, your_version, their_version
305+
def each_conflict(&block)
306+
self.lib.conflicts(&block)
307+
end
308+
302309
# fetches a branch from a remote and merges it into the current working branch
303310
def pull(remote = 'origin', branch = 'master', message = 'origin pull')
304311
fetch(remote)
@@ -445,4 +452,4 @@ def current_branch
445452

446453
end
447454

448-
end
455+
end

lib/git/lib.rb

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,28 @@ def merge(branch, message = nil)
425425
arr_opts << branch.to_a.join(' ')
426426
command('merge', arr_opts)
427427
end
428-
428+
429+
def unmerged
430+
unmerged = []
431+
command_lines('diff', ["--cached"]).each do |line|
432+
unmerged << $1 if line =~ /^\* Unmerged path (.*)/
433+
end
434+
unmerged
435+
end
436+
437+
def conflicts #yields :file, :your, :their
438+
self.unmerged.each do |f|
439+
your = Tempfile.new("YOUR-#{File.basename(f)}").path
440+
arr_opts = [":2:#{f}", ">#{your}"]
441+
command('show', arr_opts)
442+
443+
their = Tempfile.new("THEIR-#{File.basename(f)}").path
444+
arr_opts = [":3:#{f}", ">#{their}"]
445+
command('show', arr_opts)
446+
yield(f, your, their)
447+
end
448+
end
449+
429450
def remote_add(name, url, opts = {})
430451
arr_opts = ['add']
431452
arr_opts << '-f' if opts[:with_fetch]
@@ -549,11 +570,11 @@ def archive(sha, file = nil, opts = {})
549570

550571
private
551572

552-
def command_lines(cmd, opts = {}, chdir = true)
573+
def command_lines(cmd, opts = [], chdir = true)
553574
command(cmd, opts, chdir).split("\n")
554575
end
555576

556-
def command(cmd, opts = {}, chdir = true)
577+
def command(cmd, opts = [], chdir = true)
557578
ENV['GIT_DIR'] = @git_dir if (@git_dir != ENV['GIT_DIR'])
558579
ENV['GIT_INDEX_FILE'] = @git_index_file if (@git_index_file != ENV['GIT_INDEX_FILE'])
559580
ENV['GIT_WORK_TREE'] = @git_work_dir if (@git_work_dir != ENV['GIT_WORK_TREE'])
@@ -564,9 +585,9 @@ def command(cmd, opts = {}, chdir = true)
564585

565586
out = nil
566587
if chdir && (Dir.getwd != path)
567-
Dir.chdir(path) { out = `git #{cmd} #{opts} 2>&1`.chomp }
588+
Dir.chdir(path) { out = `#{git_cmd} 2>&1`.chomp }
568589
else
569-
out = `git #{cmd} #{opts} 2>&1`.chomp
590+
out = `#{git_cmd} 2>&1`.chomp
570591
end
571592

572593
if @logger
@@ -584,4 +605,4 @@ def command(cmd, opts = {}, chdir = true)
584605
end
585606

586607
end
587-
end
608+
end

tests/test_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'test/unit'
22
require 'fileutils'
3+
require 'logger'
34
require File.dirname(__FILE__) + '/../lib/git'
45

56
class Test::Unit::TestCase

0 commit comments

Comments
 (0)