Skip to content

Commit 3090b3c

Browse files
author
Roberto Decurnex
committed
Adding mutex to command to prevent multiple parallel executions to modify each others env variables.
Adding --git-dir and --work-tree options to the git commands to prevent parallel executions to move the current working directory in the middle of each others process related to ruby-git#181
1 parent f495812 commit 3090b3c

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

lib/git/lib.rb

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ class GitExecuteError < StandardError
66
end
77

88
class Lib
9-
9+
10+
@@semaphore = Mutex.new
11+
1012
def initialize(base = nil, logger = nil)
1113
@git_dir = nil
1214
@git_index_file = nil
@@ -734,30 +736,36 @@ def command_lines(cmd, opts = [], chdir = true, redirect = '')
734736
end
735737

736738
def command(cmd, opts = [], chdir = true, redirect = '', &block)
737-
ENV['GIT_DIR'] = @git_dir
738-
ENV['GIT_WORK_TREE'] = @git_work_dir
739-
ENV['GIT_INDEX_FILE'] = @git_index_file
740-
741-
path = @git_work_dir || @git_dir || @path
742-
739+
global_opts = []
740+
global_opts << "--git-dir=#{@git_dir}" if !@git_dir.nil?
741+
global_opts << "--work-tree=#{@git_work_dir}" if !@git_work_dir.nil?
743742
opts = [opts].flatten.map {|s| escape(s) }.join(' ')
744-
745-
git_cmd = "git #{cmd} #{opts} #{redirect} 2>&1"
746-
743+
global_opts = global_opts.flatten.map {|s| escape(s) }.join(' ')
744+
git_cmd = "git #{global_opts} #{cmd} #{opts} #{redirect} 2>&1"
747745
output = nil
746+
path = @git_work_dir || @git_dir || @path
747+
command_thread = nil;
748+
exitstatus = nil
748749

749-
if chdir && (Dir.getwd != path)
750-
Dir.chdir(path) { output = run_command(git_cmd, &block) }
751-
else
752-
output = run_command(git_cmd, &block)
750+
@@semaphore.synchronize do
751+
ENV['GIT_DIR'] = @git_dir
752+
ENV['GIT_WORK_TREE'] = @git_work_dir
753+
ENV['GIT_INDEX_FILE'] = @git_index_file
754+
755+
command_thread = Thread.new do
756+
output = run_command(git_cmd, &block)
757+
exitstatus = $?.exitstatus
758+
end
753759
end
760+
761+
command_thread.join
754762

755763
if @logger
756764
@logger.info(git_cmd)
757765
@logger.debug(output)
758766
end
759767

760-
if $?.exitstatus > 1 || ($?.exitstatus == 1 && output != '')
768+
if exitstatus > 1 || (exitstatus == 1 && output != '')
761769
raise Git::GitExecuteError.new(git_cmd + ':' + output.to_s)
762770
end
763771

@@ -814,7 +822,6 @@ def log_path_options(opts)
814822

815823
arr_opts << opts[:object] if opts[:object].is_a? String
816824
arr_opts << '--' << opts[:path_limiter] if opts[:path_limiter]
817-
818825
arr_opts
819826
end
820827

tests/units/test_log.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def test_get_log_author
5757
end
5858

5959
def test_get_log_since_file
60-
l = @git.log.object('example.txt')
60+
l = @git.log.path('example.txt')
6161
assert_equal(30, l.size)
6262

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

tests/units/test_logger.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def test_logger
1919
@git.branches.size
2020

2121
logc = File.read(log.path)
22-
assert(/INFO -- : git branch '-a'/.match(logc))
22+
assert(/INFO -- : git '--git-dir=[^']+' '--work-tree=[^']+' branch '-a'/.match(logc))
2323
assert(/DEBUG -- : \* git_grep/.match(logc))
2424

2525
log = Tempfile.new('logfile')
@@ -31,7 +31,7 @@ def test_logger
3131
@git.branches.size
3232

3333
logc = File.read(log.path)
34-
assert(/INFO -- : git branch '-a'/.match(logc))
34+
assert(/INFO -- : git '--git-dir=[^']+' '--work-tree=[^']+' branch '-a'/.match(logc))
3535
assert(!/DEBUG -- : \* git_grep/.match(logc))
3636
end
3737

0 commit comments

Comments
 (0)