[PATCH] ls-tree enhancements
[git/git.git] / rev-tree.c
CommitLineData
40e88b95
LT
1#define _XOPEN_SOURCE /* glibc2 needs this */
2#include <time.h>
3#include <ctype.h>
458754a9 4
84fe9720 5#include "cache.h"
458754a9 6#include "revision.h"
84fe9720 7
28258afe 8/*
458754a9
LT
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.
28258afe 12 */
458754a9 13#define MAX_COMMITS 16
28258afe
LT
14
15static int show_edges = 0;
727ff277 16static int basemask = 0;
97d9c3cd 17
40e88b95
LT
18static 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
51static 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
84fe9720
LT
64static int parse_commit(unsigned char *sha1)
65{
97d9c3cd
LT
66 struct revision *rev = lookup_rev(sha1);
67
68 if (!(rev->flags & SEEN)) {
3d8c7a57 69 void *buffer, *bufptr;
84fe9720
LT
70 unsigned long size;
71 char type[20];
72 unsigned char parent[20];
73
97d9c3cd 74 rev->flags |= SEEN;
3d8c7a57 75 buffer = bufptr = read_sha1_file(sha1, type, &size);
84fe9720
LT
76 if (!buffer || strcmp(type, "commit"))
77 return -1;
3d8c7a57
PB
78 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
79 while (!memcmp(bufptr, "parent ", 7) && !get_sha1_hex(bufptr+7, parent)) {
97d9c3cd 80 add_relationship(rev, parent);
84fe9720 81 parse_commit(parent);
3d8c7a57 82 bufptr += 48; /* "parent " + "hex sha1" + "\n" */
84fe9720 83 }
3d8c7a57 84 rev->date = parse_commit_date(bufptr);
40e88b95 85 free(buffer);
84fe9720
LT
86 }
87 return 0;
88}
89
90static void read_cache_file(const char *path)
91{
92 FILE *file = fopen(path, "r");
40e88b95 93 char line[500];
84fe9720 94
28258afe 95 if (!file)
2de381f9 96 die("bad revtree cache file (%s)", path);
28258afe 97
84fe9720 98 while (fgets(line, sizeof(line), file)) {
40e88b95
LT
99 unsigned long date;
100 unsigned char sha1[20];
28258afe 101 struct revision *rev;
40e88b95
LT
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;
28258afe
LT
111 rev = lookup_rev(sha1);
112 rev->flags |= SEEN;
40e88b95
LT
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 }
84fe9720
LT
122 }
123 fclose(file);
124}
125
28258afe
LT
126static 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
84fe9720 141/*
28258afe
LT
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 */
150static 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 }
727ff277
DW
165 if (mask & basemask)
166 return 0;
167
28258afe
LT
168 return 1;
169}
170
171/*
172 * Usage: rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id2>]
84fe9720
LT
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 */
178int main(int argc, char **argv)
179{
180 int i;
28258afe
LT
181 int nr = 0;
182 unsigned char sha1[MAX_COMMITS][20];
84fe9720 183
28258afe
LT
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")) {
84fe9720 192 read_cache_file(argv[2]);
28258afe
LT
193 i++;
194 continue;
195 }
196
197 if (!strcmp(arg, "--edges")) {
198 show_edges = 1;
84fe9720
LT
199 continue;
200 }
28258afe 201
727ff277
DW
202 if (arg[0] == '^') {
203 arg++;
204 basemask |= 1<<nr;
205 }
28258afe
LT
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++;
84fe9720
LT
210 }
211
28258afe
LT
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 */
97d9c3cd
LT
221 for (i = 0; i < nr_revs; i++) {
222 struct revision *rev = revs[i];
223 struct parent *p;
224
28258afe
LT
225 if (!interesting(rev))
226 continue;
227
40e88b95 228 printf("%lu %s:%d", rev->date, sha1_to_hex(rev->sha1), marked(rev));
97d9c3cd
LT
229 p = rev->parent;
230 while (p) {
771364af 231 printf(" %s:%d", sha1_to_hex(p->parent->sha1), marked(p->parent));
97d9c3cd
LT
232 p = p->next;
233 }
234 printf("\n");
84fe9720
LT
235 }
236 return 0;
237}