[PATCH] Simplify date handling and make it more reliable
[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
40e88b95
LT
19static unsigned long parse_time(const char *buf)
20{
21 char c, *p;
22 char buffer[100];
23 struct tm tm;
24 const char *formats[] = {
27de946d 25 "%s",
40e88b95
LT
26 "%c",
27 "%a %b %d %T %y",
28 NULL
29 };
30 const char **fmt = formats;
31
32 p = buffer;
33 while (isspace(c = *buf))
34 buf++;
27de946d 35 while ((c = *buf++) != '\n' && c)
40e88b95
LT
36 *p++ = c;
37 *p++ = 0;
38 buf = buffer;
39 memset(&tm, 0, sizeof(tm));
40 do {
41 const char *next = strptime(buf, *fmt, &tm);
42 fmt++;
43 if (next) {
44 if (!*next)
45 return mktime(&tm);
46 buf = next;
47 }
48 } while (*buf && *fmt);
49 return mktime(&tm);
50}
51
52
53static unsigned long parse_commit_date(const char *buf)
54{
27de946d
DW
55 unsigned long time;
56
40e88b95
LT
57 if (memcmp(buf, "author", 6))
58 return 0;
59 while (*buf++ != '\n')
60 /* nada */;
61 if (memcmp(buf, "committer", 9))
62 return 0;
63 while (*buf++ != '>')
64 /* nada */;
27de946d
DW
65
66 time = strtoul(buf, NULL, 10);
67 if (!time)
68 time = parse_time(buf);
69 return time;
40e88b95
LT
70}
71
84fe9720
LT
72static int parse_commit(unsigned char *sha1)
73{
97d9c3cd
LT
74 struct revision *rev = lookup_rev(sha1);
75
76 if (!(rev->flags & SEEN)) {
3d8c7a57 77 void *buffer, *bufptr;
84fe9720
LT
78 unsigned long size;
79 char type[20];
80 unsigned char parent[20];
81
97d9c3cd 82 rev->flags |= SEEN;
3d8c7a57 83 buffer = bufptr = read_sha1_file(sha1, type, &size);
84fe9720
LT
84 if (!buffer || strcmp(type, "commit"))
85 return -1;
3d8c7a57
PB
86 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
87 while (!memcmp(bufptr, "parent ", 7) && !get_sha1_hex(bufptr+7, parent)) {
97d9c3cd 88 add_relationship(rev, parent);
84fe9720 89 parse_commit(parent);
3d8c7a57 90 bufptr += 48; /* "parent " + "hex sha1" + "\n" */
84fe9720 91 }
3d8c7a57 92 rev->date = parse_commit_date(bufptr);
40e88b95 93 free(buffer);
84fe9720
LT
94 }
95 return 0;
96}
97
98static void read_cache_file(const char *path)
99{
100 FILE *file = fopen(path, "r");
40e88b95 101 char line[500];
84fe9720 102
28258afe 103 if (!file)
2de381f9 104 die("bad revtree cache file (%s)", path);
28258afe 105
84fe9720 106 while (fgets(line, sizeof(line), file)) {
40e88b95
LT
107 unsigned long date;
108 unsigned char sha1[20];
28258afe 109 struct revision *rev;
40e88b95
LT
110 const char *buf;
111
112 if (sscanf(line, "%lu", &date) != 1)
113 break;
114 buf = strchr(line, ' ');
115 if (!buf)
116 break;
117 if (get_sha1_hex(buf+1, sha1))
118 break;
28258afe
LT
119 rev = lookup_rev(sha1);
120 rev->flags |= SEEN;
40e88b95
LT
121 rev->date = date;
122
123 /* parents? */
124 while ((buf = strchr(buf+1, ' ')) != NULL) {
125 unsigned char parent[20];
126 if (get_sha1_hex(buf + 1, parent))
127 break;
128 add_relationship(rev, parent);
129 }
84fe9720
LT
130 }
131 fclose(file);
132}
133
28258afe
LT
134static void mark_sha1_path(struct revision *rev, unsigned int mask)
135{
136 struct parent *p;
137
138 if (rev->flags & mask)
139 return;
140
141 rev->flags |= mask;
142 p = rev->parent;
143 while (p) {
144 mark_sha1_path(p->parent, mask);
145 p = p->next;
146 }
147}
148
84fe9720 149/*
28258afe
LT
150 * Some revisions are less interesting than others.
151 *
152 * For example, if we use a cache-file, that one may contain
153 * revisions that were never used. They are never interesting.
154 *
155 * And sometimes we're only interested in "edge" commits, ie
156 * places where the marking changes between parent and child.
157 */
158static int interesting(struct revision *rev)
159{
160 unsigned mask = marked(rev);
161
162 if (!mask)
163 return 0;
164 if (show_edges) {
165 struct parent *p = rev->parent;
166 while (p) {
167 if (mask != marked(p->parent))
168 return 1;
169 p = p->next;
170 }
171 return 0;
172 }
727ff277
DW
173 if (mask & basemask)
174 return 0;
175
28258afe
LT
176 return 1;
177}
178
179/*
180 * Usage: rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id2>]
84fe9720
LT
181 *
182 * The cache-file can be quite important for big trees. This is an
183 * expensive operation if you have to walk the whole chain of
184 * parents in a tree with a long revision history.
185 */
186int main(int argc, char **argv)
187{
188 int i;
28258afe
LT
189 int nr = 0;
190 unsigned char sha1[MAX_COMMITS][20];
84fe9720 191
28258afe
LT
192 /*
193 * First - pick up all the revisions we can (both from
194 * caches and from commit file chains).
195 */
196 for (i = 1; i < argc ; i++) {
197 char *arg = argv[i];
198
199 if (!strcmp(arg, "--cache")) {
84fe9720 200 read_cache_file(argv[2]);
28258afe
LT
201 i++;
202 continue;
203 }
204
205 if (!strcmp(arg, "--edges")) {
206 show_edges = 1;
84fe9720
LT
207 continue;
208 }
28258afe 209
727ff277
DW
210 if (arg[0] == '^') {
211 arg++;
212 basemask |= 1<<nr;
213 }
28258afe
LT
214 if (nr >= MAX_COMMITS || get_sha1_hex(arg, sha1[nr]))
215 usage("rev-tree [--edges] [--cache <cache-file>] <commit-id> [<commit-id>]");
216 parse_commit(sha1[nr]);
217 nr++;
84fe9720
LT
218 }
219
28258afe
LT
220 /*
221 * Now we have the maximal tree. Walk the different sha files back to the root.
222 */
223 for (i = 0; i < nr; i++)
224 mark_sha1_path(lookup_rev(sha1[i]), 1 << i);
225
226 /*
227 * Now print out the results..
228 */
97d9c3cd
LT
229 for (i = 0; i < nr_revs; i++) {
230 struct revision *rev = revs[i];
231 struct parent *p;
232
28258afe
LT
233 if (!interesting(rev))
234 continue;
235
40e88b95 236 printf("%lu %s:%d", rev->date, sha1_to_hex(rev->sha1), marked(rev));
97d9c3cd
LT
237 p = rev->parent;
238 while (p) {
771364af 239 printf(" %s:%d", sha1_to_hex(p->parent->sha1), marked(p->parent));
97d9c3cd
LT
240 p = p->next;
241 }
242 printf("\n");
84fe9720
LT
243 }
244 return 0;
245}