Skip to content

Commit

Permalink
mktree: there are only two possible line terminations
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Junio C Hamano committed Jan 15, 2016
1 parent 8f309ae commit b4df87b
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions builtin/mktree.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static const char *mktree_usage[] = {
NULL
};

static void mktree_line(char *buf, size_t len, int line_termination, int allow_missing)
static void mktree_line(char *buf, size_t len, int nul_term_line, int allow_missing)
{
char *ptr, *ntr;
unsigned mode;
Expand Down Expand Up @@ -97,7 +97,7 @@ static void mktree_line(char *buf, size_t len, int line_termination, int allow_m
*ntr++ = 0; /* now at the beginning of SHA1 */

path = ntr + 41; /* at the beginning of name */
if (line_termination && path[0] == '"') {
if (!nul_term_line && path[0] == '"') {
struct strbuf p_uq = STRBUF_INIT;
if (unquote_c_style(&p_uq, path, NULL))
die("invalid quoting");
Expand Down Expand Up @@ -141,23 +141,25 @@ int cmd_mktree(int ac, const char **av, const char *prefix)
{
struct strbuf sb = STRBUF_INIT;
unsigned char sha1[20];
int line_termination = '\n';
int nul_term_line = 0;
int allow_missing = 0;
int is_batch_mode = 0;
int got_eof = 0;
strbuf_getline_fn getline_fn;

const struct option option[] = {
OPT_SET_INT('z', NULL, &line_termination, N_("input is NUL terminated"), '\0'),
OPT_BOOL('z', NULL, &nul_term_line, N_("input is NUL terminated")),
OPT_SET_INT( 0 , "missing", &allow_missing, N_("allow missing objects"), 1),
OPT_SET_INT( 0 , "batch", &is_batch_mode, N_("allow creation of more than one tree"), 1),
OPT_END()
};

ac = parse_options(ac, av, prefix, option, mktree_usage, 0);
getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;

while (!got_eof) {
while (1) {
if (strbuf_getline(&sb, stdin, line_termination) == EOF) {
if (getline_fn(&sb, stdin) == EOF) {
got_eof = 1;
break;
}
Expand All @@ -167,7 +169,7 @@ int cmd_mktree(int ac, const char **av, const char *prefix)
break;
die("input format error: (blank line only valid in batch mode)");
}
mktree_line(sb.buf, sb.len, line_termination, allow_missing);
mktree_line(sb.buf, sb.len, nul_term_line, allow_missing);
}
if (is_batch_mode && got_eof && used < 1) {
/*
Expand Down

0 comments on commit b4df87b

Please sign in to comment.