]> git.ipfire.org Git - thirdparty/git.git/commitdiff
editor: do not rely on `the_repository` for interactive edits
authorPatrick Steinhardt <ps@pks.im>
Tue, 13 Aug 2024 09:13:25 +0000 (11:13 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 13 Aug 2024 17:01:00 +0000 (10:01 -0700)
We implicitly rely on `the_repository` when editing a file interactively
because we call `git_path()`. Adapt the function to instead take a
`struct repository` as a parameter so that we can remove this hidden
dependency.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
add-patch.c
editor.c
editor.h

index 46f6bddfe5cb75922fece4c9319899377cf2b7fb..218dda3e79a180ead4bddb16e37da3684e43f39a 100644 (file)
@@ -1140,7 +1140,8 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
                                "removed, then the edit is\n"
                                "aborted and the hunk is left unchanged.\n"));
 
-       if (strbuf_edit_interactively(&s->buf, "addp-hunk-edit.diff", NULL) < 0)
+       if (strbuf_edit_interactively(the_repository, &s->buf,
+                                     "addp-hunk-edit.diff", NULL) < 0)
                return -1;
 
        /* strip out commented lines */
index d1ba2d7c34fbf4bfa9c9b21c51ada7dd88fcf1e6..e93b4fdb09c1a004cd89be0cb1b36fa97c9c54f8 100644 (file)
--- a/editor.c
+++ b/editor.c
@@ -133,14 +133,17 @@ int launch_sequence_editor(const char *path, struct strbuf *buffer,
        return launch_specified_editor(git_sequence_editor(), path, buffer, env);
 }
 
-int strbuf_edit_interactively(struct strbuf *buffer, const char *path,
+int strbuf_edit_interactively(struct repository *r,
+                             struct strbuf *buffer, const char *path,
                              const char *const *env)
 {
-       char *path2 = NULL;
+       struct strbuf sb = STRBUF_INIT;
        int fd, res = 0;
 
-       if (!is_absolute_path(path))
-               path = path2 = xstrdup(git_path("%s", path));
+       if (!is_absolute_path(path)) {
+               strbuf_repo_git_path(&sb, r, "%s", path);
+               path = sb.buf;
+       }
 
        fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
        if (fd < 0)
@@ -157,6 +160,6 @@ int strbuf_edit_interactively(struct strbuf *buffer, const char *path,
                unlink(path);
        }
 
-       free(path2);
+       strbuf_release(&sb);
        return res;
 }
index 8016bb5e00b3ca2d21105b94d504505728fe5e69..f1c41df378c5c3ae5e54ee4985eddecba54f00b4 100644 (file)
--- a/editor.h
+++ b/editor.h
@@ -1,6 +1,7 @@
 #ifndef EDITOR_H
 #define EDITOR_H
 
+struct repository;
 struct strbuf;
 
 const char *git_editor(void);
@@ -28,7 +29,7 @@ int launch_sequence_editor(const char *path, struct strbuf *buffer,
  *
  * If `path` is relative, it refers to a file in the `.git` directory.
  */
-int strbuf_edit_interactively(struct strbuf *buffer, const char *path,
-                             const char *const *env);
+int strbuf_edit_interactively(struct repository *r, struct strbuf *buffer,
+                             const char *path, const char *const *env);
 
 #endif