2005-04-07 22:16:10 +00:00
|
|
|
/*
|
|
|
|
* GIT - The information manager from hell
|
|
|
|
*
|
|
|
|
* Copyright (C) Linus Torvalds, 2005
|
|
|
|
*/
|
2005-04-07 22:13:13 +00:00
|
|
|
#include "cache.h"
|
2006-04-02 12:44:09 +00:00
|
|
|
#include "commit.h"
|
|
|
|
#include "tree.h"
|
2006-05-23 12:15:33 +00:00
|
|
|
#include "builtin.h"
|
2006-12-22 21:06:08 +00:00
|
|
|
#include "utf8.h"
|
2005-04-07 22:13:13 +00:00
|
|
|
|
2011-11-09 19:54:04 +00:00
|
|
|
static const char commit_tree_usage[] = "git commit-tree [(-p <sha1>)...] [-m <message>] [-F <file>] <sha1> <changelog";
|
2005-04-21 02:49:16 +00:00
|
|
|
|
2008-06-27 12:24:47 +00:00
|
|
|
static void new_parent(struct commit *parent, struct commit_list **parents_p)
|
2005-06-19 17:40:10 +00:00
|
|
|
{
|
2008-06-27 12:24:47 +00:00
|
|
|
unsigned char *sha1 = parent->object.sha1;
|
|
|
|
struct commit_list *parents;
|
|
|
|
for (parents = *parents_p; parents; parents = parents->next) {
|
|
|
|
if (parents->item == parent) {
|
2005-06-19 17:40:10 +00:00
|
|
|
error("duplicate parent %s ignored", sha1_to_hex(sha1));
|
2008-06-27 12:24:47 +00:00
|
|
|
return;
|
2005-06-19 17:40:10 +00:00
|
|
|
}
|
2008-06-27 12:24:47 +00:00
|
|
|
parents_p = &parents->next;
|
2005-06-19 17:40:10 +00:00
|
|
|
}
|
2008-06-27 12:24:47 +00:00
|
|
|
commit_list_insert(parent, parents_p);
|
2005-06-19 17:40:10 +00:00
|
|
|
}
|
|
|
|
|
2008-07-01 02:37:49 +00:00
|
|
|
int cmd_commit_tree(int argc, const char **argv, const char *prefix)
|
|
|
|
{
|
2011-11-09 19:33:55 +00:00
|
|
|
int i, got_tree = 0;
|
2008-07-01 02:37:49 +00:00
|
|
|
struct commit_list *parents = NULL;
|
|
|
|
unsigned char tree_sha1[20];
|
|
|
|
unsigned char commit_sha1[20];
|
|
|
|
struct strbuf buffer = STRBUF_INIT;
|
|
|
|
|
|
|
|
git_config(git_default_config, NULL);
|
|
|
|
|
2009-11-09 15:04:44 +00:00
|
|
|
if (argc < 2 || !strcmp(argv[1], "-h"))
|
2008-07-01 02:37:49 +00:00
|
|
|
usage(commit_tree_usage);
|
|
|
|
|
2011-11-09 19:33:55 +00:00
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
const char *arg = argv[i];
|
|
|
|
if (!strcmp(arg, "-p")) {
|
|
|
|
unsigned char sha1[20];
|
|
|
|
if (argc <= ++i)
|
|
|
|
usage(commit_tree_usage);
|
|
|
|
if (get_sha1(argv[i], sha1))
|
|
|
|
die("Not a valid object name %s", argv[i]);
|
|
|
|
assert_sha1_type(sha1, OBJ_COMMIT);
|
|
|
|
new_parent(lookup_commit(sha1), &parents);
|
|
|
|
continue;
|
|
|
|
}
|
2008-07-01 02:37:49 +00:00
|
|
|
|
2011-11-09 19:54:04 +00:00
|
|
|
if (!strcmp(arg, "-m")) {
|
|
|
|
if (argc <= ++i)
|
|
|
|
usage(commit_tree_usage);
|
|
|
|
if (buffer.len)
|
|
|
|
strbuf_addch(&buffer, '\n');
|
|
|
|
strbuf_addstr(&buffer, argv[i]);
|
|
|
|
strbuf_complete_line(&buffer);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(arg, "-F")) {
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (argc <= ++i)
|
|
|
|
usage(commit_tree_usage);
|
|
|
|
if (buffer.len)
|
|
|
|
strbuf_addch(&buffer, '\n');
|
|
|
|
if (!strcmp(argv[i], "-"))
|
|
|
|
fd = 0;
|
|
|
|
else {
|
|
|
|
fd = open(argv[i], O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
die_errno("git commit-tree: failed to open '%s'",
|
|
|
|
argv[i]);
|
|
|
|
}
|
|
|
|
if (strbuf_read(&buffer, fd, 0) < 0)
|
|
|
|
die_errno("git commit-tree: failed to read '%s'",
|
|
|
|
argv[i]);
|
|
|
|
if (fd && close(fd))
|
|
|
|
die_errno("git commit-tree: failed to close '%s'",
|
|
|
|
argv[i]);
|
|
|
|
strbuf_complete_line(&buffer);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-11-09 19:33:55 +00:00
|
|
|
if (get_sha1(arg, tree_sha1))
|
|
|
|
die("Not a valid object name %s", arg);
|
|
|
|
if (got_tree)
|
|
|
|
die("Cannot give more than one trees");
|
|
|
|
got_tree = 1;
|
2008-07-01 02:37:49 +00:00
|
|
|
}
|
|
|
|
|
2011-11-09 19:54:04 +00:00
|
|
|
if (!buffer.len) {
|
|
|
|
if (strbuf_read(&buffer, 0, 0) < 0)
|
|
|
|
die_errno("git commit-tree: failed to read");
|
|
|
|
}
|
2008-07-01 02:37:49 +00:00
|
|
|
|
2011-12-15 13:47:22 +00:00
|
|
|
if (commit_tree(&buffer, tree_sha1, parents, commit_sha1, NULL)) {
|
2010-10-02 08:41:00 +00:00
|
|
|
strbuf_release(&buffer);
|
2006-03-25 06:23:25 +00:00
|
|
|
return 1;
|
2010-10-02 08:41:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("%s\n", sha1_to_hex(commit_sha1));
|
|
|
|
strbuf_release(&buffer);
|
|
|
|
return 0;
|
2005-04-07 22:13:13 +00:00
|
|
|
}
|