]> git.ipfire.org Git - thirdparty/git.git/commitdiff
editor: move editor-related functions and declarations into common file
authorElijah Newren <newren@gmail.com>
Tue, 11 Apr 2023 07:41:57 +0000 (00:41 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 11 Apr 2023 15:52:10 +0000 (08:52 -0700)
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>
21 files changed:
add-patch.c
builtin/add.c
builtin/am.c
builtin/branch.c
builtin/bugreport.c
builtin/commit.c
builtin/config.c
builtin/merge.c
builtin/notes.c
builtin/replace.c
builtin/tag.c
builtin/var.c
cache.h
color.c
editor.c
editor.h [new file with mode: 0644]
pager.c
rebase-interactive.c
sideband.c
strbuf.c
strbuf.h

index b01ba8fa81d6647bf1625f5cc81a78ac9846b2a0..8d770d203ff78f487b9497cf0603436cb56d4e74 100644 (file)
@@ -2,6 +2,7 @@
 #include "add-interactive.h"
 #include "advice.h"
 #include "alloc.h"
+#include "editor.h"
 #include "environment.h"
 #include "gettext.h"
 #include "object-name.h"
index d3c51e28142600f224c98edf4a9e451591666df9..76cc026a68a992a750b24a9ac23d05f4899dacc2 100644 (file)
@@ -9,6 +9,7 @@
 #include "config.h"
 #include "builtin.h"
 #include "lockfile.h"
+#include "editor.h"
 #include "dir.h"
 #include "gettext.h"
 #include "pathspec.h"
index 0d7ee28bddc39a1fbb83e6c728e6c741f42a25a6..f7a065e52909778f73c93bd7b64eb733b663adb0 100644 (file)
@@ -9,6 +9,7 @@
 #include "advice.h"
 #include "config.h"
 #include "builtin.h"
+#include "editor.h"
 #include "environment.h"
 #include "exec-cmd.h"
 #include "gettext.h"
index 7c1c872708f46cead6885923def31837fff3f9ed..1fb11d55e651016ec462cbe4460692abd91a1976 100644 (file)
@@ -8,6 +8,7 @@
 #include "cache.h"
 #include "config.h"
 #include "color.h"
+#include "editor.h"
 #include "environment.h"
 #include "refs.h"
 #include "commit.h"
index 03fb0536911650c4b719cffb900e27ba46939256..daf6c236577626fffd8dd61c63489d9e6e706635 100644 (file)
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "abspath.h"
+#include "editor.h"
 #include "gettext.h"
 #include "parse-options.h"
 #include "strbuf.h"
index 682f47c8d5a6eec65d0ae3a93b530da1132d9189..e67c4be2211eed8f6c8d61f510adc2fc87d9b007 100644 (file)
@@ -13,6 +13,7 @@
 #include "cache-tree.h"
 #include "color.h"
 #include "dir.h"
+#include "editor.h"
 #include "environment.h"
 #include "builtin.h"
 #include "diff.h"
index fe79fb60c4377bce46fa56f44e7534cf7b96e0d8..9401f1e5e3b21d44386f623e0037f8385041d2a4 100644 (file)
@@ -3,6 +3,7 @@
 #include "alloc.h"
 #include "config.h"
 #include "color.h"
+#include "editor.h"
 #include "environment.h"
 #include "gettext.h"
 #include "ident.h"
index 693f185d4d38f1f0f903ae0ba47c15a073732193..8da3e46abb0fdc692faac47e0b0deb9e13fe4194 100644 (file)
@@ -12,6 +12,7 @@
 #include "advice.h"
 #include "alloc.h"
 #include "config.h"
+#include "editor.h"
 #include "environment.h"
 #include "gettext.h"
 #include "hex.h"
index 896140b9055ce25a4eba970557d127544c4a260f..d5788352b6ee7c69663ddf822134b55a36bf95b0 100644 (file)
@@ -10,6 +10,7 @@
 #include "cache.h"
 #include "config.h"
 #include "builtin.h"
+#include "editor.h"
 #include "gettext.h"
 #include "hex.h"
 #include "notes.h"
index f4b3a8efb2348c5e4d10462fff0290087492e601..981f1894436dd3c3d2df15c0926e2a808557703d 100644 (file)
@@ -11,6 +11,7 @@
 #include "cache.h"
 #include "config.h"
 #include "builtin.h"
+#include "editor.h"
 #include "environment.h"
 #include "gettext.h"
 #include "hex.h"
index 88a259023091091a86d79cfce81f5abbb642adf1..7d189a4a5d4b1c52bda2466a2ea547d920db4302 100644 (file)
@@ -10,6 +10,7 @@
 #include "advice.h"
 #include "config.h"
 #include "builtin.h"
+#include "editor.h"
 #include "environment.h"
 #include "gettext.h"
 #include "hex.h"
index d9943be9afd73dfc46e471ecee426aed00f8da08..90616cf65a24f3f8176ee76ef1e2ffd1e59f6f1f 100644 (file)
@@ -5,6 +5,7 @@
  */
 #include "builtin.h"
 #include "config.h"
+#include "editor.h"
 #include "ident.h"
 #include "refs.h"
 
diff --git a/cache.h b/cache.h
index 394e8d01254e6d105a16a0df3635645a5867551e..97ddf4094a371f1cca9c55c46b059ff11c67b6d5 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -621,10 +621,7 @@ int df_name_compare(const char *name1, size_t len1, int mode1,
 int name_compare(const char *name1, size_t len1, const char *name2, size_t len2);
 int cache_name_stage_compare(const char *name1, int len1, int stage1, const char *name2, int len2, int stage2);
 
-const char *git_editor(void);
-const char *git_sequence_editor(void);
 const char *git_pager(int stdout_is_tty);
-int is_terminal_dumb(void);
 
 struct cache_def {
        struct strbuf path;
diff --git a/color.c b/color.c
index 672dcbb73a6a982a4a4c8086b6f6585fde4377a6..a8e8d5202ab3a0eb640138c34148b1882f2cc073 100644 (file)
--- a/color.c
+++ b/color.c
@@ -1,6 +1,7 @@
 #include "cache.h"
 #include "config.h"
 #include "color.h"
+#include "editor.h"
 #include "gettext.h"
 #include "hex.h"
 
index 3bea3ef72f7eb0f867eac6a8ecd7467d1e878b82..12025dfec37e8c573522990d996c248c4a2f1b21 100644 (file)
--- a/editor.c
+++ b/editor.c
@@ -2,12 +2,14 @@
 #include "abspath.h"
 #include "advice.h"
 #include "config.h"
+#include "editor.h"
 #include "environment.h"
 #include "gettext.h"
 #include "strbuf.h"
 #include "strvec.h"
 #include "run-command.h"
 #include "sigchain.h"
+#include "wrapper.h"
 
 #ifndef DEFAULT_EDITOR
 #define DEFAULT_EDITOR "vi"
@@ -130,3 +132,31 @@ 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,
+                             const char *const *env)
+{
+       char *path2 = NULL;
+       int fd, res = 0;
+
+       if (!is_absolute_path(path))
+               path = path2 = xstrdup(git_path("%s", path));
+
+       fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
+       if (fd < 0)
+               res = error_errno(_("could not open '%s' for writing"), path);
+       else if (write_in_full(fd, buffer->buf, buffer->len) < 0) {
+               res = error_errno(_("could not write to '%s'"), path);
+               close(fd);
+       } else if (close(fd) < 0)
+               res = error_errno(_("could not close '%s'"), path);
+       else {
+               strbuf_reset(buffer);
+               if (launch_editor(path, buffer, env) < 0)
+                       res = error_errno(_("could not edit '%s'"), path);
+               unlink(path);
+       }
+
+       free(path2);
+       return res;
+}
diff --git a/editor.h b/editor.h
new file mode 100644 (file)
index 0000000..8016bb5
--- /dev/null
+++ b/editor.h
@@ -0,0 +1,34 @@
+#ifndef EDITOR_H
+#define EDITOR_H
+
+struct strbuf;
+
+const char *git_editor(void);
+const char *git_sequence_editor(void);
+int is_terminal_dumb(void);
+
+/**
+ * Launch the user preferred editor to edit a file and fill the buffer
+ * with the file's contents upon the user completing their editing. The
+ * third argument can be used to set the environment which the editor is
+ * run in. If the buffer is NULL the editor is launched as usual but the
+ * file's contents are not read into the buffer upon completion.
+ */
+int launch_editor(const char *path, struct strbuf *buffer,
+                 const char *const *env);
+
+int launch_sequence_editor(const char *path, struct strbuf *buffer,
+                          const char *const *env);
+
+/*
+ * In contrast to `launch_editor()`, this function writes out the contents
+ * of the specified file first, then clears the `buffer`, then launches
+ * the editor and reads back in the file contents into the `buffer`.
+ * Finally, it deletes the temporary file.
+ *
+ * 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);
+
+#endif
diff --git a/pager.c b/pager.c
index b66bbff2785cb6ed032edb4f0d9cd68f6a841c36..6367e8ef86796de379e1955672c785fca85bda54 100644 (file)
--- a/pager.c
+++ b/pager.c
@@ -1,5 +1,6 @@
 #include "cache.h"
 #include "config.h"
+#include "editor.h"
 #include "run-command.h"
 #include "sigchain.h"
 #include "alias.h"
index 6ff12d7be2db5ad3e8d4743a6ab55a2ad46007fd..789f40736176e5f597f79d35582224ac8830495e 100644 (file)
@@ -1,5 +1,6 @@
 #include "git-compat-util.h"
 #include "commit.h"
+#include "editor.h"
 #include "environment.h"
 #include "gettext.h"
 #include "sequencer.h"
index 0af582858bf4175e8c3047cffc10eb156661a631..25e2a185716fd18812d593fd98af9307b0c54105 100644 (file)
@@ -1,6 +1,7 @@
 #include "cache.h"
 #include "color.h"
 #include "config.h"
+#include "editor.h"
 #include "gettext.h"
 #include "sideband.h"
 #include "help.h"
index b2e3735ba8a9e936ff34557e515cc77472093acf..729378ec824406da1654a514f88ab71375410bcd 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
@@ -1180,34 +1180,6 @@ int strbuf_normalize_path(struct strbuf *src)
        return 0;
 }
 
-int strbuf_edit_interactively(struct strbuf *buffer, const char *path,
-                             const char *const *env)
-{
-       char *path2 = NULL;
-       int fd, res = 0;
-
-       if (!is_absolute_path(path))
-               path = path2 = xstrdup(git_path("%s", path));
-
-       fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
-       if (fd < 0)
-               res = error_errno(_("could not open '%s' for writing"), path);
-       else if (write_in_full(fd, buffer->buf, buffer->len) < 0) {
-               res = error_errno(_("could not write to '%s'"), path);
-               close(fd);
-       } else if (close(fd) < 0)
-               res = error_errno(_("could not close '%s'"), path);
-       else {
-               strbuf_reset(buffer);
-               if (launch_editor(path, buffer, env) < 0)
-                       res = error_errno(_("could not edit '%s'"), path);
-               unlink(path);
-       }
-
-       free(path2);
-       return res;
-}
-
 void strbuf_strip_file_from_path(struct strbuf *sb)
 {
        char *path_sep = find_last_dir_sep(sb->buf);
index b980f9edc6d9b79dd0319b06d13f9e5165624653..3dfeadb44c2e93e3e47cd448eaf8fd9133ac47bc 100644 (file)
--- a/strbuf.h
+++ b/strbuf.h
@@ -640,30 +640,6 @@ void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
 void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
                              int abbrev_len);
 
-/**
- * Launch the user preferred editor to edit a file and fill the buffer
- * with the file's contents upon the user completing their editing. The
- * third argument can be used to set the environment which the editor is
- * run in. If the buffer is NULL the editor is launched as usual but the
- * file's contents are not read into the buffer upon completion.
- */
-int launch_editor(const char *path, struct strbuf *buffer,
-                 const char *const *env);
-
-int launch_sequence_editor(const char *path, struct strbuf *buffer,
-                          const char *const *env);
-
-/*
- * In contrast to `launch_editor()`, this function writes out the contents
- * of the specified file first, then clears the `buffer`, then launches
- * the editor and reads back in the file contents into the `buffer`.
- * Finally, it deletes the temporary file.
- *
- * 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);
-
 /*
  * Remove the filename from the provided path string. If the path
  * contains a trailing separator, then the path is considered a directory