aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLouis Chauvet <louis.chauvet@bootlin.com>2024-04-03 15:29:17 +0200
committerKonstantin Ryabitsev <konstantin@linuxfoundation.org>2024-04-03 12:53:38 -0400
commitcfcbbe6f23943166f11687ecf37252dc20728639 (patch)
treed9dd6d17b862e0614d46cbb0bb83236270fab86a
parent5d9e72e492094f9e1faaf54c7fd5027bcb574924 (diff)
downloadb4-cfcbbe6f23943166f11687ecf37252dc20728639.tar.gz
ez: Prevent overwriting an unrelated cover letter
When editing a cover-letter, b4 expectedly selects as base the cover-letter of the currently checked-out Git branch. When saving/closing the editor, b4 also saves the changes as the new cover-letter for the currently checked-out Git branch. While simplistic and apparently totally fine, it does not play well when working on multiple branches. Said otherwise, the following sequence of events will write the wrong file, possibly smashing a valuable cover-letter: $ b4 prep --edit-cover <make some changes> $ git checkout another-branch <oh, I forgot to save and close the cover letter editor!> *crunch* Signed-off-by: Louis Chauvet <louis.chauvet@bootlin.com> Link: https://msgid.link/20240403-avoid-overwritting-cover-letter-v2-1-cb43a8df8ce2@bootlin.com Signed-off-by: Konstantin Ryabitsev <konstantin@linuxfoundation.org>
-rw-r--r--src/b4/__init__.py13
1 files changed, 12 insertions, 1 deletions
diff --git a/src/b4/__init__.py b/src/b4/__init__.py
index e887473..e88de37 100644
--- a/src/b4/__init__.py
+++ b/src/b4/__init__.py
@@ -4164,6 +4164,9 @@ def git_fetch_am_into_repo(gitdir: Optional[str], ambytes: bytes, at_base: str =
def edit_in_editor(bdata: bytes, filehint: str = 'COMMIT_EDITMSG') -> bytes:
+ # To avoid losing the cover letter, ensure that we are still on the same branch as when the cover-letter was originally opened.
+ read_branch = git_get_current_branch()
+
corecfg = get_config_from_git(r'core\..*', {'editor': os.environ.get('EDITOR', 'vi')})
editor = corecfg.get('editor')
logger.debug('editor=%s', editor)
@@ -4183,4 +4186,12 @@ def edit_in_editor(bdata: bytes, filehint: str = 'COMMIT_EDITMSG') -> bytes:
with open(temp_fpath, 'rb') as edited_file:
bdata = edited_file.read()
- return bdata
+ write_branch = git_get_current_branch()
+ if write_branch != read_branch:
+ with tempfile.NamedTemporaryFile(mode="wb", prefix=f"old-{read_branch}".replace("/", "-"), delete=False) as save_file:
+ save_file.write(bdata)
+ logger.critical('The edition started on the branch %s but the current branch is now %s.', read_branch, write_branch)
+ logger.critical('To avoid overwriting an unrelated text, the operation is canceled now and your text is stored at %s', save_file.name)
+ raise RuntimeError(f"Branch changed during file edition, the temporary file was saved at {save_file.name}")
+ else:
+ return bdata