Signed-off-by: Kristian Høgsberg <krh@xxxxxxxxxx> --- Makefile | 2 +- builtin-commit.c | 1 + builtin-stripspace.c | 68 +------------------------------ builtin-tag.c | 40 +------------------ editor.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++ editor.h | 9 ++++ strbuf.h | 3 - 7 files changed, 122 insertions(+), 110 deletions(-) create mode 100644 editor.c create mode 100644 editor.h diff --git a/Makefile b/Makefile index 6172589..f6d991e 100644 --- a/Makefile +++ b/Makefile @@ -310,7 +310,7 @@ LIB_OBJS = \ alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \ color.o wt-status.o archive-zip.o archive-tar.o shallow.o utf8.o \ convert.o attr.o decorate.o progress.o mailmap.o symlinks.o remote.o \ - transport.o bundle.o parse-options.o + transport.o bundle.o parse-options.o editor.o BUILTIN_OBJS = \ builtin-add.o \ diff --git a/builtin-commit.c b/builtin-commit.c index 69e8b19..7d87812 100644 --- a/builtin-commit.c +++ b/builtin-commit.c @@ -19,6 +19,7 @@ #include "strbuf.h" #include "utf8.h" #include "parse-options.h" +#include "editor.h" static const char builtin_commit_usage[] = "[-a | --interactive] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit> | --amend] [-u] [-e] [--author <author>] [--template <file>] [[-i | -o] <path>...]"; diff --git a/builtin-stripspace.c b/builtin-stripspace.c index c0b2130..4e5bb07 100644 --- a/builtin-stripspace.c +++ b/builtin-stripspace.c @@ -1,72 +1,6 @@ #include "builtin.h" #include "cache.h" - -/* - * Returns the length of a line, without trailing spaces. - * - * If the line ends with newline, it will be removed too. - */ -static size_t cleanup(char *line, size_t len) -{ - while (len) { - unsigned char c = line[len - 1]; - if (!isspace(c)) - break; - len--; - } - - return len; -} - -/* - * Remove empty lines from the beginning and end - * and also trailing spaces from every line. - * - * Note that the buffer will not be NUL-terminated. - * - * Turn multiple consecutive empty lines between paragraphs - * into just one empty line. - * - * If the input has only empty lines and spaces, - * no output will be produced. - * - * If last line does not have a newline at the end, one is added. - * - * Enable skip_comments to skip every line starting with "#". - */ -void stripspace(struct strbuf *sb, int skip_comments) -{ - int empties = 0; - size_t i, j, len, newlen; - char *eol; - - /* We may have to add a newline. */ - strbuf_grow(sb, 1); - - for (i = j = 0; i < sb->len; i += len, j += newlen) { - eol = memchr(sb->buf + i, '\n', sb->len - i); - len = eol ? eol - (sb->buf + i) + 1 : sb->len - i; - - if (skip_comments && len && sb->buf[i] == '#') { - newlen = 0; - continue; - } - newlen = cleanup(sb->buf + i, len); - - /* Not just an empty line? */ - if (newlen) { - if (empties > 0 && j > 0) - sb->buf[j++] = '\n'; - empties = 0; - memmove(sb->buf + j, sb->buf + i, newlen); - sb->buf[newlen + j++] = '\n'; - } else { - empties++; - } - } - - strbuf_setlen(sb, j); -} +#include "editor.h" int cmd_stripspace(int argc, const char **argv, const char *prefix) { diff --git a/builtin-tag.c b/builtin-tag.c index 0a36a5d..c9a0497 100644 --- a/builtin-tag.c +++ b/builtin-tag.c @@ -10,6 +10,7 @@ #include "builtin.h" #include "refs.h" #include "tag.h" +#include "editor.h" #include "run-command.h" static const char builtin_tag_usage[] = @@ -17,45 +18,6 @@ static const char builtin_tag_usage[] = static char signingkey[1000]; -void launch_editor(const char *path, struct strbuf *buffer) -{ - const char *editor, *terminal; - struct child_process child; - const char *args[3]; - - editor = getenv("GIT_EDITOR"); - if (!editor && editor_program) - editor = editor_program; - if (!editor) - editor = getenv("VISUAL"); - if (!editor) - editor = getenv("EDITOR"); - - terminal = getenv("TERM"); - if (!editor && (!terminal || !strcmp(terminal, "dumb"))) { - fprintf(stderr, - "Terminal is dumb but no VISUAL nor EDITOR defined.\n" - "Please supply the message using either -m or -F option.\n"); - exit(1); - } - - if (!editor) - editor = "vi"; - - memset(&child, 0, sizeof(child)); - child.argv = args; - args[0] = editor; - args[1] = path; - args[2] = NULL; - - if (run_command(&child)) - die("There was a problem with the editor %s.", editor); - - if (strbuf_read_file(buffer, path) < 0) - die("could not read message file '%s': %s", - path, strerror(errno)); -} - struct tag_filter { const char *pattern; int lines; diff --git a/editor.c b/editor.c new file mode 100644 index 0000000..6bc3033 --- /dev/null +++ b/editor.c @@ -0,0 +1,109 @@ +#include "git-compat-util.h" +#include "editor.h" +#include "run-command.h" + +void launch_editor(const char *path, struct strbuf *buffer) +{ + const char *editor, *terminal; + struct child_process child; + const char *args[3]; + + editor = getenv("GIT_EDITOR"); + if (!editor && editor_program) + editor = editor_program; + if (!editor) + editor = getenv("VISUAL"); + if (!editor) + editor = getenv("EDITOR"); + + terminal = getenv("TERM"); + if (!editor && (!terminal || !strcmp(terminal, "dumb"))) { + fprintf(stderr, + "Terminal is dumb but no VISUAL nor EDITOR defined.\n" + "Please supply the message using either -m or -F option.\n"); + exit(1); + } + + if (!editor) + editor = "vi"; + + memset(&child, 0, sizeof(child)); + child.argv = args; + args[0] = editor; + args[1] = path; + args[2] = NULL; + + if (run_command(&child)) + die("There was a problem with the editor %s.", editor); + + if (strbuf_read_file(buffer, path) < 0) + die("could not read message file '%s': %s", + path, strerror(errno)); +} + +/* + * Returns the length of a line, without trailing spaces. + * + * If the line ends with newline, it will be removed too. + */ +static size_t cleanup(char *line, size_t len) +{ + while (len) { + unsigned char c = line[len - 1]; + if (!isspace(c)) + break; + len--; + } + + return len; +} + +/* + * Remove empty lines from the beginning and end + * and also trailing spaces from every line. + * + * Note that the buffer will not be NUL-terminated. + * + * Turn multiple consecutive empty lines between paragraphs + * into just one empty line. + * + * If the input has only empty lines and spaces, + * no output will be produced. + * + * If last line does not have a newline at the end, one is added. + * + * Enable skip_comments to skip every line starting with "#". + */ +void stripspace(struct strbuf *sb, int skip_comments) +{ + int empties = 0; + size_t i, j, len, newlen; + char *eol; + + /* We may have to add a newline. */ + strbuf_grow(sb, 1); + + for (i = j = 0; i < sb->len; i += len, j += newlen) { + eol = memchr(sb->buf + i, '\n', sb->len - i); + len = eol ? eol - (sb->buf + i) + 1 : sb->len - i; + + if (skip_comments && len && sb->buf[i] == '#') { + newlen = 0; + continue; + } + newlen = cleanup(sb->buf + i, len); + + /* Not just an empty line? */ + if (newlen) { + if (empties > 0 && j > 0) + sb->buf[j++] = '\n'; + empties = 0; + memmove(sb->buf + j, sb->buf + i, newlen); + sb->buf[newlen + j++] = '\n'; + } else { + empties++; + } + } + + strbuf_setlen(sb, j); +} diff --git a/editor.h b/editor.h new file mode 100644 index 0000000..9dc39f5 --- /dev/null +++ b/editor.h @@ -0,0 +1,9 @@ +#ifndef EDITOR_H +#define EDITOR_H + +#include "cache.h" + +extern void launch_editor(const char *path, struct strbuf *buffer); +extern void stripspace(struct strbuf *sb, int skip_comments); + +#endif diff --git a/strbuf.h b/strbuf.h index eef4e6d..d4d9e56 100644 --- a/strbuf.h +++ b/strbuf.h @@ -112,7 +112,4 @@ extern int strbuf_read_file(struct strbuf *sb, const char *path); extern int strbuf_getline(struct strbuf *, FILE *, int); -extern void stripspace(struct strbuf *buf, int skip_comments); -extern void launch_editor(const char *path, struct strbuf *buffer); - #endif /* STRBUF_H */ -- 1.5.2.GIT - To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html