[PATCH] Add '-z' to merge-tree.c
[git/jnareb-git.git] / rev-tree.c
blob33caac3d0325351f74c624a497e06e35bb0252e0
1 #define _XOPEN_SOURCE /* glibc2 needs this */
2 #define _BSD_SOURCE /* for tm.tm_gmtoff */
3 #include <time.h>
4 #include <ctype.h>
6 #include "cache.h"
7 #include "revision.h"
9 /*
10 * revision.h leaves the low 16 bits of the "flags" field of the
11 * revision data structure unused. We use it for a "reachable from
12 * this commit <N>" bitmask.
14 #define MAX_COMMITS 16
16 static int show_edges = 0;
17 static int basemask = 0;
19 static unsigned long parse_time(const char *buf)
21 char c, *p;
22 char buffer[100];
23 struct tm tm;
24 const char *formats[] = {
25 "%s",
26 "%c",
27 "%a %b %d %T %y",
28 NULL
30 const char **fmt = formats;
32 p = buffer;
33 while (isspace(c = *buf))
34 buf++;
35 while ((c = *buf++) != '\n' && c)
36 *p++ = c;
37 *p++ = 0;
38 buf = buffer;
39 memset(&tm, 0, sizeof(tm));
40 do {
41 const char *next = strptime(buf, *fmt, &tm);
42 fmt++;
43 if (next) {
44 if (!*next)
45 return mktime(&tm);
46 buf = next;
48 } while (*buf && *fmt);
49 return mktime(&tm);
53 static unsigned long parse_commit_date(const char *buf)
55 unsigned long time;
57 if (memcmp(buf, "author", 6))
58 return 0;
59 while (*buf++ != '\n')
60 /* nada */;
61 if (memcmp(buf, "committer", 9))
62 return 0;
63 while (*buf++ != '>')
64 /* nada */;
66 time = strtoul(buf, NULL, 10);
67 if (!time)
68 time = parse_time(buf);
69 return time;
72 static int parse_commit(unsigned char *sha1)
74 struct revision *rev = lookup_rev(sha1);
76 if (!(rev->flags & SEEN)) {
77 void *buffer, *bufptr;
78 unsigned long size;
79 char type[20];
80 unsigned char parent[20];
82 rev->flags |= SEEN;
83 buffer = bufptr = read_sha1_file(sha1, type, &size);
84 if (!buffer || strcmp(type, "commit"))
85 return -1;
86 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
87 while (!memcmp(bufptr, "parent ", 7) && !get_sha1_hex(bufptr+7, parent)) {
88 add_relationship(rev, parent);
89 parse_commit(parent);
90 bufptr += 48; /* "parent " + "hex sha1" + "\n" */
92 rev->date = parse_commit_date(bufptr);
93 free(buffer);
95 return 0;
98 static void read_cache_file(const char *path)
100 FILE *file = fopen(path, "r");
101 char line[500];
103 if (!file)
104 die("bad revtree cache file (%s)", path);
106 while (fgets(line, sizeof(line), file)) {
107 unsigned long date;
108 unsigned char sha1[20];
109 struct revision *rev;
110 const char *buf;
112 if (sscanf(line, "%lu", &date) != 1)
113 break;
114 buf = strchr(line, ' ');
115 if (!buf)
116 break;
117 if (get_sha1_hex(buf+1, sha1))
118 break;
119 rev = lookup_rev(sha1);
120 rev->flags |= SEEN;
121 rev->date = date;
123 /* parents? */
124 while ((buf = strchr(buf+1, ' ')) != NULL) {
125 unsigned char parent[20];
126 if (get_sha1_hex(buf + 1, parent))
127 break;
128 add_relationship(rev, parent);
131 fclose(file);
134 static void mark_sha1_path(struct revision *rev, unsigned int mask)
136 struct parent *p;
138 if (rev->flags & mask)
139 return;
141 rev->flags |= mask;
142 p = rev->parent;
143 while (p) {
144 mark_sha1_path(p->parent, mask);
145 p = p->next;
150 * Some revisions are less interesting than others.
152 * For example, if we use a cache-file, that one may contain
153 * revisions that were never used. They are never interesting.
155 * And sometimes we're only interested in "edge" commits, ie
156 * places where the marking changes between parent and child.
158 static int interesting(struct revision *rev)
160 unsigned mask = marked(rev);
162 if (!mask)
163 return 0;
164 if (show_edges) {
165 struct parent *p = rev->parent;
166 while (p) {
167 if (mask != marked(p->parent))
168 return 1;
169 p = p->next;
171 return 0;
173 if (mask & basemask)
174 return 0;
176 return 1;
180 * Usage: rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id2>]
182 * The cache-file can be quite important for big trees. This is an
183 * expensive operation if you have to walk the whole chain of
184 * parents in a tree with a long revision history.
186 int main(int argc, char **argv)
188 int i;
189 int nr = 0;
190 unsigned char sha1[MAX_COMMITS][20];
193 * First - pick up all the revisions we can (both from
194 * caches and from commit file chains).
196 for (i = 1; i < argc ; i++) {
197 char *arg = argv[i];
199 if (!strcmp(arg, "--cache")) {
200 read_cache_file(argv[2]);
201 i++;
202 continue;
205 if (!strcmp(arg, "--edges")) {
206 show_edges = 1;
207 continue;
210 if (arg[0] == '^') {
211 arg++;
212 basemask |= 1<<nr;
214 if (nr >= MAX_COMMITS || get_sha1_hex(arg, sha1[nr]))
215 usage("rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id>]");
216 parse_commit(sha1[nr]);
217 nr++;
221 * Now we have the maximal tree. Walk the different sha files back to the root.
223 for (i = 0; i < nr; i++)
224 mark_sha1_path(lookup_rev(sha1[i]), 1 << i);
227 * Now print out the results..
229 for (i = 0; i < nr_revs; i++) {
230 struct revision *rev = revs[i];
231 struct parent *p;
233 if (!interesting(rev))
234 continue;
236 printf("%lu %s:%d", rev->date, sha1_to_hex(rev->sha1), marked(rev));
237 p = rev->parent;
238 while (p) {
239 printf(" %s:%d", sha1_to_hex(p->parent->sha1), marked(p->parent));
240 p = p->next;
242 printf("\n");
244 return 0;