Skip to content

Commit 72ae34c

Browse files
author
scott Chacon
committed
made it not change working directories when running git commands unless it needs to
1 parent b0d47b3 commit 72ae34c

File tree

3 files changed

+30
-27
lines changed

3 files changed

+30
-27
lines changed

benchmark.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
require 'benchmark'
33
require 'rubygems'
44
require 'ruby-prof'
5-
require_gem 'git', '1.0.3'
6-
#require 'lib/git'
5+
#require_gem 'git', '1.0.3'
6+
require 'lib/git'
77

88
def main
99
@wbare = File.expand_path(File.join('tests', 'files', 'working.git'))

lib/git/lib.rb

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def log_commits(opts = {})
6666
arr_opts << opts[:object] if opts[:object].is_a? String
6767
arr_opts << '-- ' + opts[:path_limiter] if opts[:path_limiter].is_a? String
6868

69-
command_lines('log', arr_opts).map { |l| l.split.first }
69+
command_lines('log', arr_opts, true).map { |l| l.split.first }
7070
end
7171

7272
def revparse(string)
@@ -422,34 +422,37 @@ def archive(sha, file = nil, opts = {})
422422

423423
private
424424

425-
def command_lines(cmd, opts = {})
426-
command(cmd, opts).split("\n")
425+
def command_lines(cmd, opts = {}, chdir = true)
426+
command(cmd, opts, chdir).split("\n")
427427
end
428428

429-
def command(cmd, opts = {})
429+
def command(cmd, opts = {}, chdir = true)
430430
ENV['GIT_DIR'] = @git_dir if (@git_dir != ENV['GIT_DIR'])
431431
ENV['GIT_INDEX_FILE'] = @git_index_file if (@git_index_file != ENV['GIT_INDEX_FILE'])
432-
ENV['GIT_WORK_DIR'] = @git_work_dir if (@git_work_dir != ENV['GIT_WORK_DIR'])
433-
#path = @git_work_dir || @git_dir || @path
434-
#Dir.chdir(path) do
435-
opts = opts.to_a.join(' ')
436-
git_cmd = "git #{cmd} #{opts}"
432+
ENV['GIT_WORK_TREE'] = @git_work_dir if (@git_work_dir != ENV['GIT_WORK_TREE'])
433+
path = @git_work_dir || @git_dir || @path
434+
435+
opts = opts.to_a.join(' ')
436+
git_cmd = "git #{cmd} #{opts}"
437+
438+
out = nil
439+
if chdir && (Dir.getwd != path)
440+
Dir.chdir(path) { out = `git #{cmd} #{opts} 2>&1`.chomp }
441+
else
437442
out = `git #{cmd} #{opts} 2>&1`.chomp
438-
#puts path
439-
#puts "gd: #{@git_work_dir}"
440-
#puts "gi: #{@git_index_file}"
441-
#puts "pp: #{@path}"
442-
#puts git_cmd
443-
#puts out
444-
#puts
445-
if $?.exitstatus > 0
446-
if $?.exitstatus == 1 && out == ''
447-
return ''
448-
end
449-
raise Git::GitExecuteError.new(git_cmd + ':' + out.to_s)
443+
end
444+
445+
#puts git_cmd
446+
#puts out
447+
#puts
448+
449+
if $?.exitstatus > 0
450+
if $?.exitstatus == 1 && out == ''
451+
return ''
450452
end
451-
out
452-
#end
453+
raise Git::GitExecuteError.new(git_cmd + ':' + out.to_s)
454+
end
455+
out
453456
end
454457

455458
end

tests/units/test_log.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ def test_get_log_since
3131
assert_equal(30, l.size)
3232
end
3333

34-
def test_get_log_since_file
35-
l = @git.log.path('example.txt')
34+
def test_get_log_since_file
35+
l = @git.log.object('example.txt')
3636
assert_equal(30, l.size)
3737

3838
l = @git.log.between('v2.5', 'test').path('example.txt')

0 commit comments

Comments
 (0)