Skip to content

Commit 7e07ed8

Browse files
committed
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 <[email protected]>
1 parent dca9003 commit 7e07ed8

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

builtin/update-index.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -468,12 +468,14 @@ static void update_one(const char *path)
468468
report("add '%s'", path);
469469
}
470470

471-
static void read_index_info(int line_termination)
471+
static void read_index_info(int nul_term_line)
472472
{
473473
struct strbuf buf = STRBUF_INIT;
474474
struct strbuf uq = STRBUF_INIT;
475+
strbuf_getline_fn getline_fn;
475476

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) {
477479
char *ptr, *tab;
478480
char *path_name;
479481
unsigned char sha1[20];
@@ -522,7 +524,7 @@ static void read_index_info(int line_termination)
522524
goto bad_line;
523525

524526
path_name = ptr;
525-
if (line_termination && path_name[0] == '"') {
527+
if (!nul_term_line && path_name[0] == '"') {
526528
strbuf_reset(&uq);
527529
if (unquote_c_style(&uq, path_name, NULL)) {
528530
die("git update-index: bad quoting of path name");
@@ -844,12 +846,12 @@ static int cacheinfo_callback(struct parse_opt_ctx_t *ctx,
844846
static int stdin_cacheinfo_callback(struct parse_opt_ctx_t *ctx,
845847
const struct option *opt, int unset)
846848
{
847-
int *line_termination = opt->value;
849+
int *nul_term_line = opt->value;
848850

849851
if (ctx->argc != 1)
850852
return error("option '%s' must be the last argument", opt->long_name);
851853
allow_add = allow_replace = allow_remove = 1;
852-
read_index_info(*line_termination);
854+
read_index_info(*nul_term_line);
853855
return 0;
854856
}
855857

@@ -901,7 +903,7 @@ static int reupdate_callback(struct parse_opt_ctx_t *ctx,
901903

902904
int cmd_update_index(int argc, const char **argv, const char *prefix)
903905
{
904-
int newfd, entries, has_errors = 0, line_termination = '\n';
906+
int newfd, entries, has_errors = 0, nul_term_line = 0;
905907
int untracked_cache = -1;
906908
int read_from_stdin = 0;
907909
int prefix_length = prefix ? strlen(prefix) : 0;
@@ -912,6 +914,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
912914
int split_index = -1;
913915
struct lock_file *lock_file;
914916
struct parse_opt_ctx_t ctx;
917+
strbuf_getline_fn getline_fn;
915918
int parseopt_state = PARSE_OPT_UNKNOWN;
916919
struct option options[] = {
917920
OPT_BIT('q', NULL, &refresh_args.flags,
@@ -963,13 +966,13 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
963966
N_("add to index only; do not add content to object database"), 1),
964967
OPT_SET_INT(0, "force-remove", &force_remove,
965968
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")),
968971
{OPTION_LOWLEVEL_CALLBACK, 0, "stdin", &read_from_stdin, NULL,
969972
N_("read list of paths to be updated from standard input"),
970973
PARSE_OPT_NONEG | PARSE_OPT_NOARG,
971974
(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,
973976
N_("add entries from standard input to the index"),
974977
PARSE_OPT_NONEG | PARSE_OPT_NOARG,
975978
(parse_opt_cb *) stdin_cacheinfo_callback},
@@ -1057,6 +1060,8 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
10571060
}
10581061
}
10591062
argc = parse_options_end(&ctx);
1063+
1064+
getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
10601065
if (preferred_index_format) {
10611066
if (preferred_index_format < INDEX_FORMAT_LB ||
10621067
INDEX_FORMAT_UB < preferred_index_format)
@@ -1073,9 +1078,9 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
10731078
struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
10741079

10751080
setup_work_tree();
1076-
while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
1081+
while (getline_fn(&buf, stdin) != EOF) {
10771082
char *p;
1078-
if (line_termination && buf.buf[0] == '"') {
1083+
if (!nul_term_line && buf.buf[0] == '"') {
10791084
strbuf_reset(&nbuf);
10801085
if (unquote_c_style(&nbuf, buf.buf, NULL))
10811086
die("line is badly quoted");

0 commit comments

Comments
 (0)