builtin-mktree.c: use a helper function to handle one line of input
The main() function used to do the whole thing; this moves the handling of a single input line to a separate function to make it easier to read. Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
1fdee85c88
commit
fe0bb5f7bc
@ -67,39 +67,28 @@ static const char *mktree_usage[] = {
|
|||||||
NULL
|
NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
int cmd_mktree(int ac, const char **av, const char *prefix)
|
static void mktree_line(char *buf, size_t len, int line_termination)
|
||||||
{
|
{
|
||||||
struct strbuf sb = STRBUF_INIT;
|
|
||||||
struct strbuf p_uq = STRBUF_INIT;
|
|
||||||
unsigned char sha1[20];
|
|
||||||
int line_termination = '\n';
|
|
||||||
const struct option option[] = {
|
|
||||||
OPT_SET_INT('z', NULL, &line_termination, "input is NUL terminated", '\0'),
|
|
||||||
OPT_END()
|
|
||||||
};
|
|
||||||
|
|
||||||
ac = parse_options(ac, av, option, mktree_usage, 0);
|
|
||||||
|
|
||||||
while (strbuf_getline(&sb, stdin, line_termination) != EOF) {
|
|
||||||
char *ptr, *ntr;
|
char *ptr, *ntr;
|
||||||
unsigned mode;
|
unsigned mode;
|
||||||
enum object_type type;
|
enum object_type type;
|
||||||
char *path;
|
char *path;
|
||||||
|
unsigned char sha1[20];
|
||||||
|
|
||||||
ptr = sb.buf;
|
ptr = buf;
|
||||||
/*
|
/*
|
||||||
* Read non-recursive ls-tree output format:
|
* Read non-recursive ls-tree output format:
|
||||||
* mode SP type SP sha1 TAB name
|
* mode SP type SP sha1 TAB name
|
||||||
*/
|
*/
|
||||||
mode = strtoul(ptr, &ntr, 8);
|
mode = strtoul(ptr, &ntr, 8);
|
||||||
if (ptr == ntr || !ntr || *ntr != ' ')
|
if (ptr == ntr || !ntr || *ntr != ' ')
|
||||||
die("input format error: %s", sb.buf);
|
die("input format error: %s", buf);
|
||||||
ptr = ntr + 1; /* type */
|
ptr = ntr + 1; /* type */
|
||||||
ntr = strchr(ptr, ' ');
|
ntr = strchr(ptr, ' ');
|
||||||
if (!ntr || sb.buf + sb.len <= ntr + 40 ||
|
if (!ntr || buf + len <= ntr + 40 ||
|
||||||
ntr[41] != '\t' ||
|
ntr[41] != '\t' ||
|
||||||
get_sha1_hex(ntr + 1, sha1))
|
get_sha1_hex(ntr + 1, sha1))
|
||||||
die("input format error: %s", sb.buf);
|
die("input format error: %s", buf);
|
||||||
type = sha1_object_info(sha1, NULL);
|
type = sha1_object_info(sha1, NULL);
|
||||||
if (type < 0)
|
if (type < 0)
|
||||||
die("object %s unavailable", sha1_to_hex(sha1));
|
die("object %s unavailable", sha1_to_hex(sha1));
|
||||||
@ -109,16 +98,29 @@ int cmd_mktree(int ac, const char **av, const char *prefix)
|
|||||||
|
|
||||||
path = ntr + 41; /* at the beginning of name */
|
path = ntr + 41; /* at the beginning of name */
|
||||||
if (line_termination && path[0] == '"') {
|
if (line_termination && path[0] == '"') {
|
||||||
strbuf_reset(&p_uq);
|
struct strbuf p_uq = STRBUF_INIT;
|
||||||
if (unquote_c_style(&p_uq, path, NULL)) {
|
if (unquote_c_style(&p_uq, path, NULL))
|
||||||
die("invalid quoting");
|
die("invalid quoting");
|
||||||
|
path = strbuf_detach(&p_uq, NULL);
|
||||||
}
|
}
|
||||||
path = p_uq.buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
append_to_tree(mode, sha1, path);
|
append_to_tree(mode, sha1, path);
|
||||||
}
|
}
|
||||||
strbuf_release(&p_uq);
|
|
||||||
|
int cmd_mktree(int ac, const char **av, const char *prefix)
|
||||||
|
{
|
||||||
|
struct strbuf sb = STRBUF_INIT;
|
||||||
|
unsigned char sha1[20];
|
||||||
|
int line_termination = '\n';
|
||||||
|
const struct option option[] = {
|
||||||
|
OPT_SET_INT('z', NULL, &line_termination, "input is NUL terminated", '\0'),
|
||||||
|
OPT_END()
|
||||||
|
};
|
||||||
|
|
||||||
|
ac = parse_options(ac, av, option, mktree_usage, 0);
|
||||||
|
|
||||||
|
while (strbuf_getline(&sb, stdin, line_termination) != EOF)
|
||||||
|
mktree_line(sb.buf, sb.len, line_termination);
|
||||||
|
|
||||||
strbuf_release(&sb);
|
strbuf_release(&sb);
|
||||||
|
|
||||||
write_tree(sha1);
|
write_tree(sha1);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user