Move "parse_commit()" into common revision.h file.
[git/git.git] / rev-tree.c
CommitLineData
40e88b95 1#define _XOPEN_SOURCE /* glibc2 needs this */
27de946d 2#define _BSD_SOURCE /* for tm.tm_gmtoff */
40e88b95
LT
3#include <time.h>
4#include <ctype.h>
458754a9 5
84fe9720 6#include "cache.h"
458754a9 7#include "revision.h"
84fe9720 8
28258afe 9/*
458754a9
LT
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.
28258afe 13 */
458754a9 14#define MAX_COMMITS 16
28258afe
LT
15
16static int show_edges = 0;
727ff277 17static int basemask = 0;
97d9c3cd 18
84fe9720
LT
19static void read_cache_file(const char *path)
20{
21 FILE *file = fopen(path, "r");
40e88b95 22 char line[500];
84fe9720 23
28258afe 24 if (!file)
2de381f9 25 die("bad revtree cache file (%s)", path);
28258afe 26
84fe9720 27 while (fgets(line, sizeof(line), file)) {
40e88b95
LT
28 unsigned long date;
29 unsigned char sha1[20];
28258afe 30 struct revision *rev;
40e88b95
LT
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;
28258afe
LT
40 rev = lookup_rev(sha1);
41 rev->flags |= SEEN;
40e88b95
LT
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 }
84fe9720
LT
51 }
52 fclose(file);
53}
54
28258afe
LT
55static 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
84fe9720 70/*
28258afe
LT
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 */
79static 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 }
727ff277
DW
94 if (mask & basemask)
95 return 0;
96
28258afe
LT
97 return 1;
98}
99
100/*
101 * Usage: rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id2>]
84fe9720
LT
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 */
107int main(int argc, char **argv)
108{
109 int i;
28258afe
LT
110 int nr = 0;
111 unsigned char sha1[MAX_COMMITS][20];
84fe9720 112
28258afe
LT
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")) {
84fe9720 121 read_cache_file(argv[2]);
28258afe
LT
122 i++;
123 continue;
124 }
125
126 if (!strcmp(arg, "--edges")) {
127 show_edges = 1;
84fe9720
LT
128 continue;
129 }
28258afe 130
727ff277
DW
131 if (arg[0] == '^') {
132 arg++;
133 basemask |= 1<<nr;
134 }
28258afe
LT
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++;
84fe9720
LT
139 }
140
28258afe
LT
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 */
97d9c3cd
LT
150 for (i = 0; i < nr_revs; i++) {
151 struct revision *rev = revs[i];
152 struct parent *p;
153
28258afe
LT
154 if (!interesting(rev))
155 continue;
156
40e88b95 157 printf("%lu %s:%d", rev->date, sha1_to_hex(rev->sha1), marked(rev));
97d9c3cd
LT
158 p = rev->parent;
159 while (p) {
771364af 160 printf(" %s:%d", sha1_to_hex(p->parent->sha1), marked(p->parent));
97d9c3cd
LT
161 p = p->next;
162 }
163 printf("\n");
84fe9720
LT
164 }
165 return 0;
166}