From 72ae34cf263c00376d6c13f932a5fb1cf59dbef2 Mon Sep 17 00:00:00 2001 From: scott Chacon Date: Sat, 17 Nov 2007 08:42:58 -0800 Subject: made it not change working directories when running git commands unless it needs to --- benchmark.rb | 4 ++-- lib/git/lib.rb | 49 ++++++++++++++++++++++++++----------------------- tests/units/test_log.rb | 4 ++-- 3 files changed, 30 insertions(+), 27 deletions(-) diff --git a/benchmark.rb b/benchmark.rb index 7fc0b5d..58e9703 100644 --- a/benchmark.rb +++ b/benchmark.rb @@ -2,8 +2,8 @@ require 'fileutils' require 'benchmark' require 'rubygems' require 'ruby-prof' -require_gem 'git', '1.0.3' -#require 'lib/git' +#require_gem 'git', '1.0.3' +require 'lib/git' def main @wbare = File.expand_path(File.join('tests', 'files', 'working.git')) diff --git a/lib/git/lib.rb b/lib/git/lib.rb index c22042f..31cf164 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -66,7 +66,7 @@ module Git arr_opts << opts[:object] if opts[:object].is_a? String arr_opts << '-- ' + opts[:path_limiter] if opts[:path_limiter].is_a? String - command_lines('log', arr_opts).map { |l| l.split.first } + command_lines('log', arr_opts, true).map { |l| l.split.first } end def revparse(string) @@ -422,34 +422,37 @@ module Git private - def command_lines(cmd, opts = {}) - command(cmd, opts).split("\n") + def command_lines(cmd, opts = {}, chdir = true) + command(cmd, opts, chdir).split("\n") end - def command(cmd, opts = {}) + 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_DIR'] = @git_work_dir if (@git_work_dir != ENV['GIT_WORK_DIR']) - #path = @git_work_dir || @git_dir || @path - #Dir.chdir(path) do - opts = opts.to_a.join(' ') - git_cmd = "git #{cmd} #{opts}" + ENV['GIT_WORK_TREE'] = @git_work_dir if (@git_work_dir != ENV['GIT_WORK_TREE']) + path = @git_work_dir || @git_dir || @path + + opts = opts.to_a.join(' ') + git_cmd = "git #{cmd} #{opts}" + + out = nil + if chdir && (Dir.getwd != path) + Dir.chdir(path) { out = `git #{cmd} #{opts} 2>&1`.chomp } + else out = `git #{cmd} #{opts} 2>&1`.chomp - #puts path - #puts "gd: #{@git_work_dir}" - #puts "gi: #{@git_index_file}" - #puts "pp: #{@path}" - #puts git_cmd - #puts out - #puts - if $?.exitstatus > 0 - if $?.exitstatus == 1 && out == '' - return '' - end - raise Git::GitExecuteError.new(git_cmd + ':' + out.to_s) + end + + #puts git_cmd + #puts out + #puts + + if $?.exitstatus > 0 + if $?.exitstatus == 1 && out == '' + return '' end - out - #end + raise Git::GitExecuteError.new(git_cmd + ':' + out.to_s) + end + out end end diff --git a/tests/units/test_log.rb b/tests/units/test_log.rb index 5b41f39..11dcb27 100644 --- a/tests/units/test_log.rb +++ b/tests/units/test_log.rb @@ -31,8 +31,8 @@ class TestLog < Test::Unit::TestCase assert_equal(30, l.size) end - def test_get_log_since_file - l = @git.log.path('example.txt') + def test_get_log_since_file + l = @git.log.object('example.txt') assert_equal(30, l.size) l = @git.log.between('v2.5', 'test').path('example.txt') -- cgit