1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6#include "cache.h"
7
8#include <pwd.h>
9#include <time.h>
10#include <ctype.h>
11
12#define BLOCKING (1ul << 14)
13
14/*
15 * FIXME! Share the code with "write-tree.c"
16 */
17static void init_buffer(char **bufp, unsigned int *sizep)
18{
19 char *buf = xmalloc(BLOCKING);
20 *sizep = 0;
21 *bufp = buf;
22}
23
24static void add_buffer(char **bufp, unsigned int *sizep, const char *fmt, ...)
25{
26 char one_line[2048];
27 va_list args;
28 int len;
29 unsigned long alloc, size, newsize;
30 char *buf;
31
32 va_start(args, fmt);
33 len = vsnprintf(one_line, sizeof(one_line), fmt, args);
34 va_end(args);
35 size = *sizep;
36 newsize = size + len;
37 alloc = (size + 32767) & ~32767;
38 buf = *bufp;
39 if (newsize > alloc) {
40 alloc = (newsize + 32767) & ~32767;
41 buf = xrealloc(buf, alloc);
42 *bufp = buf;
43 }
44 *sizep = newsize;
45 memcpy(buf + size, one_line, len);
46}
47
48static void check_valid(unsigned char *sha1, const char *expect)
49{
50 void *buf;
51 char type[20];
52 unsigned long size;
53
54 buf = read_sha1_file(sha1, type, &size);
55 if (!buf || strcmp(type, expect))
56 die("%s is not a valid '%s' object", sha1_to_hex(sha1), expect);
57 free(buf);
58}
59
60/*
61 * Having more than two parents is not strange at all, and this is
62 * how multi-way merges are represented.
63 */
64#define MAXPARENT (16)
65static unsigned char parent_sha1[MAXPARENT][20];
66
67static char *commit_tree_usage = "git-commit-tree <sha1> [-p <sha1>]* < changelog";
68
69static int new_parent(int idx)
70{
71 int i;
72 unsigned char *sha1 = parent_sha1[idx];
73 for (i = 0; i < idx; i++) {
74 if (!memcmp(parent_sha1[i], sha1, 20)) {
75 error("duplicate parent %s ignored", sha1_to_hex(sha1));
76 return 0;
77 }
78 }
79 return 1;
80}
81
82static char *git_author_info(void)
83{
84 return get_ident(gitenv("GIT_AUTHOR_NAME"), gitenv("GIT_AUTHOR_EMAIL"), gitenv("GIT_AUTHOR_DATE"));
85}
86
87static char *git_committer_info(void)
88{
89 return get_ident(gitenv("GIT_COMMITTER_NAME"), gitenv("GIT_COMMITTER_EMAIL"), gitenv("GIT_COMMITTER_DATE"));
90}
91
92int main(int argc, char **argv)
93{
94 int i;
95 int parents = 0;
96 unsigned char tree_sha1[20];
97 unsigned char commit_sha1[20];
98 char comment[1000];
99 char *buffer;
100 unsigned int size;
101
102 if (argc < 2 || get_sha1_hex(argv[1], tree_sha1) < 0)
103 usage(commit_tree_usage);
104
105 check_valid(tree_sha1, "tree");
106 for (i = 2; i < argc; i += 2) {
107 char *a, *b;
108 a = argv[i]; b = argv[i+1];
109 if (!b || strcmp(a, "-p") || get_sha1(b, parent_sha1[parents]))
110 usage(commit_tree_usage);
111 check_valid(parent_sha1[parents], "commit");
112 if (new_parent(parents))
113 parents++;
114 }
115 if (!parents)
116 fprintf(stderr, "Committing initial tree %s\n", argv[1]);
117 setup_ident();
118
119 init_buffer(&buffer, &size);
120 add_buffer(&buffer, &size, "tree %s\n", sha1_to_hex(tree_sha1));
121
122 /*
123 * NOTE! This ordering means that the same exact tree merged with a
124 * different order of parents will be a _different_ changeset even
125 * if everything else stays the same.
126 */
127 for (i = 0; i < parents; i++)
128 add_buffer(&buffer, &size, "parent %s\n", sha1_to_hex(parent_sha1[i]));
129
130 /* Person/date information */
131 add_buffer(&buffer, &size, "author %s\n", git_author_info());
132 add_buffer(&buffer, &size, "committer %s\n\n", git_committer_info());
133
134 /* And add the comment */
135 while (fgets(comment, sizeof(comment), stdin) != NULL)
136 add_buffer(&buffer, &size, "%s", comment);
137
138 write_sha1_file(buffer, size, "commit", commit_sha1);
139 printf("%s\n", sha1_to_hex(commit_sha1));
140 return 0;
141}