update-index: there are only two possible line terminations

The program by default reads LF terminated lines, with an option to use NUL terminated records. Instead of pretending that there can be other useful values for line_termination, use a boolean variable, nul_term_line, to tell if NUL terminated records are used, and switch between strbuf_getline_{lf,nul} based on it. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jan 14, 2016 at 13:34 UTC 7e07ed84186d4047b39a4e9047d41df0322c7338
1 file changed +16 -11
builtin/update-index.c
+16 -11
@@ -468,12 +468,14 @@ static void update_one(const char *path)
468 report("add '%s'", path);
469 }
470
471 -static void read_index_info(int line_termination)
471 +static void read_index_info(int nul_term_line)
472 {
473 struct strbuf buf = STRBUF_INIT;
474 struct strbuf uq = STRBUF_INIT;
475 + strbuf_getline_fn getline_fn;
476
476 - while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
477 + getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
478 + while (getline_fn(&buf, stdin) != EOF) {
479 char *ptr, *tab;
480 char *path_name;
481 unsigned char sha1[20];
@@ -522,7 +524,7 @@ static void read_index_info(int line_termination)
524 goto bad_line;
525
526 path_name = ptr;
525 - if (line_termination && path_name[0] == '"') {
527 + if (!nul_term_line && path_name[0] == '"') {
528 strbuf_reset(&uq);
529 if (unquote_c_style(&uq, path_name, NULL)) {
530 die("git update-index: bad quoting of path name");
@@ -844,12 +846,12 @@ static int cacheinfo_callback(struct parse_opt_ctx_t *ctx,
846 static int stdin_cacheinfo_callback(struct parse_opt_ctx_t *ctx,
847 const struct option *opt, int unset)
848 {
847 - int *line_termination = opt->value;
849 + int *nul_term_line = opt->value;
850
851 if (ctx->argc != 1)
852 return error("option '%s' must be the last argument", opt->long_name);
853 allow_add = allow_replace = allow_remove = 1;
852 - read_index_info(*line_termination);
854 + read_index_info(*nul_term_line);
855 return 0;
856 }
857
@@ -901,7 +903,7 @@ static int reupdate_callback(struct parse_opt_ctx_t *ctx,
903
904 int cmd_update_index(int argc, const char **argv, const char *prefix)
905 {
904 - int newfd, entries, has_errors = 0, line_termination = '\n';
906 + int newfd, entries, has_errors = 0, nul_term_line = 0;
907 int untracked_cache = -1;
908 int read_from_stdin = 0;
909 int prefix_length = prefix ? strlen(prefix) : 0;
@@ -912,6 +914,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
914 int split_index = -1;
915 struct lock_file *lock_file;
916 struct parse_opt_ctx_t ctx;
917 + strbuf_getline_fn getline_fn;
918 int parseopt_state = PARSE_OPT_UNKNOWN;
919 struct option options[] = {
920 OPT_BIT('q', NULL, &refresh_args.flags,
@@ -963,13 +966,13 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
966 N_("add to index only; do not add content to object database"), 1),
967 OPT_SET_INT(0, "force-remove", &force_remove,
968 N_("remove named paths even if present in worktree"), 1),
966 - OPT_SET_INT('z', NULL, &line_termination,
967 - N_("with --stdin: input lines are terminated by null bytes"), '\0'),
969 + OPT_BOOL('z', NULL, &nul_term_line,
970 + N_("with --stdin: input lines are terminated by null bytes")),
971 {OPTION_LOWLEVEL_CALLBACK, 0, "stdin", &read_from_stdin, NULL,
972 N_("read list of paths to be updated from standard input"),
973 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
974 (parse_opt_cb *) stdin_callback},
972 - {OPTION_LOWLEVEL_CALLBACK, 0, "index-info", &line_termination, NULL,
975 + {OPTION_LOWLEVEL_CALLBACK, 0, "index-info", &nul_term_line, NULL,
976 N_("add entries from standard input to the index"),
977 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
978 (parse_opt_cb *) stdin_cacheinfo_callback},
@@ -1057,6 +1060,8 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
1060 }
1061 }
1062 argc = parse_options_end(&ctx);
1063 +
1064 + getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
1065 if (preferred_index_format) {
1066 if (preferred_index_format < INDEX_FORMAT_LB ||
1067 INDEX_FORMAT_UB < preferred_index_format)
@@ -1073,9 +1078,9 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
1078 struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
1079
1080 setup_work_tree();
1076 - while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
1081 + while (getline_fn(&buf, stdin) != EOF) {
1082 char *p;
1078 - if (line_termination && buf.buf[0] == '"') {
1083 + if (!nul_term_line && buf.buf[0] == '"') {
1084 strbuf_reset(&nbuf);
1085 if (unquote_c_style(&nbuf, buf.buf, NULL))
1086 die("line is badly quoted");