Commit 45d799e2247f32829160ec48664dbccfae98150f

Stefan Sperling 2018-12-23T12:10:27

make commit objects opaque in the library's API

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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
diff --git a/got/got.c b/got/got.c
index 7cd07bd..9ca1e6c 100644
--- a/got/got.c
+++ b/got/got.c
@@ -305,11 +305,12 @@ print_patch(struct got_commit_object *commit, struct got_object_id *id,
 	struct got_object_qid *qid;
 	char *id_str1 = NULL, *id_str2;
 
-	err = got_object_open_as_tree(&tree2, repo, commit->tree_id);
+	err = got_object_open_as_tree(&tree2, repo,
+	    got_object_commit_get_tree_id(commit));
 	if (err)
 		return err;
 
-	qid = SIMPLEQ_FIRST(&commit->parent_ids);
+	qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
 	if (qid != NULL) {
 		struct got_commit_object *pcommit;
 
@@ -317,7 +318,8 @@ print_patch(struct got_commit_object *commit, struct got_object_id *id,
 		if (err)
 			return err;
 
-		err = got_object_open_as_tree(&tree1, repo, pcommit->tree_id);
+		err = got_object_open_as_tree(&tree1, repo,
+		    got_object_commit_get_tree_id(pcommit));
 		got_object_commit_close(pcommit);
 		if (err)
 			return err;
@@ -359,6 +361,8 @@ print_commit(struct got_commit_object *commit, struct got_object_id *id,
 	const struct got_error *err = NULL;
 	char *id_str, *datestr, *logmsg0, *logmsg, *line;
 	char datebuf[26];
+	time_t committer_time;
+	const char *author, *committer;
 
 	err = got_object_id_str(&id_str, id);
 	if (err)
@@ -367,15 +371,20 @@ print_commit(struct got_commit_object *commit, struct got_object_id *id,
 	printf("-----------------------------------------------\n");
 	printf("commit %s\n", id_str);
 	free(id_str);
-	printf("from: %s\n", commit->author);
-	datestr = get_datestr(&commit->committer_time, datebuf);
+	printf("from: %s\n", got_object_commit_get_author(commit));
+	committer_time = got_object_commit_get_committer_time(commit);
+	datestr = get_datestr(&committer_time, datebuf);
 	printf("date: %s UTC\n", datestr);
-	if (strcmp(commit->author, commit->committer) != 0)
-		printf("via: %s\n", commit->committer);
-	if (commit->nparents > 1) {
+	author = got_object_commit_get_author(commit);
+	committer = got_object_commit_get_committer(commit);
+	if (strcmp(author, committer) != 0)
+		printf("via: %s\n", committer);
+	if (got_object_commit_get_nparents(commit) > 1) {
+		const struct got_object_id_queue *parent_ids;
 		struct got_object_qid *qid;
 		int n = 1;
-		SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
+		parent_ids = got_object_commit_get_parent_ids(commit);
+		SIMPLEQ_FOREACH(qid, parent_ids, entry) {
 			err = got_object_id_str(&id_str, qid->id);
 			if (err)
 				return err;
@@ -384,7 +393,7 @@ print_commit(struct got_commit_object *commit, struct got_object_id *id,
 		}
 	}
 
-	logmsg0 = strdup(commit->logmsg);
+	logmsg0 = strdup(got_object_commit_get_logmsg(commit));
 	if (logmsg0 == NULL)
 		return got_error_from_errno();
 
diff --git a/include/got_object.h b/include/got_object.h
index 4381cdf..af264e3 100644
--- a/include/got_object.h
+++ b/include/got_object.h
@@ -19,6 +19,7 @@ struct got_object_id;
 struct got_blob_object;
 struct got_tree_object;
 struct got_tag_object;
+struct got_commit_object;
 
 struct got_tree_entry {
 	SIMPLEQ_ENTRY(got_tree_entry) entry;
@@ -45,21 +46,6 @@ const struct got_error *got_object_qid_alloc(struct got_object_qid **,
     struct got_object_id *);
 void got_object_qid_free(struct got_object_qid *);
 
-struct got_commit_object {
-	struct got_object_id *tree_id;
-	unsigned int nparents;
-	struct got_object_id_queue parent_ids;
-	char *author;
-	time_t author_time;	/* UTC */
-	time_t author_gmtoff;
-	char *committer;
-	time_t committer_time;	/* UTC */
-	time_t committer_gmtoff;
-	char *logmsg;
-
-	int refcnt; /* for internal use only */
-};
-
 /* A generic object. Used as a handle which holds an ID and an object type. */
 struct got_object;
 #define GOT_OBJ_TYPE_COMMIT		1
@@ -149,6 +135,37 @@ const struct got_error *got_object_commit_open(struct got_commit_object **,
 /* Dispose of a commit object. */
 void got_object_commit_close(struct got_commit_object *);
 
+/* Obtain the ID of the tree created in a commit. */
+struct got_object_id *got_object_commit_get_tree_id(struct got_commit_object *);
+
+/* Obtain the number of parent commits of a commit. */
+int got_object_commit_get_nparents(struct got_commit_object *);
+
+/* Obtain the list of parent commits of a commit. */
+const struct got_object_id_queue *got_object_commit_get_parent_ids(
+    struct got_commit_object *);
+
+/* Get the author's name and email address. */
+const char *got_object_commit_get_author(struct got_commit_object *);
+
+/* Get an author's commit timestamp. */
+time_t got_object_commit_get_author_time(struct got_commit_object *);
+
+/* Get an author's timezone offset. */
+time_t got_object_commit_get_author_gmtoff(struct got_commit_object *);
+
+/* Get the committer's name and email address. */
+const char *got_object_commit_get_committer(struct got_commit_object *);
+
+/* Get an committer's commit timestamp. */
+time_t got_object_commit_get_committer_time(struct got_commit_object *);
+
+/* Get an committer's timezone offset. */
+time_t got_object_commit_get_committer_gmtoff(struct got_commit_object *);
+
+/* Get the commit log message. */
+const char *got_object_commit_get_logmsg(struct got_commit_object *);
+
 /*
  * Attempt to open a tree object in a repository.
  * The provided object must be of type GOT_OBJ_TYPE_TREE.
diff --git a/lib/diff.c b/lib/diff.c
index 314a740..9af8a52 100644
--- a/lib/diff.c
+++ b/lib/diff.c
@@ -602,7 +602,8 @@ got_diff_objects_as_commits(struct got_object *obj1, struct got_object *obj2,
 		err = got_object_commit_open(&commit1, repo, obj1);
 		if (err)
 			goto done;
-		err = got_object_open(&tree_obj1, repo, commit1->tree_id);
+		err = got_object_open(&tree_obj1, repo,
+		    got_object_commit_get_tree_id(commit1));
 		if (err)
 			goto done;
 	}
@@ -610,7 +611,8 @@ got_diff_objects_as_commits(struct got_object *obj1, struct got_object *obj2,
 	err = got_object_commit_open(&commit2, repo, obj2);
 	if (err)
 		goto done;
-	err = got_object_open(&tree_obj2, repo, commit2->tree_id);
+	err = got_object_open(&tree_obj2, repo,
+	    got_object_commit_get_tree_id(commit2));
 	if (err)
 		goto done;
 
diff --git a/lib/got_lib_object.h b/lib/got_lib_object.h
index c261919..7bcbdfd 100644
--- a/lib/got_lib_object.h
+++ b/lib/got_lib_object.h
@@ -35,6 +35,20 @@ struct got_object {
 	int refcnt;		/* > 0 if open and/or cached */
 };
 
+struct got_commit_object {
+	struct got_object_id *tree_id;
+	unsigned int nparents;
+	struct got_object_id_queue parent_ids;
+	char *author;
+	time_t author_time;	/* UTC */
+	time_t author_gmtoff;
+	char *committer;
+	time_t committer_time;	/* UTC */
+	time_t committer_gmtoff;
+	char *logmsg;
+	int refcnt;		/* > 0 if open and/or cached */
+};
+
 struct got_tree_object {
 	struct got_tree_entries entries;
 	int refcnt;
diff --git a/lib/object_parse.c b/lib/object_parse.c
index 433a659..edf8f8d 100644
--- a/lib/object_parse.c
+++ b/lib/object_parse.c
@@ -275,6 +275,65 @@ got_object_commit_close(struct got_commit_object *commit)
 	free(commit);
 }
 
+struct got_object_id *
+got_object_commit_get_tree_id(struct got_commit_object *commit)
+{
+	return commit->tree_id;
+}
+
+int
+got_object_commit_get_nparents(struct got_commit_object *commit)
+{
+	return commit->nparents;
+}
+
+const struct got_object_id_queue *
+got_object_commit_get_parent_ids(struct got_commit_object *commit)
+{
+	return &commit->parent_ids;
+}
+
+const char *
+got_object_commit_get_author(struct got_commit_object *commit)
+{
+	return commit->author;
+}
+
+time_t
+got_object_commit_get_author_time(struct got_commit_object *commit)
+{
+	return commit->author_time;
+}
+
+time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
+{
+	return commit->author_gmtoff;
+}
+
+const char *
+got_object_commit_get_committer(struct got_commit_object *commit)
+{
+	return commit->committer;
+}
+
+time_t
+got_object_commit_get_committer_time(struct got_commit_object *commit)
+{
+	return commit->committer_time;
+}
+
+time_t
+got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
+{
+	return commit->committer_gmtoff;
+}
+
+const char *
+got_object_commit_get_logmsg(struct got_commit_object *commit)
+{
+	return commit->logmsg;
+}
+
 const struct got_error *
 got_object_parse_commit(struct got_commit_object **commit, char *buf, size_t len)
 {
diff --git a/regress/repository/repository_test.c b/regress/repository/repository_test.c
index faf7e0d..18bc93b 100644
--- a/regress/repository/repository_test.c
+++ b/regress/repository/repository_test.c
@@ -63,11 +63,13 @@ static const struct got_error *
 print_parent_commits(struct got_commit_object *commit,
     struct got_repository *repo)
 {
+	const struct got_object_id_queue *parent_ids;
 	struct got_object_qid *qid;
 	const struct got_error *err = NULL;
 	struct got_object *obj;
 
-	SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
+	parent_ids = got_object_commit_get_parent_ids(commit);
+	SIMPLEQ_FOREACH(qid, parent_ids, entry) {
 		err = got_object_open(&obj, repo, qid->id);
 		if (err != NULL)
 			return err;
@@ -148,6 +150,7 @@ static const struct got_error *
 print_commit_object(struct got_object *obj, struct got_repository *repo)
 {
 	struct got_commit_object *commit;
+	const struct got_object_id_queue *parent_ids;
 	struct got_object_qid *qid;
 	char *buf;
 	const struct got_error *err;
@@ -157,24 +160,27 @@ print_commit_object(struct got_object *obj, struct got_repository *repo)
 	if (err)
 		return err;
 
-	err = got_object_id_str(&buf, commit->tree_id);
+	err = got_object_id_str(&buf, got_object_commit_get_tree_id(commit));
 	if (err)
 		return err;
 	test_printf("tree: %s\n", buf);
 	free(buf);
-	test_printf("parent%s: ", (commit->nparents == 1) ? "" : "s");
-	SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
+	test_printf("parent%s: ",
+	    (got_object_commit_get_nparents(commit) == 1) ? "" : "s");
+	parent_ids = got_object_commit_get_parent_ids(commit);
+	SIMPLEQ_FOREACH(qid, parent_ids, entry) {
 		err = got_object_id_str(&buf, qid->id);
 		if (err)
 			return err;
 		test_printf("%s\n", buf);
 		free(buf);
 	}
-	test_printf("author: %s\n", commit->author);
-	test_printf("committer: %s\n", commit->committer);
-	test_printf("log: %s\n", commit->logmsg);
+	test_printf("author: %s\n", got_object_commit_get_author(commit));
+	test_printf("committer: %s\n", got_object_commit_get_committer(commit));
+	test_printf("log: %s\n", got_object_commit_get_logmsg(commit));
 
-	err = got_object_open(&treeobj, repo, commit->tree_id);
+	err = got_object_open(&treeobj, repo,
+	    got_object_commit_get_tree_id(commit));
 	if (err != NULL)
 		return err;
 	if (got_object_get_type(treeobj) == GOT_OBJ_TYPE_TREE) {
diff --git a/tog/tog.c b/tog/tog.c
index b0d09c8..90bfd89 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -875,8 +875,10 @@ draw_commit(struct tog_view *view, struct got_commit_object *commit,
 	static const size_t author_display_cols = 16;
 	const int avail = view->ncols;
 	struct tm tm;
+	time_t committer_time;
 
-	if (localtime_r(&commit->committer_time, &tm) == NULL)
+	committer_time = got_object_commit_get_committer_time(commit);
+	if (localtime_r(&committer_time, &tm) == NULL)
 		return got_error_from_errno();
 	if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
 	    >= sizeof(datebuf))
@@ -891,7 +893,7 @@ draw_commit(struct tog_view *view, struct got_commit_object *commit,
 	if (col > avail)
 		goto done;
 
-	author0 = strdup(commit->author);
+	author0 = strdup(got_object_commit_get_author(commit));
 	if (author0 == NULL) {
 		err = got_error_from_errno();
 		goto done;
@@ -919,7 +921,7 @@ draw_commit(struct tog_view *view, struct got_commit_object *commit,
 	if (col > avail)
 		goto done;
 
-	logmsg0 = strdup(commit->logmsg);
+	logmsg0 = strdup(got_object_commit_get_logmsg(commit));
 	if (logmsg0 == NULL) {
 		err = got_error_from_errno();
 		goto done;
@@ -1231,7 +1233,7 @@ open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
 	if (err)
 		return err;
 
-	parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
+	parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
 	if (parent_id) {
 		err = got_object_open(&obj1, repo, parent_id->id);
 		if (err)
@@ -1263,7 +1265,8 @@ browse_commit(struct tog_view **new_view, int begin_x,
 	struct got_tree_object *tree;
 	struct tog_view *tree_view;
 
-	err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
+	err = got_object_open_as_tree(&tree, repo,
+	    got_object_commit_get_tree_id(entry->commit));
 	if (err)
 		return err;
 
@@ -1830,6 +1833,8 @@ write_commit_info(struct got_object *obj, struct got_repository *repo,
 	char *id_str;
 	char datebuf[26];
 	struct got_commit_object *commit = NULL;
+	time_t committer_time;
+	const char *author, *committer;
 
 	err = got_object_id_str(&id_str, got_object_get_id(obj));
 	if (err)
@@ -1841,21 +1846,26 @@ write_commit_info(struct got_object *obj, struct got_repository *repo,
 		err = got_error_from_errno();
 		goto done;
 	}
-	if (fprintf(outfile, "from: %s\n", commit->author) < 0) {
+	if (fprintf(outfile, "from: %s\n",
+	    got_object_commit_get_author(commit)) < 0) {
 		err = got_error_from_errno();
 		goto done;
 	}
+	committer_time = got_object_commit_get_committer_time(commit);
 	if (fprintf(outfile, "date: %s UTC\n",
-	    get_datestr(&commit->committer_time, datebuf)) < 0) {
+	    get_datestr(&committer_time, datebuf)) < 0) {
 		err = got_error_from_errno();
 		goto done;
 	}
-	if (strcmp(commit->author, commit->committer) != 0 &&
-	    fprintf(outfile, "via: %s\n", commit->committer) < 0) {
+	author = got_object_commit_get_author(commit);
+	committer = got_object_commit_get_committer(commit);
+	if (strcmp(author, committer) != 0 &&
+	    fprintf(outfile, "via: %s\n", committer) < 0) {
 		err = got_error_from_errno();
 		goto done;
 	}
-	if (fprintf(outfile, "%s\n", commit->logmsg) < 0) {
+	if (fprintf(outfile, "%s\n",
+	    got_object_commit_get_logmsg(commit)) < 0) {
 		err = got_error_from_errno();
 		goto done;
 	}
@@ -1902,6 +1912,7 @@ create_diff(struct tog_diff_view_state *s)
 		    s->diff_context, s->repo, f);
 		break;
 	case GOT_OBJ_TYPE_COMMIT: {
+		const struct got_object_id_queue *parent_ids;
 		struct got_object_qid *pid;
 		struct got_commit_object *commit2;
 
@@ -1909,7 +1920,8 @@ create_diff(struct tog_diff_view_state *s)
 		if (err)
 			break;
 		/* Show commit info if we're diffing to a parent commit. */
-		SIMPLEQ_FOREACH(pid, &commit2->parent_ids, entry) {
+		parent_ids = got_object_commit_get_parent_ids(commit2);
+		SIMPLEQ_FOREACH(pid, parent_ids, entry) {
 			struct got_object_id *id1 = got_object_get_id(obj1);
 			if (got_object_id_cmp(id1, pid->id) == 0) {
 				write_commit_info(obj2, s->repo, f);
@@ -2400,7 +2412,7 @@ open_selected_commit(struct got_object **pobj, struct got_object **obj,
 	if (err)
 		goto done;
 
-	pid = SIMPLEQ_FIRST(&commit->parent_ids);
+	pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
 	if (pid) {
 		err = got_object_open(pobj, repo, pid->id);
 		if (err)
@@ -3468,7 +3480,8 @@ cmd_tree(int argc, char *argv[])
 	if (error != NULL)
 		goto done;
 
-	error = got_object_open_as_tree(&tree, repo, commit->tree_id);
+	error = got_object_open_as_tree(&tree, repo,
+	    got_object_commit_get_tree_id(commit));
 	if (error != NULL)
 		goto done;