Commit df2871d297805326f5d359be74d6b309537f768b

Stefan Sperling 2018-10-18T11:45:20

make amount of diff context configurable at API level

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
diff --git a/got/got.c b/got/got.c
index 5cf3fa0..27bc2f2 100644
--- a/got/got.c
+++ b/got/got.c
@@ -290,7 +290,7 @@ print_patch(struct got_commit_object *commit, struct got_object_id *id,
 			return err;
 	}
 
-	err = got_diff_tree(tree1, tree2, "", "", repo, stdout);
+	err = got_diff_tree(tree1, tree2, "", "", 3, repo, stdout);
 	if (tree1)
 		got_object_tree_close(tree1);
 	got_object_tree_close(tree2);
@@ -639,15 +639,15 @@ cmd_diff(int argc, char *argv[])
 
 	switch (got_object_get_type(obj1)) {
 	case GOT_OBJ_TYPE_BLOB:
-		error = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
+		error = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL, 3,
 		    repo, stdout);
 		break;
 	case GOT_OBJ_TYPE_TREE:
-		error = got_diff_objects_as_trees(obj1, obj2, "", "", repo,
+		error = got_diff_objects_as_trees(obj1, obj2, "", "", 3, repo,
 		    stdout);
 		break;
 	case GOT_OBJ_TYPE_COMMIT:
-		error = got_diff_objects_as_commits(obj1, obj2, repo, stdout);
+		error = got_diff_objects_as_commits(obj1, obj2, 3, repo, stdout);
 		break;
 	default:
 		error = got_error(GOT_ERR_OBJ_TYPE);
diff --git a/include/got_diff.h b/include/got_diff.h
index c92aa2f..2d8ae7f 100644
--- a/include/got_diff.h
+++ b/include/got_diff.h
@@ -19,42 +19,47 @@
  * to the provided output FILE. Two const char * diff header labels may
  * 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.
  */
 const struct got_error *got_diff_blob(struct got_blob_object *,
-    struct got_blob_object *, const char *, const char *, FILE *);
+    struct got_blob_object *, const char *, const char *, int, FILE *);
 
 /*
  * Compute the differences between two trees and write unified diff text
  * to the provided output FILE. Two const char * diff header labels may
  * 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.
  */
 const struct got_error *got_diff_tree(struct got_tree_object *,
     struct got_tree_object *, const char *label1, const char *label2,
-    struct got_repository *, FILE *);
+    int, struct got_repository *, FILE *);
 
 /*
  * Diff two objects, assuming both objects are blobs. Two const char * diff
  * header labels may 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.
  * Write unified diff text to the provided output FILE.
  */
 const struct got_error *got_diff_objects_as_blobs(struct got_object *,
-    struct got_object *, const char *, const char *, struct got_repository *,
-    FILE *);
+    struct got_object *, const char *, const char *, int,
+    struct got_repository *, FILE *);
 
 /*
  * Diff two objects, assuming both objects are trees. Two const char * diff
  * header labels may be provided which will be used to identify each blob in
  * the trees. If a label is NULL, use the blob's SHA1 checksum instead.
+ * The number of context lines to show in diffs must be specified.
  * Write unified diff text to the provided output FILE.
  */
 const struct got_error *got_diff_objects_as_trees(struct got_object *,
-    struct got_object *, char *, char *, struct got_repository *, FILE *);
+    struct got_object *, char *, char *, int, struct got_repository *, FILE *);
 
 /*
  * Diff two objects, assuming both objects are commits.
+ * The number of context lines to show in diffs must be specified.
  * Write unified diff text to the provided output FILE.
  */
 const struct got_error *got_diff_objects_as_commits(struct got_object *,
-    struct got_object *, struct got_repository *, FILE *);
+    struct got_object *, int, struct got_repository *, FILE *);
diff --git a/lib/diff.c b/lib/diff.c
index c412692..66dfd03 100644
--- a/lib/diff.c
+++ b/lib/diff.c
@@ -35,7 +35,7 @@
 
 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,
+    const char *label1, const char *label2, int diff_context, FILE *outfile,
     struct got_diff_changes *changes)
 {
 	struct got_diff_state ds;
@@ -98,7 +98,7 @@ diff_blobs(struct got_blob_object *blob1, struct got_blob_object *blob2,
 	args.diff_format = D_UNIFIED;
 	args.label[0] = label1 ? label1 : idstr1;
 	args.label[1] = label2 ? label2 : idstr2;
-	args.diff_context = 3;
+	args.diff_context = diff_context;
 	flags |= D_PROTOTYPE;
 
 	if (label1 && strcmp(label1, idstr1) != 0)
@@ -117,9 +117,10 @@ done:
 
 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)
+    const char *label1, const char *label2, int diff_context, FILE *outfile)
 {
-	return diff_blobs(blob1, blob2, label1, label2, outfile, NULL);
+	return diff_blobs(blob1, blob2, label1, label2, diff_context, outfile,
+	    NULL);
 }
 
 const struct got_error *
@@ -133,7 +134,7 @@ got_diff_blob_lines_changed(struct got_diff_changes **changes,
 		return got_error_from_errno();
 	SIMPLEQ_INIT(&(*changes)->entries);
 
-	err = diff_blobs(blob1, blob2, NULL, NULL, NULL, *changes);
+	err = diff_blobs(blob1, blob2, NULL, NULL, 3, NULL, *changes);
 	if (err) {
 		got_diff_free_changes(*changes);
 		*changes = NULL;
@@ -169,7 +170,7 @@ match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
 
 static const struct got_error *
 diff_added_blob(struct got_object_id *id, const char *label,
-    struct got_repository *repo, FILE *outfile)
+    int diff_context, struct got_repository *repo, FILE *outfile)
 {
 	const struct got_error *err;
 	struct got_blob_object  *blob = NULL;
@@ -182,7 +183,7 @@ diff_added_blob(struct got_object_id *id, const char *label,
 	err = got_object_blob_open(&blob, repo, obj, 8192);
 	if (err)
 		goto done;
-	err = got_diff_blob(NULL, blob, NULL, label, outfile);
+	err = got_diff_blob(NULL, blob, NULL, label, diff_context, outfile);
 done:
 	got_object_close(obj);
 	if (blob)
@@ -192,8 +193,8 @@ done:
 
 static const struct got_error *
 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
-    const char *label1, const char *label2, struct got_repository *repo,
-    FILE *outfile)
+    const char *label1, const char *label2, int diff_context,
+    struct got_repository *repo, FILE *outfile)
 {
 	const struct got_error *err;
 	struct got_object *obj1 = NULL;
@@ -225,7 +226,8 @@ diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
 	if (err)
 		goto done;
 
-	err = got_diff_blob(blob1, blob2, label1, label2, outfile);
+	err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
+	    outfile);
 
 done:
 	if (obj1)
@@ -241,7 +243,7 @@ done:
 
 static const struct got_error *
 diff_deleted_blob(struct got_object_id *id, const char *label,
-    struct got_repository *repo, FILE *outfile)
+    int diff_context, struct got_repository *repo, FILE *outfile)
 {
 	const struct got_error *err;
 	struct got_blob_object  *blob = NULL;
@@ -254,7 +256,7 @@ diff_deleted_blob(struct got_object_id *id, const char *label,
 	err = got_object_blob_open(&blob, repo, obj, 8192);
 	if (err)
 		goto done;
-	err = got_diff_blob(blob, NULL, label, NULL, outfile);
+	err = got_diff_blob(blob, NULL, label, NULL, diff_context, outfile);
 done:
 	got_object_close(obj);
 	if (blob)
@@ -264,7 +266,7 @@ done:
 
 static const struct got_error *
 diff_added_tree(struct got_object_id *id, const char *label,
-    struct got_repository *repo, FILE *outfile)
+    int diff_context, struct got_repository *repo, FILE *outfile)
 {
 	const struct got_error *err = NULL;
 	struct got_object *treeobj = NULL;
@@ -283,7 +285,8 @@ diff_added_tree(struct got_object_id *id, const char *label,
 	if (err)
 		goto done;
 
-	err = got_diff_tree(NULL, tree, NULL, label, repo, outfile);
+	err = got_diff_tree(NULL, tree, NULL, label, diff_context, repo,
+	    outfile);
 
 done:
 	if (tree)
@@ -295,8 +298,8 @@ done:
 
 static const struct got_error *
 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
-    const char *label1, const char *label2, struct got_repository *repo,
-    FILE *outfile)
+    const char *label1, const char *label2, int diff_context,
+    struct got_repository *repo, FILE *outfile)
 {
 	const struct got_error *err;
 	struct got_object *treeobj1 = NULL;
@@ -330,7 +333,8 @@ diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
 	if (err)
 		goto done;
 
-	err = got_diff_tree(tree1, tree2, label1, label2, repo, outfile);
+	err = got_diff_tree(tree1, tree2, label1, label2, diff_context, repo,
+	    outfile);
 
 done:
 	if (tree1)
@@ -346,7 +350,7 @@ done:
 
 static const struct got_error *
 diff_deleted_tree(struct got_object_id *id, const char *label,
-    struct got_repository *repo, FILE *outfile)
+    int diff_context, struct got_repository *repo, FILE *outfile)
 {
 	const struct got_error *err;
 	struct got_object *treeobj = NULL;
@@ -365,7 +369,8 @@ diff_deleted_tree(struct got_object_id *id, const char *label,
 	if (err)
 		goto done;
 
-	err = got_diff_tree(tree, NULL, label, NULL, repo, outfile);
+	err = got_diff_tree(tree, NULL, label, NULL, diff_context, repo,
+	    outfile);
 done:
 	if (tree)
 		got_object_tree_close(tree);
@@ -384,27 +389,29 @@ diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
 
 static const struct got_error *
 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
-    const char *label1, const char *label2, struct got_repository *repo,
-    FILE *outfile)
+    const char *label1, const char *label2, int diff_context,
+    struct got_repository *repo, FILE *outfile)
 {
 	const struct got_error *err = NULL;
 
 	if (te2 == NULL) {
 		if (S_ISDIR(te1->mode))
-			err = diff_deleted_tree(te1->id, label1, repo, outfile);
+			err = diff_deleted_tree(te1->id, label1, diff_context,
+			    repo, outfile);
 		else
-			err = diff_deleted_blob(te1->id, label1, repo, outfile);
+			err = diff_deleted_blob(te1->id, label1, diff_context,
+			    repo, outfile);
 		return err;
 	}
 
 	if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
 		if (got_object_id_cmp(te1->id, te2->id) != 0)
 			return diff_modified_tree(te1->id, te2->id,
-			    label1, label2, repo, outfile);
+			    label1, label2, diff_context, repo, outfile);
 	} else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
 		if (got_object_id_cmp(te1->id, te2->id) != 0)
 			return diff_modified_blob(te1->id, te2->id,
-			    label1, label2, repo, outfile);
+			    label1, label2, diff_context, repo, outfile);
 	}
 
 	if (got_object_id_cmp(te1->id, te2->id) == 0)
@@ -415,21 +422,23 @@ diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
 
 static const struct got_error *
 diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_entry *te1,
-    const char *label2, struct got_repository *repo, FILE *outfile)
+    const char *label2, int diff_context, struct got_repository *repo,
+    FILE *outfile)
 {
 	if (te1 != NULL) /* handled by diff_entry_old_new() */
 		return NULL;
 
 	if (S_ISDIR(te2->mode))
-		return diff_added_tree(te2->id, label2, repo, outfile);
+		return diff_added_tree(te2->id, label2, diff_context, repo,
+		    outfile);
 
-	return diff_added_blob(te2->id, label2, repo, outfile);
+	return diff_added_blob(te2->id, label2, diff_context, repo, outfile);
 }
 
 const struct got_error *
 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
-    const char *label1, const char *label2, struct got_repository *repo,
-    FILE *outfile)
+    const char *label1, const char *label2, int diff_context,
+    struct got_repository *repo, FILE *outfile)
 {
 	const struct got_error *err = NULL;
 	struct got_tree_entry *te1 = NULL;
@@ -465,8 +474,8 @@ got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
 				    label2[0] ? "/" : "", te->name) == -1)
 					return got_error_from_errno();
 			}
-			err = diff_entry_old_new(te1, te, l1, l2, repo,
-			    outfile);
+			err = diff_entry_old_new(te1, te, l1, l2, diff_context,
+			    repo, outfile);
 			if (err)
 				break;
 		}
@@ -475,7 +484,8 @@ got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
 			struct got_tree_entry *te = NULL;
 			if (tree1)
 				te = match_entry_by_name(te2, tree1);
-			err = diff_entry_new_old(te2, te, l2, repo, outfile);
+			err = diff_entry_new_old(te2, te, l2, diff_context,
+			    repo, outfile);
 			if (err)
 				break;
 		}
@@ -505,8 +515,8 @@ 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 *obj1, struct got_object *obj2,
-    const char *label1, const char *label2, struct got_repository *repo,
-    FILE *outfile)
+    const char *label1, const char *label2, int diff_context,
+    struct got_repository *repo, FILE *outfile)
 {
 	const struct got_error *err;
 	struct got_blob_object *blob1 = NULL, *blob2 = NULL;
@@ -524,7 +534,8 @@ got_diff_objects_as_blobs(struct got_object *obj1, struct got_object *obj2,
 		if (err)
 			goto done;
 	}
-	err = got_diff_blob(blob1, blob2, label1, label2, outfile);
+	err = got_diff_blob(blob1, blob2, label1, label2, diff_context,
+	    outfile);
 done:
 	if (blob1)
 		got_object_blob_close(blob1);
@@ -535,7 +546,8 @@ done:
 
 const struct got_error *
 got_diff_objects_as_trees(struct got_object *obj1, struct got_object *obj2,
-    char *label1, char *label2, struct got_repository *repo, FILE *outfile)
+    char *label1, char *label2, int diff_context, struct got_repository *repo,
+    FILE *outfile)
 {
 	const struct got_error *err;
 	struct got_tree_object *tree1 = NULL, *tree2 = NULL;
@@ -553,7 +565,8 @@ got_diff_objects_as_trees(struct got_object *obj1, struct got_object *obj2,
 		if (err)
 			goto done;
 	}
-	err = got_diff_tree(tree1, tree2, label1, label2, repo, outfile);
+	err = got_diff_tree(tree1, tree2, label1, label2, diff_context,
+	   repo, outfile);
 done:
 	if (tree1)
 		got_object_tree_close(tree1);
@@ -574,7 +587,7 @@ get_datestr(time_t *time, char *datebuf)
 
 const struct got_error *
 got_diff_objects_as_commits(struct got_object *obj1, struct got_object *obj2,
-    struct got_repository *repo, FILE *outfile)
+    int diff_context, struct got_repository *repo, FILE *outfile)
 {
 	const struct got_error *err;
 	struct got_commit_object *commit1 = NULL, *commit2 = NULL;
@@ -630,8 +643,8 @@ got_diff_objects_as_commits(struct got_object *obj1, struct got_object *obj2,
 		goto done;
 	}
 
-	err = got_diff_objects_as_trees(tree_obj1, tree_obj2, "", "", repo,
-	    outfile);
+	err = got_diff_objects_as_trees(tree_obj1, tree_obj2, "", "",
+	    diff_context, repo, outfile);
 done:
 	if (tree_obj1)
 		got_object_close(tree_obj1);
diff --git a/regress/repository/repository_test.c b/regress/repository/repository_test.c
index 7d05f1a..faf7e0d 100644
--- a/regress/repository/repository_test.c
+++ b/regress/repository/repository_test.c
@@ -355,7 +355,7 @@ repo_diff_blob(const char *repo_path)
 	outfile = got_opentemp();
 	if (outfile == NULL)
 		return 0;
-	got_diff_blob(blob1, blob2, NULL, NULL, outfile);
+	got_diff_blob(blob1, blob2, NULL, NULL, 3, outfile);
 	rewind(outfile);
 	i = 0;
 	while ((line = fparseln(outfile, &len, NULL, delim, 0)) != NULL) {
@@ -428,7 +428,7 @@ repo_diff_tree(const char *repo_path)
 	} else
 		outfile = stdout;
 	test_printf("\n");
-	got_diff_tree(tree1, tree2, "", "", repo, outfile);
+	got_diff_tree(tree1, tree2, "", "", 3, repo, outfile);
 	test_printf("\n");
 
 	got_object_tree_close(tree1);
diff --git a/tog/tog.c b/tog/tog.c
index faaa00b..d57ef70 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -1625,14 +1625,14 @@ open_diff_view(struct tog_view *view, struct got_object *obj1,
 
 	switch (got_object_get_type(obj1 ? obj1 : obj2)) {
 	case GOT_OBJ_TYPE_BLOB:
-		err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
+		err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL, 3,
 		    repo, f);
 		break;
 	case GOT_OBJ_TYPE_TREE:
-		err = got_diff_objects_as_trees(obj1, obj2, "", "", repo, f);
+		err = got_diff_objects_as_trees(obj1, obj2, "", "", 3, repo, f);
 		break;
 	case GOT_OBJ_TYPE_COMMIT:
-		err = got_diff_objects_as_commits(obj1, obj2, repo, f);
+		err = got_diff_objects_as_commits(obj1, obj2, 3, repo, f);
 		break;
 	default:
 		return got_error(GOT_ERR_OBJ_TYPE);