Skip to content

Commit 419dbb2

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 61419a4 commit 419dbb2

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

add-patch.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,8 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
11401140
"removed, then the edit is\n"
11411141
"aborted and the hunk is left unchanged.\n"));
11421142

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)
11441145
return -1;
11451146

11461147
/* strip out commented lines */

editor.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,17 @@ int launch_sequence_editor(const char *path, struct strbuf *buffer,
133133
return launch_specified_editor(git_sequence_editor(), path, buffer, env);
134134
}
135135

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,
137138
const char *const *env)
138139
{
139-
char *path2 = NULL;
140+
struct strbuf sb = STRBUF_INIT;
140141
int fd, res = 0;
141142

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+
}
144147

145148
fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
146149
if (fd < 0)
@@ -157,6 +160,6 @@ int strbuf_edit_interactively(struct strbuf *buffer, const char *path,
157160
unlink(path);
158161
}
159162

160-
free(path2);
163+
strbuf_release(&sb);
161164
return res;
162165
}

editor.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef EDITOR_H
22
#define EDITOR_H
33

4+
struct repository;
45
struct strbuf;
56

67
const char *git_editor(void);
@@ -28,7 +29,7 @@ int launch_sequence_editor(const char *path, struct strbuf *buffer,
2829
*
2930
* If `path` is relative, it refers to a file in the `.git` directory.
3031
*/
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);
3334

3435
#endif

0 commit comments

Comments
 (0)