Commit f2ea84fab96c77d352fe460a37f2722beb6225d7

Stefan Sperling 2019-07-27T14:26:10

make 'got update' accept mulitple path arguments

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
diff --git a/got/got.1 b/got/got.1
index 50fbe74..5d1c026 100644
--- a/got/got.1
+++ b/got/got.1
@@ -158,7 +158,7 @@ will be checked out.
 .It Cm co
 Short alias for
 .Cm checkout .
-.It Cm update [ Fl b Ar branch ] [ Fl c Ar commit ] [ Ar path ]
+.It Cm update [ Fl b Ar branch ] [ Fl c Ar commit ] [ Ar path ... ]
 Update an existing work tree to a different commit.
 Show the status of each affected file, using the following status codes:
 .Bl -column YXZ description
@@ -171,12 +171,14 @@ Show the status of each affected file, using the following status codes:
 .It ! Ta a missing versioned file was restored
 .El
 .Pp
-If a
+If no
 .Ar path
-is specified, restrict the update operation to files at or within this path.
-The path is required to exist in the update operation's target commit.
-Files in the work tree outside this path will remain unchanged and will
-retain their previously recorded base commit.
+is specified, update the entire work tree.
+Otherwise, restrict the update operation to files at or within the
+specified paths.
+Each path is required to exist in the update operation's target commit.
+Files in the work tree outside specified paths will remain unchanged and
+will retain their previously recorded base commit.
 Some
 .Nm
 commands may refuse to run while the work tree contains files from
diff --git a/got/got.c b/got/got.c
index 85b2641..0d710de 100644
--- a/got/got.c
+++ b/got/got.c
@@ -791,6 +791,9 @@ cmd_checkout(int argc, char *argv[])
 	const char *branch_name = GOT_REF_HEAD;
 	char *commit_id_str = NULL;
 	int ch, same_path_prefix;
+	struct got_pathlist_head paths;
+
+	TAILQ_INIT(&paths);
 
 	while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
 		switch (ch) {
@@ -940,7 +943,10 @@ cmd_checkout(int argc, char *argv[])
 			goto done;
 	}
 
-	error = got_worktree_checkout_files(worktree, "", repo,
+	error = got_pathlist_append(NULL, &paths, "", NULL);
+	if (error)
+		goto done;
+	error = got_worktree_checkout_files(worktree, &paths, repo,
 	    checkout_progress, worktree_path, check_cancelled, NULL);
 	if (error != NULL)
 		goto done;
@@ -948,6 +954,7 @@ cmd_checkout(int argc, char *argv[])
 	printf("Now shut up and hack\n");
 
 done:
+	got_pathlist_free(&paths);
 	free(commit_id_str);
 	free(repo_path);
 	free(worktree_path);
@@ -957,7 +964,7 @@ done:
 __dead static void
 usage_update(void)
 {
-	fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
+	fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
 	    getprogname());
 	exit(1);
 }
@@ -1078,13 +1085,17 @@ cmd_update(int argc, char *argv[])
 	const struct got_error *error = NULL;
 	struct got_repository *repo = NULL;
 	struct got_worktree *worktree = NULL;
-	char *worktree_path = NULL, *path = NULL;
+	char *worktree_path = NULL;
 	struct got_object_id *commit_id = NULL;
 	char *commit_id_str = NULL;
 	const char *branch_name = NULL;
 	struct got_reference *head_ref = NULL;
+	struct got_pathlist_head paths;
+	struct got_pathlist_entry *pe;
 	int ch, did_something = 0;
 
+	TAILQ_INIT(&paths);
+
 	while ((ch = getopt(argc, argv, "b:c:")) != -1) {
 		switch (ch) {
 		case 'b':
@@ -1122,18 +1133,9 @@ cmd_update(int argc, char *argv[])
 	if (error)
 		goto done;
 
-	if (argc == 0) {
-		path = strdup("");
-		if (path == NULL) {
-			error = got_error_from_errno("strdup");
-			goto done;
-		}
-	} else if (argc == 1) {
-		error = got_worktree_resolve_path(&path, worktree, argv[0]);
-		if (error)
-			goto done;
-	} else
-		usage_update();
+	error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
+	if (error)
+		goto done;
 
 	error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
 	if (error != NULL)
@@ -1168,12 +1170,12 @@ cmd_update(int argc, char *argv[])
 
 	if (branch_name) {
 		struct got_object_id *head_commit_id;
-		if (strlen(path) != 0) {
-			fprintf(stderr, "%s: switching between branches "
-			    "requires that the entire work tree "
-			    "gets updated, not just '%s'\n",
-			    getprogname(), path);
-			error = got_error(GOT_ERR_BAD_PATH);
+		TAILQ_FOREACH(pe, &paths, entry) {
+			if (strlen(pe->path) == 0)
+				continue;
+			error = got_error_msg(GOT_ERR_BAD_PATH,
+			    "switching between branches requires that "
+			    "the entire work tree gets updated");
 			goto done;
 		}
 		error = got_ref_resolve(&head_commit_id, repo, head_ref);
@@ -1210,7 +1212,7 @@ cmd_update(int argc, char *argv[])
 			goto done;
 	}
 
-	error = got_worktree_checkout_files(worktree, path, repo,
+	error = got_worktree_checkout_files(worktree, &paths, repo,
 	    update_progress, &did_something, check_cancelled, NULL);
 	if (error != NULL)
 		goto done;
@@ -1221,7 +1223,9 @@ cmd_update(int argc, char *argv[])
 		printf("Already up-to-date\n");
 done:
 	free(worktree_path);
-	free(path);
+	TAILQ_FOREACH(pe, &paths, entry)
+		free((char *)pe->path);
+	got_pathlist_free(&paths);
 	free(commit_id);
 	free(commit_id_str);
 	return error;
diff --git a/include/got_path.h b/include/got_path.h
index c5070b4..56a5865 100644
--- a/include/got_path.h
+++ b/include/got_path.h
@@ -101,6 +101,9 @@ int got_path_dir_is_empty(const char *);
 /* dirname(3) with error handling and dynamically allocated result. */
 const struct got_error *got_path_dirname(char **, const char *);
 
+/* basename(3) with dynamically allocated result. */
+const struct got_error *got_path_basename(char **, const char *);
+
 /* Strip trailing slashes from a path; path will be modified in-place. */
 void got_path_strip_trailing_slashes(char *);
 
diff --git a/include/got_worktree.h b/include/got_worktree.h
index e98f4ee..0a7f10b 100644
--- a/include/got_worktree.h
+++ b/include/got_worktree.h
@@ -114,21 +114,21 @@ typedef const struct got_error *(*got_worktree_cancel_cb)(void *);
  * The checkout progress callback will be invoked with the provided
  * void * argument, and the path of each checked out file.
  *
- * It is possible to restrict the checkout operation to a specific path in
- * the work tree, in which case all files outside this path will remain at
+ * It is possible to restrict the checkout operation to specific paths in
+ * the work tree, in which case all files outside those paths will remain at
  * their currently recorded base commit. Inconsistent base commits can be
  * repaired later by running another update operation across the entire work
  * tree. Inconsistent base-commits may also occur if this function runs into
  * an error or if the checkout operation is cancelled by the cancel callback.
- * The specified path is relative to the work tree's root. Pass "" to check
- * out files across the entire work tree.
+ * Allspecified paths are relative to the work tree's root. Pass a pathlist
+ * with a single empty path "" to check out files across the entire work tree.
  *
  * Some operations may refuse to run while the work tree contains files from
  * multiple base commits.
  */
 const struct got_error *got_worktree_checkout_files(struct got_worktree *,
-    const char *, struct got_repository *, got_worktree_checkout_cb, void *,
-    got_worktree_cancel_cb, void *);
+    struct got_pathlist_head *, struct got_repository *,
+    got_worktree_checkout_cb, void *, got_worktree_cancel_cb, void *);
 
 /* Merge the differences between two commits into a work tree. */
 const struct got_error *
diff --git a/lib/path.c b/lib/path.c
index f3e258f..66561a0 100644
--- a/lib/path.c
+++ b/lib/path.c
@@ -371,6 +371,22 @@ got_path_dirname(char **parent, const char *path)
 	return NULL;
 }
 
+const struct got_error *
+got_path_basename(char **s, const char *path)
+{
+	char *base;
+
+	base = basename(path);
+	if (base == NULL)
+		return got_error_from_errno2("basename", path);
+
+	*s = strdup(base);
+	if (*s == NULL)
+		return got_error_from_errno("strdup");
+
+	return NULL;
+}
+
 void
 got_path_strip_trailing_slashes(char *path)
 {
diff --git a/lib/worktree.c b/lib/worktree.c
index 697a915..0c1f987 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -1774,34 +1774,59 @@ done:
 }
 
 const struct got_error *
-got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
-    struct got_repository *repo, got_worktree_checkout_cb progress_cb,
-    void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
+got_worktree_checkout_files(struct got_worktree *worktree,
+    struct got_pathlist_head *paths, struct got_repository *repo,
+    got_worktree_checkout_cb progress_cb, void *progress_arg,
+    got_worktree_cancel_cb cancel_cb, void *cancel_arg)
 {
 	const struct got_error *err = NULL, *sync_err, *unlockerr;
 	struct got_commit_object *commit = NULL;
-	struct got_object_id *tree_id = NULL;
 	struct got_tree_object *tree = NULL;
 	struct got_fileindex *fileindex = NULL;
 	char *fileindex_path = NULL;
-	char *relpath = NULL, *entry_name = NULL;
-	int entry_type;
+	struct got_pathlist_entry *pe;
+	struct tree_path_data {
+		SIMPLEQ_ENTRY(tree_path_data) entry;
+		struct got_object_id *tree_id;
+		int entry_type;
+		char *relpath;
+		char *entry_name;
+	} *tpd = NULL;
+	SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
+
+	SIMPLEQ_INIT(&tree_paths);
 
 	err = lock_worktree(worktree, LOCK_EX);
 	if (err)
 		return err;
 
-	err = find_tree_entry_for_checkout(&entry_type, &relpath, &tree_id,
-	    path, worktree, repo);
-	if (err)
-		goto done;
+	/* Map all specified paths to in-repository trees. */
+	TAILQ_FOREACH(pe, paths, entry) {
+		tpd = malloc(sizeof(*tpd));
+		if (tpd == NULL) {
+			err = got_error_from_errno("malloc");
+			goto done;
+		}
 
-	if (entry_type == GOT_OBJ_TYPE_BLOB) {
-		entry_name = basename(path);
-		if (entry_name == NULL) {
-			err = got_error_from_errno2("basename", path);
+		err = find_tree_entry_for_checkout(&tpd->entry_type,
+		    &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
+		if (err) {
+			free(tpd);
 			goto done;
 		}
+
+		if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
+			err = got_path_basename(&tpd->entry_name, pe->path);
+			if (err) {
+				free(tpd->relpath);
+				free(tpd->tree_id);
+				free(tpd);
+				goto done;
+			}
+		} else
+			tpd->entry_name = NULL;
+
+		SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
 	}
 
 	/*
@@ -1813,31 +1838,47 @@ got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
 	if (err)
 		goto done;
 
-	err = checkout_files(worktree, fileindex, relpath, tree_id, entry_name,
-	    repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
-	if (err == NULL) {
+	tpd = SIMPLEQ_FIRST(&tree_paths);
+	TAILQ_FOREACH(pe, paths, entry) {
 		struct bump_base_commit_id_arg bbc_arg;
+
+		err = checkout_files(worktree, fileindex, tpd->relpath,
+		    tpd->tree_id, tpd->entry_name, repo,
+		    progress_cb, progress_arg, cancel_cb, cancel_arg);
+		if (err)
+			break;
+
 		bbc_arg.base_commit_id = worktree->base_commit_id;
-		bbc_arg.entry_name = entry_name;
-		bbc_arg.path = path;
-		bbc_arg.path_len = strlen(path);
+		bbc_arg.entry_name = tpd->entry_name;
+		bbc_arg.path = pe->path;
+		bbc_arg.path_len = strlen(pe->path);
 		bbc_arg.progress_cb = progress_cb;
 		bbc_arg.progress_arg = progress_arg;
 		err = got_fileindex_for_each_entry_safe(fileindex,
 		    bump_base_commit_id, &bbc_arg);
+		if (err)
+			break;
+
+		tpd = SIMPLEQ_NEXT(tpd, entry);
 	}
 	sync_err = sync_fileindex(fileindex, fileindex_path);
 	if (sync_err && err == NULL)
 		err = sync_err;
 done:
 	free(fileindex_path);
-	free(relpath);
 	if (tree)
 		got_object_tree_close(tree);
 	if (commit)
 		got_object_commit_close(commit);
 	if (fileindex)
 		got_fileindex_free(fileindex);
+	while (!SIMPLEQ_EMPTY(&tree_paths)) {
+		tpd = SIMPLEQ_FIRST(&tree_paths);
+		SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
+		free(tpd->relpath);
+		free(tpd->tree_id);
+		free(tpd);
+	}
 	unlockerr = lock_worktree(worktree, LOCK_SH);
 	if (unlockerr && err == NULL)
 		err = unlockerr;
diff --git a/regress/cmdline/update.sh b/regress/cmdline/update.sh
index e3be1a7..6764523 100755
--- a/regress/cmdline/update.sh
+++ b/regress/cmdline/update.sh
@@ -1095,33 +1095,33 @@ function test_update_partial {
 	echo "modified epsilon/zeta" > $testroot/repo/epsilon/zeta
 	git_commit $testroot/repo -m "modified two files"
 
-	for f in alpha beta; do
-		echo "U  $f" > $testroot/stdout.expected
-		echo -n "Updated to commit " >> $testroot/stdout.expected
-		git_show_head $testroot/repo >> $testroot/stdout.expected
-		echo >> $testroot/stdout.expected
-
-		(cd $testroot/wt && got update $f > $testroot/stdout)
-
-		cmp -s $testroot/stdout.expected $testroot/stdout
-		ret="$?"
-		if [ "$ret" != "0" ]; then
-			diff -u $testroot/stdout.expected $testroot/stdout
-			test_done "$testroot" "$ret"
-			return 1
-		fi
-
-		echo "modified $f" > $testroot/content.expected
-		cat $testroot/wt/$f > $testroot/content
-
-		cmp -s $testroot/content.expected $testroot/content
-		ret="$?"
-		if [ "$ret" != "0" ]; then
-			diff -u $testroot/content.expected $testroot/content
-			test_done "$testroot" "$ret"
-			return 1
-		fi
-	done
+	echo "U  alpha" > $testroot/stdout.expected
+	echo "U  beta" >> $testroot/stdout.expected
+	echo -n "Updated to commit " >> $testroot/stdout.expected
+	git_show_head $testroot/repo >> $testroot/stdout.expected
+	echo >> $testroot/stdout.expected
+
+	(cd $testroot/wt && got update alpha beta > $testroot/stdout)
+
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "modified alpha" > $testroot/content.expected
+	echo "modified beta" >> $testroot/content.expected
+
+	cat $testroot/wt/alpha $testroot/wt/beta > $testroot/content
+	cmp -s $testroot/content.expected $testroot/content
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/content.expected $testroot/content
+		test_done "$testroot" "$ret"
+		return 1
+	fi
 
 	echo "U  epsilon/zeta" > $testroot/stdout.expected
 	echo -n "Updated to commit " >> $testroot/stdout.expected
@@ -1167,33 +1167,32 @@ function test_update_partial_add {
 	(cd $testroot/repo && git add .)
 	git_commit $testroot/repo -m "added two files"
 
-	for f in new epsilon/new2; do
-		echo "A  $f" > $testroot/stdout.expected
-		echo -n "Updated to commit " >> $testroot/stdout.expected
-		git_show_head $testroot/repo >> $testroot/stdout.expected
-		echo >> $testroot/stdout.expected
-
-		(cd $testroot/wt && got update $f > $testroot/stdout)
-
-		cmp -s $testroot/stdout.expected $testroot/stdout
-		ret="$?"
-		if [ "$ret" != "0" ]; then
-			diff -u $testroot/stdout.expected $testroot/stdout
-			test_done "$testroot" "$ret"
-			return 1
-		fi
-
-		echo "$f" > $testroot/content.expected
-		cat $testroot/wt/$f > $testroot/content
-
-		cmp -s $testroot/content.expected $testroot/content
-		ret="$?"
-		if [ "$ret" != "0" ]; then
-			diff -u $testroot/content.expected $testroot/content
-			test_done "$testroot" "$ret"
-			return 1
-		fi
-	done
+	echo "A  new" > $testroot/stdout.expected
+	echo "A  epsilon/new2" >> $testroot/stdout.expected
+	echo -n "Updated to commit " >> $testroot/stdout.expected
+	git_show_head $testroot/repo >> $testroot/stdout.expected
+	echo >> $testroot/stdout.expected
+
+	(cd $testroot/wt && got update new epsilon/new2 > $testroot/stdout)
+
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "new" > $testroot/content.expected
+	echo "epsilon/new2" >> $testroot/content.expected
+
+	cat $testroot/wt/new $testroot/wt/epsilon/new2 > $testroot/content
+
+	cmp -s $testroot/content.expected $testroot/content
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/content.expected $testroot/content
+	fi
 	test_done "$testroot" "$ret"
 }
 
@@ -1207,30 +1206,27 @@ function test_update_partial_rm {
 		return 1
 	fi
 
-	(cd $testroot/repo && git rm -q alpha)
-	(cd $testroot/repo && git rm -q epsilon/zeta)
+	(cd $testroot/repo && git rm -q alpha epsilon/zeta)
 	git_commit $testroot/repo -m "removed two files"
 
-	for f in alpha epsilon/zeta; do
-		echo "got: no such entry found in tree" \
-			> $testroot/stderr.expected
-
-		(cd $testroot/wt && got update $f 2> $testroot/stderr)
-		ret="$?"
-		if [ "$ret" == "0" ]; then
-			echo "update succeeded unexpectedly" >&2
-			test_done "$testroot" "1"
-			return 1
-		fi
-
-		cmp -s $testroot/stderr.expected $testroot/stderr
-		ret="$?"
-		if [ "$ret" != "0" ]; then
-			diff -u $testroot/stderr.expected $testroot/stderr
-			test_done "$testroot" "$ret"
-			return 1
-		fi
-	done
+	echo "got: no such entry found in tree" \
+		> $testroot/stderr.expected
+
+	(cd $testroot/wt && got update alpha epsilon/zeta 2> $testroot/stderr)
+	ret="$?"
+	if [ "$ret" == "0" ]; then
+		echo "update succeeded unexpectedly" >&2
+		test_done "$testroot" "1"
+		return 1
+	fi
+
+	cmp -s $testroot/stderr.expected $testroot/stderr
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stderr.expected $testroot/stderr
+		test_done "$testroot" "$ret"
+		return 1
+	fi
 	test_done "$testroot" "$ret"
 }
 
diff --git a/regress/worktree/worktree_test.c b/regress/worktree/worktree_test.c
index 26f6153..dc88ba8 100644
--- a/regress/worktree/worktree_test.c
+++ b/regress/worktree/worktree_test.c
@@ -354,6 +354,9 @@ worktree_checkout(const char *repo_path)
 	char worktree_path[PATH_MAX];
 	int ok = 0;
 	struct stat sb;
+	struct got_pathlist_head paths;
+
+	TAILQ_INIT(&paths);
 
 	err = got_repo_open(&repo, repo_path);
 	if (err != NULL || repo == NULL)
@@ -375,8 +378,11 @@ worktree_checkout(const char *repo_path)
 	if (err != NULL)
 		goto done;
 
-	err = got_worktree_checkout_files(worktree, "", repo, progress_cb, NULL,
-	    NULL, NULL);
+	err = got_pathlist_append(NULL, &paths, "", NULL);
+	if (err)
+		goto done;
+	err = got_worktree_checkout_files(worktree, &paths, repo, progress_cb,
+	    NULL, NULL, NULL);
 	if (err != NULL)
 		goto done;
 
@@ -410,6 +416,7 @@ done:
 		got_ref_close(head_ref);
 	if (repo)
 		got_repo_close(repo);
+	got_pathlist_free(&paths);
 	free(makefile_path);
 	free(cfile_path);
 	return ok;