Make "rev-tree" capable of showing the difference in reachability between two
[git/dscho.git] / rev-tree.c
blobc10f4ee295f5e082e112152e7cd815d10878f689
1 #include "cache.h"
3 /*
4 * The low 16 bits of the "flags" field shows whether
5 * a commit is part of the path to the root for that
6 * parent.
8 * Bit 16 is an internal flag that we've seen the
9 * definition for this rev, and not just seen it as
10 * a parent target.
12 #define MAX_COMMITS (16)
13 #define marked(rev) ((rev)->flags & 0xffff)
14 #define SEEN 0x10000
16 static int show_edges = 0;
18 struct parent {
19 struct revision *parent;
20 struct parent *next;
23 struct revision {
24 unsigned int flags;
25 unsigned char sha1[20];
26 struct parent *parent;
29 static struct revision **revs;
30 static int nr_revs, rev_allocs;
32 static int find_rev(unsigned char *sha1)
34 int first = 0, last = nr_revs;
36 while (first < last) {
37 int next = (first + last) / 2;
38 struct revision *rev = revs[next];
39 int cmp;
41 cmp = memcmp(sha1, rev->sha1, 20);
42 if (!cmp)
43 return next;
44 if (cmp < 0) {
45 last = next;
46 continue;
48 first = next+1;
50 return -first-1;
53 static struct revision *lookup_rev(unsigned char *sha1)
55 int pos = find_rev(sha1);
56 struct revision *n;
58 if (pos >= 0)
59 return revs[pos];
61 pos = -pos-1;
63 if (rev_allocs == nr_revs) {
64 rev_allocs = alloc_nr(rev_allocs);
65 revs = realloc(revs, rev_allocs * sizeof(struct revision *));
67 n = malloc(sizeof(struct revision));
69 n->flags = 0;
70 memcpy(n->sha1, sha1, 20);
71 n->parent = NULL;
73 /* Insert it into the right place */
74 memmove(revs + pos + 1, revs + pos, (nr_revs - pos) * sizeof(struct revision *));
75 revs[pos] = n;
76 nr_revs++;
78 return n;
81 static int add_relationship(struct revision *rev, unsigned char *parent_sha)
83 struct revision *parent_rev = lookup_rev(parent_sha);
84 struct parent **pp = &rev->parent, *p;
86 while ((p = *pp) != NULL) {
87 if (p->parent == parent_rev)
88 return 0;
89 pp = &p->next;
92 p = malloc(sizeof(*p));
93 p->parent = parent_rev;
94 p->next = NULL;
95 *pp = p;
96 return 1;
99 static int parse_commit(unsigned char *sha1)
101 struct revision *rev = lookup_rev(sha1);
103 if (!(rev->flags & SEEN)) {
104 void *buffer;
105 unsigned long size;
106 char type[20];
107 unsigned char parent[20];
109 rev->flags |= SEEN;
110 buffer = read_sha1_file(sha1, type, &size);
111 if (!buffer || strcmp(type, "commit"))
112 return -1;
113 buffer += 46; /* "tree " + "hex sha1" + "\n" */
114 while (!memcmp(buffer, "parent ", 7) && !get_sha1_hex(buffer+7, parent)) {
115 add_relationship(rev, parent);
116 parse_commit(parent);
117 buffer += 48; /* "parent " + "hex sha1" + "\n" */
120 return 0;
123 static void read_cache_file(const char *path)
125 FILE *file = fopen(path, "r");
126 char line[100];
128 if (!file)
129 usage("bad revtree cache file (%s)", path);
131 while (fgets(line, sizeof(line), file)) {
132 unsigned char sha1[20], parent[20];
133 struct revision *rev;
135 if (get_sha1_hex(line, sha1) || get_sha1_hex(line + 41, parent))
136 usage("bad rev-tree cache file %s", path);
137 rev = lookup_rev(sha1);
138 rev->flags |= SEEN;
139 add_relationship(rev, parent);
141 fclose(file);
144 static void mark_sha1_path(struct revision *rev, unsigned int mask)
146 struct parent *p;
148 if (rev->flags & mask)
149 return;
151 rev->flags |= mask;
152 p = rev->parent;
153 while (p) {
154 mark_sha1_path(p->parent, mask);
155 p = p->next;
160 * Some revisions are less interesting than others.
162 * For example, if we use a cache-file, that one may contain
163 * revisions that were never used. They are never interesting.
165 * And sometimes we're only interested in "edge" commits, ie
166 * places where the marking changes between parent and child.
168 static int interesting(struct revision *rev)
170 unsigned mask = marked(rev);
172 if (!mask)
173 return 0;
174 if (show_edges) {
175 struct parent *p = rev->parent;
176 while (p) {
177 if (mask != marked(p->parent))
178 return 1;
179 p = p->next;
181 return 0;
183 return 1;
187 * Usage: rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id2>]
189 * The cache-file can be quite important for big trees. This is an
190 * expensive operation if you have to walk the whole chain of
191 * parents in a tree with a long revision history.
193 int main(int argc, char **argv)
195 int i;
196 int nr = 0;
197 unsigned char sha1[MAX_COMMITS][20];
200 * First - pick up all the revisions we can (both from
201 * caches and from commit file chains).
203 for (i = 1; i < argc ; i++) {
204 char *arg = argv[i];
206 if (!strcmp(arg, "--cache")) {
207 read_cache_file(argv[2]);
208 i++;
209 continue;
212 if (!strcmp(arg, "--edges")) {
213 show_edges = 1;
214 continue;
217 if (nr >= MAX_COMMITS || get_sha1_hex(arg, sha1[nr]))
218 usage("rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id>]");
219 parse_commit(sha1[nr]);
220 nr++;
224 * Now we have the maximal tree. Walk the different sha files back to the root.
226 for (i = 0; i < nr; i++)
227 mark_sha1_path(lookup_rev(sha1[i]), 1 << i);
230 * Now print out the results..
232 for (i = 0; i < nr_revs; i++) {
233 struct revision *rev = revs[i];
234 struct parent *p;
236 if (!interesting(rev))
237 continue;
239 printf("%x %s", marked(rev), sha1_to_hex(rev->sha1));
240 p = rev->parent;
241 while (p) {
242 printf(" %s", sha1_to_hex(p->parent->sha1));
243 p = p->next;
245 printf("\n");
247 return 0;