@@ -134,9 +134,8 @@ def _get_ref_info(cls, repo, ref_path):
134
134
point to, or None"""
135
135
tokens = None
136
136
try :
137
- fp = open (join (repo .git_dir , ref_path ), 'rt' )
138
- value = fp .read ().rstrip ()
139
- fp .close ()
137
+ with open (join (repo .git_dir , ref_path ), 'rt' ) as fp :
138
+ value = fp .read ().rstrip ()
140
139
# Don't only split on spaces, but on whitespace, which allows to parse lines like
141
140
# 60b64ef992065e2600bfef6187a97f92398a9144 branch 'master' of git-server:/path/to/repo
142
141
tokens = value .split ()
@@ -313,13 +312,17 @@ def set_reference(self, ref, logmsg=None):
313
312
314
313
lfd = LockedFD (fpath )
315
314
fd = lfd .open (write = True , stream = True )
316
- fd .write (write_value .encode ('ascii' ) + b'\n ' )
317
- lfd .commit ()
318
-
315
+ ok = True
316
+ try :
317
+ fd .write (write_value .encode ('ascii' ) + b'\n ' )
318
+ lfd .commit ()
319
+ ok = True
320
+ finally :
321
+ if not ok :
322
+ lfd .rollback ()
319
323
# Adjust the reflog
320
324
if logmsg is not None :
321
325
self .log_append (oldbinsha , logmsg )
322
- # END handle reflog
323
326
324
327
return self
325
328
@@ -422,40 +425,36 @@ def delete(cls, repo, path):
422
425
# check packed refs
423
426
pack_file_path = cls ._get_packed_refs_path (repo )
424
427
try :
425
- reader = open (pack_file_path , 'rb' )
426
- except (OSError , IOError ):
427
- pass # it didnt exist at all
428
- else :
429
- new_lines = list ()
430
- made_change = False
431
- dropped_last_line = False
432
- for line in reader :
433
- # keep line if it is a comment or if the ref to delete is not
434
- # in the line
435
- # If we deleted the last line and this one is a tag-reference object,
436
- # we drop it as well
437
- line = line .decode (defenc )
438
- if (line .startswith ('#' ) or full_ref_path not in line ) and \
439
- (not dropped_last_line or dropped_last_line and not line .startswith ('^' )):
440
- new_lines .append (line )
441
- dropped_last_line = False
442
- continue
443
- # END skip comments and lines without our path
444
-
445
- # drop this line
446
- made_change = True
447
- dropped_last_line = True
448
- # END for each line in packed refs
449
- reader .close ()
428
+ with open (pack_file_path , 'rb' ) as reader :
429
+ new_lines = list ()
430
+ made_change = False
431
+ dropped_last_line = False
432
+ for line in reader :
433
+ # keep line if it is a comment or if the ref to delete is not
434
+ # in the line
435
+ # If we deleted the last line and this one is a tag-reference object,
436
+ # we drop it as well
437
+ line = line .decode (defenc )
438
+ if (line .startswith ('#' ) or full_ref_path not in line ) and \
439
+ (not dropped_last_line or dropped_last_line and not line .startswith ('^' )):
440
+ new_lines .append (line )
441
+ dropped_last_line = False
442
+ continue
443
+ # END skip comments and lines without our path
444
+
445
+ # drop this line
446
+ made_change = True
447
+ dropped_last_line = True
450
448
451
449
# write the new lines
452
450
if made_change :
453
451
# write-binary is required, otherwise windows will
454
452
# open the file in text mode and change LF to CRLF !
455
- open (pack_file_path , 'wb' ).writelines (l .encode (defenc ) for l in new_lines )
456
- # END write out file
457
- # END open exception handling
458
- # END handle deletion
453
+ with open (pack_file_path , 'wb' ) as fd :
454
+ fd .writelines (l .encode (defenc ) for l in new_lines )
455
+
456
+ except (OSError , IOError ):
457
+ pass # it didnt exist at all
459
458
460
459
# delete the reflog
461
460
reflog_path = RefLog .path (cls (repo , full_ref_path ))
@@ -484,7 +483,8 @@ def _create(cls, repo, path, resolve, reference, force, logmsg=None):
484
483
target_data = target .path
485
484
if not resolve :
486
485
target_data = "ref: " + target_data
487
- existing_data = open (abs_ref_path , 'rb' ).read ().decode (defenc ).strip ()
486
+ with open (abs_ref_path , 'rb' ) as fd :
487
+ existing_data = fd .read ().decode (defenc ).strip ()
488
488
if existing_data != target_data :
489
489
raise OSError ("Reference at %r does already exist, pointing to %r, requested was %r" %
490
490
(full_ref_path , existing_data , target_data ))
@@ -549,7 +549,11 @@ def rename(self, new_path, force=False):
549
549
if isfile (new_abs_path ):
550
550
if not force :
551
551
# if they point to the same file, its not an error
552
- if open (new_abs_path , 'rb' ).read ().strip () != open (cur_abs_path , 'rb' ).read ().strip ():
552
+ with open (new_abs_path , 'rb' ) as fd1 :
553
+ f1 = fd1 .read ().strip ()
554
+ with open (cur_abs_path , 'rb' ) as fd2 :
555
+ f2 = fd2 .read ().strip ()
556
+ if f1 != f2 :
553
557
raise OSError ("File at path %r already exists" % new_abs_path )
554
558
# else: we could remove ourselves and use the otherone, but
555
559
# but clarity we just continue as usual
0 commit comments