@@ -6,7 +6,9 @@ class GitExecuteError < StandardError
6
6
end
7
7
8
8
class Lib
9
-
9
+
10
+ @@semaphore = Mutex . new
11
+
10
12
def initialize ( base = nil , logger = nil )
11
13
@git_dir = nil
12
14
@git_index_file = nil
@@ -777,34 +779,47 @@ def with_custom_env_variables(&block)
777
779
ensure
778
780
restore_git_system_env_variables ( )
779
781
end
780
-
782
+
781
783
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
804
808
end
809
+ end
805
810
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 )
807
820
end
821
+
822
+ return output
808
823
end
809
824
810
825
# Takes the diff command line output (as Array) and parse it into a Hash
@@ -857,7 +872,6 @@ def log_path_options(opts)
857
872
858
873
arr_opts << opts [ :object ] if opts [ :object ] . is_a? String
859
874
arr_opts << '--' << opts [ :path_limiter ] if opts [ :path_limiter ]
860
-
861
875
arr_opts
862
876
end
863
877
0 commit comments