make commit_tree a library function
Until now, this has been part of the commit-tree builtin. However, it is already used by other builtins (like commit, merge, and notes), and it would be useful to access it from library code. The check_valid helper has to come along, too, but is given a more library-ish name of "assert_sha1_type". Otherwise, the code is unchanged. There are still a few rough edges for a library function, like printing the utf8 warning to stderr, but we can address those if and when they come up as inappropriate. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Apr 1, 2010 at 20:05 UTC
40d52ff77b093fa48f58a168f4b0c4e65b862e56
6 files changed
+72
-72
builtin.h
-3
@@ -16,9 +16,6 @@ extern const char *help_unknown_cmd(const char *cmd);
16
extern void prune_packed_objects(int);
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
- const char *author);
19
extern int commit_notes(struct notes_tree *t, const char *msg);
20
21
struct notes_rewrite_cfg {
builtin/commit-tree.c
+1
-69
@@ -9,19 +9,6 @@
9
#include "builtin.h"
10
#include "utf8.h"
11
12
-/*
13
- * FIXME! Share the code with "write-tree.c"
14
- */
15
-static void check_valid(unsigned char *sha1, enum object_type expect)
16
-{
17
- enum object_type type = sha1_object_info(sha1, NULL);
18
- if (type < 0)
19
- die("%s is not a valid object", sha1_to_hex(sha1));
20
- if (type != expect)
21
- die("%s is not a valid '%s' object", sha1_to_hex(sha1),
22
- typename(expect));
23
-}
24
-
12
static const char commit_tree_usage[] = "git commit-tree <sha1> [-p <sha1>]* < changelog";
13
14
static void new_parent(struct commit *parent, struct commit_list **parents_p)
@@ -38,61 +25,6 @@ static void new_parent(struct commit *parent, struct commit_list **parents_p)
25
commit_list_insert(parent, parents_p);
26
}
27
41
-static const char commit_utf8_warn[] =
42
-"Warning: commit message does not conform to UTF-8.\n"
43
-"You may want to amend it after fixing the message, or set the config\n"
44
-"variable i18n.commitencoding to the encoding your project uses.\n";
45
-
46
-int commit_tree(const char *msg, unsigned char *tree,
47
- struct commit_list *parents, unsigned char *ret,
48
- const char *author)
49
-{
50
- int result;
51
- int encoding_is_utf8;
52
- struct strbuf buffer;
53
-
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 */
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
64
- * different order of parents will be a _different_ changeset even
65
- * if everything else stays the same.
66
- */
67
- while (parents) {
68
- struct commit_list *next = parents->next;
69
- strbuf_addf(&buffer, "parent %s\n",
70
- sha1_to_hex(parents->item->object.sha1));
71
- free(parents);
72
- parents = next;
73
- }
74
-
75
- /* Person/date information */
76
- if (!author)
77
- author = git_author_info(IDENT_ERROR_ON_NO_NAME);
78
- strbuf_addf(&buffer, "author %s\n", author);
79
- strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
80
- if (!encoding_is_utf8)
81
- strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
82
- strbuf_addch(&buffer, '\n');
83
-
84
- /* And add the comment */
85
- strbuf_addstr(&buffer, msg);
86
-
87
- /* And check the encoding */
88
- if (encoding_is_utf8 && !is_utf8(buffer.buf))
89
- fprintf(stderr, commit_utf8_warn);
90
-
91
- result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
92
- strbuf_release(&buffer);
93
- return result;
94
-}
95
-
28
int cmd_commit_tree(int argc, const char **argv, const char *prefix)
29
{
30
int i;
@@ -117,7 +49,7 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix)
49
50
if (get_sha1(b, sha1))
51
die("Not a valid object name %s", b);
120
- check_valid(sha1, OBJ_COMMIT);
52
+ assert_sha1_type(sha1, OBJ_COMMIT);
53
new_parent(lookup_commit(sha1), &parents);
54
}
55
cache.h
+2
@@ -718,6 +718,8 @@ extern int has_loose_object_nonlocal(const unsigned char *sha1);
718
719
extern int has_pack_index(const unsigned char *sha1);
720
721
+extern void assert_sha1_type(const unsigned char *sha1, enum object_type expect);
722
+
723
extern const signed char hexval_table[256];
724
static inline unsigned int hexval(unsigned char c)
725
{
commit.c
+55
@@ -790,3 +790,58 @@ struct commit_list *reduce_heads(struct commit_list *heads)
790
free(other);
791
return result;
792
}
793
+
794
+static const char commit_utf8_warn[] =
795
+"Warning: commit message does not conform to UTF-8.\n"
796
+"You may want to amend it after fixing the message, or set the config\n"
797
+"variable i18n.commitencoding to the encoding your project uses.\n";
798
+
799
+int commit_tree(const char *msg, unsigned char *tree,
800
+ struct commit_list *parents, unsigned char *ret,
801
+ const char *author)
802
+{
803
+ int result;
804
+ int encoding_is_utf8;
805
+ struct strbuf buffer;
806
+
807
+ assert_sha1_type(tree, OBJ_TREE);
808
+
809
+ /* Not having i18n.commitencoding is the same as having utf-8 */
810
+ encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
811
+
812
+ strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
813
+ strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
814
+
815
+ /*
816
+ * NOTE! This ordering means that the same exact tree merged with a
817
+ * different order of parents will be a _different_ changeset even
818
+ * if everything else stays the same.
819
+ */
820
+ while (parents) {
821
+ struct commit_list *next = parents->next;
822
+ strbuf_addf(&buffer, "parent %s\n",
823
+ sha1_to_hex(parents->item->object.sha1));
824
+ free(parents);
825
+ parents = next;
826
+ }
827
+
828
+ /* Person/date information */
829
+ if (!author)
830
+ author = git_author_info(IDENT_ERROR_ON_NO_NAME);
831
+ strbuf_addf(&buffer, "author %s\n", author);
832
+ strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
833
+ if (!encoding_is_utf8)
834
+ strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
835
+ strbuf_addch(&buffer, '\n');
836
+
837
+ /* And add the comment */
838
+ strbuf_addstr(&buffer, msg);
839
+
840
+ /* And check the encoding */
841
+ if (encoding_is_utf8 && !is_utf8(buffer.buf))
842
+ fprintf(stderr, commit_utf8_warn);
843
+
844
+ result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
845
+ strbuf_release(&buffer);
846
+ return result;
847
+}
commit.h
+4
@@ -158,4 +158,8 @@ static inline int single_parent(struct commit *commit)
158
159
struct commit_list *reduce_heads(struct commit_list *heads);
160
161
+extern int commit_tree(const char *msg, unsigned char *tree,
162
+ struct commit_list *parents, unsigned char *ret,
163
+ const char *author);
164
+
165
#endif /* COMMIT_H */
sha1_file.c
+10
@@ -2516,3 +2516,13 @@ int read_pack_header(int fd, struct pack_header *header)
2516
return PH_ERROR_PROTOCOL;
2517
return 0;
2518
}
2519
+
2520
+void assert_sha1_type(const unsigned char *sha1, enum object_type expect)
2521
+{
2522
+ enum object_type type = sha1_object_info(sha1, NULL);
2523
+ if (type < 0)
2524
+ die("%s is not a valid object", sha1_to_hex(sha1));
2525
+ if (type != expect)
2526
+ die("%s is not a valid '%s' object", sha1_to_hex(sha1),
2527
+ typename(expect));
2528
+}