Skip to content

Commit ff84b26

Browse files
committed
Refactor try-finally cleanup in git/
This is, in part, to help avoid (or be able to notice) other bugs like the rollback bug that affected SymbolicReference. However, it also includes a simplification of try-(try-except)-finally to try-except-finally.
1 parent 592ec84 commit ff84b26

File tree

3 files changed

+14
-19
lines changed

3 files changed

+14
-19
lines changed

git/config.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -406,15 +406,14 @@ def release(self) -> None:
406406
return
407407

408408
try:
409-
try:
410-
self.write()
411-
except IOError:
412-
log.error("Exception during destruction of GitConfigParser", exc_info=True)
413-
except ReferenceError:
414-
# This happens in PY3 ... and usually means that some state cannot be written
415-
# as the sections dict cannot be iterated
416-
# Usually when shutting down the interpreter, don'y know how to fix this
417-
pass
409+
self.write()
410+
except IOError:
411+
log.error("Exception during destruction of GitConfigParser", exc_info=True)
412+
except ReferenceError:
413+
# This happens in PY3 ... and usually means that some state cannot be
414+
# written as the sections dict cannot be iterated
415+
# Usually when shutting down the interpreter, don't know how to fix this
416+
pass
418417
finally:
419418
if self._lock is not None:
420419
self._lock._release_lock()

git/index/base.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,11 @@ def write(
224224
lfd = LockedFD(file_path or self._file_path)
225225
stream = lfd.open(write=True, stream=True)
226226

227-
ok = False
228227
try:
229228
self._serialize(stream, ignore_extension_data)
230-
ok = True
231-
finally:
232-
if not ok:
233-
lfd.rollback()
229+
except BaseException:
230+
lfd.rollback()
231+
raise
234232

235233
lfd.commit()
236234

git/refs/symbolic.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -370,14 +370,12 @@ def set_reference(
370370

371371
lfd = LockedFD(fpath)
372372
fd = lfd.open(write=True, stream=True)
373-
ok = False
374373
try:
375374
fd.write(write_value.encode("utf-8") + b"\n")
376375
lfd.commit()
377-
ok = True
378-
finally:
379-
if not ok:
380-
lfd.rollback()
376+
except BaseException:
377+
lfd.rollback()
378+
raise
381379
# Adjust the reflog
382380
if logmsg is not None:
383381
self.log_append(oldbinsha, logmsg)

0 commit comments

Comments
 (0)