[PATCH] Rename and extend read_tree_with_tree_or_commit_sha1
[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"
5873b67e 7#include "commit.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{
5873b67e 21 die("no revtree cache file yet");
84fe9720
LT
22}
23
24/*
28258afe
LT
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 */
5873b67e 33static int interesting(struct commit *rev)
28258afe 34{
5873b67e 35 unsigned mask = rev->object.flags;
28258afe
LT
36
37 if (!mask)
38 return 0;
39 if (show_edges) {
5873b67e 40 struct commit_list *p = rev->parents;
28258afe 41 while (p) {
5873b67e 42 if (mask != p->item->object.flags)
28258afe
LT
43 return 1;
44 p = p->next;
45 }
46 return 0;
47 }
727ff277
DW
48 if (mask & basemask)
49 return 0;
50
28258afe
LT
51 return 1;
52}
53
5873b67e
DB
54void process_commit(unsigned char *sha1)
55{
56 struct commit_list *parents;
57 struct commit *obj = lookup_commit(sha1);
3b7d368f
LT
58
59 if (obj->object.parsed)
60 return;
61
5873b67e
DB
62 parse_commit(obj);
63
64 parents = obj->parents;
65 while (parents) {
66 process_commit(parents->item->object.sha1);
67 parents = parents->next;
68 }
69}
70
28258afe
LT
71/*
72 * Usage: rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id2>]
84fe9720
LT
73 *
74 * The cache-file can be quite important for big trees. This is an
75 * expensive operation if you have to walk the whole chain of
76 * parents in a tree with a long revision history.
77 */
78int main(int argc, char **argv)
79{
80 int i;
28258afe
LT
81 int nr = 0;
82 unsigned char sha1[MAX_COMMITS][20];
84fe9720 83
28258afe
LT
84 /*
85 * First - pick up all the revisions we can (both from
86 * caches and from commit file chains).
87 */
88 for (i = 1; i < argc ; i++) {
89 char *arg = argv[i];
90
91 if (!strcmp(arg, "--cache")) {
94dfb7f2 92 read_cache_file(argv[++i]);
28258afe
LT
93 continue;
94 }
95
96 if (!strcmp(arg, "--edges")) {
97 show_edges = 1;
84fe9720
LT
98 continue;
99 }
28258afe 100
727ff277
DW
101 if (arg[0] == '^') {
102 arg++;
103 basemask |= 1<<nr;
104 }
28258afe
LT
105 if (nr >= MAX_COMMITS || get_sha1_hex(arg, sha1[nr]))
106 usage("rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id>]");
5873b67e 107 process_commit(sha1[nr]);
28258afe 108 nr++;
84fe9720
LT
109 }
110
28258afe
LT
111 /*
112 * Now we have the maximal tree. Walk the different sha files back to the root.
113 */
114 for (i = 0; i < nr; i++)
5873b67e 115 mark_reachable(&lookup_commit(sha1[i])->object, 1 << i);
28258afe
LT
116
117 /*
118 * Now print out the results..
119 */
5873b67e
DB
120 for (i = 0; i < nr_objs; i++) {
121 struct object *obj = objs[i];
122 struct commit *commit;
123 struct commit_list *p;
124
125 if (obj->type != commit_type)
126 continue;
127
128 commit = (struct commit *) obj;
97d9c3cd 129
5873b67e 130 if (!interesting(commit))
28258afe
LT
131 continue;
132
5873b67e
DB
133 printf("%lu %s:%d", commit->date, sha1_to_hex(obj->sha1),
134 obj->flags);
135 p = commit->parents;
97d9c3cd 136 while (p) {
5873b67e
DB
137 printf(" %s:%d", sha1_to_hex(p->item->object.sha1),
138 p->item->object.flags);
97d9c3cd
LT
139 p = p->next;
140 }
141 printf("\n");
84fe9720
LT
142 }
143 return 0;
144}