convert bad symlinks to regular files during 'got commit'
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
diff --git a/lib/worktree.c b/lib/worktree.c
index 4f2a832..8d06a93 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -1025,7 +1025,8 @@ static const struct got_error *
install_blob(struct got_worktree *worktree, const char *ondisk_path,
const char *path, mode_t te_mode, mode_t st_mode,
struct got_blob_object *blob, int restoring_missing_file,
- int reverting_versioned_file, struct got_repository *repo,
+ int reverting_versioned_file, int installing_bad_symlink,
+ struct got_repository *repo,
got_worktree_checkout_cb progress_cb, void *progress_arg);
static const struct got_error *
@@ -1057,7 +1058,7 @@ install_symlink(struct got_worktree *worktree, const char *ondisk_path,
return install_blob(worktree, ondisk_path, path,
GOT_DEFAULT_FILE_MODE, st_mode, blob,
restoring_missing_file, reverting_versioned_file,
- repo, progress_cb, progress_arg);
+ 1, repo, progress_cb, progress_arg);
}
if (len > 0) {
/* Skip blob object header first time around. */
@@ -1106,7 +1107,7 @@ install_symlink(struct got_worktree *worktree, const char *ondisk_path,
got_object_blob_rewind(blob);
err = install_blob(worktree, ondisk_path, path,
GOT_DEFAULT_FILE_MODE, st_mode, blob,
- restoring_missing_file, reverting_versioned_file,
+ restoring_missing_file, reverting_versioned_file, 1,
repo, progress_cb, progress_arg);
goto done;
}
@@ -1123,7 +1124,7 @@ install_symlink(struct got_worktree *worktree, const char *ondisk_path,
got_object_blob_rewind(blob);
err = install_blob(worktree, ondisk_path, path,
GOT_DEFAULT_FILE_MODE, st_mode, blob,
- restoring_missing_file, reverting_versioned_file,
+ restoring_missing_file, reverting_versioned_file, 1,
repo, progress_cb, progress_arg);
goto done;
}
@@ -1158,8 +1159,10 @@ install_symlink(struct got_worktree *worktree, const char *ondisk_path,
target_len);
if (err)
goto done;
- err = (*progress_cb)(progress_arg,
- GOT_STATUS_UPDATE, path);
+ if (progress_cb) {
+ err = (*progress_cb)(progress_arg,
+ GOT_STATUS_UPDATE, path);
+ }
goto done;
}
}
@@ -1190,7 +1193,7 @@ install_symlink(struct got_worktree *worktree, const char *ondisk_path,
got_object_blob_rewind(blob);
err = install_blob(worktree, ondisk_path, path,
GOT_DEFAULT_FILE_MODE, st_mode, blob,
- restoring_missing_file, reverting_versioned_file,
+ restoring_missing_file, reverting_versioned_file, 1,
repo, progress_cb, progress_arg);
} else if (errno == ENOTDIR) {
err = got_error_path(ondisk_path,
@@ -1199,7 +1202,7 @@ install_symlink(struct got_worktree *worktree, const char *ondisk_path,
err = got_error_from_errno3("symlink",
target_path, ondisk_path);
}
- } else
+ } else if (progress_cb)
err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
done:
free(resolved_path);
@@ -1212,8 +1215,9 @@ static const struct got_error *
install_blob(struct got_worktree *worktree, const char *ondisk_path,
const char *path, mode_t te_mode, mode_t st_mode,
struct got_blob_object *blob, int restoring_missing_file,
- int reverting_versioned_file, struct got_repository *repo,
- got_worktree_checkout_cb progress_cb, void *progress_arg)
+ int reverting_versioned_file, int installing_bad_symlink,
+ struct got_repository *repo, got_worktree_checkout_cb progress_cb,
+ void *progress_arg)
{
const struct got_error *err = NULL;
int fd = -1;
@@ -1243,7 +1247,7 @@ install_blob(struct got_worktree *worktree, const char *ondisk_path,
return got_error_from_errno2("open",
ondisk_path);
} else if (errno == EEXIST) {
- if (!S_ISREG(st_mode)) {
+ if (!S_ISREG(st_mode) && !installing_bad_symlink) {
/* TODO file is obstructed; do something */
err = got_error_path(ondisk_path,
GOT_ERR_FILE_OBSTRUCTED);
@@ -1259,15 +1263,19 @@ install_blob(struct got_worktree *worktree, const char *ondisk_path,
return got_error_from_errno2("open", ondisk_path);
}
- if (restoring_missing_file)
- err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
- else if (reverting_versioned_file)
- err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
- else
- err = (*progress_cb)(progress_arg,
- update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
- if (err)
- goto done;
+ if (progress_cb) {
+ if (restoring_missing_file)
+ err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
+ path);
+ else if (reverting_versioned_file)
+ err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
+ path);
+ else
+ err = (*progress_cb)(progress_arg,
+ update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
+ if (err)
+ goto done;
+ }
hdrlen = got_object_blob_get_hdrlen(blob);
do {
@@ -1727,7 +1735,7 @@ update_blob(struct got_worktree *worktree,
goto done;
} else {
err = install_blob(worktree, ondisk_path, path, te->mode,
- sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
+ sb.st_mode, blob, status == GOT_STATUS_MISSING, 0, 0,
repo, progress_cb, progress_arg);
if (err)
goto done;
@@ -2559,7 +2567,7 @@ merge_file_cb(void *arg, struct got_blob_object *blob1,
err = install_blob(a->worktree, ondisk_path, path2,
/* XXX get this from parent tree! */
GOT_DEFAULT_FILE_MODE,
- sb.st_mode, blob2, 0, 0, repo,
+ sb.st_mode, blob2, 0, 0, 0, repo,
a->progress_cb, a->progress_arg);
if (err)
goto done;
@@ -4073,7 +4081,7 @@ revert_file(void *arg, unsigned char status, unsigned char staged_status,
} else {
err = install_blob(a->worktree, ondisk_path, ie->path,
te ? te->mode : GOT_DEFAULT_FILE_MODE,
- got_fileindex_perms_to_st(ie), blob, 0, 1,
+ got_fileindex_perms_to_st(ie), blob, 0, 1, 0,
a->repo, a->progress_cb, a->progress_arg);
if (err)
goto done;
@@ -4889,9 +4897,35 @@ commit_worktree(struct got_object_id **new_commit_id,
goto done;
}
err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
- free(ondisk_path);
- if (err)
+ if (err) {
+ free(ondisk_path);
goto done;
+ }
+
+ /*
+ * When comitting a symlink we convert "bad" symlinks (those
+ * which point outside the work tree or into .got) to regular
+ * files. This way, the post-commit work tree state matches
+ * a fresh checkout of the tree which was committed.
+ */
+ if (S_ISLNK(get_ct_file_mode(ct))) {
+ struct got_blob_object *blob;
+ err = got_object_open_as_blob(&blob, repo, ct->blob_id,
+ PATH_MAX);
+ if (err) {
+ free(ondisk_path);
+ goto done;
+ }
+ err = install_symlink(worktree, ondisk_path, ct->path,
+ get_ct_file_mode(ct), GOT_DEFAULT_FILE_MODE, blob,
+ 0, 0, repo, NULL, NULL);
+ got_object_blob_close(blob);
+ if (err) {
+ free(ondisk_path);
+ goto done;
+ }
+ }
+ free(ondisk_path);
}
/* Recursively write new tree objects. */
diff --git a/regress/cmdline/commit.sh b/regress/cmdline/commit.sh
index 9bf3085..45cd97f 100755
--- a/regress/cmdline/commit.sh
+++ b/regress/cmdline/commit.sh
@@ -902,101 +902,107 @@ function test_commit_with_unrelated_submodule {
test_done "$testroot" "$ret"
}
-function test_commit_symlink {
- local testroot=`test_init commit_symlink`
+function check_symlinks {
+ local wtpath="$1"
+ if ! [ -h $wtpath/alpha.link ]; then
+ echo "alpha.link is not a symlink"
+ return 1
+ fi
- got checkout $testroot/repo $testroot/wt > /dev/null
+ readlink $wtpath/alpha.link > $testroot/stdout
+ echo "alpha" > $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
ret="$?"
if [ "$ret" != "0" ]; then
- test_done "$testroot" "$ret"
+ diff -u $testroot/stdout.expected $testroot/stdout
return 1
fi
- (cd $testroot/wt && ln -s alpha alpha.link)
- (cd $testroot/wt && ln -s epsilon epsilon.link)
- (cd $testroot/wt && ln -s /etc/passwd passwd.link)
- (cd $testroot/wt && ln -s ../beta epsilon/beta.link)
- (cd $testroot/wt && ln -s nonexistent nonexistent.link)
- (cd $testroot/wt && got add alpha.link epsilon.link passwd.link \
- epsilon/beta.link nonexistent.link > /dev/null)
-
- (cd $testroot/wt && got commit -m 'test commit_symlink' > $testroot/stdout)
-
- local head_rev=`git_show_head $testroot/repo`
- echo "A alpha.link" > $testroot/stdout.expected
- echo "A epsilon.link" >> $testroot/stdout.expected
- echo "A nonexistent.link" >> $testroot/stdout.expected
- echo "A passwd.link" >> $testroot/stdout.expected
- echo "A epsilon/beta.link" >> $testroot/stdout.expected
- echo "Created commit $head_rev" >> $testroot/stdout.expected
+ if ! [ -h $wtpath/epsilon.link ]; then
+ echo "epsilon.link is not a symlink"
+ return 1
+ fi
+ readlink $wtpath/epsilon.link > $testroot/stdout
+ echo "epsilon" > $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
- got checkout $testroot/repo $testroot/wt2 > /dev/null
+ if [ -h $wtpath/passwd.link ]; then
+ echo -n "passwd.link is a symlink and points outside of work tree: " >&2
+ readlink $wtpath/passwd.link >&2
+ return 1
+ fi
+
+ echo -n "/etc/passwd" > $testroot/content.expected
+ cp $wtpath/passwd.link $testroot/content
ret="$?"
if [ "$ret" != "0" ]; then
- test_done "$testroot" "$ret"
+ echo "cp command failed unexpectedly" >&2
return 1
fi
- if ! [ -h $testroot/wt2/alpha.link ]; then
- echo "alpha.link is not a symlink"
- test_done "$testroot" "1"
+ cmp -s $testroot/content.expected $testroot/content
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/content.expected $testroot/content
return 1
fi
- readlink $testroot/wt2/alpha.link > $testroot/stdout
- echo "alpha" > $testroot/stdout.expected
+ readlink $wtpath/epsilon/beta.link > $testroot/stdout
+ echo "../beta" > $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
-
- if ! [ -h $testroot/wt2/epsilon.link ]; then
- echo "epsilon.link is not a symlink"
- test_done "$testroot" "1"
return 1
fi
- readlink $testroot/wt2/epsilon.link > $testroot/stdout
- echo "epsilon" > $testroot/stdout.expected
+ readlink $wtpath/nonexistent.link > $testroot/stdout
+ echo "nonexistent" > $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
- if [ -h $testroot/wt2/passwd.link ]; then
- echo -n "passwd.link symlink points outside of work tree: " >&2
- readlink $testroot/wt2/passwd.link >&2
- test_done "$testroot" "1"
- return 1
- fi
+ return 0
+}
- echo -n "/etc/passwd" > $testroot/content.expected
- cp $testroot/wt2/passwd.link $testroot/content
+function test_commit_symlink {
+ local testroot=`test_init commit_symlink`
- cmp -s $testroot/content.expected $testroot/content
+ got checkout $testroot/repo $testroot/wt > /dev/null
ret="$?"
if [ "$ret" != "0" ]; then
- diff -u $testroot/content.expected $testroot/content
test_done "$testroot" "$ret"
return 1
fi
- readlink $testroot/wt2/epsilon/beta.link > $testroot/stdout
- echo "../beta" > $testroot/stdout.expected
+ (cd $testroot/wt && ln -s alpha alpha.link)
+ (cd $testroot/wt && ln -s epsilon epsilon.link)
+ (cd $testroot/wt && ln -s /etc/passwd passwd.link)
+ (cd $testroot/wt && ln -s ../beta epsilon/beta.link)
+ (cd $testroot/wt && ln -s nonexistent nonexistent.link)
+ (cd $testroot/wt && got add alpha.link epsilon.link passwd.link \
+ epsilon/beta.link nonexistent.link > /dev/null)
+
+ (cd $testroot/wt && got commit -m 'test commit_symlink' > $testroot/stdout)
+ #(cd $testroot/wt && egdb --args got commit -m 'test commit_symlink')
+
+ local head_rev=`git_show_head $testroot/repo`
+ echo "A alpha.link" > $testroot/stdout.expected
+ echo "A epsilon.link" >> $testroot/stdout.expected
+ echo "A nonexistent.link" >> $testroot/stdout.expected
+ echo "A passwd.link" >> $testroot/stdout.expected
+ echo "A epsilon/beta.link" >> $testroot/stdout.expected
+ echo "Created commit $head_rev" >> $testroot/stdout.expected
+
cmp -s $testroot/stdout.expected $testroot/stdout
ret="$?"
if [ "$ret" != "0" ]; then
@@ -1005,14 +1011,28 @@ function test_commit_symlink {
return 1
fi
- readlink $testroot/wt2/nonexistent.link > $testroot/stdout
- echo "nonexistent" > $testroot/stdout.expected
- cmp -s $testroot/stdout.expected $testroot/stdout
+ # verify created in-repository tree
+ got checkout $testroot/repo $testroot/wt2 > /dev/null
ret="$?"
if [ "$ret" != "0" ]; then
- diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
fi
- test_done "$testroot" "$ret"
+ check_symlinks $testroot/wt2
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ # verify post-commit work tree state matches a fresh checkout
+ check_symlinks $testroot/wt
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+ test_done "$testroot" "0"
}
run_test test_commit_basic