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 | |
Brad King | df45cb3 | 2013-03-25 17:00:07 -0400 | [diff] [blame] | 13 | static const char commit_tree_usage[] = "git commit-tree [(-p <sha1>)...] [-S[<keyid>]] [-m <message>] [-F <file>] <sha1> <changelog"; |
Junio C Hamano | c5bac17 | 2005-04-20 19:49:16 -0700 | [diff] [blame] | 14 | |
Nicolas Vigier | d95bfb1 | 2013-11-05 00:14:41 +0100 | [diff] [blame] | 15 | static const char *sign_commit; |
| 16 | |
Johannes Schindelin | ef98c5c | 2008-06-27 13:24:47 +0100 | [diff] [blame] | 17 | static void new_parent(struct commit *parent, struct commit_list **parents_p) |
Linus Torvalds | b389237 | 2005-06-19 10:40:10 -0700 | [diff] [blame] | 18 | { |
Johannes Schindelin | ef98c5c | 2008-06-27 13:24:47 +0100 | [diff] [blame] | 19 | unsigned char *sha1 = parent->object.sha1; |
| 20 | struct commit_list *parents; |
| 21 | for (parents = *parents_p; parents; parents = parents->next) { |
| 22 | if (parents->item == parent) { |
Linus Torvalds | b389237 | 2005-06-19 10:40:10 -0700 | [diff] [blame] | 23 | error("duplicate parent %s ignored", sha1_to_hex(sha1)); |
Johannes Schindelin | ef98c5c | 2008-06-27 13:24:47 +0100 | [diff] [blame] | 24 | return; |
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 | parents_p = &parents->next; |
Linus Torvalds | b389237 | 2005-06-19 10:40:10 -0700 | [diff] [blame] | 27 | } |
Johannes Schindelin | ef98c5c | 2008-06-27 13:24:47 +0100 | [diff] [blame] | 28 | commit_list_insert(parent, parents_p); |
Linus Torvalds | b389237 | 2005-06-19 10:40:10 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Junio C Hamano | ba3c69a | 2011-10-05 17:23:20 -0700 | [diff] [blame] | 31 | static int commit_tree_config(const char *var, const char *value, void *cb) |
| 32 | { |
| 33 | int status = git_gpg_config(var, value, NULL); |
| 34 | if (status) |
| 35 | return status; |
Nicolas Vigier | d95bfb1 | 2013-11-05 00:14:41 +0100 | [diff] [blame] | 36 | if (!strcmp(var, "commit.gpgsign")) { |
| 37 | sign_commit = git_config_bool(var, value) ? "" : NULL; |
| 38 | return 0; |
| 39 | } |
Junio C Hamano | ba3c69a | 2011-10-05 17:23:20 -0700 | [diff] [blame] | 40 | return git_default_config(var, value, cb); |
| 41 | } |
| 42 | |
Miklos Vajna | 7b9c0a6 | 2008-07-01 04:37:49 +0200 | [diff] [blame] | 43 | int cmd_commit_tree(int argc, const char **argv, const char *prefix) |
| 44 | { |
Junio C Hamano | 79a9312 | 2011-11-09 11:33:55 -0800 | [diff] [blame] | 45 | int i, got_tree = 0; |
Miklos Vajna | 7b9c0a6 | 2008-07-01 04:37:49 +0200 | [diff] [blame] | 46 | struct commit_list *parents = NULL; |
| 47 | unsigned char tree_sha1[20]; |
| 48 | unsigned char commit_sha1[20]; |
| 49 | struct strbuf buffer = STRBUF_INIT; |
| 50 | |
Junio C Hamano | ba3c69a | 2011-10-05 17:23:20 -0700 | [diff] [blame] | 51 | git_config(commit_tree_config, NULL); |
Miklos Vajna | 7b9c0a6 | 2008-07-01 04:37:49 +0200 | [diff] [blame] | 52 | |
Jonathan Nieder | 6e9daef | 2009-11-09 09:04:44 -0600 | [diff] [blame] | 53 | if (argc < 2 || !strcmp(argv[1], "-h")) |
Miklos Vajna | 7b9c0a6 | 2008-07-01 04:37:49 +0200 | [diff] [blame] | 54 | usage(commit_tree_usage); |
Miklos Vajna | 7b9c0a6 | 2008-07-01 04:37:49 +0200 | [diff] [blame] | 55 | |
Junio C Hamano | 79a9312 | 2011-11-09 11:33:55 -0800 | [diff] [blame] | 56 | for (i = 1; i < argc; i++) { |
| 57 | const char *arg = argv[i]; |
| 58 | if (!strcmp(arg, "-p")) { |
| 59 | unsigned char sha1[20]; |
| 60 | if (argc <= ++i) |
| 61 | usage(commit_tree_usage); |
Junio C Hamano | 75f5ac0 | 2012-07-03 10:03:38 -0700 | [diff] [blame] | 62 | if (get_sha1_commit(argv[i], sha1)) |
Junio C Hamano | 79a9312 | 2011-11-09 11:33:55 -0800 | [diff] [blame] | 63 | die("Not a valid object name %s", argv[i]); |
| 64 | assert_sha1_type(sha1, OBJ_COMMIT); |
| 65 | new_parent(lookup_commit(sha1), &parents); |
| 66 | continue; |
| 67 | } |
Miklos Vajna | 7b9c0a6 | 2008-07-01 04:37:49 +0200 | [diff] [blame] | 68 | |
René Scharfe | 8547e0f | 2014-12-24 01:18:11 +0100 | [diff] [blame] | 69 | if (skip_prefix(arg, "-S", &sign_commit)) |
Junio C Hamano | ba3c69a | 2011-10-05 17:23:20 -0700 | [diff] [blame] | 70 | continue; |
Junio C Hamano | ba3c69a | 2011-10-05 17:23:20 -0700 | [diff] [blame] | 71 | |
Junio C Hamano | 55ca3f9 | 2013-12-13 15:40:35 -0800 | [diff] [blame] | 72 | if (!strcmp(arg, "--no-gpg-sign")) { |
| 73 | sign_commit = NULL; |
| 74 | continue; |
| 75 | } |
| 76 | |
Junio C Hamano | 96b8d93 | 2011-11-09 11:54:04 -0800 | [diff] [blame] | 77 | if (!strcmp(arg, "-m")) { |
| 78 | if (argc <= ++i) |
| 79 | usage(commit_tree_usage); |
| 80 | if (buffer.len) |
| 81 | strbuf_addch(&buffer, '\n'); |
| 82 | strbuf_addstr(&buffer, argv[i]); |
| 83 | strbuf_complete_line(&buffer); |
| 84 | continue; |
| 85 | } |
| 86 | |
| 87 | if (!strcmp(arg, "-F")) { |
| 88 | int fd; |
| 89 | |
| 90 | if (argc <= ++i) |
| 91 | usage(commit_tree_usage); |
| 92 | if (buffer.len) |
| 93 | strbuf_addch(&buffer, '\n'); |
| 94 | if (!strcmp(argv[i], "-")) |
| 95 | fd = 0; |
| 96 | else { |
| 97 | fd = open(argv[i], O_RDONLY); |
| 98 | if (fd < 0) |
| 99 | die_errno("git commit-tree: failed to open '%s'", |
| 100 | argv[i]); |
| 101 | } |
| 102 | if (strbuf_read(&buffer, fd, 0) < 0) |
| 103 | die_errno("git commit-tree: failed to read '%s'", |
| 104 | argv[i]); |
| 105 | if (fd && close(fd)) |
| 106 | die_errno("git commit-tree: failed to close '%s'", |
| 107 | argv[i]); |
| 108 | strbuf_complete_line(&buffer); |
| 109 | continue; |
| 110 | } |
| 111 | |
Junio C Hamano | 75f5ac0 | 2012-07-03 10:03:38 -0700 | [diff] [blame] | 112 | if (get_sha1_tree(arg, tree_sha1)) |
Junio C Hamano | 79a9312 | 2011-11-09 11:33:55 -0800 | [diff] [blame] | 113 | die("Not a valid object name %s", arg); |
| 114 | if (got_tree) |
| 115 | die("Cannot give more than one trees"); |
| 116 | got_tree = 1; |
Miklos Vajna | 7b9c0a6 | 2008-07-01 04:37:49 +0200 | [diff] [blame] | 117 | } |
| 118 | |
Junio C Hamano | 96b8d93 | 2011-11-09 11:54:04 -0800 | [diff] [blame] | 119 | if (!buffer.len) { |
| 120 | if (strbuf_read(&buffer, 0, 0) < 0) |
| 121 | die_errno("git commit-tree: failed to read"); |
| 122 | } |
Miklos Vajna | 7b9c0a6 | 2008-07-01 04:37:49 +0200 | [diff] [blame] | 123 | |
Jeff King | 3ffefb5 | 2014-06-10 17:36:52 -0400 | [diff] [blame] | 124 | if (commit_tree(buffer.buf, buffer.len, tree_sha1, parents, |
| 125 | commit_sha1, NULL, sign_commit)) { |
Jonathan Nieder | 79bc2af | 2010-10-02 03:41:00 -0500 | [diff] [blame] | 126 | strbuf_release(&buffer); |
Junio C Hamano | 7561d9f | 2006-03-24 22:23:25 -0800 | [diff] [blame] | 127 | return 1; |
Jonathan Nieder | 79bc2af | 2010-10-02 03:41:00 -0500 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | printf("%s\n", sha1_to_hex(commit_sha1)); |
| 131 | strbuf_release(&buffer); |
| 132 | return 0; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 133 | } |