1#define _XOPEN_SOURCE /* glibc2 needs this */
2#define _BSD_SOURCE /* for tm.tm_gmtoff */
3#include <time.h>
4#include <ctype.h>
56
#include "cache.h"
7#include "revision.h"
89
/*
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
1516
static int show_edges = 0;
17static int basemask = 0;
1819
static void read_cache_file(const char *path)
20{
21FILE *file = fopen(path, "r");
22char line[500];
2324
if (!file)
25die("bad revtree cache file (%s)", path);
2627
while (fgets(line, sizeof(line), file)) {
28unsigned long date;
29unsigned char sha1[20];
30struct revision *rev;
31const char *buf;
3233
if (sscanf(line, "%lu", &date) != 1)
34break;
35buf = strchr(line, ' ');
36if (!buf)
37break;
38if (get_sha1_hex(buf+1, sha1))
39break;
40rev = lookup_rev(sha1, "commit");
41rev->flags |= SEEN;
42rev->date = date;
4344
/* parents? */
45while ((buf = strchr(buf+1, ' ')) != NULL) {
46unsigned char parent[20];
47if (get_sha1_hex(buf + 1, parent))
48break;
49add_relationship(rev, parent, "commit");
50}
51}
52fclose(file);
53}
5455
/*
56* Some revisions are less interesting than others.
57*
58* For example, if we use a cache-file, that one may contain
59* revisions that were never used. They are never interesting.
60*
61* And sometimes we're only interested in "edge" commits, ie
62* places where the marking changes between parent and child.
63*/
64static int interesting(struct revision *rev)
65{
66unsigned mask = marked(rev);
6768
if (!mask)
69return 0;
70if (show_edges) {
71struct parent *p = rev->parent;
72while (p) {
73if (mask != marked(p->parent))
74return 1;
75p = p->next;
76}
77return 0;
78}
79if (mask & basemask)
80return 0;
8182
return 1;
83}
8485
/*
86* Usage: rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id2>]
87*
88* The cache-file can be quite important for big trees. This is an
89* expensive operation if you have to walk the whole chain of
90* parents in a tree with a long revision history.
91*/
92int main(int argc, char **argv)
93{
94int i;
95int nr = 0;
96unsigned char sha1[MAX_COMMITS][20];
9798
/*
99* First - pick up all the revisions we can (both from
100* caches and from commit file chains).
101*/
102for (i = 1; i < argc ; i++) {
103char *arg = argv[i];
104105
if (!strcmp(arg, "--cache")) {
106read_cache_file(argv[2]);
107i++;
108continue;
109}
110111
if (!strcmp(arg, "--edges")) {
112show_edges = 1;
113continue;
114}
115116
if (arg[0] == '^') {
117arg++;
118basemask |= 1<<nr;
119}
120if (nr >= MAX_COMMITS || get_sha1_hex(arg, sha1[nr]))
121usage("rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id>]");
122parse_commit(sha1[nr]);
123nr++;
124}
125126
/*
127* Now we have the maximal tree. Walk the different sha files back to the root.
128*/
129for (i = 0; i < nr; i++)
130mark_reachable(lookup_rev(sha1[i], "commit"), 1 << i);
131132
/*
133* Now print out the results..
134*/
135for (i = 0; i < nr_revs; i++) {
136struct revision *rev = revs[i];
137struct parent *p;
138139
if (!interesting(rev))
140continue;
141142
printf("%lu %s:%d", rev->date, sha1_to_hex(rev->sha1), marked(rev));
143p = rev->parent;
144while (p) {
145printf(" %s:%d", sha1_to_hex(p->parent->sha1), marked(p->parent));
146p = p->next;
147}
148printf("\n");
149}
150return 0;
151}