@@ -16,7 +16,9 @@ module FixtureRepo
16
16
# Create a new, empty repository.
17
17
def self . empty ( *args )
18
18
path = Dir . mktmpdir ( "rugged-empty" )
19
- with_cleanup ( Rugged ::Repository . init_at ( path , *args ) , path )
19
+ Rugged ::Repository . init_at ( path , *args ) . tap do |repo |
20
+ schedule_cleanup ( repo , path )
21
+ end
20
22
rescue
21
23
FileUtils . remove_entry_secure ( path )
22
24
raise
@@ -30,7 +32,10 @@ def self.from_rugged(name, *args)
30
32
31
33
prepare ( path )
32
34
33
- with_cleanup ( Rugged ::Repository . new ( path , *args ) , path )
35
+ Rugged ::Repository . new ( path , *args ) . tap do |repo |
36
+ schedule_cleanup ( repo , path )
37
+ rewrite_gitmodules ( repo ) unless repo . bare?
38
+ end
34
39
rescue
35
40
FileUtils . remove_entry_secure ( path )
36
41
raise
@@ -44,7 +49,10 @@ def self.from_libgit2(name, *args)
44
49
45
50
prepare ( path )
46
51
47
- with_cleanup ( Rugged ::Repository . new ( path , *args ) , path )
52
+ Rugged ::Repository . new ( path , *args ) . tap do |repo |
53
+ schedule_cleanup ( repo , path )
54
+ rewrite_gitmodules ( repo ) unless repo . bare?
55
+ end
48
56
rescue
49
57
FileUtils . remove_entry_secure ( path )
50
58
raise
@@ -56,7 +64,9 @@ def self.clone(repository)
56
64
57
65
`git clone --quiet -- #{ repository . path } #{ path } `
58
66
59
- with_cleanup ( Rugged ::Repository . new ( path ) , path )
67
+ Rugged ::Repository . new ( path ) . tap do |repo |
68
+ schedule_cleanup ( repo , path )
69
+ end
60
70
rescue
61
71
FileUtils . remove_entry_secure ( path )
62
72
raise
@@ -70,6 +80,53 @@ def self.prepare(path)
70
80
end
71
81
end
72
82
83
+ # Rugged reuses libgit2 fixtures and needs the same setup code.
84
+ #
85
+ # This should be the same as the libgit2 fixture
86
+ # setup in vendor/libgit2/tests/submodule/submodule_helpers.c
87
+ def self . rewrite_gitmodules ( repo )
88
+ workdir = repo . workdir
89
+
90
+ return unless File . exist? ( File . join ( workdir , 'gitmodules' ) )
91
+
92
+ input_path = File . join ( workdir , 'gitmodules' )
93
+ output_path = File . join ( workdir , '.gitmodules' )
94
+ submodules = [ ]
95
+
96
+ File . open ( input_path , 'r' ) do |input |
97
+ File . open ( output_path , 'w' ) do |output |
98
+ input . each_line do |line |
99
+ if %r{path = (?<submodule>.+$)} =~ line
100
+ submodules << submodule . strip
101
+ elsif %r{url = \. \. \/ (?<url>.+$)} =~ line
102
+ # Copy repositories pointed to by relative urls
103
+ # and replace the relative url by the absolute path to the
104
+ # copied repo.
105
+ url . strip!
106
+ path = Dir . mktmpdir ( url )
107
+ FileUtils . cp_r ( File . join ( TestCase ::LIBGIT2_FIXTURE_DIR , url , "." ) , path )
108
+ schedule_cleanup ( repo , path )
109
+
110
+ line = "url = #{ path } \n "
111
+ end
112
+ output . write ( line )
113
+ end
114
+ end
115
+ end
116
+
117
+ FileUtils . remove_entry_secure ( input_path )
118
+
119
+ # rename .gitted -> .git in submodule dirs
120
+ submodules . each do |submodule |
121
+ submodule_path = File . join ( workdir , submodule )
122
+ if File . exist? ( File . join ( submodule_path , '.gitted' ) )
123
+ Dir . chdir ( submodule_path ) do
124
+ File . rename ( '.gitted' , '.git' )
125
+ end
126
+ end
127
+ end
128
+ end
129
+
73
130
def self . finalize_cleanup ( path )
74
131
proc { FileUtils . remove_entry_secure ( path ) if File . exist? ( path ) }
75
132
end
@@ -88,10 +145,9 @@ def self.directories
88
145
# Schedule the given +path+ to be deleted, either when
89
146
# +FixtureRepo.eager_teardown+ is called or when the given +repo+
90
147
# gets gc'ed.
91
- def self . with_cleanup ( repo , path )
148
+ def self . schedule_cleanup ( repo , path )
92
149
ObjectSpace . define_finalizer ( repo , finalize_cleanup ( path ) )
93
150
self . directories << path
94
- repo
95
151
end
96
152
end
97
153
@@ -134,69 +190,4 @@ def ssh_key_credential_from_agent
134
190
} )
135
191
end
136
192
end
137
-
138
- # Rugged reuses libgit2 fixtures and needs the same setup code.
139
- #
140
- # This should be the same as the libgit2 fixture
141
- # setup in vendor/libgit2/tests/submodule/submodule_helpers.c
142
- class SubmoduleTestCase < Rugged ::TestCase
143
- def setup_submodule
144
- repository = FixtureRepo . from_libgit2 ( 'submod2' )
145
-
146
- rewrite_gitmodules ( repository )
147
-
148
- Dir . chdir ( repository . workdir ) do
149
- File . rename (
150
- File . join ( 'not-submodule' , '.gitted' ) ,
151
- File . join ( 'not-submodule' , '.git' )
152
- )
153
-
154
- File . rename (
155
- File . join ( 'not' , '.gitted' ) ,
156
- File . join ( 'not' , '.git' )
157
- )
158
- end
159
-
160
- repository
161
- end
162
-
163
- def rewrite_gitmodules ( repo )
164
- workdir = repo . workdir
165
-
166
- input_path = File . join ( workdir , 'gitmodules' )
167
- output_path = File . join ( workdir , '.gitmodules' )
168
- submodules = [ ]
169
-
170
- File . open ( input_path , 'r' ) do |input |
171
- File . open ( output_path , 'w' ) do |output |
172
- input . each_line do |line |
173
- if %r{path = (?<submodule>.+$)} =~ line
174
- submodules << submodule . strip
175
- elsif %r{url = \. \. \/ (?<url>.+$)} =~ line
176
- # Copy repositories pointed to by relative urls
177
- # and replace the relative url by the absolute path to the
178
- # copied repo.
179
- path = Dir . mktmpdir ( url )
180
- FileUtils . cp_r ( File . join ( TestCase ::LIBGIT2_FIXTURE_DIR , url , "." ) , path )
181
- ObjectSpace . define_finalizer ( repo , FixtureRepo . finalize_cleanup ( path ) )
182
- line = "url = #{ path } \n "
183
- end
184
- output . write ( line )
185
- end
186
- end
187
- end
188
-
189
- FileUtils . remove_entry_secure ( input_path )
190
-
191
- # rename .gitted -> .git in submodule dirs
192
- submodules . each do |submodule |
193
- submodule_path = File . join ( workdir , submodule )
194
- if File . exist? ( File . join ( submodule_path , '.gitted' ) )
195
- Dir . chdir ( submodule_path ) do
196
- File . rename ( '.gitted' , '.git' )
197
- end
198
- end
199
- end
200
- end
201
- end
202
193
end
0 commit comments