Commit 788c352e1f3b00dfa5628e7ccfcfb056433c1948

Stefan Sperling 2018-06-16T19:18:32

store commit timestamps as 'struct tm' in UTC

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
diff --git a/got/got.c b/got/got.c
index a8dad30..a80083b 100644
--- a/got/got.c
+++ b/got/got.c
@@ -323,23 +323,29 @@ print_commit(struct got_commit_object *commit, struct got_object_id *id,
 	const struct got_error *err = NULL;
 	char *id_str, *datestr, *logmsg, *line;
 	char datebuf[26];
+	time_t author_time, committer_time;
 
 	err = got_object_id_str(&id_str, id);
 	if (err)
 		return err;
 
+	author_time = mktime(&commit->tm_author);
+	committer_time = mktime(&commit->tm_committer);
+#if 0
+	/* This would express the date in committer's timezone. */
+	author_time += commit->tm_author.tm_gmtoff;
+	committer_time += commit->tm_committer.tm_gmtoff;
+#endif
+
 	printf("-----------------------------------------------\n");
 	printf("commit %s\n", id_str);
 	free(id_str);
-	datestr = get_datestr(&commit->author_time, datebuf);
-	printf("author: %s  %s %s\n", commit->author, datestr,
-	    commit->author_tzoff);
+	datestr = get_datestr(&author_time, datebuf);
+	printf("author: %s  %s UTC\n", commit->author, datestr);
 	if (strcmp(commit->author, commit->committer) != 0 ||
-	    commit->author_time != commit->committer_time ||
-	    strcmp(commit->author_tzoff, commit->committer_tzoff) != 0) {
-		datestr = get_datestr(&commit->committer_time, datebuf);
-		printf("committer: %s  %s %s\n", commit->committer,
-		    datestr, commit->committer_tzoff);
+	    author_time != committer_time) {
+		datestr = get_datestr(&committer_time, datebuf);
+		printf("committer: %s  %s UTC\n", commit->committer, datestr);
 	}
 	if (commit->nparents > 1) {
 		struct got_object_qid *qid;
diff --git a/include/got_object.h b/include/got_object.h
index 1f6e4e0..ec5c5b1 100644
--- a/include/got_object.h
+++ b/include/got_object.h
@@ -42,11 +42,9 @@ struct got_commit_object {
 	unsigned int nparents;
 	struct got_object_id_queue parent_ids;
 	char *author;
-	time_t author_time;	/* local time */
-	char *author_tzoff;	/* timezone offset description */
+	struct tm tm_author;	/* UTC */
 	char *committer;
-	time_t committer_time;	/* local time */
-	char *committer_tzoff;	/* timezone offset description */
+	struct tm tm_committer;	/* UTC */
 	char *logmsg;
 };
 
diff --git a/lib/commit_graph.c b/lib/commit_graph.c
index 9e8852e..958b66e 100644
--- a/lib/commit_graph.c
+++ b/lib/commit_graph.c
@@ -281,7 +281,9 @@ add_node(struct got_commit_graph_node **new_node,
 			return err;
 		node->nparents++;
 	}
-	node->commit_timestamp = commit->committer_time; /* XXX not UTC! */
+	node->commit_timestamp = mktime(&commit->tm_committer); 
+	if (node->commit_timestamp == -1)
+		return got_error_from_errno();
 
 	err = got_object_idset_add((void **)(&existing_node),
 	    graph->node_ids, &node->id, node);
diff --git a/lib/delta.c b/lib/delta.c
index af295fc..5d81bff 100644
--- a/lib/delta.c
+++ b/lib/delta.c
@@ -21,6 +21,7 @@
 #include <string.h>
 #include <zlib.h>
 #include <sha1.h>
+#include <time.h>
 
 #include "got_error.h"
 #include "got_repository.h"
diff --git a/lib/got_lib_privsep.h b/lib/got_lib_privsep.h
index 0f2d783..67a687b 100644
--- a/lib/got_lib_privsep.h
+++ b/lib/got_lib_privsep.h
@@ -99,17 +99,14 @@ struct got_imsg_object {
 struct got_imsg_commit_object {
 	uint8_t tree_id[SHA1_DIGEST_LENGTH];
 	size_t author_len;
-	time_t author_time;
-	size_t author_tzoff_len;
+	struct tm tm_author;
 	size_t committer_len;
-	time_t committer_time;
-	size_t committer_tzoff_len;
+	struct tm tm_committer;
 	size_t logmsg_len;
 	int nparents;
 
 	/*
-	 * Followed by author_len + author_tzoff_len + committer_len +
-	 * committer_tzoff_len + logmsg_len data bytes
+	 * Followed by author_len + committer_len + logmsg_len data bytes
 	 */
 
 	/* Followed by 'nparents' SHA1_DIGEST_LENGTH length strings */
diff --git a/lib/object.c b/lib/object.c
index 498d4c5..1765ec7 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -32,6 +32,7 @@
 #include <ctype.h>
 #include <limits.h>
 #include <imsg.h>
+#include <time.h>
 
 #include "got_error.h"
 #include "got_object.h"
@@ -493,20 +494,52 @@ got_object_commit_add_parent(struct got_commit_object *commit,
 }
 
 static const struct got_error *
-parse_commit_time(time_t *time, char **tzoff, char *committer)
+parse_gmtoff(time_t *gmtoff, const char *tzstr)
 {
-	const char *errstr;
-	char *space;
+	int sign = 1;
+	const char *p = tzstr;
+	time_t h, m;
+
+	*gmtoff = 0;
+
+	if (*p == '-')
+		sign = -1;
+	else if (*p != '+')
+		return got_error(GOT_ERR_BAD_OBJ_DATA);
+	p++;
+	if (!isdigit(*p) && !isdigit(*(p + 1)))
+		return got_error(GOT_ERR_BAD_OBJ_DATA);
+	h = (((*p - '0') * 10) + (*(p + 1) - '0'));
 
-	*time = 0;
+	p += 2;
+	if (!isdigit(*p) && !isdigit(*(p + 1)))
+		return got_error(GOT_ERR_BAD_OBJ_DATA);
+	m = ((*p - '0') * 10) + (*(p + 1) - '0');
 
-	/* Parse and then strip trailing timezone indicator. */
+	*gmtoff = (h * 60 * 60 + m * 60) * sign;
+	return NULL;
+}
+
+static const struct got_error *
+parse_commit_time(struct tm *tm, char *committer)
+{
+	const struct got_error *err = NULL;
+	const char *errstr;
+	char *space, *tzstr;
+	time_t gmtoff;
+	time_t time;
+
+	/* Parse and strip off trailing timezone indicator string. */
 	space = strrchr(committer, ' ');
 	if (space == NULL)
 		return got_error(GOT_ERR_BAD_OBJ_DATA);
-	*tzoff = strdup(space + 1);
-	if (*tzoff == NULL)
+	tzstr = strdup(space + 1);
+	if (tzstr == NULL)
 		return got_error_from_errno();
+	err = parse_gmtoff(&gmtoff, tzstr);
+	free(tzstr);
+	if (err)
+		return err;
 	*space = '\0';
 
 	/* Timestamp is separated from committer name + email by space. */
@@ -514,12 +547,21 @@ parse_commit_time(time_t *time, char **tzoff, char *committer)
 	if (space == NULL)
 		return got_error(GOT_ERR_BAD_OBJ_DATA);
 
-	*time = strtonum(space + 1, 0, INT64_MAX, &errstr);
+	/* Timestamp parsed here is expressed in comitter's local time. */
+	time = strtonum(space + 1, 0, INT64_MAX, &errstr);
 	if (errstr)
 		return got_error(GOT_ERR_BAD_OBJ_DATA);
 
+	/* Express the time stamp in UTC. */
+	memset(tm, 0, sizeof(*tm));
+	time -= gmtoff;
+	if (localtime_r(&time, tm) == NULL)
+		return got_error_from_errno();
+	tm->tm_gmtoff = gmtoff;
+
 	/* Strip off parsed time information, leaving just author and email. */
 	*space = '\0';
+
 	return NULL;
 }
 
@@ -588,8 +630,7 @@ parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
 		}
 		*p = '\0';
 		slen = strlen(s);
-		err = parse_commit_time(&(*commit)->author_time,
-		    &(*commit)->author_tzoff, s);
+		err = parse_commit_time(&(*commit)->tm_author, s);
 		if (err)
 			goto done;
 		(*commit)->author = strdup(s);
@@ -619,8 +660,7 @@ parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
 		}
 		*p = '\0';
 		slen = strlen(s);
-		err = parse_commit_time(&(*commit)->committer_time,
-		    &(*commit)->committer_tzoff, s);
+		err = parse_commit_time(&(*commit)->tm_committer, s);
 		if (err)
 			goto done;
 		(*commit)->committer = strdup(s);
@@ -949,9 +989,7 @@ got_object_commit_close(struct got_commit_object *commit)
 
 	free(commit->tree_id);
 	free(commit->author);
-	free(commit->author_tzoff);
 	free(commit->committer);
-	free(commit->committer_tzoff);
 	free(commit->logmsg);
 	free(commit);
 }
diff --git a/lib/object_idset.c b/lib/object_idset.c
index e7e8b1b..ae64f9a 100644
--- a/lib/object_idset.c
+++ b/lib/object_idset.c
@@ -22,6 +22,7 @@
 #include <stdio.h>
 #include <zlib.h>
 #include <limits.h>
+#include <time.h>
 
 #include "got_object.h"
 #include "got_error.h"
diff --git a/lib/privsep.c b/lib/privsep.c
index 1c18f16..2fc13c0 100644
--- a/lib/privsep.c
+++ b/lib/privsep.c
@@ -27,6 +27,7 @@
 #include <imsg.h>
 #include <sha1.h>
 #include <zlib.h>
+#include <time.h>
 
 #include "got_object.h"
 #include "got_error.h"
@@ -266,17 +267,16 @@ got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
 
 	memcpy(icommit.tree_id, commit->tree_id->sha1, sizeof(icommit.tree_id));
 	icommit.author_len = strlen(commit->author);
-	icommit.author_time = commit->author_time;
-	icommit.author_tzoff_len = strlen(commit->author_tzoff);
+	memcpy(&icommit.tm_author, &commit->tm_author,
+	    sizeof(icommit.tm_author));
 	icommit.committer_len = strlen(commit->committer);
-	icommit.committer_time = commit->committer_time;
-	icommit.committer_tzoff_len = strlen(commit->committer_tzoff);
+	memcpy(&icommit.tm_committer, &commit->tm_committer,
+	    sizeof(icommit.tm_committer));
 	icommit.logmsg_len = strlen(commit->logmsg);
 	icommit.nparents = commit->nparents;
 
 	total = sizeof(icommit) + icommit.author_len +
-	    icommit.author_tzoff_len + icommit.committer_len +
-	    icommit.committer_tzoff_len + icommit.logmsg_len +
+	    icommit.committer_len + icommit.logmsg_len +
 	    icommit.nparents * SHA1_DIGEST_LENGTH;
 	/* XXX TODO support very large log messages properly */
 	if (total > MAX_IMSGSIZE)
@@ -291,12 +291,8 @@ got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
 	len += sizeof(icommit);
 	memcpy(buf + len, commit->author, icommit.author_len);
 	len += icommit.author_len;
-	memcpy(buf + len, commit->author_tzoff, icommit.author_tzoff_len);
-	len += icommit.author_tzoff_len;
 	memcpy(buf + len, commit->committer, icommit.committer_len);
 	len += icommit.committer_len;
-	memcpy(buf + len, commit->committer_tzoff, icommit.committer_tzoff_len);
-	len += icommit.committer_tzoff_len;
 	memcpy(buf + len, commit->logmsg, icommit.logmsg_len);
 	len += icommit.logmsg_len;
 	SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
@@ -349,8 +345,7 @@ got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
 
 		memcpy(&icommit, data, sizeof(icommit));
 		if (datalen != sizeof(icommit) + icommit.author_len +
-		    icommit.author_tzoff_len + icommit.committer_len +
-		    icommit.committer_tzoff_len + icommit.logmsg_len +
+		    icommit.committer_len + icommit.logmsg_len +
 		    icommit.nparents * SHA1_DIGEST_LENGTH) {
 			err = got_error(GOT_ERR_PRIVSEP_LEN);
 			break;
@@ -369,6 +364,10 @@ got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
 
 		memcpy((*commit)->tree_id->sha1, icommit.tree_id,
 		    SHA1_DIGEST_LENGTH);
+		memcpy(&(*commit)->tm_author, &icommit.tm_author,
+		    sizeof((*commit)->tm_author));
+		memcpy(&(*commit)->tm_committer, &icommit.tm_committer,
+		    sizeof((*commit)->tm_committer));
 
 		if (icommit.author_len == 0) {
 			(*commit)->author = strdup("");
@@ -376,7 +375,6 @@ got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
 				err = got_error_from_errno();
 				break;
 			}
-			(*commit)->author_time = 0;
 		} else {
 			(*commit)->author = malloc(icommit.author_len + 1);
 			if ((*commit)->author == NULL) {
@@ -389,34 +387,12 @@ got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
 		}
 		len += icommit.author_len;
 
-		(*commit)->author_time = icommit.author_time;
-		if (icommit.author_tzoff_len == 0) {
-			(*commit)->author_tzoff = strdup("");
-			if ((*commit)->author_tzoff == NULL) {
-				err = got_error_from_errno();
-				break;
-			}
-		} else {
-			(*commit)->author_tzoff =
-			    malloc(icommit.author_tzoff_len + 1);
-			if ((*commit)->author_tzoff == NULL) {
-				err = got_error_from_errno();
-				break;
-			}
-			memcpy((*commit)->author_tzoff, data + len,
-			    icommit.author_tzoff_len);
-			(*commit)->author_tzoff[icommit.author_tzoff_len] =
-			    '\0';
-		}
-		len += icommit.author_tzoff_len;
-
 		if (icommit.committer_len == 0) {
 			(*commit)->committer = strdup("");
 			if ((*commit)->committer == NULL) {
 				err = got_error_from_errno();
 				break;
 			}
-			(*commit)->committer_time = 0;
 		} else {
 			(*commit)->committer =
 			    malloc(icommit.committer_len + 1);
@@ -430,27 +406,6 @@ got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
 		}
 		len += icommit.committer_len;
 
-		(*commit)->committer_time = icommit.committer_time;
-		if (icommit.committer_tzoff_len == 0) {
-			(*commit)->committer_tzoff = strdup("");
-			if ((*commit)->committer_tzoff == NULL) {
-				err = got_error_from_errno();
-				break;
-			}
-		} else {
-			(*commit)->committer_tzoff =
-			    malloc(icommit.committer_tzoff_len + 1);
-			if ((*commit)->committer_tzoff == NULL) {
-				err = got_error_from_errno();
-				break;
-			}
-			memcpy((*commit)->committer_tzoff, data + len,
-			    icommit.committer_tzoff_len);
-			(*commit)->committer_tzoff[icommit.committer_tzoff_len]
-			    = '\0';
-		}
-		len += icommit.committer_tzoff_len;
-
 		if (icommit.logmsg_len == 0) {
 			(*commit)->logmsg = strdup("");
 			if ((*commit)->logmsg == NULL) {
diff --git a/lib/reference.c b/lib/reference.c
index 8336d48..635623e 100644
--- a/lib/reference.c
+++ b/lib/reference.c
@@ -23,6 +23,7 @@
 #include <string.h>
 #include <util.h>
 #include <zlib.h>
+#include <time.h>
 
 #include "got_error.h"
 #include "got_object.h"
diff --git a/lib/zbuf.c b/lib/zbuf.c
index 90a2cab..cd8dc7d 100644
--- a/lib/zbuf.c
+++ b/lib/zbuf.c
@@ -21,6 +21,7 @@
 #include <string.h>
 #include <sha1.h>
 #include <zlib.h>
+#include <time.h>
 
 #include "got_error.h"
 #include "got_object.h"
diff --git a/regress/idset/idset_test.c b/regress/idset/idset_test.c
index 3b62000..35a2c48 100644
--- a/regress/idset/idset_test.c
+++ b/regress/idset/idset_test.c
@@ -23,6 +23,7 @@
 #include <err.h>
 #include <sha1.h>
 #include <zlib.h>
+#include <time.h>
 
 #include "got_error.h"
 #include "got_object.h"
diff --git a/tog/tog.c b/tog/tog.c
index 9687506..b4bebe2 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -31,6 +31,7 @@
 #include <util.h>
 #include <limits.h>
 #include <wchar.h>
+#include <time.h>
 
 #include "got_error.h"
 #include "got_object.h"