fix bug where update forgot to create a parent directory
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
diff --git a/lib/worktree.c b/lib/worktree.c
index 45ace73..f51b680 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -507,6 +507,40 @@ lock_worktree(struct got_worktree *worktree, int operation)
}
static const struct got_error *
+add_dir_on_disk(struct got_worktree *worktree, const char *path)
+{
+ const struct got_error *err = NULL;
+ char *abspath;
+
+ if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
+ return got_error_from_errno();
+
+ /* XXX queue work rather than editing disk directly? */
+ if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
+ struct stat sb;
+
+ if (errno != EEXIST) {
+ err = got_error_from_errno();
+ goto done;
+ }
+
+ if (lstat(abspath, &sb) == -1) {
+ err = got_error_from_errno();
+ goto done;
+ }
+
+ if (!S_ISDIR(sb.st_mode)) {
+ /* TODO directory is obstructed; do something */
+ return got_error(GOT_ERR_FILE_OBSTRUCTED);
+ }
+ }
+
+done:
+ free(abspath);
+ return err;
+}
+
+static const struct got_error *
install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
struct got_fileindex_entry *entry, const char *path,
struct got_blob_object *blob,
@@ -526,7 +560,19 @@ install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
GOT_DEFAULT_FILE_MODE);
if (fd == -1) {
- if (errno == EEXIST) {
+ if (errno == ENOENT) {
+ char *parent = dirname(path);
+ if (parent == NULL)
+ return got_error_from_errno();
+ err = add_dir_on_disk(worktree, parent);
+ if (err)
+ return err;
+ fd = open(ondisk_path,
+ O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
+ GOT_DEFAULT_FILE_MODE);
+ if (fd == -1)
+ return got_error_from_errno();
+ } else if (errno == EEXIST) {
struct stat sb;
if (lstat(ondisk_path, &sb) == -1) {
err = got_error_from_errno();
@@ -599,40 +645,6 @@ done:
}
static const struct got_error *
-add_dir_on_disk(struct got_worktree *worktree, const char *path)
-{
- const struct got_error *err = NULL;
- char *abspath;
-
- if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
- return got_error_from_errno();
-
- /* XXX queue work rather than editing disk directly? */
- if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
- struct stat sb;
-
- if (errno != EEXIST) {
- err = got_error_from_errno();
- goto done;
- }
-
- if (lstat(abspath, &sb) == -1) {
- err = got_error_from_errno();
- goto done;
- }
-
- if (!S_ISDIR(sb.st_mode)) {
- /* TODO directory is obstructed; do something */
- return got_error(GOT_ERR_FILE_OBSTRUCTED);
- }
- }
-
-done:
- free(abspath);
- return err;
-}
-
-static const struct got_error *
update_blob(struct got_worktree *worktree,
struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
struct got_tree_entry *te, const char *path,
diff --git a/regress/cmdline/common.sh b/regress/cmdline/common.sh
index a8a3ea5..a7e8448 100644
--- a/regress/cmdline/common.sh
+++ b/regress/cmdline/common.sh
@@ -55,6 +55,7 @@ function make_test_tree
function test_init
{
local testname="$1"
+ local no_tree="$2"
if [ -z "$testname" ]; then
echo "No test name provided" >&2
return 1
@@ -62,8 +63,10 @@ function test_init
local testroot=`mktemp -p /tmp -d got-test-$testname-XXXXXXXX`
mkdir $testroot/repo
git_init $testroot/repo
- make_test_tree $testroot/repo
- git_commit $testroot/repo -m "adding the test tree"
+ if [ -z "$no_tree" ]; then
+ make_test_tree $testroot/repo
+ git_commit $testroot/repo -m "adding the test tree"
+ fi
echo "$testroot"
}
diff --git a/regress/cmdline/update.sh b/regress/cmdline/update.sh
index 2967505..b7d9d15 100755
--- a/regress/cmdline/update.sh
+++ b/regress/cmdline/update.sh
@@ -464,6 +464,59 @@ function test_update_moves_files_to_new_dir {
test_done "$testroot" "0"
}
+function test_update_creates_missing_parent {
+ local testroot=`test_init update_creates_missing_parent no_tree`
+
+ touch $testroot/repo/Makefile
+ touch $testroot/repo/snake.6
+ touch $testroot/repo/snake.c
+ (cd $testroot/repo && git add .)
+ git_commit $testroot/repo -m "adding initial snake tree"
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ if [ "$?" != "0" ]; then
+ test_done "$testroot" "$?"
+ return 1
+ fi
+
+ mkdir -p $testroot/repo/snake
+ (cd $testroot/repo && git mv Makefile snake.6 snake.c snake)
+ touch $testroot/repo/snake/move.c
+ touch $testroot/repo/snake/pathnames.h
+ touch $testroot/repo/snake/snake.h
+ mkdir -p $testroot/repo/snscore
+ touch $testroot/repo/snscore/Makefile
+ touch $testroot/repo/snscore/snscore.c
+ (cd $testroot/repo && git add .)
+ git_commit $testroot/repo -m "restructuring snake tree"
+
+ echo "D Makefile" > $testroot/stdout.expected
+ echo "A snake/Makefile" >> $testroot/stdout.expected
+ echo "A snake/move.c" >> $testroot/stdout.expected
+ echo "A snake/pathnames.h" >> $testroot/stdout.expected
+ echo "A snake/snake.6" >> $testroot/stdout.expected
+ echo "A snake/snake.c" >> $testroot/stdout.expected
+ echo "A snake/snake.h" >> $testroot/stdout.expected
+ echo "D snake.6" >> $testroot/stdout.expected
+ echo "D snake.c" >> $testroot/stdout.expected
+ echo "A snscore/Makefile" >> $testroot/stdout.expected
+ echo "A snscore/snscore.c" >> $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
+
+ test_done "$testroot" "0"
+}
+
run_test test_update_basic
run_test test_update_adds_file
run_test test_update_deletes_file
@@ -474,3 +527,4 @@ run_test test_update_sibling_dirs_with_common_prefix
run_test test_update_dir_with_dot_sibling
run_test test_update_moves_files_upwards
#run_test test_update_moves_files_to_new_dir # test is failing
+run_test test_update_creates_missing_parent