Commit 63035f9f4ff2f4ea5b7361830edad916a30e37cd

Stefan Sperling 2019-10-06T19:24:31

add -w (ignore whitespace) option to 'got diff'

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
diff --git a/got/got.1 b/got/got.1
index 20222ee..74fbaf4 100644
--- a/got/got.1
+++ b/got/got.1
@@ -361,7 +361,7 @@ If this directory is a
 .Nm
 work tree, use the repository path associated with this work tree.
 .El
-.It Cm diff Oo Fl C Ar number Oc Oo Fl r Ar repository-path Oc Oo Fl s Oc Op Ar object1 Ar object2 | Ar path
+.It Cm diff Oo Fl C Ar number Oc Oo Fl r Ar repository-path Oc Oo Fl s Oc Oo Fl w Oc Op Ar object1 Ar object2 | Ar path
 When invoked within a work tree with less than two arguments, display
 uncommitted changes in the work tree.
 If a
@@ -396,6 +396,8 @@ instead of showing local changes.
 This option is only valid when
 .Cm got diff
 is invoked in a work tree.
+.It Fl w
+Ignore whitespace-only changes.
 .El
 .It Cm di
 Short alias for
diff --git a/got/got.c b/got/got.c
index 13dea80..0c7661c 100644
--- a/got/got.c
+++ b/got/got.c
@@ -1366,7 +1366,8 @@ done:
 
 static const struct got_error *
 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
-    const char *path, int diff_context, struct got_repository *repo)
+    const char *path, int diff_context, int ignore_whitespace,
+    struct got_repository *repo)
 {
 	const struct got_error *err = NULL;
 	struct got_blob_object *blob1 = NULL, *blob2 = NULL;
@@ -1384,7 +1385,7 @@ diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
 	while (path[0] == '/')
 		path++;
 	err = got_diff_blob(blob1, blob2, path, path, diff_context,
-	    stdout);
+	    ignore_whitespace, stdout);
 done:
 	if (blob1)
 		got_object_blob_close(blob1);
@@ -1394,7 +1395,8 @@ done:
 
 static const struct got_error *
 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
-    const char *path, int diff_context, struct got_repository *repo)
+    const char *path, int diff_context, int ignore_whitespace,
+    struct got_repository *repo)
 {
 	const struct got_error *err = NULL;
 	struct got_tree_object *tree1 = NULL, *tree2 = NULL;
@@ -1411,6 +1413,7 @@ diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
 		goto done;
 
 	arg.diff_context = diff_context;
+	arg.ignore_whitespace = ignore_whitespace;
 	arg.outfile = stdout;
 	while (path[0] == '/')
 		path++;
@@ -1473,11 +1476,11 @@ print_patch(struct got_commit_object *commit, struct got_object_id *id,
 		switch (obj_type) {
 		case GOT_OBJ_TYPE_BLOB:
 			err = diff_blobs(obj_id1, obj_id2, path, diff_context,
-			    repo);
+			    0, repo);
 			break;
 		case GOT_OBJ_TYPE_TREE:
 			err = diff_trees(obj_id1, obj_id2, path, diff_context,
-			    repo);
+			    0, repo);
 			break;
 		default:
 			err = got_error(GOT_ERR_OBJ_TYPE);
@@ -1495,7 +1498,7 @@ print_patch(struct got_commit_object *commit, struct got_object_id *id,
 		if (err)
 			goto done;
 		printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
-		err = diff_trees(obj_id1, obj_id2, "", diff_context, repo);
+		err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
 	}
 
 done:
@@ -1939,7 +1942,7 @@ __dead static void
 usage_diff(void)
 {
 	fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
-	    "[object1 object2 | path]\n", getprogname());
+	    "[-w] [object1 object2 | path]\n", getprogname());
 	exit(1);
 }
 
@@ -1950,6 +1953,7 @@ struct print_diff_arg {
 	const char *id_str;
 	int header_shown;
 	int diff_staged;
+	int ignore_whitespace;
 };
 
 static const struct got_error *
@@ -2005,7 +2009,8 @@ print_diff(void *arg, unsigned char status, unsigned char staged_status,
 			return got_error(GOT_ERR_FILE_STATUS);
 		}
 		return got_diff_objects_as_blobs(blob_id, staged_blob_id,
-		    label1, label2, a->diff_context, a->repo, stdout);
+		    label1, label2, a->diff_context, a->ignore_whitespace,
+		    a->repo, stdout);
 	}
 
 	if (staged_status == GOT_STATUS_ADD ||
@@ -2050,7 +2055,7 @@ print_diff(void *arg, unsigned char status, unsigned char staged_status,
 		sb.st_size = 0;
 
 	err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
-	    a->diff_context, stdout);
+	    a->diff_context, a->ignore_whitespace, stdout);
 done:
 	if (blob1)
 		got_object_blob_close(blob1);
@@ -2130,7 +2135,7 @@ cmd_diff(int argc, char *argv[])
 	const char *id_str1 = NULL, *id_str2 = NULL;
 	char *label1 = NULL, *label2 = NULL;
 	int type1, type2;
-	int diff_context = 3, diff_staged = 0, ch;
+	int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
 	const char *errstr;
 	char *path = NULL;
 
@@ -2140,7 +2145,7 @@ cmd_diff(int argc, char *argv[])
 		err(1, "pledge");
 #endif
 
-	while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
+	while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
 		switch (ch) {
 		case 'C':
 			diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
@@ -2156,6 +2161,9 @@ cmd_diff(int argc, char *argv[])
 		case 's':
 			diff_staged = 1;
 			break;
+		case 'w':
+			ignore_whitespace = 1;
+			break;
 		default:
 			usage_diff();
 			/* NOTREACHED */
@@ -2248,6 +2256,7 @@ cmd_diff(int argc, char *argv[])
 		arg.id_str = id_str;
 		arg.header_shown = 0;
 		arg.diff_staged = diff_staged;
+		arg.ignore_whitespace = ignore_whitespace;
 
 		error = got_pathlist_append(&paths, path, NULL);
 		if (error)
@@ -2286,16 +2295,16 @@ cmd_diff(int argc, char *argv[])
 	switch (type1) {
 	case GOT_OBJ_TYPE_BLOB:
 		error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
-		    diff_context, repo, stdout);
+		    diff_context, ignore_whitespace, repo, stdout);
 		break;
 	case GOT_OBJ_TYPE_TREE:
 		error = got_diff_objects_as_trees(id1, id2, "", "",
-		    diff_context, repo, stdout);
+		    diff_context, ignore_whitespace, repo, stdout);
 		break;
 	case GOT_OBJ_TYPE_COMMIT:
 		printf("diff %s %s\n", label1, label2);
 		error = got_diff_objects_as_commits(id1, id2, diff_context,
-		    repo, stdout);
+		    ignore_whitespace, repo, stdout);
 		break;
 	default:
 		error = got_error(GOT_ERR_OBJ_TYPE);
diff --git a/include/got_diff.h b/include/got_diff.h
index 927863b..2cbdb36 100644
--- a/include/got_diff.h
+++ b/include/got_diff.h
@@ -20,9 +20,10 @@
  * be provided which will be used to identify each blob in the diff output.
  * If a label is NULL, use the blob's SHA1 checksum instead.
  * The number of context lines to show in the diff must be specified as well.
+ * Whitespace differences may optionally be ignored.
  */
 const struct got_error *got_diff_blob(struct got_blob_object *,
-    struct got_blob_object *, const char *, const char *, int, FILE *);
+    struct got_blob_object *, const char *, const char *, int, int, FILE *);
 
 /*
  * Compute the differences between a blob and a file and write unified diff
@@ -30,9 +31,10 @@ const struct got_error *got_diff_blob(struct got_blob_object *,
  * well as a const char * diff header label which identifies the file.
  * An optional const char * diff header label for the blob may be provided, too.
  * The number of context lines to show in the diff must be specified as well.
+ * Whitespace differences may optionally be ignored.
  */
 const struct got_error *got_diff_blob_file(struct got_blob_object *,
-    const char *, FILE *, size_t, const char *, int, FILE *);
+    const char *, FILE *, size_t, const char *, int, int, FILE *);
 
 /*
  * A callback function invoked to handle the differences between two blobs
@@ -56,6 +58,7 @@ typedef const struct got_error *(*got_diff_blob_cb)(void *,
 struct got_diff_blob_output_unidiff_arg {
 	FILE *outfile;		/* Unidiff text will be written here. */
 	int diff_context;	/* Sets the number of context lines. */
+	int ignore_whitespace;	/* Ignore whitespace differences. */
 };
 const struct got_error *got_diff_blob_output_unidiff(void *,
     struct got_blob_object *, struct got_blob_object *,
@@ -81,7 +84,7 @@ const struct got_error *got_diff_tree(struct got_tree_object *,
  * Write unified diff text to the provided output FILE.
  */
 const struct got_error *got_diff_objects_as_blobs(struct got_object_id *,
-    struct got_object_id *, const char *, const char *, int,
+    struct got_object_id *, const char *, const char *, int, int,
     struct got_repository *, FILE *);
 
 /*
@@ -92,7 +95,8 @@ const struct got_error *got_diff_objects_as_blobs(struct got_object_id *,
  * Write unified diff text to the provided output FILE.
  */
 const struct got_error *got_diff_objects_as_trees(struct got_object_id *,
-    struct got_object_id *, char *, char *, int, struct got_repository *, FILE *);
+    struct got_object_id *, char *, char *, int, int,
+    struct got_repository *, FILE *);
 
 /*
  * Diff two objects, assuming both objects are commits.
@@ -100,6 +104,6 @@ const struct got_error *got_diff_objects_as_trees(struct got_object_id *,
  * Write unified diff text to the provided output FILE.
  */
 const struct got_error *got_diff_objects_as_commits(struct got_object_id *,
-    struct got_object_id *, int, struct got_repository *, FILE *);
+    struct got_object_id *, int, int, struct got_repository *, FILE *);
 
 #define GOT_DIFF_MAX_CONTEXT	64
diff --git a/lib/diff.c b/lib/diff.c
index e269983..c100c66 100644
--- a/lib/diff.c
+++ b/lib/diff.c
@@ -38,8 +38,8 @@
 
 static const struct got_error *
 diff_blobs(struct got_blob_object *blob1, struct got_blob_object *blob2,
-    const char *label1, const char *label2, int diff_context, FILE *outfile,
-    struct got_diff_changes *changes)
+    const char *label1, const char *label2, int diff_context,
+    int ignore_whitespace, FILE *outfile, struct got_diff_changes *changes)
 {
 	struct got_diff_state ds;
 	struct got_diff_args args;
@@ -106,6 +106,8 @@ diff_blobs(struct got_blob_object *blob1, struct got_blob_object *blob2,
 	args.label[1] = label2 ? label2 : idstr2;
 	args.diff_context = diff_context;
 	flags |= D_PROTOTYPE;
+	if (ignore_whitespace)
+		flags |= D_IGNOREBLANKS;
 
 	if (outfile) {
 		fprintf(outfile, "blob - %s\n", idstr1);
@@ -130,15 +132,16 @@ got_diff_blob_output_unidiff(void *arg, struct got_blob_object *blob1,
 	struct got_diff_blob_output_unidiff_arg *a = arg;
 
 	return diff_blobs(blob1, blob2, label1, label2, a->diff_context,
-	    a->outfile, NULL);
+	    a->ignore_whitespace, a->outfile, NULL);
 }
 
 const struct got_error *
 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
-    const char *label1, const char *label2, int diff_context, FILE *outfile)
+    const char *label1, const char *label2, int diff_context,
+    int ignore_whitespace, FILE *outfile)
 {
-	return diff_blobs(blob1, blob2, label1, label2, diff_context, outfile,
-	    NULL);
+	return diff_blobs(blob1, blob2, label1, label2, diff_context,
+	    ignore_whitespace, outfile, NULL);
 }
 
 static const struct got_error *
@@ -154,7 +157,7 @@ alloc_changes(struct got_diff_changes **changes)
 static const struct got_error *
 diff_blob_file(struct got_diff_changes **changes,
     struct got_blob_object *blob1, const char *label1, FILE *f2, size_t size2,
-    const char *label2, int diff_context, FILE *outfile)
+    const char *label2, int diff_context, int ignore_whitespace, FILE *outfile)
 {
 	struct got_diff_state ds;
 	struct got_diff_args args;
@@ -203,6 +206,8 @@ diff_blob_file(struct got_diff_changes **changes,
 	args.label[1] = label2;
 	args.diff_context = diff_context;
 	flags |= D_PROTOTYPE;
+	if (ignore_whitespace)
+		flags |= D_IGNOREBLANKS;
 
 	if (outfile) {
 		fprintf(outfile, "blob - %s\n", label1 ? label1 : idstr1);
@@ -226,10 +231,10 @@ done:
 const struct got_error *
 got_diff_blob_file(struct got_blob_object *blob1, const char *label1,
     FILE *f2, size_t size2, const char *label2, int diff_context,
-    FILE *outfile)
+    int ignore_whitespace, FILE *outfile)
 {
 	return diff_blob_file(NULL, blob1, label1, f2, size2, label2,
-	    diff_context, outfile);
+	    diff_context, ignore_whitespace, outfile);
 }
 
 const struct got_error *
@@ -237,7 +242,7 @@ got_diff_blob_file_lines_changed(struct got_diff_changes **changes,
     struct got_blob_object *blob1, FILE *f2, size_t size2)
 {
 	return diff_blob_file(changes, blob1, NULL, f2, size2, NULL,
-	    0, NULL);
+	    0, 0, NULL);
 }
 
 const struct got_error *
@@ -250,7 +255,7 @@ got_diff_blob_lines_changed(struct got_diff_changes **changes,
 	if (err)
 		return err;
 
-	err = diff_blobs(blob1, blob2, NULL, NULL, 3, NULL, *changes);
+	err = diff_blobs(blob1, blob2, NULL, NULL, 3, 0, NULL, *changes);
 	if (err) {
 		got_diff_free_changes(*changes);
 		*changes = NULL;
@@ -658,7 +663,7 @@ got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
 const struct got_error *
 got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
     const char *label1, const char *label2, int diff_context,
-    struct got_repository *repo, FILE *outfile)
+    int ignore_whitespace, struct got_repository *repo, FILE *outfile)
 {
 	const struct got_error *err;
 	struct got_blob_object *blob1 = NULL, *blob2 = NULL;
@@ -677,7 +682,7 @@ got_diff_objects_as_blobs(struct got_object_id *id1, struct got_object_id *id2,
 			goto done;
 	}
 	err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
-	    outfile);
+	    ignore_whitespace, outfile);
 done:
 	if (blob1)
 		got_object_blob_close(blob1);
@@ -688,8 +693,8 @@ done:
 
 const struct got_error *
 got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
-    char *label1, char *label2, int diff_context, struct got_repository *repo,
-    FILE *outfile)
+    char *label1, char *label2, int diff_context, int ignore_whitespace,
+    struct got_repository *repo, FILE *outfile)
 {
 	const struct got_error *err;
 	struct got_tree_object *tree1 = NULL, *tree2 = NULL;
@@ -709,6 +714,7 @@ got_diff_objects_as_trees(struct got_object_id *id1, struct got_object_id *id2,
 			goto done;
 	}
 	arg.diff_context = diff_context;
+	arg.ignore_whitespace = ignore_whitespace;
 	arg.outfile = outfile;
 	err = got_diff_tree(tree1, tree2, label1, label2, repo,
 	    got_diff_blob_output_unidiff, &arg, 1);
@@ -722,7 +728,7 @@ done:
 
 const struct got_error *
 got_diff_objects_as_commits(struct got_object_id *id1,
-    struct got_object_id *id2, int diff_context,
+    struct got_object_id *id2, int diff_context, int ignore_whitespace,
     struct got_repository *repo, FILE *outfile)
 {
 	const struct got_error *err;
@@ -743,8 +749,8 @@ got_diff_objects_as_commits(struct got_object_id *id1,
 
 	err = got_diff_objects_as_trees(
 	    commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
-	    got_object_commit_get_tree_id(commit2), "", "", diff_context, repo,
-	    outfile);
+	    got_object_commit_get_tree_id(commit2), "", "", diff_context,
+	    ignore_whitespace, repo, outfile);
 done:
 	if (commit1)
 		got_object_commit_close(commit1);
diff --git a/regress/cmdline/diff.sh b/regress/cmdline/diff.sh
old mode 100755
new mode 100744
index 455469c..d5083e0
--- a/regress/cmdline/diff.sh
+++ b/regress/cmdline/diff.sh
@@ -219,6 +219,36 @@ function test_diff_tag {
 	test_done "$testroot" "$ret"
 }
 
+function test_diff_ignore_whitespace {
+	local testroot=`test_init diff_ignore_whitespace`
+	local commit_id0=`git_show_head $testroot/repo`
+
+	got checkout $testroot/repo $testroot/wt > /dev/null
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "alpha   " > $testroot/wt/alpha
+
+	(cd $testroot/wt && got diff -w > $testroot/stdout)
+
+	echo "diff $commit_id0 $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
+	echo 'file + alpha' >> $testroot/stdout.expected
+
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+	fi
+	test_done "$testroot" "$ret"
+}
+
 run_test test_diff_basic
 run_test test_diff_shows_conflict
 run_test test_diff_tag
+run_test test_diff_ignore_whitespace
diff --git a/tog/tog.c b/tog/tog.c
index 9c5b076..aad8017 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -2580,11 +2580,11 @@ create_diff(struct tog_diff_view_state *s)
 	switch (obj_type) {
 	case GOT_OBJ_TYPE_BLOB:
 		err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
-		    s->diff_context, s->repo, f);
+		    s->diff_context, 0, s->repo, f);
 		break;
 	case GOT_OBJ_TYPE_TREE:
 		err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
-		    s->diff_context, s->repo, f);
+		    s->diff_context, 0, s->repo, f);
 		break;
 	case GOT_OBJ_TYPE_COMMIT: {
 		const struct got_object_id_queue *parent_ids;
@@ -2610,7 +2610,7 @@ create_diff(struct tog_diff_view_state *s)
 		got_object_commit_close(commit2);
 
 		err = got_diff_objects_as_commits(s->id1, s->id2,
-		    s->diff_context, s->repo, f);
+		    s->diff_context, 0, s->repo, f);
 		break;
 	}
 	default: