check file status before applying the patch Don't allow `got patch' to delete files that are not known, or add files that are already known and to edit files that are known, not obstructed and without conflicts.
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
diff --git a/got/got.c b/got/got.c
index 09c52c1..4bcea52 100644
--- a/got/got.c
+++ b/got/got.c
@@ -7245,7 +7245,7 @@ cmd_patch(int argc, char *argv[])
#endif
error = got_patch(patchfd, worktree, repo, &print_remove_status,
- NULL, &add_progress, NULL);
+ NULL, &add_progress, NULL, check_cancelled, NULL);
done:
if (repo) {
diff --git a/include/got_patch.h b/include/got_patch.h
index 04a23fc..448bb17 100644
--- a/include/got_patch.h
+++ b/include/got_patch.h
@@ -22,4 +22,5 @@
*/
const struct got_error *
got_patch(int, struct got_worktree *, struct got_repository *,
- got_worktree_delete_cb, void *, got_worktree_checkout_cb, void *);
+ got_worktree_delete_cb, void *, got_worktree_checkout_cb, void *,
+ got_cancel_cb, void *);
diff --git a/lib/patch.c b/lib/patch.c
index 8dc632e..478ade4 100644
--- a/lib/patch.c
+++ b/lib/patch.c
@@ -28,6 +28,7 @@
#include <sys/socket.h>
#include <sys/uio.h>
+#include <errno.h>
#include <limits.h>
#include <sha1.h>
#include <stdint.h>
@@ -381,44 +382,6 @@ apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *lineno)
}
static const struct got_error *
-schedule_add(const char *path, struct got_worktree *worktree,
- struct got_repository *repo, got_worktree_checkout_cb add_cb,
- void *add_arg)
-{
- static const struct got_error *err = NULL;
- struct got_pathlist_head paths;
- struct got_pathlist_entry *pe;
-
- TAILQ_INIT(&paths);
-
- err = got_pathlist_insert(&pe, &paths, path, NULL);
- if (err == NULL)
- err = got_worktree_schedule_add(worktree, &paths,
- add_cb, add_arg, repo, 1);
- got_pathlist_free(&paths);
- return err;
-}
-
-static const struct got_error *
-schedule_del(const char *path, struct got_worktree *worktree,
- struct got_repository *repo, got_worktree_delete_cb delete_cb,
- void *delete_arg)
-{
- static const struct got_error *err = NULL;
- struct got_pathlist_head paths;
- struct got_pathlist_entry *pe;
-
- TAILQ_INIT(&paths);
-
- err = got_pathlist_insert(&pe, &paths, path, NULL);
- if (err == NULL)
- err = got_worktree_schedule_delete(worktree, &paths,
- 0, NULL, delete_cb, delete_arg, repo, 0, 0);
- got_pathlist_free(&paths);
- return err;
-}
-
-static const struct got_error *
patch_file(struct got_patch *p, const char *path, FILE *tmp)
{
const struct got_error *err = NULL;
@@ -503,23 +466,121 @@ done:
}
static const struct got_error *
+build_pathlist(const char *p, char **path, struct got_pathlist_head *head,
+ struct got_worktree *worktree)
+{
+ const struct got_error *err;
+ struct got_pathlist_entry *pe;
+
+ err = got_worktree_resolve_path(path, worktree, p);
+ if (err == NULL)
+ err = got_pathlist_insert(&pe, head, *path, NULL);
+ return err;
+}
+
+static const struct got_error *
+can_rm(void *arg, unsigned char status, unsigned char staged_status,
+ const char *path, struct got_object_id *blob_id,
+ struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
+ int dirfd, const char *de_name)
+{
+ if (status == GOT_STATUS_NONEXISTENT)
+ return got_error_set_errno(ENOENT, path);
+ if (status != GOT_STATUS_NO_CHANGE &&
+ status != GOT_STATUS_ADD &&
+ status != GOT_STATUS_MODIFY &&
+ status != GOT_STATUS_MODE_CHANGE)
+ return got_error_path(path, GOT_ERR_FILE_STATUS);
+ if (staged_status == GOT_STATUS_DELETE)
+ return got_error_path(path, GOT_ERR_FILE_STATUS);
+ return NULL;
+}
+
+static const struct got_error *
+can_add(void *arg, unsigned char status, unsigned char staged_status,
+ const char *path, struct got_object_id *blob_id,
+ struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
+ int dirfd, const char *de_name)
+{
+ if (status != GOT_STATUS_NONEXISTENT)
+ return got_error_path(path, GOT_ERR_FILE_STATUS);
+ return NULL;
+}
+
+static const struct got_error *
+can_edit(void *arg, unsigned char status, unsigned char staged_status,
+ const char *path, struct got_object_id *blob_id,
+ struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
+ int dirfd, const char *de_name)
+{
+ if (status == GOT_STATUS_NONEXISTENT)
+ return got_error_set_errno(ENOENT, path);
+ if (status != GOT_STATUS_NO_CHANGE &&
+ status != GOT_STATUS_ADD &&
+ status != GOT_STATUS_MODIFY)
+ return got_error_path(path, GOT_ERR_FILE_STATUS);
+ if (staged_status == GOT_STATUS_DELETE)
+ return got_error_path(path, GOT_ERR_FILE_STATUS);
+ return NULL;
+}
+
+static const struct got_error *
+check_file_status(struct got_patch *p, int file_renamed,
+ struct got_worktree *worktree, struct got_repository *repo,
+ struct got_pathlist_head *old, struct got_pathlist_head *new,
+ got_cancel_cb cancel_cb, void *cancel_arg)
+{
+ static const struct got_error *err;
+
+ if (p->old != NULL && p->new == NULL)
+ return got_worktree_status(worktree, old, repo, 0,
+ can_rm, NULL, cancel_cb, cancel_arg);
+ else if (file_renamed) {
+ err = got_worktree_status(worktree, old, repo, 0,
+ can_rm, NULL, cancel_cb, cancel_arg);
+ if (err)
+ return err;
+ return got_worktree_status(worktree, new, repo, 0,
+ can_add, NULL, cancel_cb, cancel_arg);
+ } else if (p->old == NULL)
+ return got_worktree_status(worktree, new, repo, 0,
+ can_add, NULL, cancel_cb, cancel_arg);
+ else
+ return got_worktree_status(worktree, new, repo, 0,
+ can_edit, NULL, cancel_cb, cancel_arg);
+}
+
+static const struct got_error *
apply_patch(struct got_worktree *worktree, struct got_repository *repo,
struct got_patch *p, got_worktree_delete_cb delete_cb, void *delete_arg,
- got_worktree_checkout_cb add_cb, void *add_arg)
+ got_worktree_checkout_cb add_cb, void *add_arg, got_cancel_cb cancel_cb,
+ void *cancel_arg)
{
const struct got_error *err = NULL;
+ struct got_pathlist_head oldpaths, newpaths;
int file_renamed = 0;
char *oldpath = NULL, *newpath = NULL;
char *tmppath = NULL, *template = NULL;
FILE *tmp = NULL;
- err = got_worktree_resolve_path(&oldpath, worktree,
- p->old != NULL ? p->old : p->new);
+ TAILQ_INIT(&oldpaths);
+ TAILQ_INIT(&newpaths);
+
+ err = build_pathlist(p->old != NULL ? p->old : p->new, &oldpath,
+ &oldpaths, worktree);
if (err)
goto done;
- err = got_worktree_resolve_path(&newpath, worktree,
- p->new != NULL ? p->new : p->old);
+ err = build_pathlist(p->new != NULL ? p->new : p->old, &newpath,
+ &newpaths, worktree);
+ if (err)
+ goto done;
+
+ if (p->old != NULL && p->new != NULL && strcmp(p->old, p->new))
+ file_renamed = 1;
+
+ err = check_file_status(p, file_renamed, worktree, repo, &oldpaths,
+ &newpaths, cancel_cb, cancel_arg);
if (err)
goto done;
@@ -528,8 +589,8 @@ apply_patch(struct got_worktree *worktree, struct got_repository *repo,
* special case: delete a file. don't try to match
* the lines but just schedule the removal.
*/
- err = schedule_del(p->old, worktree, repo, delete_cb,
- delete_arg);
+ err = got_worktree_schedule_delete(worktree, &oldpaths,
+ 0, NULL, delete_cb, delete_arg, repo, 0, 0);
goto done;
}
@@ -551,26 +612,27 @@ apply_patch(struct got_worktree *worktree, struct got_repository *repo,
goto done;
}
- file_renamed = p->old != NULL && strcmp(p->old, p->new);
if (file_renamed) {
- err = schedule_del(oldpath, worktree, repo, delete_cb,
- delete_arg);
+ err = got_worktree_schedule_delete(worktree, &oldpaths,
+ 0, NULL, delete_cb, delete_arg, repo, 0, 0);
if (err == NULL)
- err = schedule_add(newpath, worktree, repo,
- add_cb, add_arg);
+ err = got_worktree_schedule_add(worktree, &newpaths,
+ add_cb, add_arg, repo, 1);
} else if (p->old == NULL)
- err = schedule_add(newpath, worktree, repo, add_cb,
- add_arg);
+ err = got_worktree_schedule_add(worktree, &newpaths,
+ add_cb, add_arg, repo, 1);
else
printf("M %s\n", oldpath); /* XXX */
done:
- if (err != NULL && (file_renamed || p->old == NULL))
+ if (err != NULL && newpath != NULL && (file_renamed || p->old == NULL))
unlink(newpath);
free(template);
if (tmppath != NULL)
unlink(tmppath);
free(tmppath);
+ got_pathlist_free(&oldpaths);
+ got_pathlist_free(&newpaths);
free(oldpath);
free(newpath);
return err;
@@ -579,7 +641,8 @@ done:
const struct got_error *
got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
got_worktree_delete_cb delete_cb, void *delete_arg,
- got_worktree_checkout_cb add_cb, void *add_arg)
+ got_worktree_checkout_cb add_cb, void *add_arg, got_cancel_cb cancel_cb,
+ void *cancel_arg)
{
const struct got_error *err = NULL;
struct imsgbuf *ibuf;
@@ -628,7 +691,7 @@ got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
break;
err = apply_patch(worktree, repo, &p, delete_cb, delete_arg,
- add_cb, add_arg);
+ add_cb, add_arg, cancel_cb, cancel_arg);
patch_free(&p);
if (err)
break;
diff --git a/regress/cmdline/patch.sh b/regress/cmdline/patch.sh
index 5916921..36c9ef5 100755
--- a/regress/cmdline/patch.sh
+++ b/regress/cmdline/patch.sh
@@ -685,6 +685,7 @@ EOF
test_done $testroot $ret
return 1
fi
+ rm $testroot/wt/eta
cat <<EOF > $testroot/wt/patch
--- alpha
@@ -730,6 +731,151 @@ EOF
test_done $testroot $ret
}
+test_patch_illegal_status() {
+ local testroot=`test_init patch_illegal_status`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ test_done $testroot $ret
+ return 1
+ fi
+
+ # edit an non-existent and unknown file
+ cat <<EOF > $testroot/wt/patch
+--- iota
++++ iota
+@@ -1 +1 @@
+- iota
++ IOTA
+EOF
+
+ (cd $testroot/wt && got patch patch) > /dev/null \
+ 2> $testroot/stderr
+ ret=$?
+ if [ $ret -eq 0 ]; then
+ echo "edited a missing file" >&2
+ test_done $testroot $ret
+ return 1
+ fi
+
+ echo "got: iota: No such file or directory" \
+ > $testroot/stderr.expected
+ cmp -s $testroot/stderr.expected $testroot/stderr
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ diff -u $testroot/stderr.expected $testroot/stderr
+ test_done $testroot $ret
+ return 1
+ fi
+
+ # create iota and re-try
+ echo iota > $testroot/wt/iota
+
+ (cd $testroot/wt && got patch patch) > /dev/null \
+ 2> $testroot/stderr
+ ret=$?
+ if [ $ret -eq 0 ]; then
+ echo "patched an unknown file" >&2
+ test_done $testroot $ret
+ return 1
+ fi
+
+ echo "got: iota: file has unexpected status" \
+ > $testroot/stderr.expected
+ cmp -s $testroot/stderr.expected $testroot/stderr
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ diff -u $testroot/stderr.expected $testroot/stderr
+ test_done $testroot $ret
+ return 1
+ fi
+
+ rm $testroot/wt/iota
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ test_done $testroot $ret
+ return 1
+ fi
+
+ # edit obstructed file
+ rm $testroot/wt/alpha
+ mkdir $testroot/wt/alpha
+ cat <<EOF > $testroot/wt/patch
+--- alpha
++++ alpha
+@@ -1 +1,2 @@
+ alpha
++was edited
+EOF
+
+ (cd $testroot/wt && got patch patch) > /dev/null \
+ 2> $testroot/stderr
+ ret=$?
+ if [ $ret -eq 0 ]; then
+ echo "edited a missing file" >&2
+ test_done $testroot $ret
+ return 1
+ fi
+
+ echo "got: alpha: file has unexpected status" \
+ > $testroot/stderr.expected
+ cmp -s $testroot/stderr.expected $testroot/stderr
+ ret=$?
+ if [ $ret -ne 0 ]; then
+ diff -u $testroot/stderr.expected $testroot/stderr
+ test_done $testroot $ret
+ return 1
+ fi
+
+ # delete an unknown file
+ cat <<EOF > $testroot/wt/patch
+--- iota
++++ /dev/null
+@@ -1 +0,0 @@
+-iota
+EOF
+
+ (cd $testroot/wt && got patch patch) > /dev/null \
+ 2> $testroot/stderr
+ ret=$?
+ if [ $ret -eq 0 ]; then
+ echo "deleted a missing file?" >&2
+ test_done $testroot $ret
+ return 1
+ fi
+
+ echo "got: iota: No such file or directory" \
+ > $testroot/stderr.expected
+ cmp -s $testroot/stderr.expected $testroot/stderr
+ ret=$?
+ if [ $ret -eq 0 ]; then
+ diff -u $testroot/stderr.expected $testroot/stderr
+ test_done $testroot $ret
+ return 1
+ fi
+
+ # try again with iota in place but still not registered
+ echo iota > $testroot/wt/iota
+ (cd $testroot/wt && got patch patch) > /dev/null \
+ 2> $testroot/stderr
+ ret=$?
+ if [ $ret -eq 0 ]; then
+ echo "deleted an unversioned file?" >&2
+ test_done $testroot $ret
+ return 1
+ fi
+
+ echo "got: iota: file has unexpected status" \
+ > $testroot/stderr.expected
+ cmp -s $testroot/stderr.expected $testroot/stderr
+ ret=$?
+ if [ $ret -eq 0 ]; then
+ diff -u $testroot/stderr.expected $testroot/stderr
+ fi
+ test_done $testroot $ret
+}
+
test_parseargs "$@"
run_test test_patch_simple_add_file
run_test test_patch_simple_rm_file
@@ -743,3 +889,4 @@ run_test test_patch_malformed
run_test test_patch_no_patch
run_test test_patch_equals_for_context
run_test test_patch_rename
+run_test test_patch_illegal_status