diff mbox series

[b4,v2] ez: Prevent overwriting an unrelated cover letter

Message ID 20240403-avoid-overwritting-cover-letter-v2-1-cb43a8df8ce2@bootlin.com (mailing list archive)
State Accepted
Headers show
Series [b4,v2] ez: Prevent overwriting an unrelated cover letter | expand

Commit Message

Louis Chauvet April 3, 2024, 1:29 p.m. UTC
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*

As suggested by Konstantin in [1], instead of checking only for the cover
letter, this patch add a check around edit_in_editor. If the branch
changes during the edition, the operation is aborted and a backup of the
new file is saved.

Signed-off-by: Louis Chauvet <louis.chauvet@bootlin.com>

[1]: https://lore.kernel.org/all/20240328-premium-mouflon-of-tolerance-bdcdb5@lemur/

---
Changes in v2:
- Moved the check to edit_in_editor
- Link to v1: https://msgid.link/20240328-avoid-overwritting-cover-letter-v1-1-161d30ee533c@bootlin.com
---
 src/b4/__init__.py | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)


---
base-commit: 23970c613f40356cc88716d07c2d427ca024e489
change-id: 20240328-avoid-overwritting-cover-letter-554bcd29b240

Best regards,

Comments

Konstantin Ryabitsev April 3, 2024, 4:55 p.m. UTC | #1
On Wed, 03 Apr 2024 15:29:17 +0200, Louis Chauvet wrote:
> 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:
> 
> [...]

Applied, thanks!

[1/1] ez: Prevent overwriting an unrelated cover letter
      commit: cfcbbe6f23943166f11687ecf37252dc20728639

Best regards,
diff mbox series

Patch

diff --git a/src/b4/__init__.py b/src/b4/__init__.py
index 169b461bca1a..3af032e3ff83 100644
--- a/src/b4/__init__.py
+++ b/src/b4/__init__.py
@@ -4157,6 +4157,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)
@@ -4176,4 +4179,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