Merge the new object model thing from Daniel Barkalow
[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 "commit.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         die("no revtree cache file yet");
22 }
23
24 /*
25  * Some revisions are less interesting than others.
26  *
27  * For example, if we use a cache-file, that one may contain
28  * revisions that were never used. They are never interesting.
29  *
30  * And sometimes we're only interested in "edge" commits, ie
31  * places where the marking changes between parent and child.
32  */
33 static int interesting(struct commit *rev)
34 {
35         unsigned mask = rev->object.flags;
36
37         if (!mask)
38                 return 0;
39         if (show_edges) {
40                 struct commit_list *p = rev->parents;
41                 while (p) {
42                         if (mask != p->item->object.flags)
43                                 return 1;
44                         p = p->next;
45                 }
46                 return 0;
47         }
48         if (mask & basemask)
49                 return 0;
50
51         return 1;
52 }
53
54 void process_commit(unsigned char *sha1)
55 {
56         struct commit_list *parents;
57         struct commit *obj = lookup_commit(sha1);
58         parse_commit(obj);
59         
60         parents = obj->parents;
61         while (parents) {
62                 process_commit(parents->item->object.sha1);
63                 parents = parents->next;
64         }
65 }
66
67 /*
68  * Usage: rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id2>]
69  *
70  * The cache-file can be quite important for big trees. This is an
71  * expensive operation if you have to walk the whole chain of
72  * parents in a tree with a long revision history.
73  */
74 int main(int argc, char **argv)
75 {
76         int i;
77         int nr = 0;
78         unsigned char sha1[MAX_COMMITS][20];
79
80         /*
81          * First - pick up all the revisions we can (both from
82          * caches and from commit file chains).
83          */
84         for (i = 1; i < argc ; i++) {
85                 char *arg = argv[i];
86
87                 if (!strcmp(arg, "--cache")) {
88                         read_cache_file(argv[2]);
89                         i++;
90                         continue;
91                 }
92
93                 if (!strcmp(arg, "--edges")) {
94                         show_edges = 1;
95                         continue;
96                 }
97
98                 if (arg[0] == '^') {
99                         arg++;
100                         basemask |= 1<<nr;
101                 }
102                 if (nr >= MAX_COMMITS || get_sha1_hex(arg, sha1[nr]))
103                         usage("rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id>]");
104                 process_commit(sha1[nr]);
105                 nr++;
106         }
107
108         /*
109          * Now we have the maximal tree. Walk the different sha files back to the root.
110          */
111         for (i = 0; i < nr; i++)
112                 mark_reachable(&lookup_commit(sha1[i])->object, 1 << i);
113
114         /*
115          * Now print out the results..
116          */
117         for (i = 0; i < nr_objs; i++) {
118                 struct object *obj = objs[i];
119                 struct commit *commit;
120                 struct commit_list *p;
121
122                 if (obj->type != commit_type)
123                         continue;
124
125                 commit = (struct commit *) obj;
126
127                 if (!interesting(commit))
128                         continue;
129
130                 printf("%lu %s:%d", commit->date, sha1_to_hex(obj->sha1), 
131                        obj->flags);
132                 p = commit->parents;
133                 while (p) {
134                         printf(" %s:%d", sha1_to_hex(p->item->object.sha1), 
135                                p->item->object.flags);
136                         p = p->next;
137                 }
138                 printf("\n");
139         }
140         return 0;
141 }