1 #define _XOPEN_SOURCE /* glibc2 needs this */
9 * revision.h leaves the low 16 bits of the "flags" field of the
10 * revision data structure unused. We use it for a "reachable from
11 * this commit <N>" bitmask.
13 #define MAX_COMMITS 16
15 static int show_edges = 0;
16 static int basemask = 0;
18 static unsigned long parse_time(const char *buf)
23 const char *formats[] = {
28 const char **fmt = formats;
31 while (isspace(c = *buf))
33 while ((c = *buf++) != '\n')
37 memset(&tm, 0, sizeof(tm));
39 const char *next = strptime(buf, *fmt, &tm);
46 } while (*buf && *fmt);
51 static unsigned long parse_commit_date(const char *buf)
53 if (memcmp(buf, "author", 6))
55 while (*buf++ != '\n')
57 if (memcmp(buf, "committer", 9))
61 return parse_time(buf);
64 static int parse_commit(unsigned char *sha1)
66 struct revision *rev = lookup_rev(sha1);
68 if (!(rev->flags & SEEN)) {
69 void *buffer, *bufptr;
72 unsigned char parent[20];
75 buffer = bufptr = read_sha1_file(sha1, type, &size);
76 if (!buffer || strcmp(type, "commit"))
78 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
79 while (!memcmp(bufptr, "parent ", 7) && !get_sha1_hex(bufptr+7, parent)) {
80 add_relationship(rev, parent);
82 bufptr += 48; /* "parent " + "hex sha1" + "\n" */
84 rev->date = parse_commit_date(bufptr);
90 static void read_cache_file(const char *path)
92 FILE *file = fopen(path, "r");
96 die("bad revtree cache file (%s)", path);
98 while (fgets(line, sizeof(line), file)) {
100 unsigned char sha1[20];
101 struct revision *rev;
104 if (sscanf(line, "%lu", &date) != 1)
106 buf = strchr(line, ' ');
109 if (get_sha1_hex(buf+1, sha1))
111 rev = lookup_rev(sha1);
116 while ((buf = strchr(buf+1, ' ')) != NULL) {
117 unsigned char parent[20];
118 if (get_sha1_hex(buf + 1, parent))
120 add_relationship(rev, parent);
126 static void mark_sha1_path(struct revision *rev, unsigned int mask)
130 if (rev->flags & mask)
136 mark_sha1_path(p->parent, mask);
142 * Some revisions are less interesting than others.
144 * For example, if we use a cache-file, that one may contain
145 * revisions that were never used. They are never interesting.
147 * And sometimes we're only interested in "edge" commits, ie
148 * places where the marking changes between parent and child.
150 static int interesting(struct revision *rev)
152 unsigned mask = marked(rev);
157 struct parent *p = rev->parent;
159 if (mask != marked(p->parent))
172 * Usage: rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id2>]
174 * The cache-file can be quite important for big trees. This is an
175 * expensive operation if you have to walk the whole chain of
176 * parents in a tree with a long revision history.
178 int main(int argc, char **argv)
182 unsigned char sha1[MAX_COMMITS][20];
185 * First - pick up all the revisions we can (both from
186 * caches and from commit file chains).
188 for (i = 1; i < argc ; i++) {
191 if (!strcmp(arg, "--cache")) {
192 read_cache_file(argv[2]);
197 if (!strcmp(arg, "--edges")) {
206 if (nr >= MAX_COMMITS || get_sha1_hex(arg, sha1[nr]))
207 usage("rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id>]");
208 parse_commit(sha1[nr]);
213 * Now we have the maximal tree. Walk the different sha files back to the root.
215 for (i = 0; i < nr; i++)
216 mark_sha1_path(lookup_rev(sha1[i]), 1 << i);
219 * Now print out the results..
221 for (i = 0; i < nr_revs; i++) {
222 struct revision *rev = revs[i];
225 if (!interesting(rev))
228 printf("%lu %s:%d", rev->date, sha1_to_hex(rev->sha1), marked(rev));
231 printf(" %s:%d", sha1_to_hex(p->parent->sha1), marked(p->parent));