Commit 404c43c4fa13923747c649f395f5b2bcb9fcd484

Stefan Sperling 2018-06-21T23:22:57

basic 'got blame' implementation

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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
diff --git a/got/Makefile b/got/Makefile
index f3bd403..4ca05ec 100644
--- a/got/Makefile
+++ b/got/Makefile
@@ -1,7 +1,7 @@
 .PATH:${.CURDIR}/../lib
 
 PROG=		got
-SRCS=		got.c commit_graph.c delta.c diff.c diffreg.c error.c \
+SRCS=		got.c blame.c commit_graph.c delta.c diff.c diffreg.c error.c \
 		fileindex.c object.c object_idset.c opentemp.c path.c \
 		pack.c privsep.c reference.c repository.c sha1.c \
 		worktree.c zbuf.c
diff --git a/got/got.c b/got/got.c
index cdf523b..bbdd8fe 100644
--- a/got/got.c
+++ b/got/got.c
@@ -36,6 +36,7 @@
 #include "got_worktree.h"
 #include "got_diff.h"
 #include "got_commit_graph.h"
+#include "got_blame.h"
 
 #ifndef nitems
 #define nitems(_a)	(sizeof((_a)) / sizeof((_a)[0]))
@@ -52,10 +53,12 @@ __dead static void	usage(void);
 __dead static void	usage_checkout(void);
 __dead static void	usage_log(void);
 __dead static void	usage_diff(void);
+__dead static void	usage_blame(void);
 
 static const struct got_error*		cmd_checkout(int, char *[]);
 static const struct got_error*		cmd_log(int, char *[]);
 static const struct got_error*		cmd_diff(int, char *[]);
+static const struct got_error*		cmd_blame(int, char *[]);
 #ifdef notyet
 static const struct got_error*		cmd_status(int, char *[]);
 #endif
@@ -67,6 +70,8 @@ static struct cmd got_commands[] = {
 	    "show repository history" },
 	{ "diff",	cmd_diff,	usage_diff,
 	    "compare files and directories" },
+	{ "blame",	cmd_blame,	usage_blame,
+	    " show when lines in a file were changed" },
 #ifdef notyet
 	{ "status",	cmd_status,	usage_status,
 	    "show modification status of files" },
@@ -650,6 +655,92 @@ done:
 	return error;
 }
 
+__dead static void
+usage_blame(void)
+{
+	fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
+	    getprogname());
+	exit(1);
+}
+
+static const struct got_error *
+cmd_blame(int argc, char *argv[])
+{
+	const struct got_error *error;
+	struct got_repository *repo = NULL;
+	char *repo_path = NULL;
+	char *path = NULL;
+	struct got_object_id *commit_id = NULL;
+	char *commit_id_str = NULL;
+	int ch;
+
+#ifndef PROFILE
+	if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
+		err(1, "pledge");
+#endif
+
+	while ((ch = getopt(argc, argv, "c:")) != -1) {
+		switch (ch) {
+		case 'c':
+			commit_id_str = optarg;
+			break;
+		default:
+			usage();
+			/* NOTREACHED */
+		}
+	}
+
+	argc -= optind;
+	argv += optind;
+
+	if (argc == 0) {
+		usage_blame();
+	} else if (argc == 1) {
+		repo_path = getcwd(NULL, 0);
+		if (repo_path == NULL)
+			return got_error_from_errno();
+		path = argv[0];
+	} else if (argc == 2) {
+		repo_path = realpath(argv[0], NULL);
+		if (repo_path == NULL)
+			return got_error_from_errno();
+		path = argv[1];
+	} else
+		usage_blame();
+
+	error = got_repo_open(&repo, repo_path);
+	free(repo_path);
+	if (error != NULL)
+		goto done;
+
+	if (commit_id_str == NULL) {
+		struct got_reference *head_ref;
+		error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
+		if (error != NULL)
+			return error;
+		error = got_ref_resolve(&commit_id, repo, head_ref);
+		got_ref_close(head_ref);
+		if (error != NULL)
+			return error;
+	} else {
+		struct got_object *obj;
+		error = got_object_open_by_id_str(&obj, repo, commit_id_str);
+		if (error != NULL)
+			return error;
+		commit_id = got_object_get_id(obj);
+		if (commit_id == NULL)
+			error = got_error_from_errno();
+		got_object_close(obj);
+	}
+
+	error = got_blame(path, commit_id, repo, stdout);
+done:
+	free(commit_id);
+	if (repo)
+		got_repo_close(repo);
+	return error;
+}
+
 #ifdef notyet
 static const struct got_error *
 cmd_status(int argc __unused, char *argv[] __unused)
diff --git a/lib/blame.c b/lib/blame.c
new file mode 100644
index 0000000..0f683c8
--- /dev/null
+++ b/lib/blame.c
@@ -0,0 +1,363 @@
+/*
+ * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/queue.h>
+#include <sys/stat.h>
+
+#include <sha1.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <util.h>
+#include <zlib.h>
+
+#include "got_error.h"
+#include "got_object.h"
+#include "got_blame.h"
+#include "got_opentemp.h"
+
+#include "got_lib_zbuf.h"
+#include "got_lib_delta.h"
+#include "got_lib_object.h"
+#include "got_lib_diff.h"
+
+struct got_blame_line {
+	int annotated;
+	struct got_object_id id; /* one per line */
+};
+
+struct got_blame {
+	FILE *f;
+	size_t nlines;
+	struct got_blame_line *lines; /* one per line */
+};
+
+static const struct got_error *
+dump_blob_and_count_lines(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;
+
+	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 const struct got_error *
+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 got_error(GOT_ERR_RANGE);
+	
+	line = &blame->lines[lineno - 1];
+	if (line->annotated)
+		return NULL;
+
+	memcpy(&line->id, id, sizeof(line->id));
+	line->annotated = 1;
+	return NULL;
+}
+
+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)
+{
+	const struct got_error *err = NULL;
+	struct got_object *obj = NULL, *pobj = NULL;
+	struct got_blob_object *blob = NULL, *pblob = NULL;
+	struct got_diff_changes *changes = NULL;
+
+	err = got_object_open_by_path(&obj, repo, id, path);
+	if (err)
+		goto done;
+	if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
+		err = got_error(GOT_ERR_OBJ_TYPE);
+		goto done;
+	}
+
+	err = got_object_open_by_path(&pobj, repo, pid, path);
+	if (err) {
+		if (err->code == GOT_ERR_NO_OBJ) {
+			/* Blob's history began in previous commit. */
+			err = got_error(GOT_ERR_ITER_COMPLETED);
+		}
+		goto done;
+	}
+	if (got_object_get_type(pobj) != GOT_OBJ_TYPE_BLOB) {
+		/*
+		 * Encountered a non-blob at the path (probably a tree).
+		 * Blob's history began in previous commit.
+		 */
+		err = got_error(GOT_ERR_ITER_COMPLETED);
+		goto done;
+	}
+
+	/* If blob hashes match then don't bother with diffing. */
+	if (got_object_id_cmp(&obj->id, &pobj->id) == 0)
+		goto done;
+
+	err = got_object_blob_open(&blob, repo, obj, 8192);
+	if (err)
+		goto done;
+
+	err = got_object_blob_open(&pblob, repo, pobj, 8192);
+	if (err)
+		goto done;
+
+	err = got_diff_blob_lines_changed(&changes, blob, pblob);
+	if (err)
+		goto done;
+
+	if (changes) {
+		struct got_diff_change *change;
+		char *id_str;
+		err = got_object_id_str(&id_str, id);
+		if (err)
+			goto done;
+
+		SIMPLEQ_FOREACH(change, &changes->entries, entry) {
+			int a = change->cv.a;
+			int b = change->cv.b;
+			int lineno;
+			for (lineno = a; lineno <= b; lineno++) {
+				err = annotate_line(blame, lineno, id);
+				if (err)
+					goto done;
+			}
+		}
+		free(id_str);
+	}
+done:
+	if (obj)
+		got_object_close(obj);
+	if (pobj)
+		got_object_close(pobj);
+	if (blob)
+		got_object_blob_close(blob);
+	if (pblob)
+		got_object_blob_close(pblob);
+	return err;
+}
+
+static void
+blame_close(struct got_blame *blame)
+{
+	if (blame->f)
+		fclose(blame->f);
+	free(blame->lines);
+	free(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)
+{
+	const struct got_error *err = NULL;
+	struct got_object *obj = NULL;
+	struct got_blob_object *blob = NULL;
+	struct got_blame *blame = NULL;
+	struct got_commit_object *commit = NULL;
+	struct got_object_id *id = NULL;
+	int lineno;
+
+	*blamep = NULL;
+
+	err = got_object_open_by_path(&obj, repo, start_commit_id, path);
+	if (err)
+		return err;
+	if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
+		err = got_error(GOT_ERR_OBJ_TYPE);
+		goto done;
+	}
+
+	err = got_object_blob_open(&blob, repo, obj, 8192);
+	if (err)
+		goto done;
+
+	blame = calloc(1, sizeof(*blame));
+	if (blame == NULL)
+		return got_error_from_errno();
+
+	blame->f = got_opentemp();
+	if (blame->f == NULL) {
+		err = got_error_from_errno();
+		goto done;
+	}
+	err = dump_blob_and_count_lines(&blame->nlines, blame->f, blob);
+	if (err)
+		goto done;
+
+	blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
+	if (blame->lines == NULL) {
+		err = got_error_from_errno();
+		goto done;
+	}
+
+	/* Loop over first-parent history and try to blame commits. */
+	err = got_object_open_as_commit(&commit, repo, start_commit_id);
+	if (err)
+		goto done;
+	id = got_object_id_dup(start_commit_id);
+	if (id == NULL) {
+		err = got_error_from_errno();
+		goto done;
+	}
+	while (1) {
+		struct got_object_qid *pid;
+		struct got_commit_object *pcommit;
+
+		pid = SIMPLEQ_FIRST(&commit->parent_ids);
+		if (pid == NULL)
+			break;
+
+		err = got_object_open_as_commit(&pcommit, repo, pid->id);
+		if (err)
+			break;
+
+		err = blame_commit(blame, id, pid->id, path, repo);
+		if (err) {
+			if (err->code == GOT_ERR_ITER_COMPLETED)
+				err = NULL;
+			got_object_commit_close(pcommit);
+			break;
+		}
+		free(id);
+		id = got_object_id_dup(pid->id);
+		got_object_commit_close(commit);
+		commit = pcommit;
+		if (id == NULL) {
+			err = got_error_from_errno();
+			goto done;
+		}
+	}
+
+	/* Annotate remaining non-annotated lines with last commit. */
+	for (lineno = 1; lineno < blame->nlines; lineno++) {
+		err = annotate_line(blame, lineno, id);
+		if (err)
+			break;
+	}
+
+done:
+	free(id);
+	if (obj)
+		got_object_close(obj);
+	if (blob)
+		got_object_blob_close(blob);
+	if (commit)
+		got_object_commit_close(commit);
+	if (err)
+		blame_close(blame);
+	else
+		*blamep = blame;
+
+	return err;
+}
+
+static const struct got_error *
+blame_line(struct got_object_id **id, struct got_blame *blame, int lineno)
+{
+	if (lineno < 1 || lineno > blame->nlines)
+		return got_error(GOT_ERR_RANGE);
+	*id = &blame->lines[lineno - 1].id;
+	return NULL;
+}
+
+static char *
+parse_next_line(FILE *f, size_t *len)
+{
+	char *line;
+	size_t linelen;
+	size_t lineno;
+	const char delim[3] = { '\0', '\0', '\0'};
+
+	line = fparseln(f, &linelen, &lineno, delim, 0);
+	if (len)
+		*len = linelen;
+	return line;
+}
+
+const struct got_error *
+got_blame(const char *path, struct got_object_id *start_commit_id,
+    struct got_repository *repo, FILE *outfile)
+{
+	const struct got_error *err = NULL;
+	struct got_blame *blame;
+	int lineno;
+	char *abspath;
+
+	if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
+		return got_error_from_errno();
+
+	err = blame_open(&blame, abspath, start_commit_id, repo);
+	if (err) {
+		free(abspath);
+		return err;
+	}
+
+	for (lineno = 1; lineno < blame->nlines; lineno++) {
+		struct got_object_id *id;
+		char *line, *id_str;
+		
+		line = parse_next_line(blame->f, NULL);
+		if (line == NULL)
+			break;
+
+		err = blame_line(&id, blame, lineno);
+		if (err)
+			break;
+
+		err = got_object_id_str(&id_str, id);
+		if (err) {
+			free(line);
+			break;
+		}
+
+		fprintf(outfile, "%.8s %s\n", id_str, line);
+		free(line);
+		free(id_str);
+	}
+
+	blame_close(blame);
+	free(abspath);
+	return err;
+}
diff --git a/lib/diff.c b/lib/diff.c
index 2fb482f..e4f7dbb 100644
--- a/lib/diff.c
+++ b/lib/diff.c
@@ -33,9 +33,10 @@
 #include "got_lib_diff.h"
 #include "got_lib_path.h"
 
-const struct got_error *
-got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
-    const char *label1, const char *label2, FILE *outfile)
+static const struct got_error *
+diff_blobs(struct got_blob_object *blob1, struct got_blob_object *blob2,
+    const char *label1, const char *label2, FILE *outfile,
+    struct got_diff_changes *changes)
 {
 	struct got_diff_state ds;
 	struct got_diff_args args;
@@ -100,7 +101,7 @@ got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
 	args.diff_context = 3;
 	flags |= D_PROTOTYPE;
 
-	err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile);
+	err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile, changes);
 done:
 	if (f1)
 		fclose(f1);
@@ -109,6 +110,44 @@ done:
 	return err;
 }
 
+const struct got_error *
+got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
+    const char *label1, const char *label2, FILE *outfile)
+{
+	return diff_blobs(blob1, blob2, label1, label2, outfile, NULL);
+}
+
+const struct got_error *
+got_diff_blob_lines_changed(struct got_diff_changes **changes,
+    struct got_blob_object *blob1, struct got_blob_object *blob2)
+{
+	const struct got_error *err = NULL;
+
+	*changes = calloc(1, sizeof(**changes));
+	if (*changes == NULL)
+		return got_error_from_errno();
+	SIMPLEQ_INIT(&(*changes)->entries);
+
+	err = diff_blobs(blob1, blob2, NULL, NULL, NULL, *changes);
+	if (err) {
+		got_diff_free_changes(*changes);
+		*changes = NULL;
+	}
+	return err;
+}
+
+void
+got_diff_free_changes(struct got_diff_changes *changes)
+{
+	struct got_diff_change *change;
+	while (!SIMPLEQ_EMPTY(&changes->entries)) {
+		change = SIMPLEQ_FIRST(&changes->entries);
+		SIMPLEQ_REMOVE_HEAD(&changes->entries, entry);
+		free(change);
+	}
+	free(changes);
+}
+
 struct got_tree_entry *
 match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
 {
diff --git a/lib/diffreg.c b/lib/diffreg.c
index bae6487..b2fe324 100644
--- a/lib/diffreg.c
+++ b/lib/diffreg.c
@@ -171,29 +171,17 @@ struct line {
 	int	value;
 };
 
-/*
- * The following struct is used to record change information when
- * doing a "context" or "unified" diff.  (see routine "change" to
- * understand the highly mnemonic field names)
- */
-struct context_vec {
-	int	a;		/* start line in old file */
-	int	b;		/* end line in old file */
-	int	c;		/* start line in new file */
-	int	d;		/* end line in new file */
-};
-
 static void	 diff_output(FILE *, const char *, ...);
-static int	 output(FILE *, struct got_diff_state *, struct got_diff_args *, const char *, FILE *, const char *, FILE *, int);
+static int	 output(FILE *, struct got_diff_changes *, struct got_diff_state *, struct got_diff_args *, const char *, FILE *, const char *, FILE *, int);
 static void	 check(struct got_diff_state *, FILE *, FILE *, int);
 static void	 uni_range(FILE *, int, int);
-static void	 dump_unified_vec(FILE *, struct got_diff_state *, struct got_diff_args *, FILE *, FILE *, int);
+static void	 dump_unified_vec(FILE *, struct got_diff_changes *, struct got_diff_state *, struct got_diff_args *, FILE *, FILE *, int);
 static int	 prepare(struct got_diff_state *, int, FILE *, off_t, int);
 static void	 prune(struct got_diff_state *);
 static void	 equiv(struct line *, int, struct line *, int, int *);
 static void	 unravel(struct got_diff_state *, int);
 static int	 unsort(struct line *, int, int *);
-static int	 change(FILE *, struct got_diff_state *, struct got_diff_args *, const char *, FILE *, const char *, FILE *, int, int, int, int, int *);
+static int	 change(FILE *, struct got_diff_changes *, struct got_diff_state *, struct got_diff_args *, const char *, FILE *, const char *, FILE *, int, int, int, int, int *);
 static void	 sort(struct line *, int);
 static void	 print_header(FILE *, struct got_diff_state *, struct got_diff_args *, const char *, const char *);
 static int	 asciifile(FILE *);
@@ -270,6 +258,9 @@ diff_output(FILE *outfile, const char *fmt, ...)
 {
 	va_list ap;
 
+	if (outfile == NULL)
+		return;
+
 	va_start(ap, fmt);
 	vfprintf(outfile, fmt, ap);
 	va_end(ap);
@@ -277,7 +268,8 @@ diff_output(FILE *outfile, const char *fmt, ...)
 
 const struct got_error *
 got_diffreg(int *rval, FILE *f1, FILE *f2, int flags,
-    struct got_diff_args *args, struct got_diff_state *ds, FILE *outfile)
+    struct got_diff_args *args, struct got_diff_state *ds, FILE *outfile,
+    struct got_diff_changes *changes)
 {
 	const struct got_error *err = NULL;
 	int i, *p;
@@ -410,8 +402,8 @@ got_diffreg(int *rval, FILE *f1, FILE *f2, int flags,
 	}
 	ds->ixnew = lp;
 	check(ds, f1, f2, flags);
-	if (output(outfile, ds, args, args->label[0], f1, args->label[1], f2,
-	    flags))
+	if (output(outfile, changes, ds, args, args->label[0], f1,
+	    args->label[1], f2, flags))
 		err = got_error_from_errno();
 closem:
 	free(ds->J);
@@ -850,7 +842,8 @@ skipline(FILE *f)
 }
 
 static int
-output(FILE *outfile, struct got_diff_state *ds, struct got_diff_args *args,
+output(FILE *outfile, struct got_diff_changes *changes,
+    struct got_diff_state *ds, struct got_diff_args *args,
     const char *file1, FILE *f1, const char *file2, FILE *f2, int flags)
 {
 	int m, i0, i1, j0, j1;
@@ -870,19 +863,19 @@ output(FILE *outfile, struct got_diff_state *ds, struct got_diff_args *args,
 			i1++;
 		j1 = ds->J[i1 + 1] - 1;
 		ds->J[i1] = j1;
-		error = change(outfile, ds, args, file1, f1, file2, f2,
+		error = change(outfile, changes, ds, args, file1, f1, file2, f2,
 		    i0, i1, j0, j1, &flags);
 		if (error)
 			return (error);
 	}
 	if (m == 0) {
-		error = change(outfile, ds, args, file1, f1, file2, f2, 1, 0,
-		    1, ds->len[1], &flags);
+		error = change(outfile, changes, ds, args, file1, f1, file2, f2,
+		    1, 0, 1, ds->len[1], &flags);
 		if (error)
 			return (error);
 	}
 	if (ds->anychange != 0)
-		dump_unified_vec(outfile, ds, args, f1, f2, flags);
+		dump_unified_vec(outfile, changes, ds, args, f1, f2, flags);
 
 	return (0);
 }
@@ -906,7 +899,8 @@ uni_range(FILE *outfile, int a, int b)
  * lines missing from the to file.
  */
 static int
-change(FILE *outfile, struct got_diff_state *ds, struct got_diff_args *args,
+change(FILE *outfile, struct got_diff_changes *changes,
+    struct got_diff_state *ds, struct got_diff_args *args,
     const char *file1, FILE *f1, const char *file2, FILE *f2,
     int a, int b, int c, int d, int *pflags)
 {
@@ -952,7 +946,8 @@ change(FILE *outfile, struct got_diff_state *ds, struct got_diff_args *args,
 			 * If this change is more than 'diff_context' lines from the
 			 * previous change, dump the record and reset it.
 			 */
-			dump_unified_vec(outfile, ds, args, f1, f2, *pflags);
+			dump_unified_vec(outfile, changes, ds, args, f1, f2,
+			    *pflags);
 		}
 		ds->context_vec_ptr++;
 		ds->context_vec_ptr->a = a;
@@ -1133,7 +1128,8 @@ match_function(struct got_diff_state *ds, const long *f, int pos, FILE *fp)
 
 /* dump accumulated "unified" diff changes */
 static void
-dump_unified_vec(FILE *outfile, struct got_diff_state *ds, struct got_diff_args *args,
+dump_unified_vec(FILE *outfile, struct got_diff_changes *changes,
+    struct got_diff_state *ds, struct got_diff_args *args,
     FILE *f1, FILE *f2, int flags)
 {
 	struct context_vec *cvp = ds->context_vec_start;
@@ -1167,6 +1163,17 @@ dump_unified_vec(FILE *outfile, struct got_diff_state *ds, struct got_diff_args 
 	 * are printed together.
 	 */
 	for (; cvp <= ds->context_vec_ptr; cvp++) {
+		if (changes) {
+			struct got_diff_change *change;
+			change = calloc(1, sizeof(*change));
+			if (change) {
+				memcpy(&change->cv, cvp, sizeof(change->cv));
+				SIMPLEQ_INSERT_TAIL(&changes->entries, change,
+				    entry);
+				changes->nchanges++;
+			}
+		}
+
 		a = cvp->a;
 		b = cvp->b;
 		c = cvp->c;
diff --git a/lib/got_lib_diff.h b/lib/got_lib_diff.h
index c317a77..93a2f44 100644
--- a/lib/got_lib_diff.h
+++ b/lib/got_lib_diff.h
@@ -74,6 +74,28 @@ struct excludes {
 	struct excludes *next;
 };
 
+/*
+ * The following struct is used to record change information when
+ * doing a "context" or "unified" diff.  (see routine "change" to
+ * understand the highly mnemonic field names)
+ */
+struct context_vec {
+	int	a;		/* start line in old file */
+	int	b;		/* end line in old file */
+	int	c;		/* start line in new file */
+	int	d;		/* end line in new file */
+};
+
+struct got_diff_change {
+	SIMPLEQ_ENTRY(got_diff_change) entry;
+	struct context_vec cv;
+};
+
+struct got_diff_changes {
+	size_t nchanges;
+	SIMPLEQ_HEAD(, got_diff_change) entries;
+};
+
 struct got_diff_state {
 	int  *J;			/* will be overlaid on class */
 	int  *class;		/* will be overlaid on file[0] */
@@ -110,4 +132,9 @@ struct got_diff_args {
 };
 
 const struct got_error *got_diffreg(int *, FILE *,
-    FILE *, int, struct got_diff_args *, struct got_diff_state *, FILE *);
+    FILE *, int, struct got_diff_args *, struct got_diff_state *, FILE *,
+    struct got_diff_changes *);
+
+const struct got_error *got_diff_blob_lines_changed(struct got_diff_changes **,
+    struct got_blob_object *, struct got_blob_object *);
+void got_diff_free_changes(struct got_diff_changes *);