Commit 84451b3ef755f3226d0d79af367632e5f3a830e7

Stefan Sperling 2018-07-10T13:17:00

implement incremental blame display for tog

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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
diff --git a/include/got_blame.h b/include/got_blame.h
index b53ca46..c787aad 100644
--- a/include/got_blame.h
+++ b/include/got_blame.h
@@ -14,5 +14,22 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+/*
+ * Write an annotated version of a file at a given in-repository path,
+ * as found in the commit specified by ID, to the specified output file.
+ */
 const struct got_error *got_blame(const char *, struct got_object_id *,
     struct got_repository *, FILE *);
+
+/*
+ * Like got_blame() but instead of generating an output file invoke
+ * a callback whenever an annotation has been computed for a line.
+ *
+ * The callback receives the provided void * argument, the total number
+ * of lines of the annotated file, a line number, and the ID of the commit
+ * which last changed this line.
+ */
+const struct got_error *got_blame_incremental(const char *,
+    struct got_object_id *, struct got_repository *,
+    const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
+    void *);
diff --git a/include/got_object.h b/include/got_object.h
index 52bd46f..16aa7b9 100644
--- a/include/got_object.h
+++ b/include/got_object.h
@@ -190,11 +190,13 @@ const struct got_error *got_object_blob_read_block(size_t *,
 
 /*
  * Read the entire content of a blob and write it to the specified file.
- * Flush and rewind the file as well, and indicate the amount of bytes
- * written in the size_t output argument.
+ * Flush and rewind the file as well. Indicate the amount of bytes
+ * written in the first size_t output argument, and the number of lines
+ * in the file in the second size_t output argument (NULL can be passed
+ * for either output argument).
  */
-const struct got_error *got_object_blob_dump_to_file(size_t *, FILE *,
-    struct got_blob_object *);
+const struct got_error *got_object_blob_dump_to_file(size_t *, size_t *,
+    FILE *, struct got_blob_object *);
 
 const struct got_error *
 got_object_open_as_commit(struct got_commit_object **,
diff --git a/lib/blame.c b/lib/blame.c
index 7f25ad2..1a2d03c 100644
--- a/lib/blame.c
+++ b/lib/blame.c
@@ -47,58 +47,32 @@ struct got_blame {
 };
 
 static const struct got_error *
-dump_blob_and_count_lines(size_t *nlines, FILE *outfile,
-    struct got_blob_object *blob)
+annotate_line(struct got_blame *blame, int lineno, struct got_object_id *id,
+    const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
+    void *arg)
 {
 	const struct got_error *err = NULL;
-	size_t len, hdrlen;
-	const uint8_t *buf;
-	int i;
-
-	hdrlen = got_object_blob_get_hdrlen(blob);
-	*nlines = 0;
-	do {
-		err = got_object_blob_read_block(&len, blob);
-		if (err)
-			return err;
-		if (len == 0)
-			break;
-		buf = got_object_blob_get_read_buf(blob);
-		for (i = 0; i < len; i++) {
-			if (buf[i] == '\n')
-				(*nlines)++;
-		}
-		/* Skip blob object header first time around. */
-		fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
-		hdrlen = 0;
-	} while (len != 0);
-
-
-	fflush(outfile);
-	rewind(outfile);
-
-	return NULL;
-}
-
-static void
-annotate_line(struct got_blame *blame, int lineno, struct got_object_id *id)
-{
 	struct got_blame_line *line;
 
 	if (lineno < 1 || lineno > blame->nlines)
-		return;
+		return got_error(GOT_ERR_RANGE);
 	
 	line = &blame->lines[lineno - 1];
 	if (line->annotated)
-		return;
+		return NULL;
 
 	memcpy(&line->id, id, sizeof(line->id));
 	line->annotated = 1;
+	if (cb)
+		err = cb(arg, blame->nlines, lineno, id);
+	return err;
 }
 
 static const struct got_error *
 blame_commit(struct got_blame *blame, struct got_object_id *id,
-    struct got_object_id *pid, const char *path, struct got_repository *repo)
+    struct got_object_id *pid, const char *path, struct got_repository *repo,
+    const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
+    void *arg)
 {
 	const struct got_error *err = NULL;
 	struct got_object *obj = NULL, *pobj = NULL;
@@ -152,8 +126,11 @@ blame_commit(struct got_blame *blame, struct got_object_id *id,
 			int a = change->cv.a;
 			int b = change->cv.b;
 			int lineno;
-			for (lineno = a; lineno <= b; lineno++)
-				annotate_line(blame, lineno, id);
+			for (lineno = a; lineno <= b; lineno++) {
+				err = annotate_line(blame, lineno, id, cb, arg);
+				if (err)
+					goto done;
+			}
 		}
 	}
 done:
@@ -179,7 +156,9 @@ blame_close(struct got_blame *blame)
 
 static const struct got_error *
 blame_open(struct got_blame **blamep, const char *path,
-    struct got_object_id *start_commit_id, struct got_repository *repo)
+    struct got_object_id *start_commit_id, struct got_repository *repo,
+    const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
+    void *arg)
 {
 	const struct got_error *err = NULL;
 	struct got_object *obj = NULL;
@@ -212,7 +191,8 @@ blame_open(struct got_blame **blamep, const char *path,
 		err = got_error_from_errno();
 		goto done;
 	}
-	err = dump_blob_and_count_lines(&blame->nlines, blame->f, blob);
+	err = got_object_blob_dump_to_file(NULL, &blame->nlines, blame->f,
+	    blob);
 	if (err)
 		goto done;
 
@@ -238,7 +218,7 @@ blame_open(struct got_blame **blamep, const char *path,
 		if (pid == NULL)
 			break;
 
-		err = blame_commit(blame, id, pid->id, path, repo);
+		err = blame_commit(blame, id, pid->id, path, repo, cb, arg);
 		if (err) {
 			if (err->code == GOT_ERR_ITER_COMPLETED)
 				err = NULL;
@@ -254,12 +234,15 @@ blame_open(struct got_blame **blamep, const char *path,
 		got_object_commit_close(commit);
 		err = got_object_open_as_commit(&commit, repo, id);
 		if (err)
-			break;
+			goto done;
 	}
 
 	/* Annotate remaining non-annotated lines with last commit. */
-	for (lineno = 1; lineno <= blame->nlines; lineno++)
-		annotate_line(blame, lineno, id);
+	for (lineno = 1; lineno <= blame->nlines; lineno++) {
+		err = annotate_line(blame, lineno, id, cb, arg);
+		if (err)
+			goto done;
+	}
 
 done:
 	free(id);
@@ -313,7 +296,7 @@ got_blame(const char *path, struct got_object_id *start_commit_id,
 	if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
 		return got_error_from_errno();
 
-	err = blame_open(&blame, abspath, start_commit_id, repo);
+	err = blame_open(&blame, abspath, start_commit_id, repo, NULL, NULL);
 	if (err) {
 		free(abspath);
 		return err;
@@ -346,3 +329,22 @@ got_blame(const char *path, struct got_object_id *start_commit_id,
 	free(abspath);
 	return err;
 }
+
+const struct got_error *
+got_blame_incremental(const char *path, struct got_object_id *commit_id,
+    struct got_repository *repo,
+    const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
+    void *arg)
+{
+	const struct got_error *err = NULL;
+	struct got_blame *blame;
+	char *abspath;
+
+	if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
+		return got_error_from_errno();
+
+	err = blame_open(&blame, abspath, commit_id, repo, cb, arg);
+	free(abspath);
+	blame_close(blame);
+	return err;
+}
diff --git a/lib/diff.c b/lib/diff.c
index abdee82..8fc337d 100644
--- a/lib/diff.c
+++ b/lib/diff.c
@@ -67,7 +67,7 @@ diff_blobs(struct got_blob_object *blob1, struct got_blob_object *blob2,
 	size1 = 0;
 	if (blob1) {
 		idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
-		err = got_object_blob_dump_to_file(&size1, f1, blob1);
+		err = got_object_blob_dump_to_file(&size1, NULL, f1, blob1);
 		if (err)
 			goto done;
 	} else
@@ -76,7 +76,7 @@ diff_blobs(struct got_blob_object *blob1, struct got_blob_object *blob2,
 	size2 = 0;
 	if (blob2) {
 		idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
-		err = got_object_blob_dump_to_file(&size2, f2, blob2);
+		err = got_object_blob_dump_to_file(&size2, NULL, f2, blob2);
 		if (err)
 			goto done;
 	} else
diff --git a/lib/object.c b/lib/object.c
index 4cf62dd..8bc417f 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -1438,13 +1438,19 @@ got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
 }
 
 const struct got_error *
-got_object_blob_dump_to_file(size_t *total_len, FILE *outfile,
-    struct got_blob_object *blob)
+got_object_blob_dump_to_file(size_t *total_len, size_t *nlines,
+    FILE *outfile, struct got_blob_object *blob)
 {
 	const struct got_error *err = NULL;
 	size_t len, hdrlen;
+	const uint8_t *buf;
+	int i;
+
+	if (total_len)
+		*total_len = 0;
+	if (nlines)
+		*nlines = 0;
 
-	*total_len = 0;
 	hdrlen = got_object_blob_get_hdrlen(blob);
 	do {
 		err = got_object_blob_read_block(&len, blob);
@@ -1452,10 +1458,17 @@ got_object_blob_dump_to_file(size_t *total_len, FILE *outfile,
 			return err;
 		if (len == 0)
 			break;
-		*total_len += len;
+		if (total_len)
+			*total_len += len;
+		buf = got_object_blob_get_read_buf(blob);
+		if (nlines) {
+			for (i = 0; i < len; i++) {
+				if (buf[i] == '\n')
+					(*nlines)++;
+			}
+		}
 		/* Skip blob object header first time around. */
-		fwrite(got_object_blob_get_read_buf(blob) + hdrlen,
-		    len - hdrlen, 1, outfile);
+		fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
 		hdrlen = 0;
 	} while (len != 0);
 
diff --git a/tog/Makefile b/tog/Makefile
index 76ebd6a..06bda66 100644
--- a/tog/Makefile
+++ b/tog/Makefile
@@ -7,7 +7,7 @@ SRCS=		tog.c blame.c commit_graph.c delta.c diff.c diffreg.c error.c \
 		sha1.c worktree.c utf8.c inflate.c
 
 CPPFLAGS = -I${.CURDIR}/../include -I${.CURDIR}/../lib
-LDADD = -lpanel -lncursesw -lutil -lz
+LDADD = -lpanel -lncursesw -lutil -lz -lpthread
 DPADD = ${LIBZ} ${LIBUTIL}
 .if defined(PROFILE)
 CC = gcc
diff --git a/tog/tog.c b/tog/tog.c
index c668240..343f4c6 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -33,6 +33,7 @@
 #include <limits.h>
 #include <wchar.h>
 #include <time.h>
+#include <pthread.h>
 
 #include "got_error.h"
 #include "got_object.h"
@@ -1048,24 +1049,173 @@ usage_blame(void)
 	exit(1);
 }
 
+struct tog_blame_line {
+	int annotated;
+	struct got_object_id *id;
+};
+
+static const struct got_error *
+draw_blame(WINDOW *window, FILE *f, struct tog_blame_line *lines, int nlines,
+    int *first_displayed_line, int *last_displayed_line, int *eof,
+    int max_lines)
+{
+	const struct got_error *err;
+	int lineno = 0, nprinted = 0;
+	char *line;
+	size_t len;
+	wchar_t *wline;
+	int width;
+	struct tog_blame_line *blame_line;
+
+	rewind(f);
+	werase(window);
+
+	*eof = 0;
+	while (nprinted < max_lines) {
+		line = parse_next_line(f, &len);
+		if (line == NULL) {
+			*eof = 1;
+			break;
+		}
+		if (++lineno < *first_displayed_line) {
+			free(line);
+			continue;
+		}
+
+		err = format_line(&wline, &width, line, COLS - 9);
+		if (err) {
+			free(line);
+			return err;
+		}
+
+		blame_line = &lines[lineno - 1];
+		if (blame_line->annotated) {
+			char *id_str;
+			err = got_object_id_str(&id_str, blame_line->id);
+			if (err) {
+				free(line);
+				return err;
+			}
+			wprintw(window, "%.8s ", id_str);
+			free(id_str);
+		} else
+			waddstr(window, "         ");
+
+		waddwstr(window, wline);
+		if (width < COLS - 9)
+			waddch(window, '\n');
+		if (++nprinted == 1)
+			*first_displayed_line = lineno;
+		free(line);
+	}
+	*last_displayed_line = lineno;
+
+	update_panels();
+	doupdate();
+
+	return NULL;
+}
+
+struct tog_blame_cb_args {
+	pthread_mutex_t *mutex;
+	struct tog_blame_line *lines; /* one per line */
+	int nlines;
+
+	FILE *f;
+	WINDOW *window;
+	int *first_displayed_line;
+	int *last_displayed_line;
+};
+
+static const struct got_error *
+blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
+{
+	const struct got_error *err = NULL;
+	struct tog_blame_cb_args *a = arg;
+	struct tog_blame_line *line;
+	int eof;
+
+	if (nlines != a->nlines || lineno < 1 || lineno > a->nlines)
+		return got_error(GOT_ERR_RANGE);
+
+	if (pthread_mutex_lock(a->mutex) != 0)
+		return got_error_from_errno();
+
+	line = &a->lines[lineno - 1];
+	line->id = got_object_id_dup(id);
+	if (line->id == NULL) {
+		err = got_error_from_errno();
+		goto done;
+	}
+	line->annotated = 1;
+
+	err = draw_blame(a->window, a->f, a->lines, a->nlines,
+	    a->first_displayed_line, a->last_displayed_line, &eof, LINES);
+done:
+	if (pthread_mutex_unlock(a->mutex) != 0)
+		return got_error_from_errno();
+	return err;
+}
+
+struct tog_blame_thread_args {
+	const char *path;
+	struct got_object_id *commit_id;
+	struct got_repository *repo;
+	void *blame_cb_args;
+};
+
+static void *
+blame_thread(void *arg)
+{
+	struct tog_blame_thread_args *a = arg;
+	return (void *)got_blame_incremental(a->path, a->commit_id, a->repo,
+	    blame_cb, a->blame_cb_args);
+}
+
 static const struct got_error *
 show_blame_view(const char *path, struct got_object_id *commit_id,
     struct got_repository *repo)
 {
 	const struct got_error *err = NULL;
-	FILE *f;
 	int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
 	int eof, i;
+	struct got_object *obj = NULL;
+	struct got_blob_object *blob = NULL;
+	FILE *f = NULL;
+	size_t filesize, nlines;
+	struct tog_blame_line *lines = NULL;
+	pthread_t thread = NULL;
+	pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+	struct tog_blame_cb_args blame_cb_args;
+	struct tog_blame_thread_args blame_thread_args;
+
+	err = got_object_open_by_path(&obj, repo, commit_id, path);
+	if (err)
+		goto done;
+	if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
+		err = got_error(GOT_ERR_OBJ_TYPE);
+		got_object_close(obj);
+		goto done;
+	}
 
+	err = got_object_blob_open(&blob, repo, obj, 8192);
+	got_object_close(obj);
+	if (err)
+		goto done;
 	f = got_opentemp();
-	if (f == NULL)
-		return got_error_from_errno();
-
-	err = got_blame(path, commit_id, repo, f);
+	if (f == NULL) {
+		err = got_error_from_errno();
+		goto done;
+	}
+	err = got_object_blob_dump_to_file(&filesize, &nlines, f, blob);
 	if (err)
 		goto done;
 
-	fflush(f);
+	lines = calloc(nlines, sizeof(*lines));
+	if (lines == NULL) {
+		err = got_error_from_errno();
+		goto done;
+	}
 
 	if (tog_blame_view.window == NULL) {
 		tog_blame_view.window = newwin(0, 0, 0, 0);
@@ -1080,14 +1230,49 @@ show_blame_view(const char *path, struct got_object_id *commit_id,
 	} else
 		show_panel(tog_blame_view.panel);
 
+	if (pthread_mutex_init(&mutex, NULL) != 0) {
+		err = got_error_from_errno();
+		goto done;
+	}
+	blame_cb_args.lines = lines;
+	blame_cb_args.nlines = nlines;
+	blame_cb_args.mutex = &mutex;
+	blame_cb_args.f = f;
+	blame_cb_args.window = tog_blame_view.window;
+	blame_cb_args.first_displayed_line = &first_displayed_line;
+	blame_cb_args.last_displayed_line = &last_displayed_line;
+
+	blame_thread_args.path = path;
+	blame_thread_args.commit_id = commit_id;
+	blame_thread_args.repo = repo;
+	blame_thread_args.blame_cb_args = &blame_cb_args;
+
+	if (pthread_create(&thread, NULL, blame_thread,
+	    &blame_thread_args) != 0) {
+		err = got_error_from_errno();
+		goto done;
+	}
+
 	while (!done) {
-		err = draw_file(tog_blame_view.window, f, &first_displayed_line,
-		    &last_displayed_line, &eof, LINES);
+		if (pthread_mutex_lock(&mutex) != 0) {
+			err = got_error_from_errno();
+			goto done;
+		}
+		err = draw_blame(tog_blame_view.window, f, lines, nlines,
+		    &first_displayed_line, &last_displayed_line, &eof, LINES);
+		if (pthread_mutex_unlock(&mutex) != 0) {
+			err = got_error_from_errno();
+			goto done;
+		}
 		if (err)
 			break;
 		nodelay(stdscr, FALSE);
 		ch = wgetch(tog_blame_view.window);
 		nodelay(stdscr, TRUE);
+		if (pthread_mutex_lock(&mutex) != 0) {
+			err = got_error_from_errno();
+			goto done;
+		}
 		switch (ch) {
 			case 'q':
 				done = 1;
@@ -1124,9 +1309,21 @@ show_blame_view(const char *path, struct got_object_id *commit_id,
 			default:
 				break;
 		}
+		if (pthread_mutex_unlock(&mutex) != 0) {
+			err = got_error_from_errno();
+			goto done;
+		}
 	}
 done:
-	fclose(f);
+	if (blob)
+		got_object_blob_close(blob);
+	if (f)
+		fclose(f);
+	free(lines);
+	if (thread) {
+		if (pthread_join(thread, (void **)&err) != 0)
+			err = got_error_from_errno();
+	}
 	return err;
 }