]> git.ipfire.org Git - thirdparty/git.git/blob - commit-tree.c
Initial revision of "git", the information manager from hell
[thirdparty/git.git] / commit-tree.c
1 #include "cache.h"
2
3 #include <pwd.h>
4 #include <time.h>
5
6 #define BLOCKING (1ul << 14)
7 #define ORIG_OFFSET (40)
8
9 /*
10 * Leave space at the beginning to insert the tag
11 * once we know how big things are.
12 *
13 * FIXME! Share the code with "write-tree.c"
14 */
15 static void init_buffer(char **bufp, unsigned int *sizep)
16 {
17 char *buf = malloc(BLOCKING);
18 memset(buf, 0, ORIG_OFFSET);
19 *sizep = ORIG_OFFSET;
20 *bufp = buf;
21 }
22
23 static void add_buffer(char **bufp, unsigned int *sizep, const char *fmt, ...)
24 {
25 char one_line[2048];
26 va_list args;
27 int len;
28 unsigned long alloc, size, newsize;
29 char *buf;
30
31 va_start(args, fmt);
32 len = vsnprintf(one_line, sizeof(one_line), fmt, args);
33 va_end(args);
34 size = *sizep;
35 newsize = size + len;
36 alloc = (size + 32767) & ~32767;
37 buf = *bufp;
38 if (newsize > alloc) {
39 alloc = (newsize + 32767) & ~32767;
40 buf = realloc(buf, alloc);
41 *bufp = buf;
42 }
43 *sizep = newsize;
44 memcpy(buf + size, one_line, len);
45 }
46
47 static int prepend_integer(char *buffer, unsigned val, int i)
48 {
49 buffer[--i] = '\0';
50 do {
51 buffer[--i] = '0' + (val % 10);
52 val /= 10;
53 } while (val);
54 return i;
55 }
56
57 static void finish_buffer(char *tag, char **bufp, unsigned int *sizep)
58 {
59 int taglen;
60 int offset;
61 char *buf = *bufp;
62 unsigned int size = *sizep;
63
64 offset = prepend_integer(buf, size - ORIG_OFFSET, ORIG_OFFSET);
65 taglen = strlen(tag);
66 offset -= taglen;
67 buf += offset;
68 size -= offset;
69 memcpy(buf, tag, taglen);
70
71 *bufp = buf;
72 *sizep = size;
73 }
74
75 static void remove_special(char *p)
76 {
77 char c;
78 char *dst = p;
79
80 for (;;) {
81 c = *p;
82 p++;
83 switch(c) {
84 case '\n': case '<': case '>':
85 continue;
86 }
87 *dst++ = c;
88 if (!c)
89 break;
90 }
91 }
92
93 /*
94 * Having more than two parents may be strange, but hey, there's
95 * no conceptual reason why the file format couldn't accept multi-way
96 * merges. It might be the "union" of several packages, for example.
97 *
98 * I don't really expect that to happen, but this is here to make
99 * it clear that _conceptually_ it's ok..
100 */
101 #define MAXPARENT (16)
102
103 int main(int argc, char **argv)
104 {
105 int i, len;
106 int parents = 0;
107 unsigned char tree_sha1[20];
108 unsigned char parent_sha1[MAXPARENT][20];
109 char *gecos, *realgecos;
110 char *email, realemail[1000];
111 char *date, *realdate;
112 char comment[1000];
113 struct passwd *pw;
114 time_t now;
115 char *buffer;
116 unsigned int size;
117
118 if (argc < 2 || get_sha1_hex(argv[1], tree_sha1) < 0)
119 usage("commit-tree <sha1> [-p <sha1>]* < changelog");
120
121 for (i = 2; i < argc; i += 2) {
122 char *a, *b;
123 a = argv[i]; b = argv[i+1];
124 if (!b || strcmp(a, "-p") || get_sha1_hex(b, parent_sha1[parents]))
125 usage("commit-tree <sha1> [-p <sha1>]* < changelog");
126 parents++;
127 }
128 if (!parents)
129 fprintf(stderr, "Committing initial tree %s\n", argv[1]);
130 pw = getpwuid(getuid());
131 if (!pw)
132 usage("You don't exist. Go away!");
133 realgecos = pw->pw_gecos;
134 len = strlen(pw->pw_name);
135 memcpy(realemail, pw->pw_name, len);
136 realemail[len] = '@';
137 gethostname(realemail+len+1, sizeof(realemail)-len-1);
138 time(&now);
139 realdate = ctime(&now);
140
141 gecos = getenv("COMMITTER_NAME") ? : realgecos;
142 email = getenv("COMMITTER_EMAIL") ? : realemail;
143 date = getenv("COMMITTER_DATE") ? : realdate;
144
145 remove_special(gecos); remove_special(realgecos);
146 remove_special(email); remove_special(realemail);
147 remove_special(date); remove_special(realdate);
148
149 init_buffer(&buffer, &size);
150 add_buffer(&buffer, &size, "tree %s\n", sha1_to_hex(tree_sha1));
151
152 /*
153 * NOTE! This ordering means that the same exact tree merged with a
154 * different order of parents will be a _different_ changeset even
155 * if everything else stays the same.
156 */
157 for (i = 0; i < parents; i++)
158 add_buffer(&buffer, &size, "parent %s\n", sha1_to_hex(parent_sha1[i]));
159
160 /* Person/date information */
161 add_buffer(&buffer, &size, "author %s <%s> %s\n", gecos, email, date);
162 add_buffer(&buffer, &size, "committer %s <%s> %s\n\n", realgecos, realemail, realdate);
163
164 /* And add the comment */
165 while (fgets(comment, sizeof(comment), stdin) != NULL)
166 add_buffer(&buffer, &size, "%s", comment);
167
168 finish_buffer("commit ", &buffer, &size);
169
170 write_sha1_file(buffer, size);
171 return 0;
172 }