Use common "revision.h" header for both fsck and rev-tree.
[git/gitweb-caching.git] / rev-tree.c
blobd3fc93c61a6c0629c32413bb7b64f06facda803d
1 #define _XOPEN_SOURCE /* glibc2 needs this */
2 #include <time.h>
3 #include <ctype.h>
5 #include "cache.h"
6 #include "revision.h"
8 /*
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)
20 char c, *p;
21 char buffer[100];
22 struct tm tm;
23 const char *formats[] = {
24 "%c",
25 "%a %b %d %T %y",
26 NULL
28 const char **fmt = formats;
30 p = buffer;
31 while (isspace(c = *buf))
32 buf++;
33 while ((c = *buf++) != '\n')
34 *p++ = c;
35 *p++ = 0;
36 buf = buffer;
37 memset(&tm, 0, sizeof(tm));
38 do {
39 const char *next = strptime(buf, *fmt, &tm);
40 fmt++;
41 if (next) {
42 if (!*next)
43 return mktime(&tm);
44 buf = next;
46 } while (*buf && *fmt);
47 return mktime(&tm);
51 static unsigned long parse_commit_date(const char *buf)
53 if (memcmp(buf, "author", 6))
54 return 0;
55 while (*buf++ != '\n')
56 /* nada */;
57 if (memcmp(buf, "committer", 9))
58 return 0;
59 while (*buf++ != '>')
60 /* nada */;
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;
70 unsigned long size;
71 char type[20];
72 unsigned char parent[20];
74 rev->flags |= SEEN;
75 buffer = bufptr = read_sha1_file(sha1, type, &size);
76 if (!buffer || strcmp(type, "commit"))
77 return -1;
78 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
79 while (!memcmp(bufptr, "parent ", 7) && !get_sha1_hex(bufptr+7, parent)) {
80 add_relationship(rev, parent);
81 parse_commit(parent);
82 bufptr += 48; /* "parent " + "hex sha1" + "\n" */
84 rev->date = parse_commit_date(bufptr);
85 free(buffer);
87 return 0;
90 static void read_cache_file(const char *path)
92 FILE *file = fopen(path, "r");
93 char line[500];
95 if (!file)
96 die("bad revtree cache file (%s)", path);
98 while (fgets(line, sizeof(line), file)) {
99 unsigned long date;
100 unsigned char sha1[20];
101 struct revision *rev;
102 const char *buf;
104 if (sscanf(line, "%lu", &date) != 1)
105 break;
106 buf = strchr(line, ' ');
107 if (!buf)
108 break;
109 if (get_sha1_hex(buf+1, sha1))
110 break;
111 rev = lookup_rev(sha1);
112 rev->flags |= SEEN;
113 rev->date = date;
115 /* parents? */
116 while ((buf = strchr(buf+1, ' ')) != NULL) {
117 unsigned char parent[20];
118 if (get_sha1_hex(buf + 1, parent))
119 break;
120 add_relationship(rev, parent);
123 fclose(file);
126 static void mark_sha1_path(struct revision *rev, unsigned int mask)
128 struct parent *p;
130 if (rev->flags & mask)
131 return;
133 rev->flags |= mask;
134 p = rev->parent;
135 while (p) {
136 mark_sha1_path(p->parent, mask);
137 p = p->next;
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);
154 if (!mask)
155 return 0;
156 if (show_edges) {
157 struct parent *p = rev->parent;
158 while (p) {
159 if (mask != marked(p->parent))
160 return 1;
161 p = p->next;
163 return 0;
165 if (mask & basemask)
166 return 0;
168 return 1;
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)
180 int i;
181 int nr = 0;
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++) {
189 char *arg = argv[i];
191 if (!strcmp(arg, "--cache")) {
192 read_cache_file(argv[2]);
193 i++;
194 continue;
197 if (!strcmp(arg, "--edges")) {
198 show_edges = 1;
199 continue;
202 if (arg[0] == '^') {
203 arg++;
204 basemask |= 1<<nr;
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]);
209 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];
223 struct parent *p;
225 if (!interesting(rev))
226 continue;
228 printf("%lu %s:%d", rev->date, sha1_to_hex(rev->sha1), marked(rev));
229 p = rev->parent;
230 while (p) {
231 printf(" %s:%d", sha1_to_hex(p->parent->sha1), marked(p->parent));
232 p = p->next;
234 printf("\n");
236 return 0;