File tree Expand file tree Collapse file tree 1 file changed +30
-5
lines changed Expand file tree Collapse file tree 1 file changed +30
-5
lines changed Original file line number Diff line number Diff line change 3
3
module Git
4
4
5
5
class Base
6
+ # Adding a mutex to the class because each repo should be sharing the same mutex
7
+ # in case we need to Dir.chdir and we don't have fork() support to isolate that
8
+ class << self
9
+ attr_accessor :chdir_semaphore
10
+ end
11
+ Git ::Base . chdir_semaphore = Mutex . new
6
12
7
13
include Git ::Base ::Factory
8
14
@@ -92,18 +98,37 @@ def initialize(options = {})
92
98
@index = options [ :index ] ? Git ::Index . new ( options [ :index ] , false ) : nil
93
99
end
94
100
95
- # changes current working directory for a block
96
- # to the git working directory
101
+ # changes current working directory for a block to the git working directory.
102
+ #
103
+ # Note: If we can fork() or spawn(), Dir.chdir will happen in a new process
104
+ # otherwise, we will use a mutex to prevent threading errors
105
+ # See https://github.com/ruby-git/ruby-git/issues/355 for more info
97
106
#
98
107
# example
99
108
# @git.chdir do
100
109
# # write files
101
110
# @git.add
102
111
# @git.commit('message')
103
112
# end
104
- def chdir # :yields: the Git::Path
105
- Dir . chdir ( dir . path ) do
106
- yield dir . path
113
+ def chdir ( &block ) # :yields: the Git::Path
114
+ chdir_block = Proc . new do
115
+ Dir . chdir ( dir . path ) do
116
+ block . call ( dir . path )
117
+ end
118
+ end
119
+
120
+ if Process . respond_to? ( :fork )
121
+ # Forking this process so that we can be threadsafe
122
+ pid = Process . fork do
123
+ chdir_block . call
124
+ end
125
+ Process . wait ( pid )
126
+ else
127
+ # Windows and NetBSD 4 don't support fork()
128
+ # let's use a mutex to prevent race conditions with threads
129
+ Git ::Base . chdir_semaphore . synchronize do
130
+ chdir_block . call
131
+ end
107
132
end
108
133
end
109
134
You can’t perform that action at this time.
0 commit comments