[PATCH] Fix a crash when doing rev-tree
[git/gitweb.git] / rev-tree.c
blobf2414b20fea128ff0fe13497b65587badcadf2b1
1 #define _XOPEN_SOURCE /* glibc2 needs this */
2 #include <time.h>
3 #include <ctype.h>
4 #include "cache.h"
6 /*
7 * The low 16 bits of the "flags" field shows whether
8 * a commit is part of the path to the root for that
9 * parent.
11 * Bit 16 is an internal flag that we've seen the
12 * definition for this rev, and not just seen it as
13 * a parent target.
15 #define MAX_COMMITS (16)
16 #define marked(rev) ((rev)->flags & 0xffff)
17 #define SEEN 0x10000
19 static int show_edges = 0;
20 static int basemask = 0;
22 struct parent {
23 struct revision *parent;
24 struct parent *next;
27 struct revision {
28 unsigned int flags;
29 unsigned char sha1[20];
30 unsigned long date;
31 struct parent *parent;
34 static struct revision **revs;
35 static int nr_revs, rev_allocs;
37 static int find_rev(unsigned char *sha1)
39 int first = 0, last = nr_revs;
41 while (first < last) {
42 int next = (first + last) / 2;
43 struct revision *rev = revs[next];
44 int cmp;
46 cmp = memcmp(sha1, rev->sha1, 20);
47 if (!cmp)
48 return next;
49 if (cmp < 0) {
50 last = next;
51 continue;
53 first = next+1;
55 return -first-1;
58 static struct revision *lookup_rev(unsigned char *sha1)
60 int pos = find_rev(sha1);
61 struct revision *n;
63 if (pos >= 0)
64 return revs[pos];
66 pos = -pos-1;
68 if (rev_allocs == nr_revs) {
69 rev_allocs = alloc_nr(rev_allocs);
70 revs = realloc(revs, rev_allocs * sizeof(struct revision *));
72 n = malloc(sizeof(struct revision));
74 n->flags = 0;
75 memcpy(n->sha1, sha1, 20);
76 n->parent = NULL;
78 /* Insert it into the right place */
79 memmove(revs + pos + 1, revs + pos, (nr_revs - pos) * sizeof(struct revision *));
80 revs[pos] = n;
81 nr_revs++;
83 return n;
86 static int add_relationship(struct revision *rev, unsigned char *parent_sha)
88 struct revision *parent_rev = lookup_rev(parent_sha);
89 struct parent **pp = &rev->parent, *p;
91 while ((p = *pp) != NULL) {
92 if (p->parent == parent_rev)
93 return 0;
94 pp = &p->next;
97 p = malloc(sizeof(*p));
98 p->parent = parent_rev;
99 p->next = NULL;
100 *pp = p;
101 return 1;
104 static unsigned long parse_time(const char *buf)
106 char c, *p;
107 char buffer[100];
108 struct tm tm;
109 const char *formats[] = {
110 "%c",
111 "%a %b %d %T %y",
112 NULL
114 const char **fmt = formats;
116 p = buffer;
117 while (isspace(c = *buf))
118 buf++;
119 while ((c = *buf++) != '\n')
120 *p++ = c;
121 *p++ = 0;
122 buf = buffer;
123 memset(&tm, 0, sizeof(tm));
124 do {
125 const char *next = strptime(buf, *fmt, &tm);
126 fmt++;
127 if (next) {
128 if (!*next)
129 return mktime(&tm);
130 buf = next;
132 } while (*buf && *fmt);
133 return mktime(&tm);
137 static unsigned long parse_commit_date(const char *buf)
139 if (memcmp(buf, "author", 6))
140 return 0;
141 while (*buf++ != '\n')
142 /* nada */;
143 if (memcmp(buf, "committer", 9))
144 return 0;
145 while (*buf++ != '>')
146 /* nada */;
147 return parse_time(buf);
150 static int parse_commit(unsigned char *sha1)
152 struct revision *rev = lookup_rev(sha1);
154 if (!(rev->flags & SEEN)) {
155 void *buffer, *bufptr;
156 unsigned long size;
157 char type[20];
158 unsigned char parent[20];
160 rev->flags |= SEEN;
161 buffer = bufptr = read_sha1_file(sha1, type, &size);
162 if (!buffer || strcmp(type, "commit"))
163 return -1;
164 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
165 while (!memcmp(bufptr, "parent ", 7) && !get_sha1_hex(bufptr+7, parent)) {
166 add_relationship(rev, parent);
167 parse_commit(parent);
168 bufptr += 48; /* "parent " + "hex sha1" + "\n" */
170 rev->date = parse_commit_date(bufptr);
171 free(buffer);
173 return 0;
176 static void read_cache_file(const char *path)
178 FILE *file = fopen(path, "r");
179 char line[500];
181 if (!file)
182 usage("bad revtree cache file (%s)", path);
184 while (fgets(line, sizeof(line), file)) {
185 unsigned long date;
186 unsigned char sha1[20];
187 struct revision *rev;
188 const char *buf;
190 if (sscanf(line, "%lu", &date) != 1)
191 break;
192 buf = strchr(line, ' ');
193 if (!buf)
194 break;
195 if (get_sha1_hex(buf+1, sha1))
196 break;
197 rev = lookup_rev(sha1);
198 rev->flags |= SEEN;
199 rev->date = date;
201 /* parents? */
202 while ((buf = strchr(buf+1, ' ')) != NULL) {
203 unsigned char parent[20];
204 if (get_sha1_hex(buf + 1, parent))
205 break;
206 add_relationship(rev, parent);
209 fclose(file);
212 static void mark_sha1_path(struct revision *rev, unsigned int mask)
214 struct parent *p;
216 if (rev->flags & mask)
217 return;
219 rev->flags |= mask;
220 p = rev->parent;
221 while (p) {
222 mark_sha1_path(p->parent, mask);
223 p = p->next;
228 * Some revisions are less interesting than others.
230 * For example, if we use a cache-file, that one may contain
231 * revisions that were never used. They are never interesting.
233 * And sometimes we're only interested in "edge" commits, ie
234 * places where the marking changes between parent and child.
236 static int interesting(struct revision *rev)
238 unsigned mask = marked(rev);
240 if (!mask)
241 return 0;
242 if (show_edges) {
243 struct parent *p = rev->parent;
244 while (p) {
245 if (mask != marked(p->parent))
246 return 1;
247 p = p->next;
249 return 0;
251 if (mask & basemask)
252 return 0;
254 return 1;
258 * Usage: rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id2>]
260 * The cache-file can be quite important for big trees. This is an
261 * expensive operation if you have to walk the whole chain of
262 * parents in a tree with a long revision history.
264 int main(int argc, char **argv)
266 int i;
267 int nr = 0;
268 unsigned char sha1[MAX_COMMITS][20];
271 * First - pick up all the revisions we can (both from
272 * caches and from commit file chains).
274 for (i = 1; i < argc ; i++) {
275 char *arg = argv[i];
277 if (!strcmp(arg, "--cache")) {
278 read_cache_file(argv[2]);
279 i++;
280 continue;
283 if (!strcmp(arg, "--edges")) {
284 show_edges = 1;
285 continue;
288 if (arg[0] == '^') {
289 arg++;
290 basemask |= 1<<nr;
292 if (nr >= MAX_COMMITS || get_sha1_hex(arg, sha1[nr]))
293 usage("rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id>]");
294 parse_commit(sha1[nr]);
295 nr++;
299 * Now we have the maximal tree. Walk the different sha files back to the root.
301 for (i = 0; i < nr; i++)
302 mark_sha1_path(lookup_rev(sha1[i]), 1 << i);
305 * Now print out the results..
307 for (i = 0; i < nr_revs; i++) {
308 struct revision *rev = revs[i];
309 struct parent *p;
311 if (!interesting(rev))
312 continue;
314 printf("%lu %s:%d", rev->date, sha1_to_hex(rev->sha1), marked(rev));
315 p = rev->parent;
316 while (p) {
317 printf(" %s:%d", sha1_to_hex(p->parent->sha1), marked(p->parent));
318 p = p->next;
320 printf("\n");
322 return 0;