Skip to content

Commit 79a9312

Browse files
committed
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 <[email protected]>
1 parent ed7a42a commit 79a9312

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

builtin/commit-tree.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static void new_parent(struct commit *parent, struct commit_list **parents_p)
2727

2828
int cmd_commit_tree(int argc, const char **argv, const char *prefix)
2929
{
30-
int i;
30+
int i, got_tree = 0;
3131
struct commit_list *parents = NULL;
3232
unsigned char tree_sha1[20];
3333
unsigned char commit_sha1[20];
@@ -37,20 +37,25 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix)
3737

3838
if (argc < 2 || !strcmp(argv[1], "-h"))
3939
usage(commit_tree_usage);
40-
if (get_sha1(argv[1], tree_sha1))
41-
die("Not a valid object name %s", argv[1]);
4240

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+
}
4953

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;
5459
}
5560

5661
if (strbuf_read(&buffer, 0, 0) < 0)

0 commit comments

Comments
 (0)