add a basic implementation of 'got rm'
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 574 575 576 577 578 579 580 581 582 583 584
diff --git a/got/got.1 b/got/got.1
index 7bf08a3..2944724 100644
--- a/got/got.1
+++ b/got/got.1
@@ -286,6 +286,17 @@ Delete the reference with the specified name from the repository.
.It Cm add Ar file-path
Schedule an unversioned file in a work tree for addition to the
repository in the next commit.
+.It Cm rm Ar file-path
+Remove a versioned file from a work tree and schedule it for deletion
+from the repository in the next commit.
+.Pp
+The options for
+.Cm got rm
+are as follows:
+.Bl -tag -width Ds
+.It Fl f
+Perform the operation even if the file contains uncommitted modifications.
+.El
.El
.Sh EXIT STATUS
.Ex -std got
diff --git a/got/got.c b/got/got.c
index ec9e9c4..9580988 100644
--- a/got/got.c
+++ b/got/got.c
@@ -78,6 +78,7 @@ __dead static void usage_tree(void);
__dead static void usage_status(void);
__dead static void usage_ref(void);
__dead static void usage_add(void);
+__dead static void usage_rm(void);
static const struct got_error* cmd_checkout(int, char *[]);
static const struct got_error* cmd_update(int, char *[]);
@@ -88,6 +89,7 @@ static const struct got_error* cmd_tree(int, char *[]);
static const struct got_error* cmd_status(int, char *[]);
static const struct got_error* cmd_ref(int, char *[]);
static const struct got_error* cmd_add(int, char *[]);
+static const struct got_error* cmd_rm(int, char *[]);
static struct cmd got_commands[] = {
{ "checkout", cmd_checkout, usage_checkout,
@@ -108,6 +110,8 @@ static struct cmd got_commands[] = {
"manage references in repository" },
{ "add", cmd_add, usage_add,
"add a new file to version control" },
+ { "rm", cmd_rm, usage_rm,
+ "remove a versioned file" },
};
int
@@ -996,7 +1000,8 @@ print_diff(void *arg, unsigned char status, const char *path,
char *abspath = NULL;
struct stat sb;
- if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD)
+ if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
+ status != GOT_STATUS_DELETE)
return NULL;
if (!a->header_shown) {
@@ -1005,28 +1010,31 @@ print_diff(void *arg, unsigned char status, const char *path,
a->header_shown = 1;
}
- if (status == GOT_STATUS_MODIFY) {
+ if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_DELETE) {
err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
if (err)
goto done;
}
- if (asprintf(&abspath, "%s/%s",
- got_worktree_get_root_path(a->worktree), path) == -1) {
- err = got_error_from_errno();
- goto done;
- }
+ if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
+ if (asprintf(&abspath, "%s/%s",
+ got_worktree_get_root_path(a->worktree), path) == -1) {
+ err = got_error_from_errno();
+ goto done;
+ }
- f2 = fopen(abspath, "r");
- if (f2 == NULL) {
- err = got_error_from_errno();
- goto done;
- }
- if (lstat(abspath, &sb) == -1) {
- err = got_error_from_errno();
- goto done;
- }
+ f2 = fopen(abspath, "r");
+ if (f2 == NULL) {
+ err = got_error_from_errno();
+ goto done;
+ }
+ if (lstat(abspath, &sb) == -1) {
+ err = got_error_from_errno();
+ goto done;
+ }
+ } else
+ sb.st_size = 0;
err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
stdout);
@@ -1896,3 +1904,73 @@ done:
free(cwd);
return error;
}
+
+__dead static void
+usage_rm(void)
+{
+ fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
+ exit(1);
+}
+
+static const struct got_error *
+cmd_rm(int argc, char *argv[])
+{
+ const struct got_error *error = NULL;
+ struct got_worktree *worktree = NULL;
+ struct got_repository *repo = NULL;
+ char *cwd = NULL, *path = NULL;
+ int ch, delete_local_mods = 0;
+
+ while ((ch = getopt(argc, argv, "f")) != -1) {
+ switch (ch) {
+ case 'f':
+ delete_local_mods = 1;
+ break;
+ default:
+ usage_add();
+ /* NOTREACHED */
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ if (argc != 1)
+ usage_rm();
+
+ path = realpath(argv[0], NULL);
+ if (path == NULL) {
+ error = got_error_from_errno();
+ goto done;
+ }
+
+ cwd = getcwd(NULL, 0);
+ if (cwd == NULL) {
+ error = got_error_from_errno();
+ goto done;
+ }
+ error = got_worktree_open(&worktree, cwd);
+ if (error)
+ goto done;
+
+ error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
+ if (error != NULL)
+ goto done;
+
+ error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
+ if (error)
+ goto done;
+
+ error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
+ print_status, NULL, repo);
+ if (error)
+ goto done;
+done:
+ if (repo)
+ got_repo_close(repo);
+ if (worktree)
+ got_worktree_close(worktree);
+ free(path);
+ free(cwd);
+ return error;
+}
diff --git a/include/got_error.h b/include/got_error.h
index 67326be..f60e3a7 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -80,6 +80,8 @@
#define GOT_ERR_LOCKFILE_TIMEOUT 64
#define GOT_ERR_BAD_REF_NAME 65
#define GOT_ERR_WORKTREE_REPO 66
+#define GOT_ERR_FILE_MODIFIED 67
+#define GOT_ERR_FILE_STATUS 68
static const struct got_error {
int code;
@@ -150,6 +152,8 @@ static const struct got_error {
{ GOT_ERR_LOCKFILE_TIMEOUT,"lockfile timeout" },
{ GOT_ERR_BAD_REF_NAME, "bad reference name" },
{ GOT_ERR_WORKTREE_REPO,"cannot create worktree inside a git repository" },
+ { GOT_ERR_FILE_MODIFIED,"file contains modifications" },
+ { GOT_ERR_FILE_STATUS, "file has unexpected status" },
};
/*
diff --git a/include/got_worktree.h b/include/got_worktree.h
index 6c64d84..51e30c8 100644
--- a/include/got_worktree.h
+++ b/include/got_worktree.h
@@ -139,3 +139,12 @@ const struct got_error *got_worktree_resolve_path(char **,
*/
const struct got_error *got_worktree_schedule_add(char **,
struct got_worktree *, const char *);
+
+/*
+ * Remove a file from disk and schedule it to be deleted in the next commit.
+ * Don't allow deleting files with uncommitted modifications, unless the
+ * parameter 'delete_local_mods' is set.
+ */
+const struct got_error *
+got_worktree_schedule_delete(struct got_worktree *, const char *, int,
+ got_worktree_status_cb, void *, struct got_repository *);
diff --git a/lib/fileindex.c b/lib/fileindex.c
index 3530726..3421739 100644
--- a/lib/fileindex.c
+++ b/lib/fileindex.c
@@ -42,6 +42,7 @@
#define GOT_FILEIDX_F_NOT_FLUSHED 0x00010000
#define GOT_FILEIDX_F_NO_BLOB 0x00020000
#define GOT_FILEIDX_F_NO_COMMIT 0x00040000
+#define GOT_FILEIDX_F_NO_FILE_ON_DISK 0x00080000
struct got_fileindex {
struct got_fileindex_tree entries;
@@ -59,6 +60,8 @@ got_fileindex_entry_update(struct got_fileindex_entry *entry,
if (lstat(ondisk_path, &sb) != 0)
return got_error_from_errno();
+ entry->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
+
if (update_timestamps) {
entry->ctime_sec = sb.st_ctime;
entry->ctime_nsec = sb.st_ctimensec;
@@ -90,6 +93,12 @@ got_fileindex_entry_update(struct got_fileindex_entry *entry,
return NULL;
}
+void
+got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *entry)
+{
+ entry->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
+}
+
const struct got_error *
got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
@@ -137,6 +146,12 @@ got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
}
+int
+got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
+{
+ return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
+}
+
static const struct got_error *
add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *entry)
{
diff --git a/lib/got_lib_fileindex.h b/lib/got_lib_fileindex.h
index 98ab532..9829455 100644
--- a/lib/got_lib_fileindex.h
+++ b/lib/got_lib_fileindex.h
@@ -141,3 +141,6 @@ const struct got_error *got_fileindex_diff_dir(struct got_fileindex *, DIR *,
int got_fileindex_entry_has_blob(struct got_fileindex_entry *);
int got_fileindex_entry_has_commit(struct got_fileindex_entry *);
+int got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *);
+
+void got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *);
diff --git a/lib/worktree.c b/lib/worktree.c
index bd919fb..9b86570 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -967,7 +967,10 @@ get_file_status(unsigned char *status, struct stat *sb,
if (lstat(abspath, sb) == -1) {
if (errno == ENOENT) {
if (ie) {
- *status = GOT_STATUS_MISSING;
+ if (got_fileindex_entry_has_file_on_disk(ie))
+ *status = GOT_STATUS_MISSING;
+ else
+ *status = GOT_STATUS_DELETE;
sb->st_mode =
((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
& (S_IRWXU | S_IRWXG | S_IRWXO));
@@ -986,7 +989,10 @@ get_file_status(unsigned char *status, struct stat *sb,
if (ie == NULL)
return NULL;
- if (!got_fileindex_entry_has_blob(ie)) {
+ if (!got_fileindex_entry_has_file_on_disk(ie)) {
+ *status = GOT_STATUS_DELETE;
+ return NULL;
+ } else if (!got_fileindex_entry_has_blob(ie)) {
*status = GOT_STATUS_ADD;
return NULL;
}
@@ -1424,13 +1430,17 @@ status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
{
struct diff_dir_cb_arg *a = arg;
struct got_object_id id;
+ unsigned char status;
if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
return NULL;
memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
- return (*a->status_cb)(a->status_arg, GOT_STATUS_MISSING, ie->path,
- &id);
+ if (got_fileindex_entry_has_file_on_disk(ie))
+ status = GOT_STATUS_MISSING;
+ else
+ status = GOT_STATUS_DELETE;
+ return (*a->status_cb)(a->status_arg, status, ie->path, &id);
}
static const struct got_error *
@@ -1511,7 +1521,7 @@ got_worktree_status(struct got_worktree *worktree, const char *path,
}
workdir = opendir(ondisk_path);
if (workdir == NULL) {
- if (errno == ENOTDIR) {
+ if (errno == ENOTDIR || errno == ENOENT) {
struct got_fileindex_entry *ie;
ie = got_fileindex_entry_get(fileindex, path);
if (ie == NULL) {
@@ -1682,3 +1692,114 @@ done:
}
return err;
}
+
+const struct got_error *
+got_worktree_schedule_delete(struct got_worktree *worktree,
+ const char *ondisk_path, int delete_local_mods,
+ got_worktree_status_cb status_cb, void *status_arg,
+ struct got_repository *repo)
+{
+ struct got_fileindex *fileindex = NULL;
+ struct got_fileindex_entry *ie = NULL;
+ char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
+ FILE *index = NULL, *new_index = NULL;
+ const struct got_error *err = NULL, *unlockerr = NULL;
+ unsigned char status;
+ struct stat sb;
+
+ err = lock_worktree(worktree, LOCK_EX);
+ if (err)
+ return err;
+
+ err = got_path_skip_common_ancestor(&relpath,
+ got_worktree_get_root_path(worktree), ondisk_path);
+ if (err)
+ goto done;
+
+ fileindex = got_fileindex_alloc();
+ if (fileindex == NULL) {
+ err = got_error_from_errno();
+ goto done;
+ }
+
+ if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
+ GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
+ err = got_error_from_errno();
+ fileindex_path = NULL;
+ goto done;
+ }
+
+ index = fopen(fileindex_path, "rb");
+ if (index == NULL) {
+ err = got_error_from_errno();
+ goto done;
+ }
+
+ err = got_fileindex_read(fileindex, index);
+ if (err)
+ goto done;
+
+ ie = got_fileindex_entry_get(fileindex, relpath);
+ if (ie == NULL) {
+ err = got_error(GOT_ERR_BAD_PATH);
+ goto done;
+ }
+
+ err = get_file_status(&status, &sb, ie, ondisk_path, repo);
+ if (err)
+ goto done;
+
+ if (status != GOT_STATUS_NO_CHANGE) {
+ if (status != GOT_STATUS_MODIFY) {
+ err = got_error(GOT_ERR_FILE_STATUS);
+ goto done;
+ }
+ if (!delete_local_mods) {
+ err = got_error(GOT_ERR_FILE_MODIFIED);
+ goto done;
+ }
+ }
+
+ if (unlink(ondisk_path) != 0) {
+ err = got_error_from_errno();
+ goto done;
+ }
+
+ got_fileindex_entry_mark_deleted_from_disk(ie);
+
+ err = got_opentemp_named(&new_fileindex_path, &new_index,
+ fileindex_path);
+ if (err)
+ goto done;
+
+ err = got_fileindex_write(fileindex, new_index);
+ if (err)
+ goto done;
+
+ if (rename(new_fileindex_path, fileindex_path) != 0) {
+ err = got_error_from_errno();
+ goto done;
+ }
+
+ free(new_fileindex_path);
+ new_fileindex_path = NULL;
+
+ err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
+done:
+ free(relpath);
+ if (index) {
+ if (fclose(index) != 0 && err == NULL)
+ err = got_error_from_errno();
+ }
+ if (new_fileindex_path) {
+ if (unlink(new_fileindex_path) != 0 && err == NULL)
+ err = got_error_from_errno();
+ free(new_fileindex_path);
+ }
+ if (fileindex)
+ got_fileindex_free(fileindex);
+ unlockerr = lock_worktree(worktree, LOCK_SH);
+ if (unlockerr && err == NULL)
+ err = unlockerr;
+ return err;
+}
diff --git a/regress/cmdline/Makefile b/regress/cmdline/Makefile
index fb63dcc..3692fa0 100644
--- a/regress/cmdline/Makefile
+++ b/regress/cmdline/Makefile
@@ -1,4 +1,4 @@
-REGRESS_TARGETS=checkout update status log add
+REGRESS_TARGETS=checkout update status log add rm
NOOBJ=Yes
checkout:
@@ -16,4 +16,8 @@ log:
add:
./add.sh
+rm:
+ ./rm.sh
+
+
.include <bsd.regress.mk>
diff --git a/regress/cmdline/rm.sh b/regress/cmdline/rm.sh
new file mode 100755
index 0000000..8753e23
--- /dev/null
+++ b/regress/cmdline/rm.sh
@@ -0,0 +1,88 @@
+#!/bin/sh
+#
+# Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
+#
+# Permission to use, copy, modify, and distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+. ./common.sh
+
+function test_rm_basic {
+ local testroot=`test_init rm_basic`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo 'D beta' > $testroot/stdout.expected
+ (cd $testroot/wt && got rm beta > $testroot/stdout)
+
+ cmp $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+
+ if [ -e $testroot/wt/beta ]; then
+ echo "removed file beta still exists on disk" >&2
+ test_done "$testroot" "1"
+ return 1
+ fi
+
+ test_done "$testroot" "$ret"
+}
+
+function test_rm_with_local_mods {
+ local testroot=`test_init rm_with_local_mods`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "modified beta" > $testroot/wt/beta
+ echo 'got: file contains modifications' > $testroot/stderr.expected
+ (cd $testroot/wt && got rm beta 2>$testroot/stderr)
+
+ cmp $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 'D beta' > $testroot/stdout.expected
+ (cd $testroot/wt && got rm -f beta > $testroot/stdout)
+
+ cmp $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+
+ if [ -e $testroot/wt/beta ]; then
+ echo "removed file beta still exists on disk" >&2
+ test_done "$testroot" "1"
+ return 1
+ fi
+
+ test_done "$testroot" "$ret"
+}
+
+run_test test_rm_basic
+run_test test_rm_with_local_mods
diff --git a/regress/cmdline/status.sh b/regress/cmdline/status.sh
index bdf87dc..84bbf2e 100755
--- a/regress/cmdline/status.sh
+++ b/regress/cmdline/status.sh
@@ -27,6 +27,7 @@ function test_status_basic {
fi
echo "modified alpha" > $testroot/wt/alpha
+ (cd $testroot/wt && got rm beta >/dev/null)
echo "unversioned file" > $testroot/wt/foo
rm $testroot/wt/epsilon/zeta
touch $testroot/wt/beta
@@ -34,6 +35,7 @@ function test_status_basic {
(cd $testroot/wt && got add new >/dev/null)
echo 'M alpha' > $testroot/stdout.expected
+ echo 'D beta' >> $testroot/stdout.expected
echo '! epsilon/zeta' >> $testroot/stdout.expected
echo '? foo' >> $testroot/stdout.expected
echo 'A new' >> $testroot/stdout.expected