]> git.ipfire.org Git - thirdparty/git.git/commitdiff
interactive: refactor code asking the user for interactive input
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Fri, 10 Apr 2020 11:27:50 +0000 (11:27 +0000)
committerJunio C Hamano <gitster@pobox.com>
Fri, 10 Apr 2020 17:26:31 +0000 (10:26 -0700)
There are quite a few code locations (e.g. `git clean --interactive`)
where Git asks the user for an answer. In preparation for fixing a bug
shared by all of them, and also to DRY up the code, let's refactor it.

Please note that most of these callers trimmed white-space both at the
beginning and at the end of the answer, instead of trimming only the
end (as the caller in `add-patch.c` does).

Therefore, technically speaking, we change behavior in this patch. At
the same time, it can be argued that this is actually a bug fix.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
add-interactive.c
add-patch.c
builtin/clean.c
prompt.c
prompt.h
shell.c

index 4a9bf85cac033b0cf22665918fccee561678eb3d..29cd2fe02014b32c21b69059e5bbf93018a77223 100644 (file)
@@ -9,6 +9,7 @@
 #include "lockfile.h"
 #include "dir.h"
 #include "run-command.h"
+#include "prompt.h"
 
 static void init_color(struct repository *r, struct add_i_state *s,
                       const char *slot_name, char *dst,
@@ -289,13 +290,12 @@ static ssize_t list_and_choose(struct add_i_state *s,
                fputs(singleton ? "> " : ">> ", stdout);
                fflush(stdout);
 
-               if (strbuf_getline(&input, stdin) == EOF) {
+               if (git_read_line_interactively(&input) == EOF) {
                        putchar('\n');
                        if (immediate)
                                res = LIST_AND_CHOOSE_QUIT;
                        break;
                }
-               strbuf_trim(&input);
 
                if (!input.len)
                        break;
index d8dafa8168dc8389468d8c2c3cd6221220826605..d8bfe379be4d50893b8b977efff9c79de54b38af 100644 (file)
@@ -7,6 +7,7 @@
 #include "color.h"
 #include "diff.h"
 #include "compat/terminal.h"
+#include "prompt.h"
 
 enum prompt_mode_type {
        PROMPT_MODE_CHANGE = 0, PROMPT_DELETION, PROMPT_HUNK,
@@ -1158,9 +1159,8 @@ static int read_single_character(struct add_p_state *s)
                return res;
        }
 
-       if (strbuf_getline(&s->answer, stdin) == EOF)
+       if (git_read_line_interactively(&s->answer) == EOF)
                return EOF;
-       strbuf_trim_trailing_newline(&s->answer);
        return 0;
 }
 
index 5abf087e7c495153b36b927b5b615f80a68d414c..c8c011d2ddfa3e2653535c6d4b36675a20a90031 100644 (file)
@@ -18,6 +18,7 @@
 #include "color.h"
 #include "pathspec.h"
 #include "help.h"
+#include "prompt.h"
 
 static int force = -1; /* unset */
 static int interactive;
@@ -420,7 +421,6 @@ static int find_unique(const char *choice, struct menu_stuff *menu_stuff)
        return found;
 }
 
-
 /*
  * Parse user input, and return choice(s) for menu (menu_stuff).
  *
@@ -580,9 +580,7 @@ static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)
                               clean_get_color(CLEAN_COLOR_RESET));
                }
 
-               if (strbuf_getline_lf(&choice, stdin) != EOF) {
-                       strbuf_trim(&choice);
-               } else {
+               if (git_read_line_interactively(&choice) == EOF) {
                        eof = 1;
                        break;
                }
@@ -662,9 +660,7 @@ static int filter_by_patterns_cmd(void)
                clean_print_color(CLEAN_COLOR_PROMPT);
                printf(_("Input ignore patterns>> "));
                clean_print_color(CLEAN_COLOR_RESET);
-               if (strbuf_getline_lf(&confirm, stdin) != EOF)
-                       strbuf_trim(&confirm);
-               else
+               if (git_read_line_interactively(&confirm) == EOF)
                        putchar('\n');
 
                /* quit filter_by_pattern mode if press ENTER or Ctrl-D */
@@ -760,9 +756,7 @@ static int ask_each_cmd(void)
                        qname = quote_path_relative(item->string, NULL, &buf);
                        /* TRANSLATORS: Make sure to keep [y/N] as is */
                        printf(_("Remove %s [y/N]? "), qname);
-                       if (strbuf_getline_lf(&confirm, stdin) != EOF) {
-                               strbuf_trim(&confirm);
-                       } else {
+                       if (git_read_line_interactively(&confirm) == EOF) {
                                putchar('\n');
                                eof = 1;
                        }
index 6d5885d0096407d7d12f4511100ebee5cb5f752d..098dcfb7cf94e6571902ea70c16b8d17a21a13ef 100644 (file)
--- a/prompt.c
+++ b/prompt.c
@@ -74,3 +74,13 @@ char *git_prompt(const char *prompt, int flags)
        }
        return r;
 }
+
+int git_read_line_interactively(struct strbuf *line)
+{
+       int ret = strbuf_getline_lf(line, stdin);
+
+       if (ret != EOF)
+               strbuf_trim_trailing_newline(line);
+
+       return ret;
+}
index e04cced030ca4df6d729c2132ca4671b959952b4..e294e93541cfa0749cfb60fdceac2ebd3f41751d 100644 (file)
--- a/prompt.h
+++ b/prompt.h
@@ -6,4 +6,6 @@
 
 char *git_prompt(const char *prompt, int flags);
 
+int git_read_line_interactively(struct strbuf *line);
+
 #endif /* PROMPT_H */
diff --git a/shell.c b/shell.c
index 54cca7439de636daa37b7f8627c5cdd8bb431703..cef7ffdc9e1d3040930841134577c591cf02f650 100644 (file)
--- a/shell.c
+++ b/shell.c
@@ -4,6 +4,7 @@
 #include "strbuf.h"
 #include "run-command.h"
 #include "alias.h"
+#include "prompt.h"
 
 #define COMMAND_DIR "git-shell-commands"
 #define HELP_COMMAND COMMAND_DIR "/help"
@@ -76,12 +77,11 @@ static void run_shell(void)
                int count;
 
                fprintf(stderr, "git> ");
-               if (strbuf_getline_lf(&line, stdin) == EOF) {
+               if (git_read_line_interactively(&line) == EOF) {
                        fprintf(stderr, "\n");
                        strbuf_release(&line);
                        break;
                }
-               strbuf_trim(&line);
                rawargs = strbuf_detach(&line, NULL);
                split_args = xstrdup(rawargs);
                count = split_cmdline(split_args, &argv);