Commit 6d022e974716847d3f3c3bc97dac50e61663c49c

Stefan Sperling 2019-08-04T12:45:17

use get_worktree_paths_from_argv consistently; improves add/rm edge cases Double 'got rm' becomes a no-op like double 'got add' already is, and 'got add' of an already staged file is now an error.

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
diff --git a/got/got.c b/got/got.c
index e415b9e..0b09fb1 100644
--- a/got/got.c
+++ b/got/got.c
@@ -2945,7 +2945,7 @@ cmd_add(int argc, char *argv[])
 	char *cwd = NULL;
 	struct got_pathlist_head paths;
 	struct got_pathlist_entry *pe;
-	int ch, x;
+	int ch;
 
 	TAILQ_INIT(&paths);
 
@@ -2987,20 +2987,10 @@ cmd_add(int argc, char *argv[])
 	if (error)
 		goto done;
 
-	for (x = 0; x < argc; x++) {
-		char *path = realpath(argv[x], NULL);
-		if (path == NULL) {
-			error = got_error_from_errno2("realpath", argv[x]);
-			goto done;
-		}
+	error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
+	if (error)
+		goto done;
 
-		got_path_strip_trailing_slashes(path);
-		error = got_pathlist_insert(&pe, &paths, path, NULL);
-		if (error) {
-			free(path);
-			goto done;
-		}
-	}
 	error = got_worktree_schedule_add(worktree, &paths, print_status,
 	    NULL, repo);
 done:
@@ -3031,7 +3021,7 @@ cmd_remove(int argc, char *argv[])
 	char *cwd = NULL;
 	struct got_pathlist_head paths;
 	struct got_pathlist_entry *pe;
-	int ch, i, delete_local_mods = 0;
+	int ch, delete_local_mods = 0;
 
 	TAILQ_INIT(&paths);
 
@@ -3075,20 +3065,10 @@ cmd_remove(int argc, char *argv[])
 	if (error)
 		goto done;
 
-	for (i = 0; i < argc; i++) {
-		char *path = realpath(argv[i], NULL);
-		if (path == NULL) {
-			error = got_error_from_errno2("realpath", argv[i]);
-			goto done;
-		}
+	error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
+	if (error)
+		goto done;
 
-		got_path_strip_trailing_slashes(path);
-		error = got_pathlist_insert(&pe, &paths, path, NULL);
-		if (error) {
-			free(path);
-			goto done;
-		}
-	}
 	error = got_worktree_schedule_delete(worktree, &paths,
 	    delete_local_mods, print_status, NULL, repo);
 	if (error)
@@ -3129,9 +3109,7 @@ cmd_revert(int argc, char *argv[])
 	struct got_repository *repo = NULL;
 	char *cwd = NULL, *path = NULL;
 	struct got_pathlist_head paths;
-	struct got_pathlist_entry *pe;
-	const char *worktree_path;
-	int ch, i;
+	int ch;
 
 	TAILQ_INIT(&paths);
 
@@ -3172,38 +3150,9 @@ cmd_revert(int argc, char *argv[])
 	if (error)
 		goto done;
 
-	worktree_path = got_worktree_get_root_path(worktree);
-	for (i = 0; i < argc; i++) {
-		char *path = realpath(argv[i], NULL);
-		if (path == NULL) {
-			if (errno != ENOENT) {
-				error = got_error_from_errno2("realpath",
-				    argv[i]);
-				goto done;
-			}
-			if (got_path_is_child(argv[i], worktree_path,
-			    strlen(worktree_path))) {
-				path = strdup(argv[i]);
-				if (path == NULL) {
-					error = got_error_from_errno("strdup");
-					goto done;
-				}
-
-			} else if (asprintf(&path, "%s/%s",
-			    got_worktree_get_root_path(worktree),
-			    argv[i]) == -1) {
-				error = got_error_from_errno("asprintf");
-				goto done;
-			}
-		}
-
-		got_path_strip_trailing_slashes(path);
-		error = got_pathlist_insert(&pe, &paths, path, NULL);
-		if (error) {
-			free(path);
-			goto done;
-		}
-	}
+	error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
+	if (error)
+		goto done;
 
 	error = got_worktree_revert(worktree, &paths,
 	    revert_progress, NULL, repo);
@@ -5246,8 +5195,7 @@ cmd_stage(int argc, char *argv[])
 	char *cwd = NULL;
 	struct got_pathlist_head paths;
 	struct got_pathlist_entry *pe;
-	const char *worktree_path;
-	int ch, x, list_stage = 0;
+	int ch, list_stage = 0;
 
 	TAILQ_INIT(&paths);
 
@@ -5292,60 +5240,14 @@ cmd_stage(int argc, char *argv[])
 	if (error)
 		goto done;
 
-	worktree_path = got_worktree_get_root_path(worktree);
-	for (x = 0; x < argc; x++) {
-		char *path;
-		if (list_stage) {
-			error = got_worktree_resolve_path(&path, worktree,
-			    argv[x]);
-			if (error)
-				break;
-		} else
-			path = realpath(argv[x], NULL);
-		if (path == NULL) {
-			if (errno != ENOENT) {
-				error = got_error_from_errno2("realpath",
-				    argv[x]);
-				goto done;
-			}
-			if (got_path_is_child(argv[x], worktree_path,
-			    strlen(worktree_path))) {
-				path = strdup(argv[x]);
-				if (path == NULL) {
-					error = got_error_from_errno("strdup");
-					goto done;
-				}
-
-			} else if (asprintf(&path, "%s/%s",
-			    got_worktree_get_root_path(worktree),
-			    argv[x]) == -1) {
-				error = got_error_from_errno("asprintf");
-				goto done;
-			}
-		}
-
-		got_path_strip_trailing_slashes(path);
-		error = got_pathlist_insert(&pe, &paths, path, NULL);
-		if (error) {
-			free(path);
-			goto done;
-		}
-	}
+	error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
+	if (error)
+		goto done;
 
-	if (list_stage) {
-		if (TAILQ_EMPTY(&paths)) {
-			char *s = strdup("");
-			if (s == NULL) {
-				error = got_error_from_errno("strdup");
-				goto done;
-			}
-			error = got_pathlist_append(&paths, s, NULL);
-			if (error)
-				goto done;
-		}
+	if (list_stage)
 		error = got_worktree_status(worktree, &paths, repo,
 		    print_stage, NULL, check_cancelled, NULL);
-	} else
+	else
 		error = got_worktree_stage(worktree, &paths, print_status,
 		    NULL, repo);
 done:
diff --git a/lib/worktree.c b/lib/worktree.c
index 45b06f7..b1367b4 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -2498,10 +2498,19 @@ schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
 {
 	const struct got_error *err = NULL;
 	struct got_fileindex_entry *ie;
+	unsigned char status;
+	struct stat sb;
 
-	/* Re-adding an existing entry is a no-op. */
-	if (got_fileindex_entry_get(fileindex, relpath, strlen(relpath)))
-		return NULL;
+	ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
+	if (ie) {
+		err = get_file_status(&status, &sb, ie, ondisk_path, repo);
+		if (err)
+			return err;
+		/* Re-adding an existing entry is a no-op. */
+		if (status == GOT_STATUS_ADD)
+			return NULL;
+		return got_error_path(relpath, GOT_ERR_FILE_STATUS);
+	}
 
 	err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
 	if (err)
@@ -2518,7 +2527,7 @@ schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
 
 const struct got_error *
 got_worktree_schedule_add(struct got_worktree *worktree,
-    struct got_pathlist_head *ondisk_paths,
+    struct got_pathlist_head *paths,
     got_worktree_status_cb status_cb, void *status_arg,
     struct got_repository *repo)
 {
@@ -2535,15 +2544,14 @@ got_worktree_schedule_add(struct got_worktree *worktree,
 	if (err)
 		goto done;
 
-	TAILQ_FOREACH(pe, ondisk_paths, entry) {
-		char *relpath;
-		err = got_path_skip_common_ancestor(&relpath,
-		    got_worktree_get_root_path(worktree), pe->path);
-		if (err)
-			break;
-		err = schedule_addition(pe->path, fileindex, relpath,
+	TAILQ_FOREACH(pe, paths, entry) {
+		char *ondisk_path;
+		if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
+		    pe->path) == -1)
+			return got_error_from_errno("asprintf");
+		err = schedule_addition(ondisk_path, fileindex, pe->path,
 		    status_cb, status_arg, repo);
-		free(relpath);
+		free(ondisk_path);
 		if (err)
 			break;
 	}
@@ -2604,7 +2612,7 @@ schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
 
 const struct got_error *
 got_worktree_schedule_delete(struct got_worktree *worktree,
-    struct got_pathlist_head *ondisk_paths, int delete_local_mods,
+    struct got_pathlist_head *paths, int delete_local_mods,
     got_worktree_status_cb status_cb, void *status_arg,
     struct got_repository *repo)
 {
@@ -2621,15 +2629,14 @@ got_worktree_schedule_delete(struct got_worktree *worktree,
 	if (err)
 		goto done;
 
-	TAILQ_FOREACH(pe, ondisk_paths, entry) {
-		char *relpath;
-		err = got_path_skip_common_ancestor(&relpath,
-		    got_worktree_get_root_path(worktree), pe->path);
-		if (err)
-			break;
-		err = schedule_for_deletion(pe->path, fileindex, relpath,
+	TAILQ_FOREACH(pe, paths, entry) {
+		char *ondisk_path;
+		if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
+		    pe->path) == -1)
+			return got_error_from_errno("asprintf");
+		err = schedule_for_deletion(ondisk_path, fileindex, pe->path,
 		    delete_local_mods, status_cb, status_arg, repo);
-		free(relpath);
+		free(ondisk_path);
 		if (err)
 			break;
 	}
@@ -2817,8 +2824,13 @@ got_worktree_revert(struct got_worktree *worktree,
 		goto done;
 
 	TAILQ_FOREACH(pe, ondisk_paths, entry) {
-		err = revert_file(worktree, fileindex, pe->path,
+		char *ondisk_path;
+		if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
+		    pe->path) == -1)
+			return got_error_from_errno("asprintf");
+		err = revert_file(worktree, fileindex, ondisk_path,
 		    progress_cb, progress_arg, repo);
+		free(ondisk_path);
 		if (err)
 			break;
 	}
@@ -5188,28 +5200,26 @@ got_worktree_stage(struct got_worktree *worktree,
 
 	/* Check out-of-dateness before staging anything. */
 	TAILQ_FOREACH(pe, paths, entry) {
-		char *relpath;
-		err = got_path_skip_common_ancestor(&relpath,
-		    got_worktree_get_root_path(worktree), pe->path);
-		if (err)
-			goto done;
-		err = stage_check_out_of_date(relpath, pe->path,
+		char *ondisk_path;
+		if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
+		    pe->path) == -1)
+			return got_error_from_errno("asprintf");
+		err = stage_check_out_of_date(pe->path, ondisk_path,
 		    head_commit_id, worktree, fileindex, repo);
-		free(relpath);
+		free(ondisk_path);
 		if (err)
 			goto done;
 	}
 
 	TAILQ_FOREACH(pe, paths, entry) {
-		char *relpath;
-		err = got_path_skip_common_ancestor(&relpath,
-		    got_worktree_get_root_path(worktree), pe->path);
-		if (err)
-			break;
-		err = stage_path(relpath, pe->path,
+		char *ondisk_path;
+		if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
+		    pe->path) == -1)
+			return got_error_from_errno("asprintf");
+		err = stage_path(pe->path, ondisk_path,
 		    (const char *)pe->data, worktree, fileindex, repo,
 		    status_cb, status_arg);
-		free(relpath);
+		free(ondisk_path);
 		if (err)
 			break;
 	}
diff --git a/regress/cmdline/add.sh b/regress/cmdline/add.sh
index 727c09d..0634eee 100755
--- a/regress/cmdline/add.sh
+++ b/regress/cmdline/add.sh
@@ -84,9 +84,9 @@ function test_add_multiple {
 		return 1
 	fi
 
-	echo "A  bar" > $testroot/stdout.expected
+	echo "A  foo" > $testroot/stdout.expected
+	echo "A  bar" >> $testroot/stdout.expected
 	echo "A  baz" >> $testroot/stdout.expected
-	echo "A  foo" >> $testroot/stdout.expected
 
 	cmp -s $testroot/stdout.expected $testroot/stdout
 	ret="$?"
@@ -120,7 +120,39 @@ function test_add_file_in_new_subdir {
 	test_done "$testroot" "$ret"
 }
 
+function test_add_deleted {
+	local testroot=`test_init add_deleted`
+
+	got checkout $testroot/repo $testroot/wt > /dev/null
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	(cd $testroot/wt && got rm beta > /dev/null)
+
+	echo -n > $testroot/stdout.expected
+	(cd $testroot/wt && got add beta > $testroot/stdout 2> $testroot/stderr)
+	ret="$?"
+	if [ "$ret" == "0" ]; then
+		echo "got add command succeeded unexpectedly" >&2
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "1"
+		return 1
+	fi
+
+	echo "got: beta: file has unexpected status" > $testroot/stderr.expected
+	cmp -s $testroot/stderr.expected $testroot/stderr
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stderr.expected $testroot/stderr
+	fi
+	test_done "$testroot" "$ret"
+}
+
 run_test test_add_basic
 run_test test_double_add
 run_test test_add_multiple
 run_test test_add_file_in_new_subdir
+run_test test_add_deleted
diff --git a/regress/cmdline/rm.sh b/regress/cmdline/rm.sh
index 34059af..8d663c8 100755
--- a/regress/cmdline/rm.sh
+++ b/regress/cmdline/rm.sh
@@ -112,19 +112,23 @@ function test_double_rm {
 	(cd $testroot/wt && got rm beta > /dev/null)
 
 	for fflag in "" "-f"; do
-		(cd $testroot/wt && got rm $fflag beta 2> $testroot/stderr)
+		echo -n > $testroot/stderr.expected
+		(cd $testroot/wt && got rm $fflag beta > $testroot/stdout \
+			2> $testroot/stderr)
 		ret="$?"
-		if [ "$ret" == "0" ]; then
-			echo "got rm command succeeded unexpectedly" >&2
-			test_done "$testroot" 1
+		if [ "$ret" != "0" ]; then
+			echo "got rm command failed unexpectedly" >&2
+			diff -u $testroot/stderr.expected $testroot/stderr
+			test_done "$testroot" "$ret"
+			return 1
 		fi
-
-		grep "No such file or directory" $testroot/stderr > \
-			$testroot/stderr.actual
+		echo -n > $testroot/stdout.expected
+		cmp -s $testroot/stdout.expected $testroot/stdout
 		ret="$?"
 		if [ "$ret" != "0" ]; then
-			cat $testroot/stderr
+			diff -u $testroot/stdout.expected $testroot/stdout
 			test_done "$testroot" "$ret"
+			return 1
 		fi
 	done
 	test_done "$testroot" "0"
diff --git a/regress/cmdline/stage.sh b/regress/cmdline/stage.sh
index 01207ce..90a4974 100755
--- a/regress/cmdline/stage.sh
+++ b/regress/cmdline/stage.sh
@@ -326,32 +326,17 @@ function test_stage_add_already_staged_file {
 
 	(cd $testroot/wt && got stage alpha beta foo > $testroot/stdout)
 
-	(cd $testroot/wt && got add beta \
-		> $testroot/stdout 2> $testroot/stderr)
-	ret="$?"
-	if [ "$ret" == "0" ]; then
-		echo "got add command succeeded unexpectedly" >&2
-		test_done "$testroot" "1"
-		return 1
-	fi
-	echo "got: realpath: beta: No such file or directory" \
-		> $testroot/stderr.expected
-	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
-
 	echo -n > $testroot/stdout.expected
-	for f in alpha foo; do
+	for f in alpha beta foo; do
 		(cd $testroot/wt && got add $f \
 			> $testroot/stdout 2> $testroot/stderr)
+		echo "got: $f: file has unexpected status" \
+			> $testroot/stderr.expected
+		cmp -s $testroot/stderr.expected $testroot/stderr
 		ret="$?"
 		if [ "$ret" != "0" ]; then
-			echo "got add command failed unexpectedly" >&2
-			test_done "$testroot" "1"
+			diff -u $testroot/stderr.expected $testroot/stderr
+			test_done "$testroot" "$ret"
 			return 1
 		fi
 		cmp -s $testroot/stdout.expected $testroot/stdout
@@ -396,13 +381,20 @@ function test_stage_rm_already_staged_file {
 	(cd $testroot/wt && got rm beta \
 		> $testroot/stdout 2> $testroot/stderr)
 	ret="$?"
-	if [ "$ret" == "0" ]; then
-		echo "got rm command succeeded unexpectedly" >&2
+	if [ "$ret" != "0" ]; then
+		echo "got rm command failed unexpectedly" >&2
 		test_done "$testroot" "1"
 		return 1
 	fi
-	echo "got: realpath: beta: No such file or directory" \
-		> $testroot/stderr.expected
+	echo -n > $testroot/stdout.expected
+	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 -n > $testroot/stderr.expected
 	cmp -s $testroot/stderr.expected $testroot/stderr
 	ret="$?"
 	if [ "$ret" != "0" ]; then