Move "parse_commit()" into common revision.h file.
[git/git.git] / rev-tree.c
1 #define _XOPEN_SOURCE /* glibc2 needs this */
2 #define _BSD_SOURCE /* for tm.tm_gmtoff */
3 #include <time.h>
4 #include <ctype.h>
5
6 #include "cache.h"
7 #include "revision.h"
8
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.
13 */
14 #define MAX_COMMITS 16
15
16 static int show_edges = 0;
17 static int basemask = 0;
18
19 static void read_cache_file(const char *path)
20 {
21 FILE *file = fopen(path, "r");
22 char line[500];
23
24 if (!file)
25 die("bad revtree cache file (%s)", path);
26
27 while (fgets(line, sizeof(line), file)) {
28 unsigned long date;
29 unsigned char sha1[20];
30 struct revision *rev;
31 const char *buf;
32
33 if (sscanf(line, "%lu", &date) != 1)
34 break;
35 buf = strchr(line, ' ');
36 if (!buf)
37 break;
38 if (get_sha1_hex(buf+1, sha1))
39 break;
40 rev = lookup_rev(sha1);
41 rev->flags |= SEEN;
42 rev->date = date;
43
44 /* parents? */
45 while ((buf = strchr(buf+1, ' ')) != NULL) {
46 unsigned char parent[20];
47 if (get_sha1_hex(buf + 1, parent))
48 break;
49 add_relationship(rev, parent);
50 }
51 }
52 fclose(file);
53 }
54
55 static void mark_sha1_path(struct revision *rev, unsigned int mask)
56 {
57 struct parent *p;
58
59 if (rev->flags & mask)
60 return;
61
62 rev->flags |= mask;
63 p = rev->parent;
64 while (p) {
65 mark_sha1_path(p->parent, mask);
66 p = p->next;
67 }
68 }
69
70 /*
71 * Some revisions are less interesting than others.
72 *
73 * For example, if we use a cache-file, that one may contain
74 * revisions that were never used. They are never interesting.
75 *
76 * And sometimes we're only interested in "edge" commits, ie
77 * places where the marking changes between parent and child.
78 */
79 static int interesting(struct revision *rev)
80 {
81 unsigned mask = marked(rev);
82
83 if (!mask)
84 return 0;
85 if (show_edges) {
86 struct parent *p = rev->parent;
87 while (p) {
88 if (mask != marked(p->parent))
89 return 1;
90 p = p->next;
91 }
92 return 0;
93 }
94 if (mask & basemask)
95 return 0;
96
97 return 1;
98 }
99
100 /*
101 * Usage: rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id2>]
102 *
103 * The cache-file can be quite important for big trees. This is an
104 * expensive operation if you have to walk the whole chain of
105 * parents in a tree with a long revision history.
106 */
107 int main(int argc, char **argv)
108 {
109 int i;
110 int nr = 0;
111 unsigned char sha1[MAX_COMMITS][20];
112
113 /*
114 * First - pick up all the revisions we can (both from
115 * caches and from commit file chains).
116 */
117 for (i = 1; i < argc ; i++) {
118 char *arg = argv[i];
119
120 if (!strcmp(arg, "--cache")) {
121 read_cache_file(argv[2]);
122 i++;
123 continue;
124 }
125
126 if (!strcmp(arg, "--edges")) {
127 show_edges = 1;
128 continue;
129 }
130
131 if (arg[0] == '^') {
132 arg++;
133 basemask |= 1<<nr;
134 }
135 if (nr >= MAX_COMMITS || get_sha1_hex(arg, sha1[nr]))
136 usage("rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id>]");
137 parse_commit(sha1[nr]);
138 nr++;
139 }
140
141 /*
142 * Now we have the maximal tree. Walk the different sha files back to the root.
143 */
144 for (i = 0; i < nr; i++)
145 mark_sha1_path(lookup_rev(sha1[i]), 1 << i);
146
147 /*
148 * Now print out the results..
149 */
150 for (i = 0; i < nr_revs; i++) {
151 struct revision *rev = revs[i];
152 struct parent *p;
153
154 if (!interesting(rev))
155 continue;
156
157 printf("%lu %s:%d", rev->date, sha1_to_hex(rev->sha1), marked(rev));
158 p = rev->parent;
159 while (p) {
160 printf(" %s:%d", sha1_to_hex(p->parent->sha1), marked(p->parent));
161 p = p->next;
162 }
163 printf("\n");
164 }
165 return 0;
166 }