4 * The low 16 bits of the "flags" field shows whether
5 * a commit is part of the path to the root for that
8 * Bit 16 is an internal flag that we've seen the
9 * definition for this rev, and not just seen it as
12 #define MAX_COMMITS (16)
13 #define marked(rev) ((rev)->flags & 0xffff)
16 static int show_edges = 0;
17 static int basemask = 0;
20 struct revision *parent;
26 unsigned char sha1[20];
27 struct parent *parent;
30 static struct revision **revs;
31 static int nr_revs, rev_allocs;
33 static int find_rev(unsigned char *sha1)
35 int first = 0, last = nr_revs;
37 while (first < last) {
38 int next = (first + last) / 2;
39 struct revision *rev = revs[next];
42 cmp = memcmp(sha1, rev->sha1, 20);
54 static struct revision *lookup_rev(unsigned char *sha1)
56 int pos = find_rev(sha1);
64 if (rev_allocs == nr_revs) {
65 rev_allocs = alloc_nr(rev_allocs);
66 revs = realloc(revs, rev_allocs * sizeof(struct revision *));
68 n = malloc(sizeof(struct revision));
71 memcpy(n->sha1, sha1, 20);
74 /* Insert it into the right place */
75 memmove(revs + pos + 1, revs + pos, (nr_revs - pos) * sizeof(struct revision *));
82 static int add_relationship(struct revision *rev, unsigned char *parent_sha)
84 struct revision *parent_rev = lookup_rev(parent_sha);
85 struct parent **pp = &rev->parent, *p;
87 while ((p = *pp) != NULL) {
88 if (p->parent == parent_rev)
93 p = malloc(sizeof(*p));
94 p->parent = parent_rev;
100 static int parse_commit(unsigned char *sha1)
102 struct revision *rev = lookup_rev(sha1);
104 if (!(rev->flags & SEEN)) {
108 unsigned char parent[20];
111 buffer = read_sha1_file(sha1, type, &size);
112 if (!buffer || strcmp(type, "commit"))
114 buffer += 46; /* "tree " + "hex sha1" + "\n" */
115 while (!memcmp(buffer, "parent ", 7) && !get_sha1_hex(buffer+7, parent)) {
116 add_relationship(rev, parent);
117 parse_commit(parent);
118 buffer += 48; /* "parent " + "hex sha1" + "\n" */
124 static void read_cache_file(const char *path)
126 FILE *file = fopen(path, "r");
130 usage("bad revtree cache file (%s)", path);
132 while (fgets(line, sizeof(line), file)) {
133 unsigned char sha1[20], parent[20];
134 struct revision *rev;
136 if (get_sha1_hex(line, sha1) || get_sha1_hex(line + 41, parent))
137 usage("bad rev-tree cache file %s", path);
138 rev = lookup_rev(sha1);
140 add_relationship(rev, parent);
145 static void mark_sha1_path(struct revision *rev, unsigned int mask)
149 if (rev->flags & mask)
155 mark_sha1_path(p->parent, mask);
161 * Some revisions are less interesting than others.
163 * For example, if we use a cache-file, that one may contain
164 * revisions that were never used. They are never interesting.
166 * And sometimes we're only interested in "edge" commits, ie
167 * places where the marking changes between parent and child.
169 static int interesting(struct revision *rev)
171 unsigned mask = marked(rev);
176 struct parent *p = rev->parent;
178 if (mask != marked(p->parent))
191 * Usage: rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id2>]
193 * The cache-file can be quite important for big trees. This is an
194 * expensive operation if you have to walk the whole chain of
195 * parents in a tree with a long revision history.
197 int main(int argc, char **argv)
201 unsigned char sha1[MAX_COMMITS][20];
204 * First - pick up all the revisions we can (both from
205 * caches and from commit file chains).
207 for (i = 1; i < argc ; i++) {
210 if (!strcmp(arg, "--cache")) {
211 read_cache_file(argv[2]);
216 if (!strcmp(arg, "--edges")) {
225 if (nr >= MAX_COMMITS || get_sha1_hex(arg, sha1[nr]))
226 usage("rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id>]");
227 parse_commit(sha1[nr]);
232 * Now we have the maximal tree. Walk the different sha files back to the root.
234 for (i = 0; i < nr; i++)
235 mark_sha1_path(lookup_rev(sha1[i]), 1 << i);
238 * Now print out the results..
240 for (i = 0; i < nr_revs; i++) {
241 struct revision *rev = revs[i];
244 if (!interesting(rev))
247 printf("%s:%d", sha1_to_hex(rev->sha1), marked(rev));
250 printf(" %s:%d", sha1_to_hex(p->parent->sha1), marked(p->parent));