Linux maintainer tooling and workflows
 help / color / mirror / Atom feed
From: Louis Chauvet <louis.chauvet@bootlin.com>
To: "Kernel.org Tools" <tools@kernel.org>
Cc: Konstantin Ryabitsev <konstantin@linuxfoundation.org>,
	 miquel.raynal@bootlin.com, thomas.petazzoni@bootlin.com,
	 Louis Chauvet <louis.chauvet@bootlin.com>
Subject: [PATCH b4] ez: Prevent overwriting an unrelated cover letter
Date: Thu, 28 Mar 2024 10:25:43 +0100	[thread overview]
Message-ID: <20240328-avoid-overwritting-cover-letter-v1-1-161d30ee533c@bootlin.com> (raw)

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*

Add a simple check to only overwrite the cover-letter if it corresponds
to the one that was originally opened, otherwise warn the user and save
it in a temporary spot.

Cc: tools@kernel.org
Cc: miquel.raynal@bootlin.com
Cc: thomas.petazzoni@bootlin.com

Signed-off-by: Louis Chauvet <louis.chauvet@bootlin.com>
---
 src/b4/ez.py | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/src/b4/ez.py b/src/b4/ez.py
index 1d360410d771..c322953564a0 100644
--- a/src/b4/ez.py
+++ b/src/b4/ez.py
@@ -8,6 +8,8 @@ __author__ = 'Konstantin Ryabitsev <konstantin@linuxfoundation.org>'
 import email.message
 import os
 import sys
+from tempfile import NamedTemporaryFile
+
 import b4
 import re
 import argparse
@@ -758,6 +760,8 @@ class FRCommitMessageEditor:
 
 
 def edit_cover() -> None:
+    # 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 = b4.git_get_current_branch()
     cover, tracking = load_cover()
     bcover = cover.encode()
     new_bcover = b4.edit_in_editor(bcover, filehint='COMMIT_EDITMSG')
@@ -768,8 +772,15 @@ def edit_cover() -> None:
     if not len(new_cover):
         logger.info('New cover letter blank, leaving current one unchanged.')
         return
-
-    store_cover(new_cover, tracking)
+    write_branch = b4.git_get_current_branch()
+    if write_branch != read_branch:
+        with NamedTemporaryFile(mode="w", prefix=f"cover-{read_branch}".replace("/", "-"), delete=False) as save_file:
+            save_file.write(new_cover)
+            logger.critical(f'The cover letter was read on the branch {read_branch} but the current branch is now {write_branch}.')
+            logger.critical(f'To avoid overwriting an unrelated cover letter, the operation is canceled and your new '
+                            f'cover letter stored at {save_file.name}')
+    else:
+        store_cover(new_cover, tracking)
     logger.info('Cover letter updated.')
 
 

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

Best regards,
-- 
Louis Chauvet <louis.chauvet@bootlin.com>


             reply	other threads:[~2024-03-28  9:35 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-28  9:25 Louis Chauvet [this message]
2024-03-28 14:19 ` [PATCH b4] ez: Prevent overwriting an unrelated cover letter Konstantin Ryabitsev

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240328-avoid-overwritting-cover-letter-v1-1-161d30ee533c@bootlin.com \
    --to=louis.chauvet@bootlin.com \
    --cc=konstantin@linuxfoundation.org \
    --cc=miquel.raynal@bootlin.com \
    --cc=thomas.petazzoni@bootlin.com \
    --cc=tools@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).