Commit 899d86c25715751ba2eb6b328649331c079f3b07

Stefan Sperling 2018-05-10T13:10:36

make tog store all first-parent commits, not just those displayed

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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
diff --git a/tog/tog.c b/tog/tog.c
index 30c2779..edb8d47 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -195,30 +195,19 @@ struct commit_queue_entry {
 };
 TAILQ_HEAD(commit_queue, commit_queue_entry);
 
-static const struct got_error *
-push_commit(struct commit_queue_entry **entryp, struct commit_queue *commits,
-    struct got_commit_object *commit, struct got_object_id *id)
+static struct commit_queue_entry *
+alloc_commit_queue_entry(struct got_commit_object *commit,
+    struct got_object_id *id)
 {
-	const struct got_error *err = NULL;
 	struct commit_queue_entry *entry;
 
-	*entryp = NULL;
-
 	entry = calloc(1, sizeof(*entry));
 	if (entry == NULL)
-		return got_error_from_errno();
-
-	entry->id = got_object_id_dup(id);
-	if (entry->id == NULL) {
-		err = got_error_from_errno();
-		free(entry);
-		return err;
-	}
+		return NULL;
 
+	entry->id = id;
 	entry->commit = commit;
-	TAILQ_INSERT_HEAD(commits, entry, entry);
-	*entryp = entry;
-	return NULL;
+	return entry;
 }
 
 static void
@@ -242,18 +231,16 @@ free_commits(struct commit_queue *commits)
 
 
 static const struct got_error *
-fetch_next_commit(struct commit_queue_entry **pentryp,
-    struct commit_queue *commits, struct got_repository *repo)
+fetch_parent_commit(struct commit_queue_entry **pentry,
+    struct commit_queue_entry *entry, struct got_repository *repo)
 {
-	const struct got_error *err;
+	const struct got_error *err = NULL;
 	struct got_object *obj = NULL;
-	struct got_commit_object *pcommit;
-	struct commit_queue_entry *entry, *pentry;
+	struct got_commit_object *commit;
+	struct got_object_id *id;
 	struct got_parent_id *pid;
 
-	if (pentryp)
-		*pentryp = NULL;
-	entry = TAILQ_LAST(commits, commit_queue);
+	*pentry = NULL;
 
 	/* Follow the first parent (TODO: handle merge commits). */
 	pid = SIMPLEQ_FIRST(&entry->commit->parent_ids);
@@ -268,60 +255,194 @@ fetch_next_commit(struct commit_queue_entry **pentryp,
 		return err;
 	}
 
-	err = got_object_commit_open(&pcommit, repo, obj);
+	err = got_object_commit_open(&commit, repo, obj);
 	got_object_close(obj);
 	if (err)
 		return err;
 
-	pentry = calloc(1, sizeof(*pentry));
-	if (pentry == NULL) {
+	id = got_object_id_dup(pid->id);
+	if (id == NULL) {
 		err = got_error_from_errno();
-		got_object_commit_close(pcommit);
-		return err;
+		got_object_commit_close(commit);
+		return err;;
 	}
-	pentry->id = got_object_id_dup(pid->id);
-	if (pentry->id == NULL) {
+
+	*pentry = alloc_commit_queue_entry(commit, id);
+	if (*pentry == NULL) {
 		err = got_error_from_errno();
-		got_object_commit_close(pcommit);
-		return err;;
+		got_object_commit_close(commit);
+	}
+
+	return err;;
+}
+
+static const struct got_error *
+get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
+{
+	const struct got_error *err = NULL;
+	struct got_reference *head_ref;
+
+	*head_id = NULL;
+
+	err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
+	if (err)
+		return err;
+
+	err = got_ref_resolve(head_id, repo, head_ref);
+	got_ref_close(head_ref);
+	if (err) {
+		*head_id = NULL;
+		return err;
 	}
-	pentry->commit = pcommit;
-	TAILQ_INSERT_TAIL(commits, pentry, entry);
-	if (pentryp)
-		*pentryp = pentry;
 
 	return NULL;
 }
 
 static const struct got_error *
-fetch_commits(struct commit_queue *commits, struct got_object *root_obj,
-    struct got_object_id *root_id, struct got_repository *repo, int limit)
+prepend_commits(int *ncommits, struct commit_queue *commits,
+    struct got_object_id *first_id, struct got_object_id *last_id,
+    int limit, struct got_repository *repo)
+{
+	const struct got_error *err = NULL;
+	struct got_object *first_obj = NULL, *last_obj = NULL;
+	struct got_commit_object *commit = NULL;
+	struct got_object_id *id = NULL;
+	struct commit_queue_entry *entry, *old_head_entry;
+
+	*ncommits = 0;
+
+	err = got_object_open(&first_obj, repo, first_id);
+	if (err)
+		goto done;
+	if (got_object_get_type(first_obj) != GOT_OBJ_TYPE_COMMIT) {
+		err = got_error(GOT_ERR_OBJ_TYPE);
+		goto done;
+	}
+	err = got_object_open(&last_obj, repo, last_id);
+	if (err)
+		goto done;
+	if (got_object_get_type(last_obj) != GOT_OBJ_TYPE_COMMIT) {
+		err = got_error(GOT_ERR_OBJ_TYPE);
+		goto done;
+	}
+
+	err = got_object_commit_open(&commit, repo, first_obj);
+	if (err)
+		goto done;
+
+	id = got_object_id_dup(first_id);
+	if (id == NULL) {
+		err = got_error_from_errno();
+		goto done;
+	}
+
+	entry = alloc_commit_queue_entry(commit, id);
+	if (entry == NULL)
+		return got_error_from_errno();
+
+	old_head_entry = TAILQ_FIRST(commits);
+	if (old_head_entry)
+		TAILQ_INSERT_BEFORE(old_head_entry, entry, entry);
+	else
+		TAILQ_INSERT_HEAD(commits, entry, entry);
+
+	*ncommits = 1;
+
+	/*
+	 * Fetch parent commits.
+	 * XXX If first and last commit aren't ancestrally related this loop
+	 * we will keep iterating until a root commit is encountered.
+	 */
+	while (1) {
+		struct commit_queue_entry *pentry;
+
+		err = fetch_parent_commit(&pentry, entry, repo);
+		if (err)
+			goto done;
+		if (pentry == NULL)
+			break;
+
+		/*
+		 * Fill up to old HEAD commit if commit queue was not empty.
+		 * We must not leave a gap in history.
+		 */
+		if (old_head_entry &&
+		    got_object_id_cmp(pentry->id, old_head_entry->id) == 0)
+			break;
+
+		TAILQ_INSERT_AFTER(commits, entry, pentry, entry);
+		(*ncommits)++;
+		if (*ncommits >= limit)
+			break;
+
+		/* Fill up to last requested commit if queue was empty. */
+		if (old_head_entry == NULL &&
+		    got_object_id_cmp(pentry->id, last_id) == 0)
+			break;
+
+		entry = pentry;
+	}
+
+done:
+	if (first_obj)
+		got_object_close(first_obj);
+	if (last_obj)
+		got_object_close(last_obj);
+	return err;
+}
+
+static const struct got_error *
+fetch_commits(struct commit_queue_entry **start_entry,
+    struct got_object_id *start_id, struct commit_queue *commits,
+    int limit, struct got_repository *repo)
 {
 	const struct got_error *err;
-	struct got_commit_object *root_commit;
 	struct commit_queue_entry *entry;
 	int ncommits = 0;
+	struct got_object_id *head_id = NULL;
 
-	err = got_object_commit_open(&root_commit, repo, root_obj);
+	*start_entry = NULL;
+
+	err = get_head_commit_id(&head_id, repo);
 	if (err)
 		return err;
 
-	err = push_commit(&entry, commits, root_commit, root_id);
+	/* Prepend HEAD commit and all ancestors up to start commit. */
+	err = prepend_commits(&ncommits, commits, head_id, start_id, limit,
+	    repo);
 	if (err)
 		return err;
 
-	while (entry->commit->nparents > 0 && ncommits < limit) {
-		err = fetch_next_commit(&entry, commits, repo);
+	if (got_object_id_cmp(head_id, start_id) == 0)
+		*start_entry = TAILQ_FIRST(commits);
+	else
+		*start_entry = TAILQ_LAST(commits, commit_queue);
+
+	if (ncommits >= limit)
+		return NULL;
+
+	/* Append more commits from start commit up to the requested limit. */
+	entry = TAILQ_LAST(commits, commit_queue);
+	while (entry && ncommits < limit) {
+		struct commit_queue_entry *pentry;
+
+		err = fetch_parent_commit(&pentry, entry, repo);
 		if (err)
 			break;
+		if (pentry)
+			TAILQ_INSERT_TAIL(commits, pentry, entry);
+		entry = pentry;
 		ncommits++;
 	}
 
+	if (err)
+		*start_entry = NULL;
 	return err;
 }
 
 static const struct got_error *
-draw_commits(struct commit_queue *commits, int selected)
+draw_commits(struct commit_queue_entry **last,
+    struct commit_queue_entry *first, int selected, int limit)
 {
 	const struct got_error *err = NULL;
 	struct commit_queue_entry *entry;
@@ -329,7 +450,11 @@ draw_commits(struct commit_queue *commits, int selected)
 
 	wclear(tog_log_view.window);
 
-	TAILQ_FOREACH(entry, commits, entry) {
+	entry = first;
+	*last = first;
+	while (entry) {
+		if (ncommits == limit)
+			break;
 		if (ncommits == selected)
 			wstandout(tog_log_view.window);
 		err = draw_commit(entry->commit, entry->id);
@@ -338,6 +463,8 @@ draw_commits(struct commit_queue *commits, int selected)
 		if (err)
 			break;
 		ncommits++;
+		*last = entry;
+		entry = TAILQ_NEXT(entry, entry);
 	}
 
 	update_panels();
@@ -347,36 +474,15 @@ draw_commits(struct commit_queue *commits, int selected)
 }
 
 static const struct got_error *
-get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
-{
-	const struct got_error *err = NULL;
-	struct got_reference *head_ref;
-
-	*head_id = NULL;
-
-	err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
-	if (err)
-		return err;
-
-	err = got_ref_resolve(head_id, repo, head_ref);
-	got_ref_close(head_ref);
-	if (err) {
-		*head_id = NULL;
-		return err;
-	}
-
-	return NULL;
-}
-
-static const struct got_error *
 show_log_view(struct got_object_id *start_id, struct got_repository *repo)
 {
 	const struct got_error *err = NULL;
-	struct got_object *obj = NULL;
 	struct got_object_id *id;
-	int ch, done = 0, selected = 0, refetch_commits = 1;
+	int ch, done = 0, selected = 0;
 	struct commit_queue commits;
-	struct commit_queue_entry *entry = NULL;
+	struct commit_queue_entry *pentry = NULL;
+	struct commit_queue_entry *first_displayed_entry = NULL;
+	struct commit_queue_entry *last_displayed_entry = NULL;
 
 	id = got_object_id_dup(start_id);
 	if (id == NULL)
@@ -394,25 +500,13 @@ show_log_view(struct got_object_id *start_id, struct got_repository *repo)
 			return got_error_from_errno();
 	}
 
-	err = got_object_open(&obj, repo, start_id);
+	TAILQ_INIT(&commits);
+	err = fetch_commits(&first_displayed_entry, id, &commits, LINES, repo);
 	if (err)
-		return err;
-	if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
-		err = got_error(GOT_ERR_OBJ_TYPE);
 		goto done;
-	}
-
-	TAILQ_INIT(&commits);
-	do {
-		if (refetch_commits) {
-			free_commits(&commits);
-			err = fetch_commits(&commits, obj, id, repo, LINES - 1);
-			refetch_commits = 0;
-			if (err)
-				goto done;
-		}
-
-		err = draw_commits(&commits, selected);
+	while (!done) {
+		err = draw_commits(&last_displayed_entry, first_displayed_entry,
+		    selected, LINES);
 		if (err)
 			goto done;
 
@@ -441,37 +535,19 @@ show_log_view(struct got_object_id *start_id, struct got_repository *repo)
 					break;
 
 				/* scroll down if there are more parents */
-				entry = TAILQ_LAST(&commits, commit_queue);
-				if (entry->commit->nparents == 0)
+				if (last_displayed_entry->commit->nparents == 0)
 					break;
-				pop_commit(&commits);
-				if (TAILQ_EMPTY(&commits)) {
-					refetch_commits = 1;
-					break;
-				}
-				entry = TAILQ_FIRST(&commits);
-				id = got_object_id_dup(entry->id);
-				if (id == NULL) {
-					err = got_error_from_errno();
-					goto done;
-				}
-				got_object_close(obj);
-				err = got_object_open(&obj, repo, id);
-				if (err) {
-					obj = NULL;
-					goto done;
-				}
-				if (got_object_get_type(obj) !=
-				    GOT_OBJ_TYPE_COMMIT) {
-					err = got_error(GOT_ERR_OBJ_TYPE);
-					goto done;
-				}
-				err = fetch_next_commit(NULL, &commits, repo);
+				first_displayed_entry =
+				    TAILQ_NEXT(first_displayed_entry, entry);
+				err = fetch_parent_commit(&pentry,
+				    last_displayed_entry, repo);
 				if (err)
-					goto done;
+					break;
+				if (pentry)
+					TAILQ_INSERT_TAIL(&commits, pentry,
+					    entry);
 				break;
 			case KEY_RESIZE:
-				refetch_commits = 1;
 				if (selected > LINES)
 					selected = LINES - 1;
 				break;
@@ -479,11 +555,9 @@ show_log_view(struct got_object_id *start_id, struct got_repository *repo)
 				break;
 		}
 		nodelay(stdscr, TRUE);
-	} while (!done);
+	}
 done:
 	free_commits(&commits);
-	if (obj)
-		got_object_close(obj);
 	return err;
 }
 
@@ -492,7 +566,7 @@ cmd_log(int argc, char *argv[])
 {
 	const struct got_error *error;
 	struct got_repository *repo;
-	struct got_object_id *id = NULL;
+	struct got_object_id *start_id = NULL;
 	char *repo_path = NULL;
 	char *start_commit = NULL;
 	int ch;
@@ -533,22 +607,22 @@ cmd_log(int argc, char *argv[])
 		return error;
 
 	if (start_commit == NULL) {
-		error = get_head_commit_id(&id, repo);
+		error = get_head_commit_id(&start_id, repo);
 		if (error != NULL)
 			return error;
 	} else {
 		struct got_object *obj;
 		error = got_object_open_by_id_str(&obj, repo, start_commit);
 		if (error == NULL) {
-			id = got_object_get_id(obj);
-			if (id == NULL)
+			start_id = got_object_get_id(obj);
+			if (start_id == NULL)
 				error = got_error_from_errno();
 		}
 	}
 	if (error != NULL)
 		return error;
-	error = show_log_view(id, repo);
-	free(id);
+	error = show_log_view(start_id, repo);
+	free(start_id);
 	got_repo_close(repo);
 	return error;
 }