1 #define _XOPEN_SOURCE /* glibc2 needs this */
7 * The low 16 bits of the "flags" field shows whether
8 * a commit is part of the path to the root for that
11 * Bit 16 is an internal flag that we've seen the
12 * definition for this rev, and not just seen it as
15 #define MAX_COMMITS (16)
16 #define marked(rev) ((rev)->flags & 0xffff)
19 static int show_edges = 0;
20 static int basemask = 0;
23 struct revision *parent;
29 unsigned char sha1[20];
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];
46 cmp = memcmp(sha1, rev->sha1, 20);
58 static struct revision *lookup_rev(unsigned char *sha1)
60 int pos = find_rev(sha1);
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));
75 memcpy(n->sha1, sha1, 20);
78 /* Insert it into the right place */
79 memmove(revs + pos + 1, revs + pos, (nr_revs - pos) * sizeof(struct revision *));
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)
97 p = malloc(sizeof(*p));
98 p->parent = parent_rev;
104 static unsigned long parse_time(const char *buf)
109 const char *formats[] = {
114 const char **fmt = formats;
117 while (isspace(c = *buf))
119 while ((c = *buf++) != '\n')
123 memset(&tm, 0, sizeof(tm));
125 const char *next = strptime(buf, *fmt, &tm);
132 } while (*buf && *fmt);
137 static unsigned long parse_commit_date(const char *buf)
139 if (memcmp(buf, "author", 6))
141 while (*buf++ != '\n')
143 if (memcmp(buf, "committer", 9))
145 while (*buf++ != '>')
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;
158 unsigned char parent[20];
161 buffer = bufptr = read_sha1_file(sha1, type, &size);
162 if (!buffer || strcmp(type, "commit"))
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);
176 static void read_cache_file(const char *path)
178 FILE *file = fopen(path, "r");
182 die("bad revtree cache file (%s)", path);
184 while (fgets(line, sizeof(line), file)) {
186 unsigned char sha1[20];
187 struct revision *rev;
190 if (sscanf(line, "%lu", &date) != 1)
192 buf = strchr(line, ' ');
195 if (get_sha1_hex(buf+1, sha1))
197 rev = lookup_rev(sha1);
202 while ((buf = strchr(buf+1, ' ')) != NULL) {
203 unsigned char parent[20];
204 if (get_sha1_hex(buf + 1, parent))
206 add_relationship(rev, parent);
212 static void mark_sha1_path(struct revision *rev, unsigned int mask)
216 if (rev->flags & mask)
222 mark_sha1_path(p->parent, mask);
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);
243 struct parent *p = rev->parent;
245 if (mask != marked(p->parent))
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)
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++) {
277 if (!strcmp(arg, "--cache")) {
278 read_cache_file(argv[2]);
283 if (!strcmp(arg, "--edges")) {
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]);
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];
311 if (!interesting(rev))
314 printf("%lu %s:%d", rev->date, sha1_to_hex(rev->sha1), marked(rev));
317 printf(" %s:%d", sha1_to_hex(p->parent->sha1), marked(p->parent));