@matheusbsbs / matheus-bagliano / commits / fe0bb5f7bc
Fork 0

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>

Junio C Hamano committed May 10, 2009 at 10:41 UTC fe0bb5f7bce3dbcc32325c74e693a726d0c2808b
1 file changed +42 -40
builtin-mktree.c
+42 -40
@@ -67,10 +67,48 @@ static const char *mktree_usage[] = {
67 NULL
68 };
69
70 +static void mktree_line(char *buf, size_t len, int line_termination)
71 +{
72 + char *ptr, *ntr;
73 + unsigned mode;
74 + enum object_type type;
75 + char *path;
76 + unsigned char sha1[20];
77 +
78 + ptr = buf;
79 + /*
80 + * Read non-recursive ls-tree output format:
81 + * mode SP type SP sha1 TAB name
82 + */
83 + mode = strtoul(ptr, &ntr, 8);
84 + if (ptr == ntr || !ntr || *ntr != ' ')
85 + die("input format error: %s", buf);
86 + ptr = ntr + 1; /* type */
87 + ntr = strchr(ptr, ' ');
88 + if (!ntr || buf + len <= ntr + 40 ||
89 + ntr[41] != '\t' ||
90 + get_sha1_hex(ntr + 1, sha1))
91 + die("input format error: %s", buf);
92 + type = sha1_object_info(sha1, NULL);
93 + if (type < 0)
94 + die("object %s unavailable", sha1_to_hex(sha1));
95 + *ntr++ = 0; /* now at the beginning of SHA1 */
96 + if (type != type_from_string(ptr))
97 + die("object type %s mismatch (%s)", ptr, typename(type));
98 +
99 + path = ntr + 41; /* at the beginning of name */
100 + if (line_termination && path[0] == '"') {
101 + struct strbuf p_uq = STRBUF_INIT;
102 + if (unquote_c_style(&p_uq, path, NULL))
103 + die("invalid quoting");
104 + path = strbuf_detach(&p_uq, NULL);
105 + }
106 + append_to_tree(mode, sha1, path);
107 +}
108 +
109 int cmd_mktree(int ac, const char **av, const char *prefix)
110 {
111 struct strbuf sb = STRBUF_INIT;
73 - struct strbuf p_uq = STRBUF_INIT;
112 unsigned char sha1[20];
113 int line_termination = '\n';
114 const struct option option[] = {
@@ -80,45 +118,9 @@ int cmd_mktree(int ac, const char **av, const char *prefix)
118
119 ac = parse_options(ac, av, option, mktree_usage, 0);
120
83 - while (strbuf_getline(&sb, stdin, line_termination) != EOF) {
84 - char *ptr, *ntr;
85 - unsigned mode;
86 - enum object_type type;
87 - char *path;
88 -
89 - ptr = sb.buf;
90 - /*
91 - * Read non-recursive ls-tree output format:
92 - * mode SP type SP sha1 TAB name
93 - */
94 - mode = strtoul(ptr, &ntr, 8);
95 - if (ptr == ntr || !ntr || *ntr != ' ')
96 - die("input format error: %s", sb.buf);
97 - ptr = ntr + 1; /* type */
98 - ntr = strchr(ptr, ' ');
99 - if (!ntr || sb.buf + sb.len <= ntr + 40 ||
100 - ntr[41] != '\t' ||
101 - get_sha1_hex(ntr + 1, sha1))
102 - die("input format error: %s", sb.buf);
103 - type = sha1_object_info(sha1, NULL);
104 - if (type < 0)
105 - die("object %s unavailable", sha1_to_hex(sha1));
106 - *ntr++ = 0; /* now at the beginning of SHA1 */
107 - if (type != type_from_string(ptr))
108 - die("object type %s mismatch (%s)", ptr, typename(type));
109 -
110 - path = ntr + 41; /* at the beginning of name */
111 - if (line_termination && path[0] == '"') {
112 - strbuf_reset(&p_uq);
113 - if (unquote_c_style(&p_uq, path, NULL)) {
114 - die("invalid quoting");
115 - }
116 - path = p_uq.buf;
117 - }
118 -
119 - append_to_tree(mode, sha1, path);
120 - }
121 - strbuf_release(&p_uq);
121 + while (strbuf_getline(&sb, stdin, line_termination) != EOF)
122 + mktree_line(sb.buf, sb.len, line_termination);
123 +
124 strbuf_release(&sb);
125
126 write_tree(sha1);