@@ -190,37 +190,37 @@ def execute_op(self):
190
190
ref = self .request_data .get ("ref" , None )
191
191
192
192
if ref :
193
- repository = Repository (project .abs_path )
194
- if ref != repository .active_branch .name :
195
- # NOTE: Command called for different branch than the one used in cache, change branch
196
- if len (repository .remotes ) != 1 :
197
- raise RenkuException ("Couldn't find remote for project in cache." )
198
- origin = repository .remotes [0 ]
199
- remote_branch = f"{ origin } /{ ref } "
200
-
201
- with project .write_lock ():
202
- # NOTE: Add new ref to remote branches
203
- repository .run_git_command ("remote" , "set-branches" , "--add" , origin , ref )
204
- if self .migrate_project or self .clone_depth == PROJECT_CLONE_NO_DEPTH :
205
- repository .fetch (origin , ref )
206
- else :
207
- repository .fetch (origin , ref , depth = self .clone_depth )
208
-
209
- # NOTE: Switch to new ref
210
- repository .run_git_command ("checkout" , "--track" , "-f" , "-b" , ref , remote_branch )
211
-
212
- # NOTE: cleanup remote branches in case a remote was deleted (fetch fails otherwise)
213
- repository .run_git_command ("remote" , "prune" , origin )
214
-
215
- for branch in repository .branches :
216
- if branch .remote_branch and not branch .remote_branch .is_valid ():
217
- repository .branches .remove (branch , force = True )
218
- # NOTE: Remove left-over refspec
219
- try :
220
- with repository .get_configuration (writable = True ) as config :
221
- config .remove_value (f"remote.{ origin } .fetch" , f"origin.{ branch } $" )
222
- except GitConfigurationError :
223
- pass
193
+ with Repository (project .abs_path ) as repository :
194
+ if ref != repository .active_branch .name :
195
+ # NOTE: Command called for different branch than the one used in cache, change branch
196
+ if len (repository .remotes ) != 1 :
197
+ raise RenkuException ("Couldn't find remote for project in cache." )
198
+ origin = repository .remotes [0 ]
199
+ remote_branch = f"{ origin } /{ ref } "
200
+
201
+ with project .write_lock ():
202
+ # NOTE: Add new ref to remote branches
203
+ repository .run_git_command ("remote" , "set-branches" , "--add" , origin , ref )
204
+ if self .migrate_project or self .clone_depth == PROJECT_CLONE_NO_DEPTH :
205
+ repository .fetch (origin , ref )
206
+ else :
207
+ repository .fetch (origin , ref , depth = self .clone_depth )
208
+
209
+ # NOTE: Switch to new ref
210
+ repository .run_git_command ("checkout" , "--track" , "-f" , "-b" , ref , remote_branch )
211
+
212
+ # NOTE: cleanup remote branches in case a remote was deleted (fetch fails otherwise)
213
+ repository .run_git_command ("remote" , "prune" , origin )
214
+
215
+ for branch in repository .branches :
216
+ if branch .remote_branch and not branch .remote_branch .is_valid ():
217
+ repository .branches .remove (branch , force = True )
218
+ # NOTE: Remove left-over refspec
219
+ try :
220
+ with repository .get_configuration (writable = True ) as config :
221
+ config .remove_value (f"remote.{ origin } .fetch" , f"origin.{ branch } $" )
222
+ except GitConfigurationError :
223
+ pass
224
224
else :
225
225
self .reset_local_repo (project )
226
226
@@ -250,33 +250,33 @@ def reset_local_repo(self, project):
250
250
# NOTE: return immediately in case of multiple writers waiting
251
251
return
252
252
253
- repository = Repository (project .abs_path )
254
- origin = None
255
- tracking_branch = repository .active_branch .remote_branch
256
- if tracking_branch :
257
- origin = tracking_branch .remote
258
- elif len (repository .remotes ) == 1 :
259
- origin = repository .remotes [0 ]
260
-
261
- if origin :
262
- unshallow = self .migrate_project or self .clone_depth == PROJECT_CLONE_NO_DEPTH
263
- if unshallow :
264
- try :
265
- # NOTE: It could happen that repository is already un-shallowed,
266
- # in this case we don't want to leak git exception, but still want to fetch.
267
- repository .fetch ("origin" , repository .active_branch , unshallow = True )
268
- except GitCommandError :
269
- repository .fetch ("origin" , repository .active_branch )
270
-
271
- repository .reset (f"{ origin } /{ repository .active_branch } " , hard = True )
272
- else :
273
- try :
274
- # NOTE: it rarely happens that origin is not reachable. Try again if it fails.
275
- repository .fetch ("origin" , repository .active_branch )
253
+ with Repository (project .abs_path ) as repository :
254
+ origin = None
255
+ tracking_branch = repository .active_branch .remote_branch
256
+ if tracking_branch :
257
+ origin = tracking_branch .remote
258
+ elif len (repository .remotes ) == 1 :
259
+ origin = repository .remotes [0 ]
260
+
261
+ if origin :
262
+ unshallow = self .migrate_project or self .clone_depth == PROJECT_CLONE_NO_DEPTH
263
+ if unshallow :
264
+ try :
265
+ # NOTE: It could happen that repository is already un-shallowed,
266
+ # in this case we don't want to leak git exception, but still want to fetch.
267
+ repository .fetch ("origin" , repository .active_branch , unshallow = True )
268
+ except GitCommandError :
269
+ repository .fetch ("origin" , repository .active_branch )
270
+
276
271
repository .reset (f"{ origin } /{ repository .active_branch } " , hard = True )
277
- except GitCommandError as e :
278
- project .purge ()
279
- raise IntermittentCacheError (e )
272
+ else :
273
+ try :
274
+ # NOTE: it rarely happens that origin is not reachable. Try again if it fails.
275
+ repository .fetch ("origin" , repository .active_branch )
276
+ repository .reset (f"{ origin } /{ repository .active_branch } " , hard = True )
277
+ except GitCommandError as e :
278
+ project .purge ()
279
+ raise IntermittentCacheError (e )
280
280
project .last_fetched_at = datetime .utcnow ()
281
281
project .save ()
282
282
except (portalocker .LockException , portalocker .AlreadyLocked ) as e :
@@ -346,7 +346,8 @@ def sync(self, remote="origin"):
346
346
if self .project_path is None :
347
347
raise RenkuException ("unable to sync with remote since no operation has been executed" )
348
348
349
- return push_changes (Repository (self .project_path ), remote = remote )
349
+ with Repository (self .project_path ) as repository :
350
+ return push_changes (repository , remote = remote )
350
351
351
352
def execute_and_sync (self , remote = "origin" ):
352
353
"""Execute operation which controller implements and sync with the remote."""
0 commit comments