1 #define _XOPEN_SOURCE /* glibc2 needs this */
2 #define _BSD_SOURCE /* for tm.tm_gmtoff */
10 * revision.h leaves the low 16 bits of the "flags" field of the
11 * revision data structure unused. We use it for a "reachable from
12 * this commit <N>" bitmask.
14 #define MAX_COMMITS 16
16 static int show_edges = 0;
17 static int basemask = 0;
19 static unsigned long parse_time(const char *buf)
24 const char *formats[] = {
30 const char **fmt = formats;
33 while (isspace(c = *buf))
35 while ((c = *buf++) != '\n' && c)
39 memset(&tm, 0, sizeof(tm));
41 const char *next = strptime(buf, *fmt, &tm);
48 } while (*buf && *fmt);
53 static unsigned long parse_commit_date(const char *buf)
57 if (memcmp(buf, "author", 6))
59 while (*buf++ != '\n')
61 if (memcmp(buf, "committer", 9))
66 time = strtoul(buf, NULL, 10);
68 time = parse_time(buf);
72 static int parse_commit(unsigned char *sha1)
74 struct revision *rev = lookup_rev(sha1);
76 if (!(rev->flags & SEEN)) {
77 void *buffer, *bufptr;
80 unsigned char parent[20];
83 buffer = bufptr = read_sha1_file(sha1, type, &size);
84 if (!buffer || strcmp(type, "commit"))
86 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
87 while (!memcmp(bufptr, "parent ", 7) && !get_sha1_hex(bufptr+7, parent)) {
88 add_relationship(rev, parent);
90 bufptr += 48; /* "parent " + "hex sha1" + "\n" */
92 rev->date = parse_commit_date(bufptr);
98 static void read_cache_file(const char *path)
100 FILE *file = fopen(path, "r");
104 die("bad revtree cache file (%s)", path);
106 while (fgets(line, sizeof(line), file)) {
108 unsigned char sha1[20];
109 struct revision *rev;
112 if (sscanf(line, "%lu", &date) != 1)
114 buf = strchr(line, ' ');
117 if (get_sha1_hex(buf+1, sha1))
119 rev = lookup_rev(sha1);
124 while ((buf = strchr(buf+1, ' ')) != NULL) {
125 unsigned char parent[20];
126 if (get_sha1_hex(buf + 1, parent))
128 add_relationship(rev, parent);
134 static void mark_sha1_path(struct revision *rev, unsigned int mask)
138 if (rev->flags & mask)
144 mark_sha1_path(p->parent, mask);
150 * Some revisions are less interesting than others.
152 * For example, if we use a cache-file, that one may contain
153 * revisions that were never used. They are never interesting.
155 * And sometimes we're only interested in "edge" commits, ie
156 * places where the marking changes between parent and child.
158 static int interesting(struct revision *rev)
160 unsigned mask = marked(rev);
165 struct parent *p = rev->parent;
167 if (mask != marked(p->parent))
180 * Usage: rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id2>]
182 * The cache-file can be quite important for big trees. This is an
183 * expensive operation if you have to walk the whole chain of
184 * parents in a tree with a long revision history.
186 int main(int argc, char **argv)
190 unsigned char sha1[MAX_COMMITS][20];
193 * First - pick up all the revisions we can (both from
194 * caches and from commit file chains).
196 for (i = 1; i < argc ; i++) {
199 if (!strcmp(arg, "--cache")) {
200 read_cache_file(argv[2]);
205 if (!strcmp(arg, "--edges")) {
214 if (nr >= MAX_COMMITS || get_sha1_hex(arg, sha1[nr]))
215 usage("rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id>]");
216 parse_commit(sha1[nr]);
221 * Now we have the maximal tree. Walk the different sha files back to the root.
223 for (i = 0; i < nr; i++)
224 mark_sha1_path(lookup_rev(sha1[i]), 1 << i);
227 * Now print out the results..
229 for (i = 0; i < nr_revs; i++) {
230 struct revision *rev = revs[i];
233 if (!interesting(rev))
236 printf("%lu %s:%d", rev->date, sha1_to_hex(rev->sha1), marked(rev));
239 printf(" %s:%d", sha1_to_hex(p->parent->sha1), marked(p->parent));