tools.linux.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH RESEND v2] Make edit_in_editor use temporary file in Git repository
@ 2025-11-18 21:34 Dave Marquardt via B4 Relay
  2025-11-18 22:41 ` Dave Marquardt
  0 siblings, 1 reply; 2+ messages in thread
From: Dave Marquardt via B4 Relay @ 2025-11-18 21:34 UTC (permalink / raw)
  To: Kernel.org Tools; +Cc: Konstantin Ryabitsev, Dave Marquardt

From: Dave Marquardt <davemarq@linux.ibm.com>



---
This is a resend of a patch I sent in June 2025. I noticed on
reinstalling b4 that this hadn't been included in recent releases, and
I couldn't find it in the master branch either.

In edit_with_editor, create temporary directory in toplevel/.git/b4 if
git top level exists. Otherwise let tempfile.TemporaryDirectory use
its default directory. This avoids issues with Emacs and Magit, which
insists COMMIT_EDITMSG must be in the repo's directory tree.

Signed-off-by: Dave Marquardt <davemarq@linux.ibm.com>
---
 src/b4/__init__.py | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/b4/__init__.py b/src/b4/__init__.py
index 7689550..cc7623a 100644
--- a/src/b4/__init__.py
+++ b/src/b4/__init__.py
@@ -4575,8 +4575,16 @@ def edit_in_editor(bdata: bytes, filehint: str = 'COMMIT_EDITMSG') -> bytes:
     corecfg = get_config_from_git(r'core\..*', {'editor': os.environ.get('EDITOR', 'vi')})
     editor = corecfg.get('editor')
     logger.debug('editor=%s', editor)
+
+    topdir = git_get_toplevel()
+    if topdir is not None:
+        p = Path(topdir)
+        p = p / '.git' / 'b4'
+        p.mkdir(parents=True, exist_ok=True)
+    else:
+        p = None
     # Use filehint name in hopes that editors autoload necessary highlight rules
-    with tempfile.TemporaryDirectory(prefix='b4-editor') as temp_dir:
+    with tempfile.TemporaryDirectory(prefix='b4-editor', dir=p) as temp_dir:
         temp_fpath = os.path.join(temp_dir, filehint)
         with open(temp_fpath, 'xb') as edit_file:
             edit_file.write(bdata)

---
base-commit: a6db3f06ce9836a7fc8921f588452804fc62bed1
change-id: 20251118-edit-with-tmp-file-in-repo-bc0d0465661a

Best regards,
--  
Dave Marquardt <davemarq@linux.ibm.com>



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH RESEND v2] Make edit_in_editor use temporary file in Git repository
  2025-11-18 21:34 [PATCH RESEND v2] Make edit_in_editor use temporary file in Git repository Dave Marquardt via B4 Relay
@ 2025-11-18 22:41 ` Dave Marquardt
  0 siblings, 0 replies; 2+ messages in thread
From: Dave Marquardt @ 2025-11-18 22:41 UTC (permalink / raw)
  To: Dave Marquardt via B4 Relay; +Cc: Kernel.org Tools, Konstantin Ryabitsev

Dave Marquardt via B4 Relay <devnull+davemarq.linux.ibm.com@kernel.org>
writes:

> This is a resend of a patch I sent in June 2025. I noticed on
> reinstalling b4 that this hadn't been included in recent releases, and
> I couldn't find it in the master branch either.
>
> In edit_with_editor, create temporary directory in toplevel/.git/b4 if
> git top level exists. Otherwise let tempfile.TemporaryDirectory use
> its default directory. This avoids issues with Emacs and Magit, which
> insists COMMIT_EDITMSG must be in the repo's directory tree.

Since I sent this, I discovered this doesn't work with Git worktrees,
where the Git toplevel .git file is a regular file, not a directory. I
tried changing the code below to use Path(topdir) / '.b4' instead, and
that works fine. I suppose if you happen to have a .b4 subdirectory in a
repo or repo worktree that won't work.

I will send another patch for this tomorrow, when I've had more time to
think about it.

> Signed-off-by: Dave Marquardt <davemarq@linux.ibm.com>
> ---
>  src/b4/__init__.py | 10 +++++++++-
>  1 file changed, 9 insertions(+), 1 deletion(-)
>
> diff --git a/src/b4/__init__.py b/src/b4/__init__.py
> index 7689550..cc7623a 100644
> --- a/src/b4/__init__.py
> +++ b/src/b4/__init__.py
> @@ -4575,8 +4575,16 @@ def edit_in_editor(bdata: bytes, filehint: str = 'COMMIT_EDITMSG') -> bytes:
>      corecfg = get_config_from_git(r'core\..*', {'editor': os.environ.get('EDITOR', 'vi')})
>      editor = corecfg.get('editor')
>      logger.debug('editor=%s', editor)
> +
> +    topdir = git_get_toplevel()
> +    if topdir is not None:
> +        p = Path(topdir)
> +        p = p / '.git' / 'b4'
> +        p.mkdir(parents=True, exist_ok=True)
> +    else:
> +        p = None
>      # Use filehint name in hopes that editors autoload necessary highlight rules
> -    with tepmfile.TemporaryDirectory(prefix='b4-editor') as temp_dir:
> +    with tempfile.TemporaryDirectory(prefix='b4-editor', dir=p) as temp_dir:
>          temp_fpath = os.path.join(temp_dir, filehint)
>          with open(temp_fpath, 'xb') as edit_file:
>              edit_file.write(bdata)
>
> ---
> base-commit: a6db3f06ce9836a7fc8921f588452804fc62bed1
> change-id: 20251118-edit-with-tmp-file-in-repo-bc0d0465661a
>
> Best regards,
> --  
>
> Dave Marquardt <davemarq@linux.ibm.com>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-11-18 22:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-18 21:34 [PATCH RESEND v2] Make edit_in_editor use temporary file in Git repository Dave Marquardt via B4 Relay
2025-11-18 22:41 ` Dave Marquardt

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 NNTP newsgroup(s).