Git fork

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>

authored by

Elijah Newren and committed by
Junio C Hamano
4e120823 d812c3b6

+80 -55
+1
add-patch.c
··· 2 2 #include "add-interactive.h" 3 3 #include "advice.h" 4 4 #include "alloc.h" 5 + #include "editor.h" 5 6 #include "environment.h" 6 7 #include "gettext.h" 7 8 #include "object-name.h"
+1
builtin/add.c
··· 9 9 #include "config.h" 10 10 #include "builtin.h" 11 11 #include "lockfile.h" 12 + #include "editor.h" 12 13 #include "dir.h" 13 14 #include "gettext.h" 14 15 #include "pathspec.h"
+1
builtin/am.c
··· 9 9 #include "advice.h" 10 10 #include "config.h" 11 11 #include "builtin.h" 12 + #include "editor.h" 12 13 #include "environment.h" 13 14 #include "exec-cmd.h" 14 15 #include "gettext.h"
+1
builtin/branch.c
··· 8 8 #include "cache.h" 9 9 #include "config.h" 10 10 #include "color.h" 11 + #include "editor.h" 11 12 #include "environment.h" 12 13 #include "refs.h" 13 14 #include "commit.h"
+1
builtin/bugreport.c
··· 1 1 #include "builtin.h" 2 2 #include "abspath.h" 3 + #include "editor.h" 3 4 #include "gettext.h" 4 5 #include "parse-options.h" 5 6 #include "strbuf.h"
+1
builtin/commit.c
··· 13 13 #include "cache-tree.h" 14 14 #include "color.h" 15 15 #include "dir.h" 16 + #include "editor.h" 16 17 #include "environment.h" 17 18 #include "builtin.h" 18 19 #include "diff.h"
+1
builtin/config.c
··· 3 3 #include "alloc.h" 4 4 #include "config.h" 5 5 #include "color.h" 6 + #include "editor.h" 6 7 #include "environment.h" 7 8 #include "gettext.h" 8 9 #include "ident.h"
+1
builtin/merge.c
··· 12 12 #include "advice.h" 13 13 #include "alloc.h" 14 14 #include "config.h" 15 + #include "editor.h" 15 16 #include "environment.h" 16 17 #include "gettext.h" 17 18 #include "hex.h"
+1
builtin/notes.c
··· 10 10 #include "cache.h" 11 11 #include "config.h" 12 12 #include "builtin.h" 13 + #include "editor.h" 13 14 #include "gettext.h" 14 15 #include "hex.h" 15 16 #include "notes.h"
+1
builtin/replace.c
··· 11 11 #include "cache.h" 12 12 #include "config.h" 13 13 #include "builtin.h" 14 + #include "editor.h" 14 15 #include "environment.h" 15 16 #include "gettext.h" 16 17 #include "hex.h"
+1
builtin/tag.c
··· 10 10 #include "advice.h" 11 11 #include "config.h" 12 12 #include "builtin.h" 13 + #include "editor.h" 13 14 #include "environment.h" 14 15 #include "gettext.h" 15 16 #include "hex.h"
+1
builtin/var.c
··· 5 5 */ 6 6 #include "builtin.h" 7 7 #include "config.h" 8 + #include "editor.h" 8 9 #include "ident.h" 9 10 #include "refs.h" 10 11
-3
cache.h
··· 621 621 int name_compare(const char *name1, size_t len1, const char *name2, size_t len2); 622 622 int cache_name_stage_compare(const char *name1, int len1, int stage1, const char *name2, int len2, int stage2); 623 623 624 - const char *git_editor(void); 625 - const char *git_sequence_editor(void); 626 624 const char *git_pager(int stdout_is_tty); 627 - int is_terminal_dumb(void); 628 625 629 626 struct cache_def { 630 627 struct strbuf path;
+1
color.c
··· 1 1 #include "cache.h" 2 2 #include "config.h" 3 3 #include "color.h" 4 + #include "editor.h" 4 5 #include "gettext.h" 5 6 #include "hex.h" 6 7
+30
editor.c
··· 2 2 #include "abspath.h" 3 3 #include "advice.h" 4 4 #include "config.h" 5 + #include "editor.h" 5 6 #include "environment.h" 6 7 #include "gettext.h" 7 8 #include "strbuf.h" 8 9 #include "strvec.h" 9 10 #include "run-command.h" 10 11 #include "sigchain.h" 12 + #include "wrapper.h" 11 13 12 14 #ifndef DEFAULT_EDITOR 13 15 #define DEFAULT_EDITOR "vi" ··· 130 132 { 131 133 return launch_specified_editor(git_sequence_editor(), path, buffer, env); 132 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 + }
+34
editor.h
··· 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
+1
pager.c
··· 1 1 #include "cache.h" 2 2 #include "config.h" 3 + #include "editor.h" 3 4 #include "run-command.h" 4 5 #include "sigchain.h" 5 6 #include "alias.h"
+1
rebase-interactive.c
··· 1 1 #include "git-compat-util.h" 2 2 #include "commit.h" 3 + #include "editor.h" 3 4 #include "environment.h" 4 5 #include "gettext.h" 5 6 #include "sequencer.h"
+1
sideband.c
··· 1 1 #include "cache.h" 2 2 #include "color.h" 3 3 #include "config.h" 4 + #include "editor.h" 4 5 #include "gettext.h" 5 6 #include "sideband.h" 6 7 #include "help.h"
-28
strbuf.c
··· 1180 1180 return 0; 1181 1181 } 1182 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 - 1211 1183 void strbuf_strip_file_from_path(struct strbuf *sb) 1212 1184 { 1213 1185 char *path_sep = find_last_dir_sep(sb->buf);
-24
strbuf.h
··· 640 640 void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid, 641 641 int abbrev_len); 642 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 - 667 643 /* 668 644 * Remove the filename from the provided path string. If the path 669 645 * contains a trailing separator, then the path is considered a directory