Skip to content

Commit b4df87b

Browse files
committed
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 <[email protected]>
1 parent 8f309ae commit b4df87b

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

builtin/mktree.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ static const char *mktree_usage[] = {
6565
NULL
6666
};
6767

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)
6969
{
7070
char *ptr, *ntr;
7171
unsigned mode;
@@ -97,7 +97,7 @@ static void mktree_line(char *buf, size_t len, int line_termination, int allow_m
9797
*ntr++ = 0; /* now at the beginning of SHA1 */
9898

9999
path = ntr + 41; /* at the beginning of name */
100-
if (line_termination && path[0] == '"') {
100+
if (!nul_term_line && path[0] == '"') {
101101
struct strbuf p_uq = STRBUF_INIT;
102102
if (unquote_c_style(&p_uq, path, NULL))
103103
die("invalid quoting");
@@ -141,23 +141,25 @@ int cmd_mktree(int ac, const char **av, const char *prefix)
141141
{
142142
struct strbuf sb = STRBUF_INIT;
143143
unsigned char sha1[20];
144-
int line_termination = '\n';
144+
int nul_term_line = 0;
145145
int allow_missing = 0;
146146
int is_batch_mode = 0;
147147
int got_eof = 0;
148+
strbuf_getline_fn getline_fn;
148149

149150
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")),
151152
OPT_SET_INT( 0 , "missing", &allow_missing, N_("allow missing objects"), 1),
152153
OPT_SET_INT( 0 , "batch", &is_batch_mode, N_("allow creation of more than one tree"), 1),
153154
OPT_END()
154155
};
155156

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

158160
while (!got_eof) {
159161
while (1) {
160-
if (strbuf_getline(&sb, stdin, line_termination) == EOF) {
162+
if (getline_fn(&sb, stdin) == EOF) {
161163
got_eof = 1;
162164
break;
163165
}
@@ -167,7 +169,7 @@ int cmd_mktree(int ac, const char **av, const char *prefix)
167169
break;
168170
die("input format error: (blank line only valid in batch mode)");
169171
}
170-
mktree_line(sb.buf, sb.len, line_termination, allow_missing);
172+
mktree_line(sb.buf, sb.len, nul_term_line, allow_missing);
171173
}
172174
if (is_batch_mode && got_eof && used < 1) {
173175
/*

0 commit comments

Comments
 (0)