diff options
author | Junio C Hamano <gitster@pobox.com> | 2009-05-10 10:41:22 -0700 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2009-05-10 12:41:35 -0700 |
commit | fe0bb5f7bce3dbcc32325c74e693a726d0c2808b (patch) | |
tree | a231d3cc1a705636d5cc3f62e8d11a4504c0f77c /builtin-mktree.c | |
parent | mktree: use parse-options (diff) | |
download | tgif-fe0bb5f7bce3dbcc32325c74e693a726d0c2808b.tar.xz |
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>
Diffstat (limited to 'builtin-mktree.c')
-rw-r--r-- | builtin-mktree.c | 82 |
1 files changed, 42 insertions, 40 deletions
diff --git a/builtin-mktree.c b/builtin-mktree.c index 2b3145bc72..133ab4b0f8 100644 --- a/builtin-mktree.c +++ b/builtin-mktree.c @@ -67,10 +67,48 @@ static const char *mktree_usage[] = { NULL }; +static void mktree_line(char *buf, size_t len, int line_termination) +{ + char *ptr, *ntr; + unsigned mode; + enum object_type type; + char *path; + unsigned char sha1[20]; + + ptr = buf; + /* + * Read non-recursive ls-tree output format: + * mode SP type SP sha1 TAB name + */ + mode = strtoul(ptr, &ntr, 8); + if (ptr == ntr || !ntr || *ntr != ' ') + die("input format error: %s", buf); + ptr = ntr + 1; /* type */ + ntr = strchr(ptr, ' '); + if (!ntr || buf + len <= ntr + 40 || + ntr[41] != '\t' || + get_sha1_hex(ntr + 1, sha1)) + die("input format error: %s", buf); + type = sha1_object_info(sha1, NULL); + if (type < 0) + die("object %s unavailable", sha1_to_hex(sha1)); + *ntr++ = 0; /* now at the beginning of SHA1 */ + if (type != type_from_string(ptr)) + die("object type %s mismatch (%s)", ptr, typename(type)); + + path = ntr + 41; /* at the beginning of name */ + if (line_termination && path[0] == '"') { + struct strbuf p_uq = STRBUF_INIT; + if (unquote_c_style(&p_uq, path, NULL)) + die("invalid quoting"); + path = strbuf_detach(&p_uq, NULL); + } + append_to_tree(mode, sha1, path); +} + int cmd_mktree(int ac, const char **av, const char *prefix) { struct strbuf sb = STRBUF_INIT; - struct strbuf p_uq = STRBUF_INIT; unsigned char sha1[20]; int line_termination = '\n'; const struct option option[] = { @@ -80,45 +118,9 @@ int cmd_mktree(int ac, const char **av, const char *prefix) ac = parse_options(ac, av, option, mktree_usage, 0); - while (strbuf_getline(&sb, stdin, line_termination) != EOF) { - char *ptr, *ntr; - unsigned mode; - enum object_type type; - char *path; - - ptr = sb.buf; - /* - * Read non-recursive ls-tree output format: - * mode SP type SP sha1 TAB name - */ - mode = strtoul(ptr, &ntr, 8); - if (ptr == ntr || !ntr || *ntr != ' ') - die("input format error: %s", sb.buf); - ptr = ntr + 1; /* type */ - ntr = strchr(ptr, ' '); - if (!ntr || sb.buf + sb.len <= ntr + 40 || - ntr[41] != '\t' || - get_sha1_hex(ntr + 1, sha1)) - die("input format error: %s", sb.buf); - type = sha1_object_info(sha1, NULL); - if (type < 0) - die("object %s unavailable", sha1_to_hex(sha1)); - *ntr++ = 0; /* now at the beginning of SHA1 */ - if (type != type_from_string(ptr)) - die("object type %s mismatch (%s)", ptr, typename(type)); - - path = ntr + 41; /* at the beginning of name */ - if (line_termination && path[0] == '"') { - strbuf_reset(&p_uq); - if (unquote_c_style(&p_uq, path, NULL)) { - die("invalid quoting"); - } - path = p_uq.buf; - } - - append_to_tree(mode, sha1, path); - } - strbuf_release(&p_uq); + while (strbuf_getline(&sb, stdin, line_termination) != EOF) + mktree_line(sb.buf, sb.len, line_termination); + strbuf_release(&sb); write_tree(sha1); |