public inbox for tools@linux.kernel.org
 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
  2026-02-24 19:28 ` Konstantin Ryabitsev
  0 siblings, 2 replies; 5+ 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] 5+ 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
  2026-02-24 19:28 ` Konstantin Ryabitsev
  1 sibling, 0 replies; 5+ 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] 5+ 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
@ 2026-02-24 19:28 ` Konstantin Ryabitsev
  2026-02-24 21:59   ` Dave Marquardt
  1 sibling, 1 reply; 5+ messages in thread
From: Konstantin Ryabitsev @ 2026-02-24 19:28 UTC (permalink / raw)
  To: Dave Marquardt; +Cc: Kernel.org Tools, Konstantin Ryabitsev

On Tue, 18 Nov 2025 15:34:25 -0600, Dave Marquardt <davemarq@linux.ibm.com> wrote:
> 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'

Hardcoding `.git` will break when b4 is run inside a git worktree,
where `.git` is a file rather than a directory.  This will cause
`p.mkdir()` below to raise NotADirectoryError.

The existing code in mbox.py:395 already solves this correctly:

    gitargs = ['rev-parse', '--git-dir']
    ecode, out = b4.git_run_command(topdir, gitargs, logstderr=True)

Consider using `git rev-parse --git-dir` to obtain the actual git
directory and then creating a `b4/` subdirectory under it.

Regards,

-- 
KR


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

* Re: [PATCH RESEND v2] Make edit_in_editor use temporary file in Git repository
  2026-02-24 19:28 ` Konstantin Ryabitsev
@ 2026-02-24 21:59   ` Dave Marquardt
  2026-02-24 22:12     ` Konstantin Ryabitsev
  0 siblings, 1 reply; 5+ messages in thread
From: Dave Marquardt @ 2026-02-24 21:59 UTC (permalink / raw)
  To: Konstantin Ryabitsev; +Cc: Kernel.org Tools

Konstantin Ryabitsev <konstantin@linuxfoundation.org> writes:

> On Tue, 18 Nov 2025 15:34:25 -0600, Dave Marquardt <davemarq@linux.ibm.com> wrote:
>> 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'
>
> Hardcoding `.git` will break when b4 is run inside a git worktree,
> where `.git` is a file rather than a directory.  This will cause
> `p.mkdir()` below to raise NotADirectoryError.
>
> The existing code in mbox.py:395 already solves this correctly:
>
>     gitargs = ['rev-parse', '--git-dir']
>     ecode, out = b4.git_run_command(topdir, gitargs, logstderr=True)
>
> Consider using `git rev-parse --git-dir` to obtain the actual git
> directory and then creating a `b4/` subdirectory under it.

The current code in the b4 tree includes my changes from Nov. 19, commit
ab9639a7685a0e488f7d32783d55aaa2101acc68, and that code doesn't do what
is shown here. I think there were more iterations. I'm not sure why
we're discussing this old thread.

-Dave


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

* Re: [PATCH RESEND v2] Make edit_in_editor use temporary file in Git repository
  2026-02-24 21:59   ` Dave Marquardt
@ 2026-02-24 22:12     ` Konstantin Ryabitsev
  0 siblings, 0 replies; 5+ messages in thread
From: Konstantin Ryabitsev @ 2026-02-24 22:12 UTC (permalink / raw)
  To: Dave Marquardt; +Cc: Kernel.org Tools

On Tue, Feb 24, 2026 at 03:59:51PM -0600, Dave Marquardt wrote:
> > Consider using `git rev-parse --git-dir` to obtain the actual git
> > directory and then creating a `b4/` subdirectory under it.
> 
> The current code in the b4 tree includes my changes from Nov. 19, commit
> ab9639a7685a0e488f7d32783d55aaa2101acc68, and that code doesn't do what
> is shown here. I think there were more iterations. I'm not sure why
> we're discussing this old thread.

Ha, because I'm dogfooding "b4 review" and I have a memory of whatever is the
opposite of elephant.

Sorry about that! :)


-- 
KR

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

end of thread, other threads:[~2026-02-24 22:12 UTC | newest]

Thread overview: 5+ 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
2026-02-24 19:28 ` Konstantin Ryabitsev
2026-02-24 21:59   ` Dave Marquardt
2026-02-24 22:12     ` Konstantin Ryabitsev

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox