add 'git credential' plumbing command
[git/git.git] / builtin / commit-tree.c
CommitLineData
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"
ba3c69a9 11#include "gpg-interface.h"
e83c5163 12
ba3c69a9 13static const char commit_tree_usage[] = "git commit-tree [(-p <sha1>)...] [-S<signer>] [-m <message>] [-F <file>] <sha1> <changelog";
c5bac17a 14
ef98c5ca 15static void new_parent(struct commit *parent, struct commit_list **parents_p)
b389237a 16{
ef98c5ca
JS
17 unsigned char *sha1 = parent->object.sha1;
18 struct commit_list *parents;
19 for (parents = *parents_p; parents; parents = parents->next) {
20 if (parents->item == parent) {
b389237a 21 error("duplicate parent %s ignored", sha1_to_hex(sha1));
ef98c5ca 22 return;
b389237a 23 }
ef98c5ca 24 parents_p = &parents->next;
b389237a 25 }
ef98c5ca 26 commit_list_insert(parent, parents_p);
b389237a
LT
27}
28
ba3c69a9
JH
29static int commit_tree_config(const char *var, const char *value, void *cb)
30{
31 int status = git_gpg_config(var, value, NULL);
32 if (status)
33 return status;
34 return git_default_config(var, value, cb);
35}
36
7b9c0a69
MV
37int cmd_commit_tree(int argc, const char **argv, const char *prefix)
38{
79a9312c 39 int i, got_tree = 0;
7b9c0a69
MV
40 struct commit_list *parents = NULL;
41 unsigned char tree_sha1[20];
42 unsigned char commit_sha1[20];
43 struct strbuf buffer = STRBUF_INIT;
ba3c69a9 44 const char *sign_commit = NULL;
7b9c0a69 45
ba3c69a9 46 git_config(commit_tree_config, NULL);
7b9c0a69 47
6e9daeff 48 if (argc < 2 || !strcmp(argv[1], "-h"))
7b9c0a69 49 usage(commit_tree_usage);
7b9c0a69 50
ba3c69a9
JH
51 if (get_sha1(argv[1], tree_sha1))
52 die("Not a valid object name %s", argv[1]);
53
79a9312c
JH
54 for (i = 1; i < argc; i++) {
55 const char *arg = argv[i];
56 if (!strcmp(arg, "-p")) {
57 unsigned char sha1[20];
58 if (argc <= ++i)
59 usage(commit_tree_usage);
60 if (get_sha1(argv[i], sha1))
61 die("Not a valid object name %s", argv[i]);
62 assert_sha1_type(sha1, OBJ_COMMIT);
63 new_parent(lookup_commit(sha1), &parents);
64 continue;
65 }
7b9c0a69 66
ba3c69a9
JH
67 if (!memcmp(arg, "-S", 2)) {
68 sign_commit = arg + 2;
69 continue;
70 }
71
96b8d93a
JH
72 if (!strcmp(arg, "-m")) {
73 if (argc <= ++i)
74 usage(commit_tree_usage);
75 if (buffer.len)
76 strbuf_addch(&buffer, '\n');
77 strbuf_addstr(&buffer, argv[i]);
78 strbuf_complete_line(&buffer);
79 continue;
80 }
81
82 if (!strcmp(arg, "-F")) {
83 int fd;
84
85 if (argc <= ++i)
86 usage(commit_tree_usage);
87 if (buffer.len)
88 strbuf_addch(&buffer, '\n');
89 if (!strcmp(argv[i], "-"))
90 fd = 0;
91 else {
92 fd = open(argv[i], O_RDONLY);
93 if (fd < 0)
94 die_errno("git commit-tree: failed to open '%s'",
95 argv[i]);
96 }
97 if (strbuf_read(&buffer, fd, 0) < 0)
98 die_errno("git commit-tree: failed to read '%s'",
99 argv[i]);
100 if (fd && close(fd))
101 die_errno("git commit-tree: failed to close '%s'",
102 argv[i]);
103 strbuf_complete_line(&buffer);
104 continue;
105 }
106
79a9312c
JH
107 if (get_sha1(arg, tree_sha1))
108 die("Not a valid object name %s", arg);
109 if (got_tree)
110 die("Cannot give more than one trees");
111 got_tree = 1;
7b9c0a69
MV
112 }
113
96b8d93a
JH
114 if (!buffer.len) {
115 if (strbuf_read(&buffer, 0, 0) < 0)
116 die_errno("git commit-tree: failed to read");
117 }
7b9c0a69 118
5de89d3a 119 if (commit_tree(&buffer, tree_sha1, parents, commit_sha1,
ba3c69a9 120 NULL, sign_commit)) {
79bc2af5 121 strbuf_release(&buffer);
7561d9f5 122 return 1;
79bc2af5
JN
123 }
124
125 printf("%s\n", sha1_to_hex(commit_sha1));
126 strbuf_release(&buffer);
127 return 0;
e83c5163 128}