editor: move editor-related functions and declarations into common file
cache.h and strbuf.[ch] had editor-related functions. Move these into editor.[ch]. Signed-off-by: Elijah Newren <newren@gmail.com> Acked-by: Calvin Wan <calvinwan@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Elijah Newren committed
Apr 11, 2023 at 00:41 UTC
4e120823a345cf348a052683d726c90bc4b256ca
21 files changed
+80
-55
add-patch.c
+1
@@ -2,6 +2,7 @@
2
#include "add-interactive.h"
3
#include "advice.h"
4
#include "alloc.h"
5
+#include "editor.h"
6
#include "environment.h"
7
#include "gettext.h"
8
#include "object-name.h"
builtin/add.c
+1
@@ -9,6 +9,7 @@
9
#include "config.h"
10
#include "builtin.h"
11
#include "lockfile.h"
12
+#include "editor.h"
13
#include "dir.h"
14
#include "gettext.h"
15
#include "pathspec.h"
builtin/am.c
+1
@@ -9,6 +9,7 @@
9
#include "advice.h"
10
#include "config.h"
11
#include "builtin.h"
12
+#include "editor.h"
13
#include "environment.h"
14
#include "exec-cmd.h"
15
#include "gettext.h"
builtin/branch.c
+1
@@ -8,6 +8,7 @@
8
#include "cache.h"
9
#include "config.h"
10
#include "color.h"
11
+#include "editor.h"
12
#include "environment.h"
13
#include "refs.h"
14
#include "commit.h"
builtin/bugreport.c
+1
@@ -1,5 +1,6 @@
1
#include "builtin.h"
2
#include "abspath.h"
3
+#include "editor.h"
4
#include "gettext.h"
5
#include "parse-options.h"
6
#include "strbuf.h"
builtin/commit.c
+1
@@ -13,6 +13,7 @@
13
#include "cache-tree.h"
14
#include "color.h"
15
#include "dir.h"
16
+#include "editor.h"
17
#include "environment.h"
18
#include "builtin.h"
19
#include "diff.h"
builtin/config.c
+1
@@ -3,6 +3,7 @@
3
#include "alloc.h"
4
#include "config.h"
5
#include "color.h"
6
+#include "editor.h"
7
#include "environment.h"
8
#include "gettext.h"
9
#include "ident.h"
builtin/merge.c
+1
@@ -12,6 +12,7 @@
12
#include "advice.h"
13
#include "alloc.h"
14
#include "config.h"
15
+#include "editor.h"
16
#include "environment.h"
17
#include "gettext.h"
18
#include "hex.h"
builtin/notes.c
+1
@@ -10,6 +10,7 @@
10
#include "cache.h"
11
#include "config.h"
12
#include "builtin.h"
13
+#include "editor.h"
14
#include "gettext.h"
15
#include "hex.h"
16
#include "notes.h"
builtin/replace.c
+1
@@ -11,6 +11,7 @@
11
#include "cache.h"
12
#include "config.h"
13
#include "builtin.h"
14
+#include "editor.h"
15
#include "environment.h"
16
#include "gettext.h"
17
#include "hex.h"
builtin/tag.c
+1
@@ -10,6 +10,7 @@
10
#include "advice.h"
11
#include "config.h"
12
#include "builtin.h"
13
+#include "editor.h"
14
#include "environment.h"
15
#include "gettext.h"
16
#include "hex.h"
builtin/var.c
+1
@@ -5,6 +5,7 @@
5
*/
6
#include "builtin.h"
7
#include "config.h"
8
+#include "editor.h"
9
#include "ident.h"
10
#include "refs.h"
11
cache.h
-3
@@ -621,10 +621,7 @@ int df_name_compare(const char *name1, size_t len1, int mode1,
621
int name_compare(const char *name1, size_t len1, const char *name2, size_t len2);
622
int cache_name_stage_compare(const char *name1, int len1, int stage1, const char *name2, int len2, int stage2);
623
624
-const char *git_editor(void);
625
-const char *git_sequence_editor(void);
624
const char *git_pager(int stdout_is_tty);
627
-int is_terminal_dumb(void);
625
626
struct cache_def {
627
struct strbuf path;
color.c
+1
@@ -1,6 +1,7 @@
1
#include "cache.h"
2
#include "config.h"
3
#include "color.h"
4
+#include "editor.h"
5
#include "gettext.h"
6
#include "hex.h"
7
editor.c
+30
@@ -2,12 +2,14 @@
2
#include "abspath.h"
3
#include "advice.h"
4
#include "config.h"
5
+#include "editor.h"
6
#include "environment.h"
7
#include "gettext.h"
8
#include "strbuf.h"
9
#include "strvec.h"
10
#include "run-command.h"
11
#include "sigchain.h"
12
+#include "wrapper.h"
13
14
#ifndef DEFAULT_EDITOR
15
#define DEFAULT_EDITOR "vi"
@@ -130,3 +132,31 @@ int launch_sequence_editor(const char *path, struct strbuf *buffer,
132
{
133
return launch_specified_editor(git_sequence_editor(), path, buffer, env);
134
}
135
+
136
+int strbuf_edit_interactively(struct strbuf *buffer, const char *path,
137
+ const char *const *env)
138
+{
139
+ char *path2 = NULL;
140
+ int fd, res = 0;
141
+
142
+ if (!is_absolute_path(path))
143
+ path = path2 = xstrdup(git_path("%s", path));
144
+
145
+ fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
146
+ if (fd < 0)
147
+ res = error_errno(_("could not open '%s' for writing"), path);
148
+ else if (write_in_full(fd, buffer->buf, buffer->len) < 0) {
149
+ res = error_errno(_("could not write to '%s'"), path);
150
+ close(fd);
151
+ } else if (close(fd) < 0)
152
+ res = error_errno(_("could not close '%s'"), path);
153
+ else {
154
+ strbuf_reset(buffer);
155
+ if (launch_editor(path, buffer, env) < 0)
156
+ res = error_errno(_("could not edit '%s'"), path);
157
+ unlink(path);
158
+ }
159
+
160
+ free(path2);
161
+ return res;
162
+}
editor.h
new
+34
@@ -0,0 +1,34 @@
1
+#ifndef EDITOR_H
2
+#define EDITOR_H
3
+
4
+struct strbuf;
5
+
6
+const char *git_editor(void);
7
+const char *git_sequence_editor(void);
8
+int is_terminal_dumb(void);
9
+
10
+/**
11
+ * Launch the user preferred editor to edit a file and fill the buffer
12
+ * with the file's contents upon the user completing their editing. The
13
+ * third argument can be used to set the environment which the editor is
14
+ * run in. If the buffer is NULL the editor is launched as usual but the
15
+ * file's contents are not read into the buffer upon completion.
16
+ */
17
+int launch_editor(const char *path, struct strbuf *buffer,
18
+ const char *const *env);
19
+
20
+int launch_sequence_editor(const char *path, struct strbuf *buffer,
21
+ const char *const *env);
22
+
23
+/*
24
+ * In contrast to `launch_editor()`, this function writes out the contents
25
+ * of the specified file first, then clears the `buffer`, then launches
26
+ * the editor and reads back in the file contents into the `buffer`.
27
+ * Finally, it deletes the temporary file.
28
+ *
29
+ * If `path` is relative, it refers to a file in the `.git` directory.
30
+ */
31
+int strbuf_edit_interactively(struct strbuf *buffer, const char *path,
32
+ const char *const *env);
33
+
34
+#endif
pager.c
+1
@@ -1,5 +1,6 @@
1
#include "cache.h"
2
#include "config.h"
3
+#include "editor.h"
4
#include "run-command.h"
5
#include "sigchain.h"
6
#include "alias.h"
rebase-interactive.c
+1
@@ -1,5 +1,6 @@
1
#include "git-compat-util.h"
2
#include "commit.h"
3
+#include "editor.h"
4
#include "environment.h"
5
#include "gettext.h"
6
#include "sequencer.h"
sideband.c
+1
@@ -1,6 +1,7 @@
1
#include "cache.h"
2
#include "color.h"
3
#include "config.h"
4
+#include "editor.h"
5
#include "gettext.h"
6
#include "sideband.h"
7
#include "help.h"
strbuf.c
-28
@@ -1180,34 +1180,6 @@ int strbuf_normalize_path(struct strbuf *src)
1180
return 0;
1181
}
1182
1183
-int strbuf_edit_interactively(struct strbuf *buffer, const char *path,
1184
- const char *const *env)
1185
-{
1186
- char *path2 = NULL;
1187
- int fd, res = 0;
1188
-
1189
- if (!is_absolute_path(path))
1190
- path = path2 = xstrdup(git_path("%s", path));
1191
-
1192
- fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
1193
- if (fd < 0)
1194
- res = error_errno(_("could not open '%s' for writing"), path);
1195
- else if (write_in_full(fd, buffer->buf, buffer->len) < 0) {
1196
- res = error_errno(_("could not write to '%s'"), path);
1197
- close(fd);
1198
- } else if (close(fd) < 0)
1199
- res = error_errno(_("could not close '%s'"), path);
1200
- else {
1201
- strbuf_reset(buffer);
1202
- if (launch_editor(path, buffer, env) < 0)
1203
- res = error_errno(_("could not edit '%s'"), path);
1204
- unlink(path);
1205
- }
1206
-
1207
- free(path2);
1208
- return res;
1209
-}
1210
-
1183
void strbuf_strip_file_from_path(struct strbuf *sb)
1184
{
1185
char *path_sep = find_last_dir_sep(sb->buf);
strbuf.h
-24
@@ -640,30 +640,6 @@ void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
640
void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
641
int abbrev_len);
642
643
-/**
644
- * Launch the user preferred editor to edit a file and fill the buffer
645
- * with the file's contents upon the user completing their editing. The
646
- * third argument can be used to set the environment which the editor is
647
- * run in. If the buffer is NULL the editor is launched as usual but the
648
- * file's contents are not read into the buffer upon completion.
649
- */
650
-int launch_editor(const char *path, struct strbuf *buffer,
651
- const char *const *env);
652
-
653
-int launch_sequence_editor(const char *path, struct strbuf *buffer,
654
- const char *const *env);
655
-
656
-/*
657
- * In contrast to `launch_editor()`, this function writes out the contents
658
- * of the specified file first, then clears the `buffer`, then launches
659
- * the editor and reads back in the file contents into the `buffer`.
660
- * Finally, it deletes the temporary file.
661
- *
662
- * If `path` is relative, it refers to a file in the `.git` directory.
663
- */
664
-int strbuf_edit_interactively(struct strbuf *buffer, const char *path,
665
- const char *const *env);
666
-
643
/*
644
* Remove the filename from the provided path string. If the path
645
* contains a trailing separator, then the path is considered a directory