mktree: 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 13, 2016 at 15:55 UTC b4df87b8ca3b9cf8e648058af4870cfc7305b082
1 file changed +8 -6
builtin/mktree.c
+8 -6
@@ -65,7 +65,7 @@ static const char *mktree_usage[] = {
65 NULL
66 };
67
68 -static void mktree_line(char *buf, size_t len, int line_termination, int allow_missing)
68 +static void mktree_line(char *buf, size_t len, int nul_term_line, int allow_missing)
69 {
70 char *ptr, *ntr;
71 unsigned mode;
@@ -97,7 +97,7 @@ static void mktree_line(char *buf, size_t len, int line_termination, int allow_m
97 *ntr++ = 0; /* now at the beginning of SHA1 */
98
99 path = ntr + 41; /* at the beginning of name */
100 - if (line_termination && path[0] == '"') {
100 + if (!nul_term_line && path[0] == '"') {
101 struct strbuf p_uq = STRBUF_INIT;
102 if (unquote_c_style(&p_uq, path, NULL))
103 die("invalid quoting");
@@ -141,23 +141,25 @@ int cmd_mktree(int ac, const char **av, const char *prefix)
141 {
142 struct strbuf sb = STRBUF_INIT;
143 unsigned char sha1[20];
144 - int line_termination = '\n';
144 + int nul_term_line = 0;
145 int allow_missing = 0;
146 int is_batch_mode = 0;
147 int got_eof = 0;
148 + strbuf_getline_fn getline_fn;
149
150 const struct option option[] = {
150 - OPT_SET_INT('z', NULL, &line_termination, N_("input is NUL terminated"), '\0'),
151 + OPT_BOOL('z', NULL, &nul_term_line, N_("input is NUL terminated")),
152 OPT_SET_INT( 0 , "missing", &allow_missing, N_("allow missing objects"), 1),
153 OPT_SET_INT( 0 , "batch", &is_batch_mode, N_("allow creation of more than one tree"), 1),
154 OPT_END()
155 };
156
157 ac = parse_options(ac, av, prefix, option, mktree_usage, 0);
158 + getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
159
160 while (!got_eof) {
161 while (1) {
160 - if (strbuf_getline(&sb, stdin, line_termination) == EOF) {
162 + if (getline_fn(&sb, stdin) == EOF) {
163 got_eof = 1;
164 break;
165 }
@@ -167,7 +169,7 @@ int cmd_mktree(int ac, const char **av, const char *prefix)
169 break;
170 die("input format error: (blank line only valid in batch mode)");
171 }
170 - mktree_line(sb.buf, sb.len, line_termination, allow_missing);
172 + mktree_line(sb.buf, sb.len, nul_term_line, allow_missing);
173 }
174 if (is_batch_mode && got_eof && used < 1) {
175 /*