trim directories in got remove -R This is a racy solution that needs to be properly implemented in the future. ok stsp
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
diff --git a/lib/worktree.c b/lib/worktree.c
index c9da5df..9f99172 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -2992,7 +2992,7 @@ schedule_for_deletion(void *arg, unsigned char status,
const struct got_error *err = NULL;
struct got_fileindex_entry *ie = NULL;
struct stat sb;
- char *ondisk_path;
+ char *ondisk_path, *parent = NULL;
ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
if (ie == NULL)
@@ -3039,6 +3039,26 @@ schedule_for_deletion(void *arg, unsigned char status,
err = got_error_from_errno2("unlink", ondisk_path);
goto done;
}
+
+ parent = dirname(ondisk_path);
+
+ if (parent == NULL) {
+ err = got_error_from_errno2("dirname", ondisk_path);
+ goto done;
+ }
+ while (parent && strcmp(parent, a->worktree->root_path) != 0) {
+ if (rmdir(parent) == -1) {
+ if (errno != ENOTEMPTY)
+ err = got_error_from_errno2("rmdir",
+ parent);
+ break;
+ }
+ parent = dirname(parent);
+ if (parent == NULL) {
+ err = got_error_from_errno2("dirname", parent);
+ goto done;
+ }
+ }
}
got_fileindex_entry_mark_deleted_from_disk(ie);
diff --git a/regress/cmdline/revert.sh b/regress/cmdline/revert.sh
index fdeae68..21b308c 100755
--- a/regress/cmdline/revert.sh
+++ b/regress/cmdline/revert.sh
@@ -990,6 +990,69 @@ function test_revert_added_subtree {
test_done "$testroot" "$ret"
}
+function test_revert_deleted_subtree {
+ local testroot=`test_init revert_deleted_subtree`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ mkdir -p $testroot/wt/epsilon/foo/bar/baz
+ mkdir -p $testroot/wt/epsilon/foo/bar/bax
+ echo "new file" > $testroot/wt/epsilon/foo/a.o
+ echo "new file" > $testroot/wt/epsilon/foo/a.o
+ echo "new file" > $testroot/wt/epsilon/foo/bar/b.o
+ echo "new file" > $testroot/wt/epsilon/foo/bar/b.d
+ echo "new file" > $testroot/wt/epsilon/foo/bar/baz/f.o
+ echo "new file" > $testroot/wt/epsilon/foo/bar/baz/f.d
+ echo "new file" > $testroot/wt/epsilon/foo/bar/baz/c.o
+ echo "new file" > $testroot/wt/epsilon/foo/bar/baz/c.d
+ echo "new file" > $testroot/wt/epsilon/foo/bar/bax/e.o
+ echo "new file" > $testroot/wt/epsilon/foo/bar/bax/e.d
+ echo "new file" > $testroot/wt/epsilon/foo/bar/bax/x.o
+ echo "new file" > $testroot/wt/epsilon/foo/bar/bax/x.d
+ (cd $testroot/wt && got add -R epsilon >/dev/null)
+ (cd $testroot/wt && got commit -m "add subtree" >/dev/null)
+
+ # now delete and revert the entire subtree
+ (cd $testroot/wt && got rm -R epsilon/foo >/dev/null)
+
+ echo "R epsilon/foo/a.o" > $testroot/stdout.expected
+ echo "R epsilon/foo/bar/b.d" >> $testroot/stdout.expected
+ echo "R epsilon/foo/bar/b.o" >> $testroot/stdout.expected
+ echo "R epsilon/foo/bar/bax/e.d" >> $testroot/stdout.expected
+ echo "R epsilon/foo/bar/bax/e.o" >> $testroot/stdout.expected
+ echo "R epsilon/foo/bar/bax/x.d" >> $testroot/stdout.expected
+ echo "R epsilon/foo/bar/bax/x.o" >> $testroot/stdout.expected
+ echo "R epsilon/foo/bar/baz/c.d" >> $testroot/stdout.expected
+ echo "R epsilon/foo/bar/baz/c.o" >> $testroot/stdout.expected
+ echo "R epsilon/foo/bar/baz/f.d" >> $testroot/stdout.expected
+ echo "R epsilon/foo/bar/baz/f.o" >> $testroot/stdout.expected
+
+ (cd $testroot/wt && got revert -R . > $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 -n > $testroot/stdout.expected
+ (cd $testroot/wt && got status > $testroot/stdout)
+
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+ test_done "$testroot" "$ret"
+}
+
run_test test_revert_basic
run_test test_revert_rm
run_test test_revert_add
@@ -1003,3 +1066,4 @@ run_test test_revert_patch_added
run_test test_revert_patch_removed
run_test test_revert_patch_one_change
run_test test_revert_added_subtree
+run_test test_revert_deleted_subtree
diff --git a/regress/cmdline/rm.sh b/regress/cmdline/rm.sh
index 9ea8be8..5699010 100755
--- a/regress/cmdline/rm.sh
+++ b/regress/cmdline/rm.sh
@@ -239,6 +239,30 @@ function test_rm_directory {
return 1
fi
+ (cd $testroot/wt && ls -l > $testroot/stdout)
+
+ 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
+
+ (cd $testroot/wt && ls -l > $testroot/stdout)
+
+ 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
+
test_done "$testroot" "$ret"
}
@@ -322,9 +346,68 @@ function test_rm_directory_keep_files {
test_done "$testroot" "$ret"
}
+function test_rm_subtree {
+ local testroot=`test_init rm_subtree`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ mkdir -p $testroot/wt/epsilon/foo/bar/baz
+ mkdir -p $testroot/wt/epsilon/foo/bar/bax
+ echo "new file" > $testroot/wt/epsilon/foo/a.o
+ echo "new file" > $testroot/wt/epsilon/foo/a.o
+ echo "new file" > $testroot/wt/epsilon/foo/bar/b.o
+ echo "new file" > $testroot/wt/epsilon/foo/bar/b.d
+ echo "new file" > $testroot/wt/epsilon/foo/bar/baz/f.o
+ echo "new file" > $testroot/wt/epsilon/foo/bar/baz/f.d
+ echo "new file" > $testroot/wt/epsilon/foo/bar/baz/c.o
+ echo "new file" > $testroot/wt/epsilon/foo/bar/baz/c.d
+ echo "new file" > $testroot/wt/epsilon/foo/bar/bax/e.o
+ echo "new file" > $testroot/wt/epsilon/foo/bar/bax/e.d
+ echo "new file" > $testroot/wt/epsilon/foo/bar/bax/x.o
+ echo "new file" > $testroot/wt/epsilon/foo/bar/bax/x.d
+ (cd $testroot/wt && got add -R epsilon >/dev/null)
+ (cd $testroot/wt && got commit -m "add subtree" >/dev/null)
+
+ # now delete and revert the entire subtree
+ (cd $testroot/wt && got rm -R epsilon/foo >/dev/null)
+
+ if [ -d $testroot/wt/epsilon/foo ]; then
+ echo "removed dir epsilon/foo still exists on disk" >&2
+ test_done "$testroot" "1"
+ return 1
+ fi
+
+ echo "D epsilon/foo/a.o" > $testroot/stdout.expected
+ echo "D epsilon/foo/bar/b.d" >> $testroot/stdout.expected
+ echo "D epsilon/foo/bar/b.o" >> $testroot/stdout.expected
+ echo "D epsilon/foo/bar/bax/e.d" >> $testroot/stdout.expected
+ echo "D epsilon/foo/bar/bax/e.o" >> $testroot/stdout.expected
+ echo "D epsilon/foo/bar/bax/x.d" >> $testroot/stdout.expected
+ echo "D epsilon/foo/bar/bax/x.o" >> $testroot/stdout.expected
+ echo "D epsilon/foo/bar/baz/c.d" >> $testroot/stdout.expected
+ echo "D epsilon/foo/bar/baz/c.o" >> $testroot/stdout.expected
+ echo "D epsilon/foo/bar/baz/f.d" >> $testroot/stdout.expected
+ echo "D epsilon/foo/bar/baz/f.o" >> $testroot/stdout.expected
+
+ (cd $testroot/wt && got status > $testroot/stdout)
+
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+ test_done "$testroot" "$ret"
+}
+
run_test test_rm_basic
run_test test_rm_with_local_mods
run_test test_double_rm
run_test test_rm_and_add_elsewhere
run_test test_rm_directory
run_test test_rm_directory_keep_files
+run_test test_rm_subtree