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