From 8185e202dfe6a70fb04d3eec7826e4ea54866c4a Mon Sep 17 00:00:00 2001 From: scott Chacon Date: Thu, 29 Nov 2007 11:16:14 -0800 Subject: applied a patch by mateusz jedruch for iterating through merge conflicts --- lib/git/base.rb | 9 ++++++++- lib/git/lib.rb | 33 +++++++++++++++++++++++++++------ tests/test_helper.rb | 1 + 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/lib/git/base.rb b/lib/git/base.rb index e9ffc61..c5fac45 100644 --- a/lib/git/base.rb +++ b/lib/git/base.rb @@ -299,6 +299,13 @@ module Git self.lib.merge(branch, message) end + # iterates over the files which are unmerged + # + # yields file, your_version, their_version + def each_conflict(&block) + self.lib.conflicts(&block) + end + # fetches a branch from a remote and merges it into the current working branch def pull(remote = 'origin', branch = 'master', message = 'origin pull') fetch(remote) @@ -445,4 +452,4 @@ module Git end -end \ No newline at end of file +end diff --git a/lib/git/lib.rb b/lib/git/lib.rb index cd24fca..ad34709 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -425,7 +425,28 @@ module Git arr_opts << branch.to_a.join(' ') command('merge', arr_opts) end - + + def unmerged + unmerged = [] + command_lines('diff', ["--cached"]).each do |line| + unmerged << $1 if line =~ /^\* Unmerged path (.*)/ + end + unmerged + end + + def conflicts #yields :file, :your, :their + self.unmerged.each do |f| + your = Tempfile.new("YOUR-#{File.basename(f)}").path + arr_opts = [":2:#{f}", ">#{your}"] + command('show', arr_opts) + + their = Tempfile.new("THEIR-#{File.basename(f)}").path + arr_opts = [":3:#{f}", ">#{their}"] + command('show', arr_opts) + yield(f, your, their) + end + end + def remote_add(name, url, opts = {}) arr_opts = ['add'] arr_opts << '-f' if opts[:with_fetch] @@ -549,11 +570,11 @@ module Git private - def command_lines(cmd, opts = {}, chdir = true) + def command_lines(cmd, opts = [], chdir = true) command(cmd, opts, chdir).split("\n") end - def command(cmd, opts = {}, chdir = true) + def command(cmd, opts = [], chdir = true) ENV['GIT_DIR'] = @git_dir if (@git_dir != ENV['GIT_DIR']) ENV['GIT_INDEX_FILE'] = @git_index_file if (@git_index_file != ENV['GIT_INDEX_FILE']) ENV['GIT_WORK_TREE'] = @git_work_dir if (@git_work_dir != ENV['GIT_WORK_TREE']) @@ -564,9 +585,9 @@ module Git out = nil if chdir && (Dir.getwd != path) - Dir.chdir(path) { out = `git #{cmd} #{opts} 2>&1`.chomp } + Dir.chdir(path) { out = `#{git_cmd} 2>&1`.chomp } else - out = `git #{cmd} #{opts} 2>&1`.chomp + out = `#{git_cmd} 2>&1`.chomp end if @logger @@ -584,4 +605,4 @@ module Git end end -end \ No newline at end of file +end diff --git a/tests/test_helper.rb b/tests/test_helper.rb index e40174a..1ee8dbf 100644 --- a/tests/test_helper.rb +++ b/tests/test_helper.rb @@ -1,5 +1,6 @@ require 'test/unit' require 'fileutils' +require 'logger' require File.dirname(__FILE__) + '/../lib/git' class Test::Unit::TestCase -- cgit