]>
Commit | Line | Data |
---|---|---|
8bc9a0c7 LT |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | */ | |
e83c5163 | 6 | #include "cache.h" |
8e440259 PE |
7 | #include "commit.h" |
8 | #include "tree.h" | |
6d96ac18 | 9 | #include "builtin.h" |
9e832665 | 10 | #include "utf8.h" |
e83c5163 | 11 | |
96b8d93a | 12 | static const char commit_tree_usage[] = "git commit-tree [(-p <sha1>)...] [-m <message>] [-F <file>] <sha1> <changelog"; |
c5bac17a | 13 | |
ef98c5ca | 14 | static void new_parent(struct commit *parent, struct commit_list **parents_p) |
b389237a | 15 | { |
ef98c5ca JS |
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) { | |
b389237a | 20 | error("duplicate parent %s ignored", sha1_to_hex(sha1)); |
ef98c5ca | 21 | return; |
b389237a | 22 | } |
ef98c5ca | 23 | parents_p = &parents->next; |
b389237a | 24 | } |
ef98c5ca | 25 | commit_list_insert(parent, parents_p); |
b389237a LT |
26 | } |
27 | ||
7b9c0a69 MV |
28 | int cmd_commit_tree(int argc, const char **argv, const char *prefix) |
29 | { | |
79a9312c | 30 | int i, got_tree = 0; |
7b9c0a69 MV |
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 | ||
6e9daeff | 38 | if (argc < 2 || !strcmp(argv[1], "-h")) |
7b9c0a69 | 39 | usage(commit_tree_usage); |
7b9c0a69 | 40 | |
79a9312c JH |
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 | } | |
7b9c0a69 | 53 | |
96b8d93a JH |
54 | if (!strcmp(arg, "-m")) { |
55 | if (argc <= ++i) | |
56 | usage(commit_tree_usage); | |
57 | if (buffer.len) | |
58 | strbuf_addch(&buffer, '\n'); | |
59 | strbuf_addstr(&buffer, argv[i]); | |
60 | strbuf_complete_line(&buffer); | |
61 | continue; | |
62 | } | |
63 | ||
64 | if (!strcmp(arg, "-F")) { | |
65 | int fd; | |
66 | ||
67 | if (argc <= ++i) | |
68 | usage(commit_tree_usage); | |
69 | if (buffer.len) | |
70 | strbuf_addch(&buffer, '\n'); | |
71 | if (!strcmp(argv[i], "-")) | |
72 | fd = 0; | |
73 | else { | |
74 | fd = open(argv[i], O_RDONLY); | |
75 | if (fd < 0) | |
76 | die_errno("git commit-tree: failed to open '%s'", | |
77 | argv[i]); | |
78 | } | |
79 | if (strbuf_read(&buffer, fd, 0) < 0) | |
80 | die_errno("git commit-tree: failed to read '%s'", | |
81 | argv[i]); | |
82 | if (fd && close(fd)) | |
83 | die_errno("git commit-tree: failed to close '%s'", | |
84 | argv[i]); | |
85 | strbuf_complete_line(&buffer); | |
86 | continue; | |
87 | } | |
88 | ||
79a9312c JH |
89 | if (get_sha1(arg, tree_sha1)) |
90 | die("Not a valid object name %s", arg); | |
91 | if (got_tree) | |
92 | die("Cannot give more than one trees"); | |
93 | got_tree = 1; | |
7b9c0a69 MV |
94 | } |
95 | ||
96b8d93a JH |
96 | if (!buffer.len) { |
97 | if (strbuf_read(&buffer, 0, 0) < 0) | |
98 | die_errno("git commit-tree: failed to read"); | |
99 | } | |
7b9c0a69 | 100 | |
79bc2af5 JN |
101 | if (commit_tree(buffer.buf, tree_sha1, parents, commit_sha1, NULL)) { |
102 | strbuf_release(&buffer); | |
7561d9f5 | 103 | return 1; |
79bc2af5 JN |
104 | } |
105 | ||
106 | printf("%s\n", sha1_to_hex(commit_sha1)); | |
107 | strbuf_release(&buffer); | |
108 | return 0; | |
e83c5163 | 109 | } |