make 'got update' remove files which were deleted
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
diff --git a/include/got_worktree.h b/include/got_worktree.h
index 1424a42..689be62 100644
--- a/include/got_worktree.h
+++ b/include/got_worktree.h
@@ -20,6 +20,7 @@ struct got_worktree;
#define GOT_STATUS_ADD 'A'
#define GOT_STATUS_EXISTS 'E'
#define GOT_STATUS_UPDATE 'U'
+#define GOT_STATUS_DELETE 'D'
/*
* Attempt to initialize a new work tree on disk.
diff --git a/lib/fileindex.c b/lib/fileindex.c
index f49097e..dbe625f 100644
--- a/lib/fileindex.c
+++ b/lib/fileindex.c
@@ -100,6 +100,14 @@ got_fileindex_entry_add(struct got_fileindex *fileindex,
return NULL;
}
+void
+got_fileindex_entry_remove(struct got_fileindex *fileindex,
+ struct got_fileindex_entry *entry)
+{
+ TAILQ_REMOVE(&fileindex->entries, entry, entry);
+ fileindex->nentries--;
+}
+
struct got_fileindex_entry *
got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
{
@@ -112,6 +120,23 @@ got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
return NULL;
}
+const struct got_error *
+got_fileindex_for_each_entry(struct got_fileindex *fileindex,
+ const struct got_error *(cb)(void *, struct got_fileindex_entry *),
+ void *cb_arg)
+{
+ const struct got_error *err = NULL;
+ struct got_fileindex_entry *entry;
+
+ TAILQ_FOREACH(entry, &fileindex->entries, entry) {
+ err = cb(cb_arg, entry);
+ if (err)
+ break;
+ }
+
+ return err;
+}
+
struct got_fileindex *
got_fileindex_alloc(void)
{
diff --git a/lib/got_lib_fileindex.h b/lib/got_lib_fileindex.h
index 78770a1..f93174f 100644
--- a/lib/got_lib_fileindex.h
+++ b/lib/got_lib_fileindex.h
@@ -95,6 +95,10 @@ void got_fileindex_free(struct got_fileindex *);
const struct got_error *got_fileindex_write(struct got_fileindex *, FILE *);
const struct got_error *got_fileindex_entry_add(struct got_fileindex *,
struct got_fileindex_entry *);
+void got_fileindex_entry_remove(struct got_fileindex *,
+ struct got_fileindex_entry *);
struct got_fileindex_entry *got_fileindex_entry_get(struct got_fileindex *,
const char *);
const struct got_error *got_fileindex_read(struct got_fileindex *, FILE *);
+const struct got_error *got_fileindex_for_each_entry(struct got_fileindex *,
+ const struct got_error *(cb)(void *, struct got_fileindex_entry *), void *);
diff --git a/lib/worktree.c b/lib/worktree.c
index 94e0615..79520bd 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -27,6 +27,7 @@
#include <sha1.h>
#include <zlib.h>
#include <fnmatch.h>
+#include <libgen.h>
#include "got_error.h"
#include "got_repository.h"
@@ -741,6 +742,105 @@ done:
return err;
}
+struct collect_missing_entry_args {
+ struct got_fileindex *fileindex;
+ const struct got_tree_entries *entries;
+ struct got_fileindex missing_entries;
+ const char *path_prefix;
+};
+
+static const struct got_error *
+collect_missing_file(void *args, struct got_fileindex_entry *entry)
+{
+ struct collect_missing_entry_args *a = args;
+ char *name;
+ struct got_tree_entry *te;
+ int found = 0;
+
+ if (a->path_prefix[0] == '\0' && strchr(entry->path, '/') != NULL)
+ return NULL;
+ if (a->path_prefix[0] != '\0' &&
+ strncmp(a->path_prefix, entry->path, strlen(a->path_prefix)) != 0)
+ return NULL;
+
+ name = basename(entry->path);
+ if (name == NULL)
+ return got_error_from_errno();
+
+ SIMPLEQ_FOREACH(te, &a->entries->head, entry) {
+ if (strcmp(te->name, name) == 0) {
+ found = 1;
+ break;
+ }
+ }
+
+ if (found)
+ return NULL;
+
+ got_fileindex_entry_remove(a->fileindex, entry);
+ return got_fileindex_entry_add(&a->missing_entries, entry);
+}
+
+/* Remove files which exist in the file index but not in the tree. */
+static const struct got_error *
+remove_missing_files(struct got_worktree *worktree, const char *path,
+ struct got_fileindex *fileindex, const struct got_tree_entries *entries,
+ got_worktree_checkout_cb progress_cb, void *progress_arg,
+ got_worktree_cancel_cb cancel_cb, void *cancel_arg)
+{
+ const struct got_error *err = NULL;
+ struct collect_missing_entry_args a;
+ struct got_fileindex_entry *entry, *tmp;
+
+ a.fileindex = fileindex;
+ a.entries = entries;
+ a.missing_entries.nentries = 0;
+ a.path_prefix = path;
+ while (a.path_prefix[0] == '/')
+ a.path_prefix++;
+ TAILQ_INIT(&a.missing_entries.entries);
+ err = got_fileindex_for_each_entry(fileindex, collect_missing_file, &a);
+ if (err)
+ return err;
+
+ TAILQ_FOREACH_SAFE(entry, &a.missing_entries.entries, entry, tmp) {
+ char *ondisk_path = NULL;
+
+ if (cancel_cb) {
+ err = (*cancel_cb)(cancel_arg);
+ if (err)
+ break;
+ }
+
+ (*progress_cb)(progress_arg, GOT_STATUS_DELETE, entry->path);
+
+ if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
+ entry->path) == -1) {
+ err = got_error_from_errno();
+ break;
+ }
+
+ if (unlink(ondisk_path) == -1)
+ err = got_error_from_errno();
+ free(ondisk_path);
+ if (err)
+ break;
+
+ TAILQ_REMOVE(&a.missing_entries.entries, entry, entry);
+ got_fileindex_entry_free(entry);
+ }
+
+ if (err) {
+ while (!TAILQ_EMPTY(&a.missing_entries.entries)) {
+ entry = TAILQ_FIRST(&a.missing_entries.entries);
+ TAILQ_REMOVE(&a.missing_entries.entries, entry, entry);
+ got_fileindex_entry_free(entry);
+ }
+ }
+
+ return err;
+}
+
static const struct got_error *
tree_checkout(struct got_worktree *worktree,
struct got_fileindex *fileindex, struct got_tree_object *tree,
@@ -763,12 +863,20 @@ tree_checkout(struct got_worktree *worktree,
if (cancel_cb) {
err = (*cancel_cb)(cancel_arg);
if (err)
- break;
+ return err;
}
err = tree_checkout_entry(worktree, fileindex, te, path, repo,
progress_cb, progress_arg, cancel_cb, cancel_arg);
if (err)
- break;
+ return err;
+ }
+
+ len = strlen(worktree->path_prefix);
+ if (strncmp(worktree->path_prefix, path, len) == 0) {
+ err = remove_missing_files(worktree, path, fileindex, entries,
+ progress_cb, progress_arg, cancel_cb, cancel_arg);
+ if (err)
+ return err;
}
return err;
diff --git a/regress/cmdline/common.sh b/regress/cmdline/common.sh
index 00ace25..a8a3ea5 100644
--- a/regress/cmdline/common.sh
+++ b/regress/cmdline/common.sh
@@ -26,6 +26,13 @@ function git_commit
(cd $repo && git commit -q -a "$@")
}
+function git_rm
+{
+ local repo="$1"
+ shift
+ (cd $repo && git rm -q "$@")
+}
+
function git_show_head
{
local repo="$1"
diff --git a/regress/cmdline/update.sh b/regress/cmdline/update.sh
index f5525cc..644f3de 100755
--- a/regress/cmdline/update.sh
+++ b/regress/cmdline/update.sh
@@ -98,5 +98,50 @@ function test_update_adds_file {
test_done "$testroot" "$?"
}
+function test_update_deletes_file {
+ local testroot=`test_init update_deletes_file`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ if [ "$?" != "0" ]; then
+ test_done "$testroot" "$?"
+ return 1
+ fi
+
+ (cd $testroot/repo && git_rm $testroot/repo beta)
+ git_commit $testroot/repo -m "deleting a file"
+
+ echo "D 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 > $testroot/stdout)
+
+ cmp $testroot/stdout.expected $testroot/stdout
+ if [ "$?" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$?"
+ return 1
+ fi
+
+ if [ -e $testroot/wt/beta ]; then
+ echo "removed file beta still exists on disk" >&2
+ return 1
+ fi
+
+ echo "alpha" >> $testroot/content.expected
+ echo "zeta" >> $testroot/content.expected
+ echo "delta" >> $testroot/content.expected
+ cat $testroot/wt/alpha $testroot/wt/epsilon/zeta \
+ $testroot/wt/gamma/delta > $testroot/content
+
+ cmp $testroot/content.expected $testroot/content
+ if [ "$?" != "0" ]; then
+ diff -u $testroot/content.expected $testroot/content
+ fi
+ test_done "$testroot" "$?"
+}
+
run_test test_update_basic
run_test test_update_adds_file
+run_test test_update_deletes_file