Merge branch 'maint-1.7.1' into maint-1.7.2
[git] / builtin / commit-tree.c
1 /*
2  * GIT - The information manager from hell
3  *
4  * Copyright (C) Linus Torvalds, 2005
5  */
6 #include "cache.h"
7 #include "commit.h"
8 #include "tree.h"
9 #include "builtin.h"
10 #include "utf8.h"
11
12 static const char commit_tree_usage[] = "git commit-tree <sha1> [-p <sha1>]* < changelog";
13
14 static void new_parent(struct commit *parent, struct commit_list **parents_p)
15 {
16         unsigned char *sha1 = parent->object.sha1;
17         struct commit_list *parents;
18         for (parents = *parents_p; parents; parents = parents->next) {
19                 if (parents->item == parent) {
20                         error("duplicate parent %s ignored", sha1_to_hex(sha1));
21                         return;
22                 }
23                 parents_p = &parents->next;
24         }
25         commit_list_insert(parent, parents_p);
26 }
27
28 int cmd_commit_tree(int argc, const char **argv, const char *prefix)
29 {
30         int i;
31         struct commit_list *parents = NULL;
32         unsigned char tree_sha1[20];
33         unsigned char commit_sha1[20];
34         struct strbuf buffer = STRBUF_INIT;
35
36         git_config(git_default_config, NULL);
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]);
42
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);
49
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         }
55
56         if (strbuf_read(&buffer, 0, 0) < 0)
57                 die_errno("git commit-tree: failed to read");
58
59         if (!commit_tree(buffer.buf, tree_sha1, parents, commit_sha1, NULL)) {
60                 printf("%s\n", sha1_to_hex(commit_sha1));
61                 return 0;
62         }
63         else
64                 return 1;
65 }