Commit 8469d82143a591d423c000c47c63bababe6f5716

Stefan Sperling 2022-06-25T09:25:07

always show commit or tree IDs in diff header, in order to help 'got patch' The idea is that got patch can simply look for a line such as: commit - abcde1234567... to find the merge base commit ID to show in diff3 conflict markers. got log -p now displays commit IDs in its diff header, instead ofl tree or blob IDs. ok op@

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
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
diff --git a/got/got.c b/got/got.c
index 79d2837..64274b1 100644
--- a/got/got.c
+++ b/got/got.c
@@ -3704,18 +3704,20 @@ print_patch(struct got_commit_object *commit, struct got_object_id *id,
 		    &qid->id);
 		if (err)
 			return err;
+		err = got_object_id_str(&id_str1, &qid->id);
+		if (err)
+			goto done;
 	}
 
+	err = got_object_id_str(&id_str2, id);
+	if (err)
+		goto done;
+
 	if (path && path[0] != '\0') {
 		int obj_type;
 		err = got_object_id_by_path(&obj_id2, repo, commit, path);
 		if (err)
 			goto done;
-		err = got_object_id_str(&id_str2, obj_id2);
-		if (err) {
-			free(obj_id2);
-			goto done;
-		}
 		if (pcommit) {
 			err = got_object_id_by_path(&obj_id1, repo,
 			    pcommit, path);
@@ -3724,12 +3726,6 @@ print_patch(struct got_commit_object *commit, struct got_object_id *id,
 					free(obj_id2);
 					goto done;
 				}
-			} else {
-				err = got_object_id_str(&id_str1, obj_id1);
-				if (err) {
-					free(obj_id2);
-					goto done;
-				}
 			}
 		}
 		err = got_object_get_type(&obj_type, repo, obj_id2);
@@ -3739,6 +3735,9 @@ print_patch(struct got_commit_object *commit, struct got_object_id *id,
 		}
 		fprintf(outfile,
 		    "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
+		fprintf(outfile, "commit - %s\n",
+		    id_str1 ? id_str1 : "/dev/null");
+		fprintf(outfile, "commit + %s\n", id_str2);
 		switch (obj_type) {
 		case GOT_OBJ_TYPE_BLOB:
 			err = diff_blobs(obj_id1, obj_id2, path, diff_context,
@@ -3756,17 +3755,13 @@ print_patch(struct got_commit_object *commit, struct got_object_id *id,
 		free(obj_id2);
 	} else {
 		obj_id2 = got_object_commit_get_tree_id(commit);
-		err = got_object_id_str(&id_str2, obj_id2);
-		if (err)
-			goto done;
-		if (pcommit) {
+		if (pcommit)
 			obj_id1 = got_object_commit_get_tree_id(pcommit);
-			err = got_object_id_str(&id_str1, obj_id1);
-			if (err)
-				goto done;
-		}
 		fprintf(outfile,
 		    "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
+		fprintf(outfile, "commit - %s\n",
+		    id_str1 ? id_str1 : "/dev/null");
+		fprintf(outfile, "commit + %s\n", id_str2);
 		err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
 		    repo, outfile);
 	}
@@ -4648,7 +4643,10 @@ print_diff(void *arg, unsigned char status, unsigned char staged_status,
 	}
 
 	if (!a->header_shown) {
-		printf("diff %s %s%s\n", a->id_str,
+		printf("diff %s%s\n", a->diff_staged ? "-s " : "",
+		    got_worktree_get_root_path(a->worktree));
+		printf("commit - %s\n", a->id_str);
+		printf("path + %s%s\n",
 		    got_worktree_get_root_path(a->worktree),
 		    a->diff_staged ? " (staged changes)" : "");
 		a->header_shown = 1;
diff --git a/lib/diff.c b/lib/diff.c
index 6319482..8337d25 100644
--- a/lib/diff.c
+++ b/lib/diff.c
@@ -875,8 +875,34 @@ done:
 	return err;
 }
 
-const struct got_error *
-got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
+static const struct got_error *
+show_object_id(off_t **line_offsets, size_t *nlines, const char *obj_typestr,
+    int ch, const char *id_str, FILE *outfile)
+{
+	const struct got_error *err;
+	int n;
+	off_t outoff = 0;
+
+	n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
+	if (line_offsets != NULL && *line_offsets != NULL) {
+		if (*nlines == 0) {
+			err = add_line_offset(line_offsets, nlines, 0);
+			if (err)
+				return err;
+		} else
+			outoff = (*line_offsets)[*nlines - 1];
+
+		outoff += n;
+		err = add_line_offset(line_offsets, nlines, outoff);
+		if (err)
+			return err;
+	}
+
+	return NULL;
+}
+
+static const struct got_error *
+diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
     FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
     struct got_pathlist_head *paths,
     char *label1, char *label2, int diff_context, int ignore_whitespace,
@@ -932,6 +958,61 @@ done:
 }
 
 const struct got_error *
+got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
+    FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
+    struct got_pathlist_head *paths,
+    char *label1, char *label2, int diff_context, int ignore_whitespace,
+    int force_text_diff, struct got_repository *repo, FILE *outfile)
+{
+	const struct got_error *err;
+	char *idstr = NULL;
+
+	if (id1 == NULL && id2 == NULL)
+		return got_error(GOT_ERR_NO_OBJ);
+
+	if (id1) {
+		err = got_object_id_str(&idstr, id1);
+		if (err)
+			goto done;
+		err = show_object_id(line_offsets, nlines, "tree", '-',
+		    idstr, outfile);
+		if (err)
+			goto done;
+		free(idstr);
+		idstr = NULL;
+	} else {
+		err = show_object_id(line_offsets, nlines, "tree", '-',
+		    "/dev/null", outfile);
+		if (err)
+			goto done;
+	}
+
+	if (id2) {
+		err = got_object_id_str(&idstr, id2);
+		if (err)
+			goto done;
+		err = show_object_id(line_offsets, nlines, "tree", '+',
+		    idstr, outfile);
+		if (err)
+			goto done;
+		free(idstr);
+		idstr = NULL;
+	} else {
+		err = show_object_id(line_offsets, nlines, "tree", '+',
+		    "/dev/null", outfile);
+		if (err)
+			goto done;
+	}
+
+	err = diff_objects_as_trees(line_offsets, nlines, f1, f2, id1, id2,
+	    paths, label1, label2, diff_context, ignore_whitespace,
+	    force_text_diff, repo, outfile);
+done:
+	free(idstr);
+	return err;
+}
+
+const struct got_error *
 got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
     FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
     struct got_pathlist_head *paths,
@@ -940,6 +1021,7 @@ got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
 {
 	const struct got_error *err;
 	struct got_commit_object *commit1 = NULL, *commit2 = NULL;
+	char *idstr = NULL;
 
 	if (id2 == NULL)
 		return got_error(GOT_ERR_NO_OBJ);
@@ -948,13 +1030,35 @@ got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
 		err = got_object_open_as_commit(&commit1, repo, id1);
 		if (err)
 			goto done;
+		err = got_object_id_str(&idstr, id1);
+		if (err)
+			goto done;
+		err = show_object_id(line_offsets, nlines, "commit", '-',
+		    idstr, outfile);
+		if (err)
+			goto done;
+		free(idstr);
+		idstr = NULL;
+	} else {
+		err = show_object_id(line_offsets, nlines, "commit", '-',
+		    "/dev/null", outfile);
+		if (err)
+			goto done;
 	}
 
 	err = got_object_open_as_commit(&commit2, repo, id2);
 	if (err)
 		goto done;
 
-	err = got_diff_objects_as_trees(line_offsets, nlines, f1, f2,
+	err = got_object_id_str(&idstr, id2);
+	if (err)
+		goto done;
+	err = show_object_id(line_offsets, nlines, "commit", '+',
+	    idstr, outfile);
+	if (err)
+		goto done;
+
+	err = diff_objects_as_trees(line_offsets, nlines, f1, f2,
 	    commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
 	    got_object_commit_get_tree_id(commit2), paths, "", "",
 	    diff_context, ignore_whitespace, force_text_diff, repo, outfile);
@@ -963,6 +1067,7 @@ done:
 		got_object_commit_close(commit1);
 	if (commit2)
 		got_object_commit_close(commit2);
+	free(idstr);
 	return err;
 }
 
diff --git a/libexec/got-read-patch/got-read-patch.c b/libexec/got-read-patch/got-read-patch.c
index 5f1a284..05415dd 100644
--- a/libexec/got-read-patch/got-read-patch.c
+++ b/libexec/got-read-patch/got-read-patch.c
@@ -191,6 +191,9 @@ find_patch(int *done, FILE *fp)
 		} else if (!git && !strncmp(line, "diff ", 5)) {
 			free(commitid);
 			err = blobid(line + 5, &commitid);
+		} else if (!git && !strncmp(line, "commit - ", 9)) {
+			free(commitid);
+			err = blobid(line + 9, &commitid);
 		}
 
 		if (err)
diff --git a/regress/cmdline/cherrypick.sh b/regress/cmdline/cherrypick.sh
index 1c68ed3..ac5a149 100755
--- a/regress/cmdline/cherrypick.sh
+++ b/regress/cmdline/cherrypick.sh
@@ -1372,7 +1372,7 @@ EOF
  
 EOF
 	(cd $testroot/wt && got diff |
-		egrep -v '^(diff|blob|file)' > $testroot/diff)
+		egrep -v '^(diff|blob|file|commit|path)' > $testroot/diff)
 	cmp -s $testroot/diff.expected $testroot/diff
 	ret=$?
 	if [ $ret -ne 0 ]; then
diff --git a/regress/cmdline/commit.sh b/regress/cmdline/commit.sh
index e590a17..5eef3d7 100755
--- a/regress/cmdline/commit.sh
+++ b/regress/cmdline/commit.sh
@@ -428,6 +428,8 @@ test_commit_path_prefix() {
 	fi
 
 	echo "diff $commit1 $commit2" > $testroot/stdout.expected
+	echo "commit - $commit1" >> $testroot/stdout.expected
+	echo "commit + $commit2" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -c $commit1 -i gamma | grep 'delta$' \
 		| cut -d' ' -f 1 >> $testroot/stdout.expected
@@ -470,6 +472,8 @@ test_commit_path_prefix() {
 	fi
 
 	echo "diff $commit2 $commit3" > $testroot/stdout.expected
+	echo "commit - $commit2" >> $testroot/stdout.expected
+	echo "commit + $commit3" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -c $commit2 -i gamma | grep 'delta$' \
 		| cut -d' ' -f 1 | sed -e 's/$/ (mode 644)/' \
diff --git a/regress/cmdline/diff.sh b/regress/cmdline/diff.sh
index 74dae87..a5ad362 100755
--- a/regress/cmdline/diff.sh
+++ b/regress/cmdline/diff.sh
@@ -32,7 +32,9 @@ test_diff_basic() {
 	echo "new file" > $testroot/wt/new
 	(cd $testroot/wt && got add new >/dev/null)
 
-	echo "diff $head_rev $testroot/wt" > $testroot/stdout.expected
+	echo "diff $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $head_rev" >> $testroot/stdout.expected
+	echo "path + $testroot/wt" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -i | grep 'alpha$' | cut -d' ' -f 1 \
 		>> $testroot/stdout.expected
@@ -115,7 +117,9 @@ test_diff_basic() {
 	echo "modified zeta" > $testroot/wt/epsilon/zeta
 
 	# diff several paths in a work tree
-	echo "diff $head_rev $testroot/wt" > $testroot/stdout.expected
+	echo "diff $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $head_rev" >> $testroot/stdout.expected
+	echo "path + $testroot/wt" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -i | grep 'alpha$' | cut -d' ' -f 1 \
 		>> $testroot/stdout.expected
@@ -205,6 +209,8 @@ test_diff_basic() {
 		return 1
 	fi
 	echo "diff refs/heads/master refs/heads/new" > $testroot/stdout.expected
+	echo "commit - $head_rev" >> $testroot/stdout.expected
+	echo "commit + $head_rev" >> $testroot/stdout.expected
 	# diff between the branches is empty
 	cmp -s $testroot/stdout.expected $testroot/stdout
 	ret=$?
@@ -222,6 +228,8 @@ test_diff_basic() {
 		return 1
 	fi
 	echo "diff refs/heads/master refs/heads/new" > $testroot/stdout.expected
+	echo "commit - $head_rev" >> $testroot/stdout.expected
+	echo "commit + $head_rev" >> $testroot/stdout.expected
 	cmp -s $testroot/stdout.expected $testroot/stdout
 	ret=$?
 	if [ $ret -ne 0 ]; then
@@ -238,6 +246,8 @@ test_diff_basic() {
 		return 1
 	fi
 	echo "diff refs/heads/master refs/heads/new" > $testroot/stdout.expected
+	echo "commit - $head_rev" >> $testroot/stdout.expected
+	echo "commit + $head_rev" >> $testroot/stdout.expected
 	cmp -s $testroot/stdout.expected $testroot/stdout
 	ret=$?
 	if [ $ret -ne 0 ]; then
@@ -254,7 +264,9 @@ test_diff_basic() {
 		test_done "$testroot" "1"
 		return 1
 	fi
-	echo "diff $head_rev $testroot/wt" > $testroot/stdout.expected
+	echo "diff $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $head_rev" >> $testroot/stdout.expected
+	echo "path + $testroot/wt" >> $testroot/stdout.expected
 	echo 'blob - /dev/null' >> $testroot/stdout.expected
 	echo 'file + master' >> $testroot/stdout.expected
 	echo '--- /dev/null' >> $testroot/stdout.expected
@@ -294,7 +306,9 @@ test_diff_basic() {
 	fi
 
 	# a single argument which can be resolved to a path is not ambiguous
-	echo "diff $head_rev $testroot/wt" > $testroot/stdout.expected
+	echo "diff $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $head_rev" >> $testroot/stdout.expected
+	echo "path + $testroot/wt" >> $testroot/stdout.expected
 	echo 'blob - /dev/null' >> $testroot/stdout.expected
 	echo 'file + new' >> $testroot/stdout.expected
 	echo '--- /dev/null' >> $testroot/stdout.expected
@@ -346,7 +360,9 @@ test_diff_basic() {
 		return 1
 	fi
 
-	echo "diff $head_rev $testroot/wt" > $testroot/stdout.expected
+	echo "diff $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $head_rev" >> $testroot/stdout.expected
+	echo "path + $testroot/wt" >> $testroot/stdout.expected
 	echo 'blob - /dev/null' >> $testroot/stdout.expected
 	echo 'file + new' >> $testroot/stdout.expected
 	echo '--- /dev/null' >> $testroot/stdout.expected
@@ -419,7 +435,9 @@ test_diff_shows_conflict() {
 		return 1
 	fi
 
-	echo "diff $head_rev $testroot/wt" > $testroot/stdout.expected
+	echo "diff $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $head_rev" >> $testroot/stdout.expected
+	echo "path + $testroot/wt" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -i | grep 'numbers$' | cut -d' ' -f 1 \
 		>> $testroot/stdout.expected
@@ -482,6 +500,8 @@ test_diff_tag() {
 	(cd $testroot/repo && git tag -m "test" $tag2)
 
 	echo "diff $commit_id0 refs/tags/$tag1" > $testroot/stdout.expected
+	echo "commit - $commit_id0" >> $testroot/stdout.expected
+	echo "commit + $commit_id1" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -c $commit_id0 -i | grep 'alpha$' | \
 		cut -d' ' -f 1 >> $testroot/stdout.expected
@@ -504,6 +524,8 @@ test_diff_tag() {
 	fi
 
 	echo "diff refs/tags/$tag1 refs/tags/$tag2" > $testroot/stdout.expected
+	echo "commit - $commit_id1" >> $testroot/stdout.expected
+	echo "commit + $commit_id2" >> $testroot/stdout.expected
 	echo "blob - /dev/null" >> $testroot/stdout.expected
 	echo -n 'blob + ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -i -c $commit_id2 | grep 'new$' | \
@@ -543,6 +565,8 @@ test_diff_lightweight_tag() {
 	(cd $testroot/repo && git tag $tag2)
 
 	echo "diff $commit_id0 refs/tags/$tag1" > $testroot/stdout.expected
+	echo "commit - $commit_id0" >> $testroot/stdout.expected
+	echo "commit + $commit_id1" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -c $commit_id0 -i | grep 'alpha$' | \
 		cut -d' ' -f 1 >> $testroot/stdout.expected
@@ -565,6 +589,8 @@ test_diff_lightweight_tag() {
 	fi
 
 	echo "diff refs/tags/$tag1 refs/tags/$tag2" > $testroot/stdout.expected
+	echo "commit - $commit_id1" >> $testroot/stdout.expected
+	echo "commit + $commit_id2" >> $testroot/stdout.expected
 	echo "blob - /dev/null" >> $testroot/stdout.expected
 	echo -n 'blob + ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -i -c $commit_id2 | grep 'new$' | \
@@ -599,7 +625,9 @@ test_diff_ignore_whitespace() {
 
 	(cd $testroot/wt && got diff -w > $testroot/stdout)
 
-	echo "diff $commit_id0 $testroot/wt" > $testroot/stdout.expected
+	echo "diff $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $commit_id0" >> $testroot/stdout.expected
+	echo "path + $testroot/wt" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -c $commit_id0 -i | grep 'alpha$' | \
 		cut -d' ' -f 1 >> $testroot/stdout.expected
@@ -675,7 +703,9 @@ test_diff_symlinks_in_work_tree() {
 	(cd $testroot/wt && got add zeta.link > /dev/null)
 	(cd $testroot/wt && got diff > $testroot/stdout)
 
-	echo "diff $commit_id1 $testroot/wt" > $testroot/stdout.expected
+	echo "diff $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $commit_id1" >> $testroot/stdout.expected
+	echo "path + $testroot/wt" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -c $commit_id1 -i | \
 		grep 'alpha.link@ -> alpha$' | \
@@ -776,6 +806,8 @@ test_diff_symlinks_in_repo() {
 	got diff -r $testroot/repo $commit_id1 $commit_id2 > $testroot/stdout
 
 	echo "diff $commit_id1 $commit_id2" > $testroot/stdout.expected
+	echo "commit - $commit_id1" >> $testroot/stdout.expected
+	echo "commit + $commit_id2" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -c $commit_id1 -i | \
 		grep 'alpha.link@ -> alpha$' | \
@@ -881,7 +913,9 @@ test_diff_binary_files() {
 	printf '\377\377\0\0\377\377\0\0' > $testroot/wt/foo
 	(cd $testroot/wt && got add foo >/dev/null)
 
-	echo "diff $head_rev $testroot/wt" > $testroot/stdout.expected
+	echo "diff $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $head_rev" >> $testroot/stdout.expected
+	echo "path + $testroot/wt" >> $testroot/stdout.expected
 	echo 'blob - /dev/null' >> $testroot/stdout.expected
 	echo 'file + foo' >> $testroot/stdout.expected
 	echo "Binary files /dev/null and foo differ" \
@@ -896,7 +930,9 @@ test_diff_binary_files() {
 		return 1
 	fi
 
-	echo "diff $head_rev $testroot/wt" > $testroot/stdout.expected
+	echo "diff $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $head_rev" >> $testroot/stdout.expected
+	echo "path + $testroot/wt" >> $testroot/stdout.expected
 	echo 'blob - /dev/null' >> $testroot/stdout.expected
 	echo 'file + foo' >> $testroot/stdout.expected
 	echo '--- /dev/null' >> $testroot/stdout.expected
@@ -919,7 +955,9 @@ test_diff_binary_files() {
 
 	printf '\377\200\0\0\377\200\0\0' > $testroot/wt/foo
 
-	echo "diff $head_rev $testroot/wt" > $testroot/stdout.expected
+	echo "diff $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $head_rev" >> $testroot/stdout.expected
+	echo "path + $testroot/wt" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -i | grep 'foo$' | cut -d' ' -f 1 \
 		>> $testroot/stdout.expected
@@ -965,6 +1003,8 @@ test_diff_commits() {
 	new_id1=`get_blob_id $testroot/repo "" new`
 
 	echo "diff $commit_id0 refs/heads/master" > $testroot/stdout.expected
+	echo "commit - $commit_id0" >> $testroot/stdout.expected
+	echo "commit + $commit_id1" >> $testroot/stdout.expected
 	echo "blob - $alpha_id0" >> $testroot/stdout.expected
 	echo "blob + $alpha_id1" >> $testroot/stdout.expected
 	echo '--- alpha' >> $testroot/stdout.expected
@@ -1007,6 +1047,8 @@ test_diff_commits() {
 
 	# same diff with commit object IDs
 	echo "diff $commit_id0 $commit_id1" > $testroot/stdout.expected
+	echo "commit - $commit_id0" >> $testroot/stdout.expected
+	echo "commit + $commit_id1" >> $testroot/stdout.expected
 	echo "blob - $alpha_id0" >> $testroot/stdout.expected
 	echo "blob + $alpha_id1" >> $testroot/stdout.expected
 	echo '--- alpha' >> $testroot/stdout.expected
@@ -1038,6 +1080,8 @@ test_diff_commits() {
 
 	# same diff, filtered by paths
 	echo "diff $commit_id0 $commit_id1" > $testroot/stdout.expected
+	echo "commit - $commit_id0" >> $testroot/stdout.expected
+	echo "commit + $commit_id1" >> $testroot/stdout.expected
 	echo "blob - $alpha_id0" >> $testroot/stdout.expected
 	echo "blob + $alpha_id1" >> $testroot/stdout.expected
 	echo '--- alpha' >> $testroot/stdout.expected
@@ -1066,6 +1110,8 @@ test_diff_commits() {
 	fi
 
 	echo "diff $commit_id0 $commit_id1" > $testroot/stdout.expected
+	echo "commit - $commit_id0" >> $testroot/stdout.expected
+	echo "commit + $commit_id1" >> $testroot/stdout.expected
 	echo "blob - $beta_id0 (mode 644)" >> $testroot/stdout.expected
 	echo 'blob + /dev/null' >> $testroot/stdout.expected
 	echo '--- beta' >> $testroot/stdout.expected
diff --git a/regress/cmdline/import.sh b/regress/cmdline/import.sh
index 5fd35f7..920610e 100755
--- a/regress/cmdline/import.sh
+++ b/regress/cmdline/import.sh
@@ -65,7 +65,9 @@ test_import_basic() {
 	echo " " >> $testroot/stdout.expected
 	echo " init" >> $testroot/stdout.expected
 	echo " " >> $testroot/stdout.expected
-	echo "diff /dev/null $tree_id" >> $testroot/stdout.expected
+	echo "diff /dev/null $head_commit" >> $testroot/stdout.expected
+	echo "commit - /dev/null" >> $testroot/stdout.expected
+	echo "commit + $head_commit" >> $testroot/stdout.expected
 	echo "blob - /dev/null" >> $testroot/stdout.expected
 	echo "blob + $id_alpha (mode 644)" >> $testroot/stdout.expected
 	echo "--- /dev/null" >> $testroot/stdout.expected
diff --git a/regress/cmdline/log.sh b/regress/cmdline/log.sh
index 7bdd9c2..f322b4b 100755
--- a/regress/cmdline/log.sh
+++ b/regress/cmdline/log.sh
@@ -352,6 +352,8 @@ test_log_patch_added_file() {
 	local commit_id1=`git_show_head $testroot/repo`
 
 	echo "commit $commit_id1 (master)" > $testroot/stdout.expected
+	echo "commit - $commit_id0" >> $testroot/stdout.expected
+	echo "commit + $commit_id1" >> $testroot/stdout.expected
 	# This used to fail with 'got: no such entry found in tree'
 	(cd $testroot/wt && got log -l1 -p new > $testroot/stdout.patch)
 	ret=$?
diff --git a/regress/cmdline/revert.sh b/regress/cmdline/revert.sh
index 53e50a5..6af534f 100755
--- a/regress/cmdline/revert.sh
+++ b/regress/cmdline/revert.sh
@@ -527,7 +527,9 @@ EOF
 		return 1
 	fi
 
-	echo "diff $commit_id $testroot/wt" > $testroot/stdout.expected
+	echo "diff $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $commit_id" >> $testroot/stdout.expected
+	echo "path + $testroot/wt" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -i -c $commit_id \
 		| grep 'numbers$' | cut -d' ' -f 1 \
@@ -625,7 +627,9 @@ EOF
 
 	(cd $testroot/wt && got diff > $testroot/stdout)
 
-	echo "diff $commit_id $testroot/wt" > $testroot/stdout.expected
+	echo "diff $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $commit_id" >> $testroot/stdout.expected
+	echo "path + $testroot/wt" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -i -c $commit_id \
 		| grep 'numbers$' | cut -d' ' -f 1 \
@@ -693,7 +697,9 @@ EOF
 
 	(cd $testroot/wt && got diff > $testroot/stdout)
 
-	echo "diff $commit_id $testroot/wt" > $testroot/stdout.expected
+	echo "diff $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $commit_id" >> $testroot/stdout.expected
+	echo "path + $testroot/wt" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -i -c $commit_id \
 		| grep 'numbers$' | cut -d' ' -f 1 \
diff --git a/regress/cmdline/stage.sh b/regress/cmdline/stage.sh
index de1799d..a521992 100755
--- a/regress/cmdline/stage.sh
+++ b/regress/cmdline/stage.sh
@@ -933,7 +933,9 @@ test_stage_diff() {
 
 	(cd $testroot/wt && got diff > $testroot/stdout)
 
-	echo "diff $head_commit $testroot/wt" > $testroot/stdout.expected
+	echo "diff $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $head_commit" >> $testroot/stdout.expected
+	echo "path + $testroot/wt" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	(cd $testroot/wt && got stage -l alpha) | cut -d' ' -f 1 | tr -d '\n' \
 		>> $testroot/stdout.expected
@@ -965,8 +967,9 @@ test_stage_diff() {
 
 	(cd $testroot/wt && got diff -s > $testroot/stdout)
 
-	echo "diff $head_commit $testroot/wt (staged changes)" \
-		> $testroot/stdout.expected
+	echo "diff -s $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $head_commit" >> $testroot/stdout.expected
+	echo "path + $testroot/wt (staged changes)" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -i | grep 'alpha$' | cut -d' ' -f 1 \
 		>> $testroot/stdout.expected
@@ -1379,6 +1382,8 @@ test_stage_commit() {
 
 	echo "diff $first_commit $head_commit" \
 		> $testroot/stdout.expected
+	echo "commit - $first_commit" >> $testroot/stdout.expected
+	echo "commit + $head_commit" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -i -c $first_commit | \
 		grep 'alpha$' | cut -d' ' -f 1 \
@@ -1586,8 +1591,9 @@ EOF
 
 	(cd $testroot/wt && got diff -s > $testroot/stdout)
 
-	echo "diff $commit_id $testroot/wt (staged changes)" \
-		> $testroot/stdout.expected
+	echo "diff -s $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $commit_id" >> $testroot/stdout.expected
+	echo "path + $testroot/wt (staged changes)" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -i -c $commit_id \
 		| grep 'numbers$' | cut -d' ' -f 1 \
@@ -1692,8 +1698,9 @@ EOF
 
 	(cd $testroot/wt && got diff -s > $testroot/stdout)
 
-	echo "diff $commit_id $testroot/wt (staged changes)" \
-		> $testroot/stdout.expected
+	echo "diff -s $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $commit_id" >> $testroot/stdout.expected
+	echo "path + $testroot/wt (staged changes)" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -i -c $commit_id \
 		| grep 'numbers$' | cut -d' ' -f 1 \
@@ -1843,8 +1850,9 @@ EOF
 
 	(cd $testroot/wt && got diff -s > $testroot/stdout)
 
-	echo "diff $commit_id $testroot/wt (staged changes)" \
-		> $testroot/stdout.expected
+	echo "diff -s $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $commit_id" >> $testroot/stdout.expected
+	echo "path + $testroot/wt (staged changes)" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -i -c $commit_id \
 		| grep 'numbers$' | cut -d' ' -f 1 \
@@ -1881,7 +1889,9 @@ EOF
 
 	(cd $testroot/wt && got diff > $testroot/stdout)
 
-	echo "diff $commit_id $testroot/wt" > $testroot/stdout.expected
+	echo "diff $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $commit_id" >> $testroot/stdout.expected
+	echo "path + $testroot/wt" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	(cd $testroot/wt && got stage -l numbers) | cut -d' ' -f 1 | \
 		tr -d '\n' >> $testroot/stdout.expected
@@ -1946,8 +1956,9 @@ test_stage_patch_added() {
 
 	(cd $testroot/wt && got diff -s > $testroot/stdout)
 
-	echo "diff $commit_id $testroot/wt (staged changes)" \
-		> $testroot/stdout.expected
+	echo "diff -s $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $commit_id" >> $testroot/stdout.expected
+	echo "path + $testroot/wt (staged changes)" >> $testroot/stdout.expected
 	echo 'blob - /dev/null' >> $testroot/stdout.expected
 	echo -n 'blob + ' >> $testroot/stdout.expected
 	(cd $testroot/wt && got stage -l epsilon/new) | cut -d' ' -f 1 \
@@ -2070,8 +2081,9 @@ test_stage_patch_removed() {
 
 	(cd $testroot/wt && got diff -s > $testroot/stdout)
 
-	echo "diff $commit_id $testroot/wt (staged changes)" \
-		> $testroot/stdout.expected
+	echo "diff -s $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $commit_id" >> $testroot/stdout.expected
+	echo "path + $testroot/wt (staged changes)" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	(cd $testroot/wt && got stage -l beta) | cut -d' ' -f 1 \
 		>> $testroot/stdout.expected
@@ -2288,8 +2300,9 @@ EOF
 
 	(cd $testroot/wt && got diff -s > $testroot/stdout)
 
-	echo "diff $commit_id $testroot/wt (staged changes)" \
-		> $testroot/stdout.expected
+	echo "diff -s $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $commit_id" >> $testroot/stdout.expected
+	echo "path + $testroot/wt (staged changes)" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -i -c $commit_id \
 		| grep 'numbers$' | cut -d' ' -f 1 \
@@ -2481,8 +2494,9 @@ EOF
 
 	(cd $testroot/wt && got diff -s > $testroot/stdout)
 
-	echo "diff $head_commit $testroot/wt (staged changes)" \
-		> $testroot/stdout.expected
+	echo "diff -s $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $head_commit" >> $testroot/stdout.expected
+	echo "path + $testroot/wt (staged changes)" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -i | grep 'alpha.link@ -> alpha$' | \
 		cut -d' ' -f 1 >> $testroot/stdout.expected
@@ -2805,8 +2819,9 @@ EOF
 
 	(cd $testroot/wt && got diff -s > $testroot/stdout)
 
-	echo "diff $head_commit $testroot/wt (staged changes)" \
-		> $testroot/stdout.expected
+	echo "diff -s $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $head_commit" >> $testroot/stdout.expected
+	echo "path + $testroot/wt (staged changes)" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -i | grep 'alpha.link@ -> alpha$' | \
 		cut -d' ' -f 1 >> $testroot/stdout.expected
diff --git a/regress/cmdline/unstage.sh b/regress/cmdline/unstage.sh
index d3635a2..db343e3 100755
--- a/regress/cmdline/unstage.sh
+++ b/regress/cmdline/unstage.sh
@@ -336,8 +336,9 @@ EOF
 
 	(cd $testroot/wt && got diff -s > $testroot/stdout)
 
-	echo "diff $commit_id $testroot/wt (staged changes)" \
-		> $testroot/stdout.expected
+	echo "diff -s $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $commit_id" >> $testroot/stdout.expected
+	echo "path + $testroot/wt (staged changes)" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -i -c $commit_id \
 		| grep 'numbers$' | cut -d' ' -f 1 \
@@ -371,7 +372,9 @@ EOF
 	fi
 
 	(cd $testroot/wt && got diff > $testroot/stdout)
-	echo "diff $commit_id $testroot/wt" > $testroot/stdout.expected
+	echo "diff $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $commit_id" >> $testroot/stdout.expected
+	echo "path + $testroot/wt" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	(cd $testroot/wt && got stage -l numbers) | cut -d' ' -f 1  | \
 		tr -d '\n' >> $testroot/stdout.expected
@@ -478,8 +481,9 @@ EOF
 
 	(cd $testroot/wt && got diff -s > $testroot/stdout)
 
-	echo "diff $commit_id $testroot/wt (staged changes)" \
-		> $testroot/stdout.expected
+	echo "diff -s $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $commit_id" >> $testroot/stdout.expected
+	echo "path + $testroot/wt (staged changes)" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -i -c $commit_id \
 		| grep 'numbers$' | cut -d' ' -f 1 \
@@ -513,7 +517,9 @@ EOF
 	fi
 
 	(cd $testroot/wt && got diff > $testroot/stdout)
-	echo "diff $commit_id $testroot/wt" > $testroot/stdout.expected
+	echo "diff $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $commit_id" >> $testroot/stdout.expected
+	echo "path + $testroot/wt" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	(cd $testroot/wt && got stage -l numbers) | cut -d' ' -f 1 | \
 		tr -d '\n' >> $testroot/stdout.expected
@@ -627,7 +633,9 @@ EOF
 
 	(cd $testroot/wt && got diff > $testroot/stdout)
 
-	echo "diff $commit_id $testroot/wt" > $testroot/stdout.expected
+	echo "diff $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $commit_id" >> $testroot/stdout.expected
+	echo "path + $testroot/wt" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -i -c $commit_id \
 		| grep 'numbers$' | cut -d' ' -f 1 \
@@ -718,7 +726,9 @@ test_unstage_patch_added() {
 
 	(cd $testroot/wt && got diff > $testroot/stdout)
 
-	echo "diff $commit_id $testroot/wt" > $testroot/stdout.expected
+	echo "diff $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $commit_id" >> $testroot/stdout.expected
+	echo "path + $testroot/wt" >> $testroot/stdout.expected
 	echo 'blob - /dev/null' >> $testroot/stdout.expected
 	echo 'file + epsilon/new' >> $testroot/stdout.expected
 	echo "--- /dev/null" >> $testroot/stdout.expected
@@ -784,8 +794,9 @@ test_unstage_patch_removed() {
 
 	(cd $testroot/wt && got diff > $testroot/stdout)
 
-	echo "diff $commit_id $testroot/wt" \
-		> $testroot/stdout.expected
+	echo "diff $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $commit_id" >> $testroot/stdout.expected
+	echo "path + $testroot/wt" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -i | grep 'beta$' | cut -d' ' -f 1 \
 		>> $testroot/stdout.expected
@@ -885,7 +896,9 @@ EOF
 
 	(cd $testroot/wt && got diff > $testroot/stdout)
 
-	echo "diff $commit_id $testroot/wt" > $testroot/stdout.expected
+	echo "diff $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $commit_id" >> $testroot/stdout.expected
+	echo "path + $testroot/wt" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	(cd $testroot/wt && got stage -l numbers) | cut -d' ' -f 1 | \
 		tr -d '\n' >> $testroot/stdout.expected
@@ -909,8 +922,9 @@ EOF
 	fi
 
 	(cd $testroot/wt && got diff -s > $testroot/stdout)
-	echo "diff $commit_id $testroot/wt (staged changes)" \
-		> $testroot/stdout.expected
+	echo "diff -s $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $commit_id" >> $testroot/stdout.expected
+	echo "path + $testroot/wt (staged changes)" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -i -c $commit_id \
 		| grep 'numbers$' | cut -d' ' -f 1 \
diff --git a/regress/cmdline/update.sh b/regress/cmdline/update.sh
index b3ecc91..9f1d031 100755
--- a/regress/cmdline/update.sh
+++ b/regress/cmdline/update.sh
@@ -1017,7 +1017,9 @@ test_update_conflict_wt_rm_vs_repo_edit() {
 
 	# 'got diff' should show post-update contents of beta being deleted
 	local head_rev=`git_show_head $testroot/repo`
-	echo "diff $head_rev $testroot/wt" > $testroot/stdout.expected
+	echo "diff $testroot/wt" > $testroot/stdout.expected
+	echo "commit - $head_rev"  >> $testroot/stdout.expected
+	echo "path + $testroot/wt" >> $testroot/stdout.expected
 	echo -n 'blob - ' >> $testroot/stdout.expected
 	got tree -r $testroot/repo -i | grep 'beta$' | cut -d' ' -f 1 \
 		>> $testroot/stdout.expected
diff --git a/tog/tog.c b/tog/tog.c
index 130c0ad..e63f4a7 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -3973,7 +3973,8 @@ open_diff_view(struct tog_view *view, struct got_object_id *id1,
 			goto done;
 
 		err = add_color(&s->colors,
-		    "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
+		    "^(commit [0-9a-f]|parent [0-9]|"
+		    "(blob|file|tree|commit) [-+] |"
 		    "[MDmA]  [^ ])", TOG_COLOR_DIFF_META,
 		    get_color_value("TOG_COLOR_DIFF_META"));
 		if (err)