Use common "revision.h" header for both fsck and rev-tree.
[git.git] / rev-tree.c
1 #define _XOPEN_SOURCE /* glibc2 needs this */
2 #include <time.h>
3 #include <ctype.h>
4
5 #include "cache.h"
6 #include "revision.h"
7
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.
12  */
13 #define MAX_COMMITS 16
14
15 static int show_edges = 0;
16 static int basemask = 0;
17
18 static unsigned long parse_time(const char *buf)
19 {
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
27         };
28         const char **fmt = formats;
29
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;
45                 }
46         } while (*buf && *fmt);
47         return mktime(&tm);
48 }
49                 
50
51 static unsigned long parse_commit_date(const char *buf)
52 {
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);
62 }
63
64 static int parse_commit(unsigned char *sha1)
65 {
66         struct revision *rev = lookup_rev(sha1);
67
68         if (!(rev->flags & SEEN)) {
69                 void *buffer, *bufptr;
70                 unsigned long size;
71                 char type[20];
72                 unsigned char parent[20];
73
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" */
83                 }
84                 rev->date = parse_commit_date(bufptr);
85                 free(buffer);
86         }
87         return 0;       
88 }
89
90 static void read_cache_file(const char *path)
91 {
92         FILE *file = fopen(path, "r");
93         char line[500];
94
95         if (!file)
96                 die("bad revtree cache file (%s)", path);
97
98         while (fgets(line, sizeof(line), file)) {
99                 unsigned long date;
100                 unsigned char sha1[20];
101                 struct revision *rev;
102                 const char *buf;
103
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;
114
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);
121                 }
122         }
123         fclose(file);
124 }
125
126 static void mark_sha1_path(struct revision *rev, unsigned int mask)
127 {
128         struct parent *p;
129
130         if (rev->flags & mask)
131                 return;
132
133         rev->flags |= mask;
134         p = rev->parent;
135         while (p) {
136                 mark_sha1_path(p->parent, mask);
137                 p = p->next;
138         }
139 }
140
141 /*
142  * Some revisions are less interesting than others.
143  *
144  * For example, if we use a cache-file, that one may contain
145  * revisions that were never used. They are never interesting.
146  *
147  * And sometimes we're only interested in "edge" commits, ie
148  * places where the marking changes between parent and child.
149  */
150 static int interesting(struct revision *rev)
151 {
152         unsigned mask = marked(rev);
153
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;
162                 }
163                 return 0;
164         }
165         if (mask & basemask)
166                 return 0;
167
168         return 1;
169 }
170
171 /*
172  * Usage: rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id2>]
173  *
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.
177  */
178 int main(int argc, char **argv)
179 {
180         int i;
181         int nr = 0;
182         unsigned char sha1[MAX_COMMITS][20];
183
184         /*
185          * First - pick up all the revisions we can (both from
186          * caches and from commit file chains).
187          */
188         for (i = 1; i < argc ; i++) {
189                 char *arg = argv[i];
190
191                 if (!strcmp(arg, "--cache")) {
192                         read_cache_file(argv[2]);
193                         i++;
194                         continue;
195                 }
196
197                 if (!strcmp(arg, "--edges")) {
198                         show_edges = 1;
199                         continue;
200                 }
201
202                 if (arg[0] == '^') {
203                         arg++;
204                         basemask |= 1<<nr;
205                 }
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++;
210         }
211
212         /*
213          * Now we have the maximal tree. Walk the different sha files back to the root.
214          */
215         for (i = 0; i < nr; i++)
216                 mark_sha1_path(lookup_rev(sha1[i]), 1 << i);
217
218         /*
219          * Now print out the results..
220          */
221         for (i = 0; i < nr_revs; i++) {
222                 struct revision *rev = revs[i];
223                 struct parent *p;
224
225                 if (!interesting(rev))
226                         continue;
227
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;
233                 }
234                 printf("\n");
235         }
236         return 0;
237 }