Linus Torvalds | 8bc9a0c | 2005-04-07 15:16:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * GIT - The information manager from hell |
| 3 | * |
| 4 | * Copyright (C) Linus Torvalds, 2005 |
| 5 | */ |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 6 | #include "cache.h" |
Peter Eriksen | 8e44025 | 2006-04-02 14:44:09 +0200 | [diff] [blame] | 7 | #include "commit.h" |
| 8 | #include "tree.h" |
Peter Eriksen | 6d96ac1 | 2006-05-23 14:15:33 +0200 | [diff] [blame] | 9 | #include "builtin.h" |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 10 | #include "utf8.h" |
Junio C Hamano | ba3c69a | 2011-10-05 17:23:20 -0700 | [diff] [blame] | 11 | #include "gpg-interface.h" |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 12 | |
Junio C Hamano | ba3c69a | 2011-10-05 17:23:20 -0700 | [diff] [blame] | 13 | static const char commit_tree_usage[] = "git commit-tree [(-p <sha1>)...] [-S<signer>] [-m <message>] [-F <file>] <sha1> <changelog"; |
Junio C Hamano | c5bac17 | 2005-04-20 19:49:16 -0700 | [diff] [blame] | 14 | |
Johannes Schindelin | ef98c5c | 2008-06-27 13:24:47 +0100 | [diff] [blame] | 15 | static void new_parent(struct commit *parent, struct commit_list **parents_p) |
Linus Torvalds | b389237 | 2005-06-19 10:40:10 -0700 | [diff] [blame] | 16 | { |
Johannes Schindelin | ef98c5c | 2008-06-27 13:24:47 +0100 | [diff] [blame] | 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) { |
Linus Torvalds | b389237 | 2005-06-19 10:40:10 -0700 | [diff] [blame] | 21 | error("duplicate parent %s ignored", sha1_to_hex(sha1)); |
Johannes Schindelin | ef98c5c | 2008-06-27 13:24:47 +0100 | [diff] [blame] | 22 | return; |
Linus Torvalds | b389237 | 2005-06-19 10:40:10 -0700 | [diff] [blame] | 23 | } |
Johannes Schindelin | ef98c5c | 2008-06-27 13:24:47 +0100 | [diff] [blame] | 24 | parents_p = &parents->next; |
Linus Torvalds | b389237 | 2005-06-19 10:40:10 -0700 | [diff] [blame] | 25 | } |
Johannes Schindelin | ef98c5c | 2008-06-27 13:24:47 +0100 | [diff] [blame] | 26 | commit_list_insert(parent, parents_p); |
Linus Torvalds | b389237 | 2005-06-19 10:40:10 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Junio C Hamano | ba3c69a | 2011-10-05 17:23:20 -0700 | [diff] [blame] | 29 | static 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 | |
Miklos Vajna | 7b9c0a6 | 2008-07-01 04:37:49 +0200 | [diff] [blame] | 37 | int cmd_commit_tree(int argc, const char **argv, const char *prefix) |
| 38 | { |
Junio C Hamano | 79a9312 | 2011-11-09 11:33:55 -0800 | [diff] [blame] | 39 | int i, got_tree = 0; |
Miklos Vajna | 7b9c0a6 | 2008-07-01 04:37:49 +0200 | [diff] [blame] | 40 | struct commit_list *parents = NULL; |
| 41 | unsigned char tree_sha1[20]; |
| 42 | unsigned char commit_sha1[20]; |
| 43 | struct strbuf buffer = STRBUF_INIT; |
Junio C Hamano | ba3c69a | 2011-10-05 17:23:20 -0700 | [diff] [blame] | 44 | const char *sign_commit = NULL; |
Miklos Vajna | 7b9c0a6 | 2008-07-01 04:37:49 +0200 | [diff] [blame] | 45 | |
Junio C Hamano | ba3c69a | 2011-10-05 17:23:20 -0700 | [diff] [blame] | 46 | git_config(commit_tree_config, NULL); |
Miklos Vajna | 7b9c0a6 | 2008-07-01 04:37:49 +0200 | [diff] [blame] | 47 | |
Jonathan Nieder | 6e9daef | 2009-11-09 09:04:44 -0600 | [diff] [blame] | 48 | if (argc < 2 || !strcmp(argv[1], "-h")) |
Miklos Vajna | 7b9c0a6 | 2008-07-01 04:37:49 +0200 | [diff] [blame] | 49 | usage(commit_tree_usage); |
Miklos Vajna | 7b9c0a6 | 2008-07-01 04:37:49 +0200 | [diff] [blame] | 50 | |
Junio C Hamano | 79a9312 | 2011-11-09 11:33:55 -0800 | [diff] [blame] | 51 | for (i = 1; i < argc; i++) { |
| 52 | const char *arg = argv[i]; |
| 53 | if (!strcmp(arg, "-p")) { |
| 54 | unsigned char sha1[20]; |
| 55 | if (argc <= ++i) |
| 56 | usage(commit_tree_usage); |
Junio C Hamano | 75f5ac0 | 2012-07-03 10:03:38 -0700 | [diff] [blame] | 57 | if (get_sha1_commit(argv[i], sha1)) |
Junio C Hamano | 79a9312 | 2011-11-09 11:33:55 -0800 | [diff] [blame] | 58 | die("Not a valid object name %s", argv[i]); |
| 59 | assert_sha1_type(sha1, OBJ_COMMIT); |
| 60 | new_parent(lookup_commit(sha1), &parents); |
| 61 | continue; |
| 62 | } |
Miklos Vajna | 7b9c0a6 | 2008-07-01 04:37:49 +0200 | [diff] [blame] | 63 | |
Junio C Hamano | ba3c69a | 2011-10-05 17:23:20 -0700 | [diff] [blame] | 64 | if (!memcmp(arg, "-S", 2)) { |
| 65 | sign_commit = arg + 2; |
| 66 | continue; |
| 67 | } |
| 68 | |
Junio C Hamano | 96b8d93 | 2011-11-09 11:54:04 -0800 | [diff] [blame] | 69 | if (!strcmp(arg, "-m")) { |
| 70 | if (argc <= ++i) |
| 71 | usage(commit_tree_usage); |
| 72 | if (buffer.len) |
| 73 | strbuf_addch(&buffer, '\n'); |
| 74 | strbuf_addstr(&buffer, argv[i]); |
| 75 | strbuf_complete_line(&buffer); |
| 76 | continue; |
| 77 | } |
| 78 | |
| 79 | if (!strcmp(arg, "-F")) { |
| 80 | int fd; |
| 81 | |
| 82 | if (argc <= ++i) |
| 83 | usage(commit_tree_usage); |
| 84 | if (buffer.len) |
| 85 | strbuf_addch(&buffer, '\n'); |
| 86 | if (!strcmp(argv[i], "-")) |
| 87 | fd = 0; |
| 88 | else { |
| 89 | fd = open(argv[i], O_RDONLY); |
| 90 | if (fd < 0) |
| 91 | die_errno("git commit-tree: failed to open '%s'", |
| 92 | argv[i]); |
| 93 | } |
| 94 | if (strbuf_read(&buffer, fd, 0) < 0) |
| 95 | die_errno("git commit-tree: failed to read '%s'", |
| 96 | argv[i]); |
| 97 | if (fd && close(fd)) |
| 98 | die_errno("git commit-tree: failed to close '%s'", |
| 99 | argv[i]); |
| 100 | strbuf_complete_line(&buffer); |
| 101 | continue; |
| 102 | } |
| 103 | |
Junio C Hamano | 75f5ac0 | 2012-07-03 10:03:38 -0700 | [diff] [blame] | 104 | if (get_sha1_tree(arg, tree_sha1)) |
Junio C Hamano | 79a9312 | 2011-11-09 11:33:55 -0800 | [diff] [blame] | 105 | die("Not a valid object name %s", arg); |
| 106 | if (got_tree) |
| 107 | die("Cannot give more than one trees"); |
| 108 | got_tree = 1; |
Miklos Vajna | 7b9c0a6 | 2008-07-01 04:37:49 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Junio C Hamano | 96b8d93 | 2011-11-09 11:54:04 -0800 | [diff] [blame] | 111 | if (!buffer.len) { |
| 112 | if (strbuf_read(&buffer, 0, 0) < 0) |
| 113 | die_errno("git commit-tree: failed to read"); |
| 114 | } |
Miklos Vajna | 7b9c0a6 | 2008-07-01 04:37:49 +0200 | [diff] [blame] | 115 | |
Junio C Hamano | 5de89d3 | 2012-01-06 12:44:07 -0800 | [diff] [blame] | 116 | if (commit_tree(&buffer, tree_sha1, parents, commit_sha1, |
Junio C Hamano | ba3c69a | 2011-10-05 17:23:20 -0700 | [diff] [blame] | 117 | NULL, sign_commit)) { |
Jonathan Nieder | 79bc2af | 2010-10-02 03:41:00 -0500 | [diff] [blame] | 118 | strbuf_release(&buffer); |
Junio C Hamano | 7561d9f | 2006-03-24 22:23:25 -0800 | [diff] [blame] | 119 | return 1; |
Jonathan Nieder | 79bc2af | 2010-10-02 03:41:00 -0500 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | printf("%s\n", sha1_to_hex(commit_sha1)); |
| 123 | strbuf_release(&buffer); |
| 124 | return 0; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 125 | } |