@matheusbsbs / matheus-bagliano / commits / 7b9c0a69a5
Fork 0

git-commit-tree: make it usable from other builtins

Move all functionality (except option parsing) from cmd_commit_tree() to commit_tree(), so that other builtins can use it without a child process. Signed-off-by: Miklos Vajna <vmiklos@frugalware.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Miklos Vajna committed Jul 1, 2008 at 04:37 UTC 7b9c0a69a5f64fffb5de8b49a96f41ac35b4a84f
2 files changed +45 -30
builtin-commit-tree.c
+41 -30
@@ -45,41 +45,19 @@ static const char commit_utf8_warn[] =
45 "You may want to amend it after fixing the message, or set the config\n"
46 "variable i18n.commitencoding to the encoding your project uses.\n";
47
48 -int cmd_commit_tree(int argc, const char **argv, const char *prefix)
48 +int commit_tree(const char *msg, unsigned char *tree,
49 + struct commit_list *parents, unsigned char *ret)
50 {
50 - int i;
51 - struct commit_list *parents = NULL;
52 - unsigned char tree_sha1[20];
53 - unsigned char commit_sha1[20];
54 - struct strbuf buffer;
51 int encoding_is_utf8;
52 + struct strbuf buffer;
53
57 - git_config(git_default_config, NULL);
58 -
59 - if (argc < 2)
60 - usage(commit_tree_usage);
61 - if (get_sha1(argv[1], tree_sha1))
62 - die("Not a valid object name %s", argv[1]);
63 -
64 - check_valid(tree_sha1, OBJ_TREE);
65 - for (i = 2; i < argc; i += 2) {
66 - unsigned char sha1[20];
67 - const char *a, *b;
68 - a = argv[i]; b = argv[i+1];
69 - if (!b || strcmp(a, "-p"))
70 - usage(commit_tree_usage);
71 -
72 - if (get_sha1(b, sha1))
73 - die("Not a valid object name %s", b);
74 - check_valid(sha1, OBJ_COMMIT);
75 - new_parent(lookup_commit(sha1), &parents);
76 - }
54 + check_valid(tree, OBJ_TREE);
55
56 /* Not having i18n.commitencoding is the same as having utf-8 */
57 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
58
59 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
82 - strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree_sha1));
60 + strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
61
62 /*
63 * NOTE! This ordering means that the same exact tree merged with a
@@ -102,14 +80,47 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix)
80 strbuf_addch(&buffer, '\n');
81
82 /* And add the comment */
105 - if (strbuf_read(&buffer, 0, 0) < 0)
106 - die("git-commit-tree: read returned %s", strerror(errno));
83 + strbuf_addstr(&buffer, msg);
84
85 /* And check the encoding */
86 if (encoding_is_utf8 && !is_utf8(buffer.buf))
87 fprintf(stderr, commit_utf8_warn);
88
112 - if (!write_sha1_file(buffer.buf, buffer.len, commit_type, commit_sha1)) {
89 + return write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
90 +}
91 +
92 +int cmd_commit_tree(int argc, const char **argv, const char *prefix)
93 +{
94 + int i;
95 + struct commit_list *parents = NULL;
96 + unsigned char tree_sha1[20];
97 + unsigned char commit_sha1[20];
98 + struct strbuf buffer = STRBUF_INIT;
99 +
100 + git_config(git_default_config, NULL);
101 +
102 + if (argc < 2)
103 + usage(commit_tree_usage);
104 + if (get_sha1(argv[1], tree_sha1))
105 + die("Not a valid object name %s", argv[1]);
106 +
107 + for (i = 2; i < argc; i += 2) {
108 + unsigned char sha1[20];
109 + const char *a, *b;
110 + a = argv[i]; b = argv[i+1];
111 + if (!b || strcmp(a, "-p"))
112 + usage(commit_tree_usage);
113 +
114 + if (get_sha1(b, sha1))
115 + die("Not a valid object name %s", b);
116 + check_valid(sha1, OBJ_COMMIT);
117 + new_parent(lookup_commit(sha1), &parents);
118 + }
119 +
120 + if (strbuf_read(&buffer, 0, 0) < 0)
121 + die("git-commit-tree: read returned %s", strerror(errno));
122 +
123 + if (!commit_tree(buffer.buf, tree_sha1, parents, commit_sha1)) {
124 printf("%s\n", sha1_to_hex(commit_sha1));
125 return 0;
126 }
builtin.h
+4
@@ -3,6 +3,8 @@
3
4 #include "git-compat-util.h"
5 #include "strbuf.h"
6 +#include "cache.h"
7 +#include "commit.h"
8
9 extern const char git_version_string[];
10 extern const char git_usage_string[];
@@ -14,6 +16,8 @@ extern void prune_packed_objects(int);
16 extern int read_line_with_nul(char *buf, int size, FILE *file);
17 extern int fmt_merge_msg(int merge_summary, struct strbuf *in,
18 struct strbuf *out);
19 +extern int commit_tree(const char *msg, unsigned char *tree,
20 + struct commit_list *parents, unsigned char *ret);
21
22 extern int cmd_add(int argc, const char **argv, const char *prefix);
23 extern int cmd_annotate(int argc, const char **argv, const char *prefix);