Commit 0208f208304c36921fbcd86d33751b877aab1e96

Stefan Sperling 2020-05-05T09:53:57

show a list of paths changed in a commit with 'got log -P', and in tog requested by matthieu@

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
diff --git a/got/got.1 b/got/got.1
index b6a8a54..37523a3 100644
--- a/got/got.1
+++ b/got/got.1
@@ -683,7 +683,7 @@ in a pattern.
 .It Cm st
 Short alias for
 .Cm status .
-.It Cm log Oo Fl b Oc Oo Fl c Ar commit Oc Oo Fl C Ar number Oc Oo Fl l Ar N Oc Oo Fl p Oc Oo Fl s Ar search-pattern Oc Oo Fl r Ar repository-path Oc Oo Fl R Oc Oo Fl x Ar commit Oc Op Ar path
+.It Cm log Oo Fl b Oc Oo Fl c Ar commit Oc Oo Fl C Ar number Oc Oo Fl l Ar N Oc Oo Fl p Oc Oo Fl P Oc Oo Fl s Ar search-pattern Oc Oo Fl r Ar repository-path Oc Oo Fl R Oc Oo Fl x Ar commit Oc Op Ar path
 Display history of a repository.
 If a
 .Ar path
@@ -729,10 +729,22 @@ Display the patch of modifications made in each commit.
 If a
 .Ar path
 is specified, only show the patch of modifications at or within this path.
+.It Fl P
+Display the list of file paths changed in each commit, using the following
+status codes:
+.Bl -column YXZ description
+.It M Ta modified file
+.It D Ta file was deleted
+.It A Ta new file was added
+.It m Ta modified file modes (executable bit only)
+.El
 .It Fl s Ar search-pattern
 If specified, show only commits with a log message matched by the extended
 regular expression
 .Ar search-pattern .
+When used together with
+.Fl P
+then the file paths changed by a commit can be matched as well.
 Regular expression syntax is documented in
 .Xr re_format 7 .
 .It Fl r Ar repository-path
diff --git a/got/got.c b/got/got.c
index 874e65a..59658b3 100644
--- a/got/got.c
+++ b/got/got.c
@@ -2892,6 +2892,49 @@ done:
 }
 
 static const struct got_error *
+get_changed_paths(struct got_pathlist_head *paths,
+    struct got_commit_object *commit, struct got_repository *repo)
+{
+	const struct got_error *err = NULL;
+	struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
+	struct got_tree_object *tree1 = NULL, *tree2 = NULL;
+	struct got_object_qid *qid;
+
+	qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
+	if (qid != NULL) {
+		struct got_commit_object *pcommit;
+		err = got_object_open_as_commit(&pcommit, repo,
+		    qid->id);
+		if (err)
+			return err;
+
+		tree_id1 = got_object_commit_get_tree_id(pcommit);
+		got_object_commit_close(pcommit);
+
+	}
+
+	if (tree_id1) {
+		err = got_object_open_as_tree(&tree1, repo, tree_id1);
+		if (err)
+			goto done;
+	}
+
+	tree_id2 = got_object_commit_get_tree_id(commit);
+	err = got_object_open_as_tree(&tree2, repo, tree_id2);
+	if (err)
+		goto done;
+
+	err = got_diff_tree(tree1, tree2, "", "", repo,
+	    got_diff_tree_collect_changed_paths, paths, 0);
+done:
+	if (tree1)
+		got_object_tree_close(tree1);
+	if (tree2)
+		got_object_tree_close(tree2);
+	return err;
+}
+
+static const struct got_error *
 print_patch(struct got_commit_object *commit, struct got_object_id *id,
     const char *path, int diff_context, struct got_repository *repo)
 {
@@ -3020,11 +3063,29 @@ done:
 	return err;
 }
 
+static void
+match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
+    regex_t *regex)
+{
+	regmatch_t regmatch;
+	struct got_pathlist_entry *pe;
+
+	*have_match = 0;
+
+	TAILQ_FOREACH(pe, changed_paths, entry) {
+		if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
+			*have_match = 1;
+			break;
+		}
+	}
+}
+
 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
 
 static const struct got_error *
 print_commit(struct got_commit_object *commit, struct got_object_id *id,
-    struct got_repository *repo, const char *path, int show_patch,
+    struct got_repository *repo, const char *path,
+    struct got_pathlist_head *changed_paths, int show_patch,
     int diff_context, struct got_reflist_head *refs)
 {
 	const struct got_error *err = NULL;
@@ -3127,6 +3188,14 @@ print_commit(struct got_commit_object *commit, struct got_object_id *id,
 	} while (line);
 	free(logmsg0);
 
+	if (changed_paths) {
+		struct got_pathlist_entry *pe;
+		TAILQ_FOREACH(pe, changed_paths, entry) {
+			struct got_diff_changed_path *cp = pe->data;
+			printf(" %c  %s\n", cp->status, pe->path);
+		}
+		printf("\n");
+	}
 	if (show_patch) {
 		err = print_patch(commit, id, path, diff_context, repo);
 		if (err == 0)
@@ -3140,9 +3209,9 @@ print_commit(struct got_commit_object *commit, struct got_object_id *id,
 
 static const struct got_error *
 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
-    struct got_repository *repo, const char *path, int show_patch,
-    const char *search_pattern, int diff_context, int limit, int log_branches,
-    int reverse_display_order, struct got_reflist_head *refs)
+    struct got_repository *repo, const char *path, int show_changed_paths,
+    int show_patch, const char *search_pattern, int diff_context, int limit,
+    int log_branches, int reverse_display_order, struct got_reflist_head *refs)
 {
 	const struct got_error *err;
 	struct got_commit_graph *graph;
@@ -3151,8 +3220,11 @@ print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
 	struct got_object_id_queue reversed_commits;
 	struct got_object_qid *qid;
 	struct got_commit_object *commit;
+	struct got_pathlist_head changed_paths;
+	struct got_pathlist_entry *pe;
 
 	SIMPLEQ_INIT(&reversed_commits);
+	TAILQ_INIT(&changed_paths);
 
 	if (search_pattern && regcomp(&regex, search_pattern,
 	    REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
@@ -3185,14 +3257,28 @@ print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
 		if (err)
 			break;
 
+		if (show_changed_paths) {
+			err = get_changed_paths(&changed_paths, commit, repo);
+			if (err)
+				break;
+		}
+
 		if (search_pattern) {
 			err = match_logmsg(&have_match, id, commit, &regex);
 			if (err) {
 				got_object_commit_close(commit);
 				break;
 			}
+			if (have_match == 0 && show_changed_paths)
+				match_changed_paths(&have_match,
+				    &changed_paths, &regex);
 			if (have_match == 0) {
 				got_object_commit_close(commit);
+				TAILQ_FOREACH(pe, &changed_paths, entry) {
+					free((char *)pe->path);
+					free(pe->data);
+				}
+				got_pathlist_free(&changed_paths);
 				continue;
 			}
 		}
@@ -3204,8 +3290,9 @@ print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
 			SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
 			got_object_commit_close(commit);
 		} else {
-			err = print_commit(commit, id, repo, path, show_patch,
-			    diff_context, refs);
+			err = print_commit(commit, id, repo, path,
+			    show_changed_paths ? &changed_paths : NULL,
+			    show_patch, diff_context, refs);
 			got_object_commit_close(commit);
 			if (err)
 				break;
@@ -3213,6 +3300,12 @@ print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
 		if ((limit && --limit == 0) ||
 		    (end_id && got_object_id_cmp(id, end_id) == 0))
 			break;
+
+		TAILQ_FOREACH(pe, &changed_paths, entry) {
+			free((char *)pe->path);
+			free(pe->data);
+		}
+		got_pathlist_free(&changed_paths);
 	}
 	if (reverse_display_order) {
 		SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
@@ -3220,6 +3313,7 @@ print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
 			if (err)
 				break;
 			err = print_commit(commit, qid->id, repo, path,
+			    show_changed_paths ? &changed_paths : NULL,
 			    show_patch, diff_context, refs);
 			got_object_commit_close(commit);
 			if (err)
@@ -3232,6 +3326,11 @@ done:
 		SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
 		got_object_qid_free(qid);
 	}
+	TAILQ_FOREACH(pe, &changed_paths, entry) {
+		free((char *)pe->path);
+		free(pe->data);
+	}
+	got_pathlist_free(&changed_paths);
 	if (search_pattern)
 		regfree(&regex);
 	got_commit_graph_close(graph);
@@ -3242,8 +3341,8 @@ __dead static void
 usage_log(void)
 {
 	fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
-	    "[-p] [-x commit] [-s search-pattern] [-r repository-path] [-R] "
-	    "[path]\n", getprogname());
+	    "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
+	    "[-R] [path]\n", getprogname());
 	exit(1);
 }
 
@@ -3322,7 +3421,7 @@ cmd_log(int argc, char *argv[])
 	const char *start_commit = NULL, *end_commit = NULL;
 	const char *search_pattern = NULL;
 	int diff_context = -1, ch;
-	int show_patch = 0, limit = 0, log_branches = 0;
+	int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
 	int reverse_display_order = 0;
 	const char *errstr;
 	struct got_reflist_head refs;
@@ -3338,11 +3437,14 @@ cmd_log(int argc, char *argv[])
 
 	limit = get_default_log_limit();
 
-	while ((ch = getopt(argc, argv, "bpc:C:l:r:Rs:x:")) != -1) {
+	while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
 		switch (ch) {
 		case 'p':
 			show_patch = 1;
 			break;
+		case 'P':
+			show_changed_paths = 1;
+			break;
 		case 'c':
 			start_commit = optarg;
 			break;
@@ -3494,8 +3596,8 @@ cmd_log(int argc, char *argv[])
 	if (error)
 		goto done;
 
-	error = print_commits(start_id, end_id, repo, path, show_patch,
-	    search_pattern, diff_context, limit, log_branches,
+	error = print_commits(start_id, end_id, repo, path, show_changed_paths,
+	    show_patch, search_pattern, diff_context, limit, log_branches,
 	    reverse_display_order, &refs);
 done:
 	free(path);
diff --git a/include/got_diff.h b/include/got_diff.h
index d7f3479..fd8185f 100644
--- a/include/got_diff.h
+++ b/include/got_diff.h
@@ -79,6 +79,27 @@ const struct got_error *got_diff_tree(struct got_tree_object *,
     struct got_repository *, got_diff_blob_cb cb, void *cb_arg, int);
 
 /*
+ * A pre-defined implementation of got_diff_blob_cb() which collects a list
+ * of file paths that differ between two trees.
+ * The caller must allocate and initialize a got_pathlist_head * argument.
+ * Data pointers of entries added to the path list will point to a struct
+ * got_diff_changed_path object.
+ * The caller is expected to free both the path and data pointers of all
+ * entries on the path list.
+ */
+struct got_diff_changed_path {
+	/*
+	 * The modification status of this path. It can be GOT_STATUS_ADD,
+	 * GOT_STATUS_DELETE, GOT_STATUS_MODIFY, or GOT_STATUS_MODE_CHANGE.
+	 */
+	int status;
+};
+const struct got_error *got_diff_tree_collect_changed_paths(void *,
+    struct got_blob_object *, struct got_blob_object *,
+    struct got_object_id *, struct got_object_id *,
+    const char *, const char *, mode_t, mode_t, struct got_repository *);
+
+/*
  * 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.
diff --git a/lib/diff.c b/lib/diff.c
index 7874be9..152352c 100644
--- a/lib/diff.c
+++ b/lib/diff.c
@@ -30,6 +30,8 @@
 #include "got_diff.h"
 #include "got_opentemp.h"
 #include "got_path.h"
+#include "got_cancel.h"
+#include "got_worktree.h"
 
 #include "got_lib_diff.h"
 #include "got_lib_delta.h"
@@ -594,6 +596,48 @@ diff_entry_new_old(struct got_tree_entry *te2,
 }
 
 const struct got_error *
+got_diff_tree_collect_changed_paths(void *arg, struct got_blob_object *blob1,
+    struct got_blob_object *blob2, struct got_object_id *id1,
+    struct got_object_id *id2, const char *label1, const char *label2,
+    mode_t mode1, mode_t mode2, struct got_repository *repo)
+{
+	const struct got_error *err = NULL;
+	struct got_pathlist_head *paths = arg;
+	struct got_diff_changed_path *change = NULL;
+	char *path = NULL;
+
+	path = strdup(label2 ? label2 : label1);
+	if (path == NULL)
+		return got_error_from_errno("malloc");
+
+	change = malloc(sizeof(*change));
+	if (change == NULL) {
+		err = got_error_from_errno("malloc");
+		goto done;
+	}
+
+	change->status = GOT_STATUS_NO_CHANGE;
+	if (id1 == NULL)
+		change->status = GOT_STATUS_ADD;
+	else if (id2 == NULL)
+		change->status = GOT_STATUS_DELETE;
+	else {
+		if (got_object_id_cmp(id1, id2) != 0)
+			change->status = GOT_STATUS_MODIFY;
+		else if (mode1 != mode2)
+			change->status = GOT_STATUS_MODE_CHANGE;
+	}
+
+	err = got_pathlist_insert(NULL, paths, path, change);
+done:
+	if (err) {
+		free(path);
+		free(change);
+	}
+	return err;
+}
+
+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,
     got_diff_blob_cb cb, void *cb_arg, int diff_content)
diff --git a/regress/cmdline/log.sh b/regress/cmdline/log.sh
index b05b0f4..d88d0fa 100755
--- a/regress/cmdline/log.sh
+++ b/regress/cmdline/log.sh
@@ -646,6 +646,50 @@ function test_log_in_worktree_different_repo {
 	test_done "$testroot" "0"
 }
 
+function test_log_changed_paths {
+	local testroot=`test_init log_changed_paths`
+	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 "modified alpha" > $testroot/wt/alpha
+	(cd $testroot/wt && got commit -m 'test log_changed_paths' > /dev/null)
+	local commit_id1=`git_show_head $testroot/repo`
+
+	(cd $testroot/wt && got rm beta >/dev/null)
+	(cd $testroot/wt && chmod +x epsilon/zeta >/dev/null)
+	(cd $testroot/wt && got commit -m 'test log_changed_paths' > /dev/null)
+	local commit_id2=`git_show_head $testroot/repo`
+
+	echo "new file" > $testroot/wt/new
+	(cd $testroot/wt && got add new >/dev/null)
+	(cd $testroot/wt && got commit -m 'test log_changed_paths' > /dev/null)
+	local commit_id3=`git_show_head $testroot/repo`
+
+	(cd $testroot/wt && got log -P | grep '^ [MDmA]' > $testroot/stdout)
+
+	echo " A  new" > $testroot/stdout.expected
+	echo " D  beta" >> $testroot/stdout.expected
+	echo " m  epsilon/zeta" >> $testroot/stdout.expected
+	echo " M  alpha" >> $testroot/stdout.expected
+	echo " A  alpha" >> $testroot/stdout.expected
+	echo " A  beta" >> $testroot/stdout.expected
+	echo " A  epsilon/zeta" >> $testroot/stdout.expected
+	echo " A  gamma/delta" >> $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_log_in_repo
 run_test test_log_in_bare_repo
 run_test test_log_in_worktree
@@ -657,3 +701,4 @@ run_test test_log_nonexistent_path
 run_test test_log_end_at_commit
 run_test test_log_reverse_display
 run_test test_log_in_worktree_different_repo
+run_test test_log_changed_paths
diff --git a/tog/tog.c b/tog/tog.c
index 59e36d0..de21c04 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -2894,6 +2894,49 @@ get_datestr(time_t *time, char *datebuf)
 }
 
 static const struct got_error *
+get_changed_paths(struct got_pathlist_head *paths,
+    struct got_commit_object *commit, struct got_repository *repo)
+{
+	const struct got_error *err = NULL;
+	struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
+	struct got_tree_object *tree1 = NULL, *tree2 = NULL;
+	struct got_object_qid *qid;
+
+	qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
+	if (qid != NULL) {
+		struct got_commit_object *pcommit;
+		err = got_object_open_as_commit(&pcommit, repo,
+		    qid->id);
+		if (err)
+			return err;
+
+		tree_id1 = got_object_commit_get_tree_id(pcommit);
+		got_object_commit_close(pcommit);
+
+	}
+
+	if (tree_id1) {
+		err = got_object_open_as_tree(&tree1, repo, tree_id1);
+		if (err)
+			goto done;
+	}
+
+	tree_id2 = got_object_commit_get_tree_id(commit);
+	err = got_object_open_as_tree(&tree2, repo, tree_id2);
+	if (err)
+		goto done;
+
+	err = got_diff_tree(tree1, tree2, "", "", repo,
+	    got_diff_tree_collect_changed_paths, paths, 0);
+done:
+	if (tree1)
+		got_object_tree_close(tree1);
+	if (tree2)
+		got_object_tree_close(tree2);
+	return err;
+}
+
+static const struct got_error *
 write_commit_info(struct got_object_id *commit_id,
     struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
 {
@@ -2904,6 +2947,10 @@ write_commit_info(struct got_object_id *commit_id,
 	time_t committer_time;
 	const char *author, *committer;
 	char *refs_str = NULL;
+	struct got_pathlist_head changed_paths;
+	struct got_pathlist_entry *pe;
+
+	TAILQ_INIT(&changed_paths);
 
 	if (refs) {
 		err = build_refs_str(&refs_str, refs, commit_id, repo);
@@ -2951,7 +2998,18 @@ write_commit_info(struct got_object_id *commit_id,
 		err = got_error_from_errno("fprintf");
 		goto done;
 	}
+	err = get_changed_paths(&changed_paths, commit, repo);
+	if (err)
+		goto done;
+	TAILQ_FOREACH(pe, &changed_paths, entry) {
+		struct got_diff_changed_path *cp = pe->data;
+		fprintf(outfile, "%c  %s\n", cp->status, pe->path);
+		free((char *)pe->path);
+		free(pe->data);
+	}
+	fputc('\n', outfile);
 done:
+	got_pathlist_free(&changed_paths);
 	free(id_str);
 	free(logmsg);
 	free(refs_str);
@@ -3089,7 +3147,7 @@ create_diff(struct tog_diff_view_state *s)
 			goto done;
 		/* Show commit info if we're diffing to a parent/root commit. */
 		if (s->id1 == NULL) {
-			err =write_commit_info(s->id2, s->refs, s->repo, s->f);
+			err = write_commit_info(s->id2, s->refs, s->repo, s->f);
 			if (err)
 				goto done;
 		} else {
@@ -3282,7 +3340,8 @@ open_diff_view(struct tog_view *view, struct got_object_id *id1,
 		}
 
 		err = add_color(&s->colors,
-		    "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
+		    "^(commit|(blob|file) [-+] |[MDmA]  [^ ])",
+		    TOG_COLOR_DIFF_META,
 		    get_color_value("TOG_COLOR_DIFF_META"));
 		if (err) {
 			free_colors(&s->colors);