add symlink support to 'got update'
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
diff --git a/lib/worktree.c b/lib/worktree.c
index 03438c0..da56ac7 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -1020,27 +1020,6 @@ install_symlink(struct got_worktree *worktree, const char *ondisk_path,
}
if (symlink(target_path, ondisk_path) == -1) {
- if (errno == ENOENT) {
- char *parent = dirname(ondisk_path);
- if (parent == NULL) {
- err = got_error_from_errno2("dirname",
- ondisk_path);
- goto done;
- }
- err = add_dir_on_disk(worktree, parent);
- if (err)
- goto done;
- /*
- * Retry, and fall through to error handling
- * below if this second attempt fails.
- */
- if (symlink(target_path, ondisk_path) != -1) {
- err = NULL; /* success */
- goto done;
- }
- }
-
- /* Handle errors from first or second creation attempt. */
if (errno == EEXIST) {
struct stat sb;
ssize_t elen;
@@ -1062,12 +1041,49 @@ install_symlink(struct got_worktree *worktree, const char *ondisk_path,
goto done;
}
if (elen == target_len &&
- memcmp(etarget, target_path, target_len) == 0)
- err = NULL;
- else
- err = got_error_path(ondisk_path,
- GOT_ERR_FILE_OBSTRUCTED);
- } else if (errno == ENAMETOOLONG) {
+ memcmp(etarget, target_path, target_len) == 0) {
+ err = NULL; /* nothing to do */
+ goto done;
+ } else {
+ if (unlink(ondisk_path) == -1) {
+ err = got_error_from_errno2("unlink",
+ ondisk_path);
+ goto done;
+ }
+ if (symlink(target_path, ondisk_path) == -1) {
+ err = got_error_from_errno3("symlink",
+ target_path, ondisk_path);
+ goto done;
+ }
+
+ err = (*progress_cb)(progress_arg,
+ GOT_STATUS_UPDATE, path);
+ goto done;
+ }
+ }
+
+ if (errno == ENOENT) {
+ char *parent = dirname(ondisk_path);
+ if (parent == NULL) {
+ err = got_error_from_errno2("dirname",
+ ondisk_path);
+ goto done;
+ }
+ err = add_dir_on_disk(worktree, parent);
+ if (err)
+ goto done;
+ /*
+ * Retry, and fall through to error handling
+ * below if this second attempt fails.
+ */
+ if (symlink(target_path, ondisk_path) != -1) {
+ err = NULL; /* success */
+ goto done;
+ }
+ }
+
+ /* Handle errors from first or second creation attempt. */
+ if (errno == ENAMETOOLONG) {
/* bad target path; install as a regular file */
got_object_blob_rewind(blob);
err = install_blob(worktree, ondisk_path, path,
@@ -1081,7 +1097,8 @@ install_symlink(struct got_worktree *worktree, const char *ondisk_path,
err = got_error_from_errno3("symlink",
target_path, ondisk_path);
}
- }
+ } else
+ err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
done:
free(resolved_path);
free(abspath);
diff --git a/regress/cmdline/update.sh b/regress/cmdline/update.sh
index 60a1a2a..b61d2a0 100755
--- a/regress/cmdline/update.sh
+++ b/regress/cmdline/update.sh
@@ -1825,6 +1825,113 @@ function test_update_conflict_wt_file_vs_repo_submodule {
test_done "$testroot" "$ret"
}
+function test_update_adds_symlink {
+ local testroot=`test_init update_adds_symlink`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ echo "checkout failed unexpectedly" >&2
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ (cd $testroot/repo && ln -s alpha alpha.link)
+ (cd $testroot/repo && ln -s epsilon epsilon.link)
+ (cd $testroot/repo && ln -s /etc/passwd passwd.link)
+ (cd $testroot/repo && ln -s ../beta epsilon/beta.link)
+ (cd $testroot/repo && ln -s nonexistent nonexistent.link)
+ (cd $testroot/repo && git add .)
+ git_commit $testroot/repo -m "add symlinks"
+
+ echo "A alpha.link" > $testroot/stdout.expected
+ echo "A epsilon/beta.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 -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 -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/wt/alpha.link ]; then
+ echo "alpha.link is not a symlink"
+ test_done "$testroot" "1"
+ return 1
+ fi
+
+ readlink $testroot/wt/alpha.link > $testroot/stdout
+ echo "alpha" > $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/wt/epsilon.link ]; then
+ echo "epsilon.link is not a symlink"
+ test_done "$testroot" "1"
+ return 1
+ fi
+
+ readlink $testroot/wt/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
+
+ if [ -h $testroot/wt/passwd.link ]; then
+ echo -n "passwd.link symlink points outside of work tree: " >&2
+ readlink $testroot/wt/passwd.link >&2
+ test_done "$testroot" "1"
+ return 1
+ fi
+
+ echo -n "/etc/passwd" > $testroot/content.expected
+ cp $testroot/wt/passwd.link $testroot/content
+
+ cmp -s $testroot/content.expected $testroot/content
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/content.expected $testroot/content
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ readlink $testroot/wt/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
+
+ readlink $testroot/wt/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
+ fi
+ test_done "$testroot" "$ret"
+}
run_test test_update_basic
run_test test_update_adds_file
@@ -1861,3 +1968,4 @@ run_test test_update_preserves_conflicted_file
run_test test_update_modified_submodules
run_test test_update_adds_submodule
run_test test_update_conflict_wt_file_vs_repo_submodule
+run_test test_update_adds_symlink