commit-tree: update the command line parsing
We have kept the original "git commit-tree <tree> -p <parent> ..." syntax forever, but "git commit-tree -p <parent> -p <parent> ... <tree>" would be more intuitive way to spell it. Dashed flags along with their arguments come first and then the "thing" argument after the flags. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Nov 9, 2011 at 11:33 UTC
79a9312cc99f9c2f2949e1211c708896e6037f64
1 file changed
+18
-13
builtin/commit-tree.c
+18
-13
@@ -27,7 +27,7 @@ static void new_parent(struct commit *parent, struct commit_list **parents_p)
27
28
int cmd_commit_tree(int argc, const char **argv, const char *prefix)
29
{
30
- int i;
30
+ int i, got_tree = 0;
31
struct commit_list *parents = NULL;
32
unsigned char tree_sha1[20];
33
unsigned char commit_sha1[20];
@@ -37,20 +37,25 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix)
37
38
if (argc < 2 || !strcmp(argv[1], "-h"))
39
usage(commit_tree_usage);
40
- if (get_sha1(argv[1], tree_sha1))
41
- die("Not a valid object name %s", argv[1]);
40
43
- for (i = 2; i < argc; i += 2) {
44
- unsigned char sha1[20];
45
- const char *a, *b;
46
- a = argv[i]; b = argv[i+1];
47
- if (!b || strcmp(a, "-p"))
48
- usage(commit_tree_usage);
41
+ for (i = 1; i < argc; i++) {
42
+ const char *arg = argv[i];
43
+ if (!strcmp(arg, "-p")) {
44
+ unsigned char sha1[20];
45
+ if (argc <= ++i)
46
+ usage(commit_tree_usage);
47
+ if (get_sha1(argv[i], sha1))
48
+ die("Not a valid object name %s", argv[i]);
49
+ assert_sha1_type(sha1, OBJ_COMMIT);
50
+ new_parent(lookup_commit(sha1), &parents);
51
+ continue;
52
+ }
53
50
- if (get_sha1(b, sha1))
51
- die("Not a valid object name %s", b);
52
- assert_sha1_type(sha1, OBJ_COMMIT);
53
- new_parent(lookup_commit(sha1), &parents);
54
+ if (get_sha1(arg, tree_sha1))
55
+ die("Not a valid object name %s", arg);
56
+ if (got_tree)
57
+ die("Cannot give more than one trees");
58
+ got_tree = 1;
59
}
60
61
if (strbuf_read(&buffer, 0, 0) < 0)