editor: do not rely on `the_repository` for interactive edits

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>

Patrick Steinhardt committed Aug 13, 2024 at 11:13 UTC 419dbb29d82b78bcaf0ff22ac7d5db7d1c327575
3 files changed +13 -8
add-patch.c
+2 -1
@@ -1140,7 +1140,8 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
1140 "removed, then the edit is\n"
1141 "aborted and the hunk is left unchanged.\n"));
1142
1143 - if (strbuf_edit_interactively(&s->buf, "addp-hunk-edit.diff", NULL) < 0)
1143 + if (strbuf_edit_interactively(the_repository, &s->buf,
1144 + "addp-hunk-edit.diff", NULL) < 0)
1145 return -1;
1146
1147 /* strip out commented lines */
editor.c
+8 -5
@@ -133,14 +133,17 @@ int launch_sequence_editor(const char *path, struct strbuf *buffer,
133 return launch_specified_editor(git_sequence_editor(), path, buffer, env);
134 }
135
136 -int strbuf_edit_interactively(struct strbuf *buffer, const char *path,
136 +int strbuf_edit_interactively(struct repository *r,
137 + struct strbuf *buffer, const char *path,
138 const char *const *env)
139 {
139 - char *path2 = NULL;
140 + struct strbuf sb = STRBUF_INIT;
141 int fd, res = 0;
142
142 - if (!is_absolute_path(path))
143 - path = path2 = xstrdup(git_path("%s", path));
143 + if (!is_absolute_path(path)) {
144 + strbuf_repo_git_path(&sb, r, "%s", path);
145 + path = sb.buf;
146 + }
147
148 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
149 if (fd < 0)
@@ -157,6 +160,6 @@ int strbuf_edit_interactively(struct strbuf *buffer, const char *path,
160 unlink(path);
161 }
162
160 - free(path2);
163 + strbuf_release(&sb);
164 return res;
165 }
editor.h
+3 -2
@@ -1,6 +1,7 @@
1 #ifndef EDITOR_H
2 #define EDITOR_H
3
4 +struct repository;
5 struct strbuf;
6
7 const char *git_editor(void);
@@ -28,7 +29,7 @@ int launch_sequence_editor(const char *path, struct strbuf *buffer,
29 *
30 * If `path` is relative, it refers to a file in the `.git` directory.
31 */
31 -int strbuf_edit_interactively(struct strbuf *buffer, const char *path,
32 - const char *const *env);
32 +int strbuf_edit_interactively(struct repository *r, struct strbuf *buffer,
33 + const char *path, const char *const *env);
34
35 #endif