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>
This commit is contained in:
Elijah Newren 2023-04-11 00:41:57 -07:00 committed by Junio C Hamano
parent d812c3b6a0
commit 4e120823a3
21 changed files with 80 additions and 55 deletions

View File

@ -2,6 +2,7 @@
#include "add-interactive.h" #include "add-interactive.h"
#include "advice.h" #include "advice.h"
#include "alloc.h" #include "alloc.h"
#include "editor.h"
#include "environment.h" #include "environment.h"
#include "gettext.h" #include "gettext.h"
#include "object-name.h" #include "object-name.h"

View File

@ -9,6 +9,7 @@
#include "config.h" #include "config.h"
#include "builtin.h" #include "builtin.h"
#include "lockfile.h" #include "lockfile.h"
#include "editor.h"
#include "dir.h" #include "dir.h"
#include "gettext.h" #include "gettext.h"
#include "pathspec.h" #include "pathspec.h"

View File

@ -9,6 +9,7 @@
#include "advice.h" #include "advice.h"
#include "config.h" #include "config.h"
#include "builtin.h" #include "builtin.h"
#include "editor.h"
#include "environment.h" #include "environment.h"
#include "exec-cmd.h" #include "exec-cmd.h"
#include "gettext.h" #include "gettext.h"

View File

@ -8,6 +8,7 @@
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"
#include "color.h" #include "color.h"
#include "editor.h"
#include "environment.h" #include "environment.h"
#include "refs.h" #include "refs.h"
#include "commit.h" #include "commit.h"

View File

@ -1,5 +1,6 @@
#include "builtin.h" #include "builtin.h"
#include "abspath.h" #include "abspath.h"
#include "editor.h"
#include "gettext.h" #include "gettext.h"
#include "parse-options.h" #include "parse-options.h"
#include "strbuf.h" #include "strbuf.h"

View File

@ -13,6 +13,7 @@
#include "cache-tree.h" #include "cache-tree.h"
#include "color.h" #include "color.h"
#include "dir.h" #include "dir.h"
#include "editor.h"
#include "environment.h" #include "environment.h"
#include "builtin.h" #include "builtin.h"
#include "diff.h" #include "diff.h"

View File

@ -3,6 +3,7 @@
#include "alloc.h" #include "alloc.h"
#include "config.h" #include "config.h"
#include "color.h" #include "color.h"
#include "editor.h"
#include "environment.h" #include "environment.h"
#include "gettext.h" #include "gettext.h"
#include "ident.h" #include "ident.h"

View File

@ -12,6 +12,7 @@
#include "advice.h" #include "advice.h"
#include "alloc.h" #include "alloc.h"
#include "config.h" #include "config.h"
#include "editor.h"
#include "environment.h" #include "environment.h"
#include "gettext.h" #include "gettext.h"
#include "hex.h" #include "hex.h"

View File

@ -10,6 +10,7 @@
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"
#include "builtin.h" #include "builtin.h"
#include "editor.h"
#include "gettext.h" #include "gettext.h"
#include "hex.h" #include "hex.h"
#include "notes.h" #include "notes.h"

View File

@ -11,6 +11,7 @@
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"
#include "builtin.h" #include "builtin.h"
#include "editor.h"
#include "environment.h" #include "environment.h"
#include "gettext.h" #include "gettext.h"
#include "hex.h" #include "hex.h"

View File

@ -10,6 +10,7 @@
#include "advice.h" #include "advice.h"
#include "config.h" #include "config.h"
#include "builtin.h" #include "builtin.h"
#include "editor.h"
#include "environment.h" #include "environment.h"
#include "gettext.h" #include "gettext.h"
#include "hex.h" #include "hex.h"

View File

@ -5,6 +5,7 @@
*/ */
#include "builtin.h" #include "builtin.h"
#include "config.h" #include "config.h"
#include "editor.h"
#include "ident.h" #include "ident.h"
#include "refs.h" #include "refs.h"

View File

@ -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 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); 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); const char *git_pager(int stdout_is_tty);
int is_terminal_dumb(void);
struct cache_def { struct cache_def {
struct strbuf path; struct strbuf path;

View File

@ -1,6 +1,7 @@
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"
#include "color.h" #include "color.h"
#include "editor.h"
#include "gettext.h" #include "gettext.h"
#include "hex.h" #include "hex.h"

View File

@ -2,12 +2,14 @@
#include "abspath.h" #include "abspath.h"
#include "advice.h" #include "advice.h"
#include "config.h" #include "config.h"
#include "editor.h"
#include "environment.h" #include "environment.h"
#include "gettext.h" #include "gettext.h"
#include "strbuf.h" #include "strbuf.h"
#include "strvec.h" #include "strvec.h"
#include "run-command.h" #include "run-command.h"
#include "sigchain.h" #include "sigchain.h"
#include "wrapper.h"
#ifndef DEFAULT_EDITOR #ifndef DEFAULT_EDITOR
#define DEFAULT_EDITOR "vi" #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); 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;
}

34
editor.h Normal file
View File

@ -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

View File

@ -1,5 +1,6 @@
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"
#include "editor.h"
#include "run-command.h" #include "run-command.h"
#include "sigchain.h" #include "sigchain.h"
#include "alias.h" #include "alias.h"

View File

@ -1,5 +1,6 @@
#include "git-compat-util.h" #include "git-compat-util.h"
#include "commit.h" #include "commit.h"
#include "editor.h"
#include "environment.h" #include "environment.h"
#include "gettext.h" #include "gettext.h"
#include "sequencer.h" #include "sequencer.h"

View File

@ -1,6 +1,7 @@
#include "cache.h" #include "cache.h"
#include "color.h" #include "color.h"
#include "config.h" #include "config.h"
#include "editor.h"
#include "gettext.h" #include "gettext.h"
#include "sideband.h" #include "sideband.h"
#include "help.h" #include "help.h"

View File

@ -1180,34 +1180,6 @@ int strbuf_normalize_path(struct strbuf *src)
return 0; 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) void strbuf_strip_file_from_path(struct strbuf *sb)
{ {
char *path_sep = find_last_dir_sep(sb->buf); char *path_sep = find_last_dir_sep(sb->buf);

View File

@ -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, void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
int abbrev_len); 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 * Remove the filename from the provided path string. If the path
* contains a trailing separator, then the path is considered a directory * contains a trailing separator, then the path is considered a directory