Skip to content

Commit d72c4ab

Browse files
Roberto Decurnexrobertodecurnex
Roberto Decurnex
authored andcommitted
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 #181
1 parent e55f3bd commit d72c4ab

File tree

3 files changed

+49
-31
lines changed

3 files changed

+49
-31
lines changed

lib/git/lib.rb

Lines changed: 40 additions & 26 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
@@ -777,34 +779,47 @@ def with_custom_env_variables(&block)
777779
ensure
778780
restore_git_system_env_variables()
779781
end
780-
782+
781783
def command(cmd, opts = [], chdir = true, redirect = '', &block)
782-
with_custom_env_variables do
783-
path = @git_work_dir || @git_dir || @path
784-
785-
opts = [opts].flatten.map {|s| escape(s) }.join(' ')
786-
787-
git_cmd = "git #{cmd} #{opts} #{redirect} 2>&1"
788-
789-
output = nil
790-
791-
if chdir && (Dir.getwd != path)
792-
Dir.chdir(path) { output = run_command(git_cmd, &block) }
793-
else
794-
output = run_command(git_cmd, &block)
795-
end
796-
797-
if @logger
798-
@logger.info(git_cmd)
799-
@logger.debug(output)
800-
end
801-
802-
if $?.exitstatus > 1 || ($?.exitstatus == 1 && output != '')
803-
raise Git::GitExecuteError.new(git_cmd + ':' + output.to_s)
784+
global_opts = []
785+
global_opts << "--git-dir=#{@git_dir}" if !@git_dir.nil?
786+
global_opts << "--work-tree=#{@git_work_dir}" if !@git_work_dir.nil?
787+
788+
opts = [opts].flatten.map {|s| escape(s) }.join(' ')
789+
790+
global_opts = global_opts.flatten.map {|s| escape(s) }.join(' ')
791+
792+
git_cmd = "git #{global_opts} #{cmd} #{opts} #{redirect} 2>&1"
793+
794+
output = nil
795+
796+
path = @git_work_dir || @git_dir || @path
797+
798+
command_thread = nil;
799+
800+
exitstatus = nil
801+
802+
@@semaphore.synchronize do
803+
with_custom_env_variables do
804+
command_thread = Thread.new do
805+
output = run_command(git_cmd, &block)
806+
exitstatus = $?.exitstatus
807+
end
804808
end
809+
end
805810

806-
return output
811+
command_thread.join
812+
813+
if @logger
814+
@logger.info(git_cmd)
815+
@logger.debug(output)
816+
end
817+
818+
if exitstatus > 1 || (exitstatus == 1 && output != '')
819+
raise Git::GitExecuteError.new(git_cmd + ':' + output.to_s)
807820
end
821+
822+
return output
808823
end
809824

810825
# Takes the diff command line output (as Array) and parse it into a Hash
@@ -857,7 +872,6 @@ def log_path_options(opts)
857872

858873
arr_opts << opts[:object] if opts[:object].is_a? String
859874
arr_opts << '--' << opts[:path_limiter] if opts[:path_limiter]
860-
861875
arr_opts
862876
end
863877

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: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ def test_logger
1919
@git.branches.size
2020

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

2527
log = Tempfile.new('logfile')
2628
log.close
@@ -31,8 +33,10 @@ def test_logger
3133
@git.branches.size
3234

3335
logc = File.read(log.path)
34-
assert(/INFO -- : git branch '-a'/.match(logc))
35-
assert(!/DEBUG -- : diff_over_patches/.match(logc))
36+
# assert(/INFO -- : git branch '-a'/.match(logc))
37+
# assert(!/DEBUG -- : diff_over_patches/.match(logc))
38+
assert(/INFO -- : git '--git-dir=[^']+' '--work-tree=[^']+' branch '-a'/.match(logc))
39+
assert(!/DEBUG -- : \* git_grep/.match(logc))
3640
end
3741

3842
end

0 commit comments

Comments
 (0)