fix bug where update got confused by sub-sub directories
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
diff --git a/lib/fileindex.c b/lib/fileindex.c
index 70174aa..a1d12e6 100644
--- a/lib/fileindex.c
+++ b/lib/fileindex.c
@@ -577,8 +577,12 @@ cmp_entries(struct got_fileindex_entry *ie, const char *parent_path,
if (!in_same_subdir(ie, parent_path, te)) {
cmp = strncmp(ie->path, parent_path, parent_len);
- if (cmp == 0)
- cmp = strcmp(ie->path + parent_len, te->name);
+ if (cmp == 0) {
+ char *ie_name = ie->path + parent_len;
+ while (ie_name[0] == '/')
+ ie_name++;
+ cmp = strcmp(ie_name, te->name);
+ }
} else {
char *ie_name = ie->path + parent_len;
while (ie_name[0] == '/')
diff --git a/lib/worktree.c b/lib/worktree.c
index 2438bbf..62fe285 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -507,6 +507,27 @@ lock_worktree(struct got_worktree *worktree, int operation)
}
static const struct got_error *
+make_parent_dirs(const char *abspath)
+{
+ const struct got_error *err = NULL;
+
+ char *parent = dirname(abspath);
+ if (parent == NULL)
+ return NULL;
+
+ if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
+ if (errno == ENOENT) {
+ err = make_parent_dirs(parent);
+ if (err)
+ return err;
+ } else
+ return got_error_from_errno();
+ }
+
+ return NULL;
+}
+
+static const struct got_error *
add_dir_on_disk(struct got_worktree *worktree, const char *path)
{
const struct got_error *err = NULL;
@@ -519,20 +540,30 @@ add_dir_on_disk(struct got_worktree *worktree, const char *path)
if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
struct stat sb;
- if (errno != EEXIST) {
- err = got_error_from_errno();
- goto done;
+ if (errno == EEXIST) {
+ 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);
+ }
+
+ return NULL;
}
- if (lstat(abspath, &sb) == -1) {
+ if (errno == ENOENT) {
+ err = make_parent_dirs(abspath);
+ if (err)
+ return err;
+ if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == 0)
+ return NULL;
+ } else {
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:
@@ -560,7 +591,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();
diff --git a/regress/cmdline/update.sh b/regress/cmdline/update.sh
index b121574..61f3c4d 100755
--- a/regress/cmdline/update.sh
+++ b/regress/cmdline/update.sh
@@ -379,9 +379,9 @@ function test_update_moves_files_upwards {
(cd $testroot/repo && git mv epsilon/psi/chi/tau epsilon/psi/tau)
git_commit $testroot/repo -m "moving files upwards"
- echo "D epsilon/psi/chi/tau" > $testroot/stdout.expected
+ echo "A epsilon/mu" > $testroot/stdout.expected
+ echo "D epsilon/psi/chi/tau" >> $testroot/stdout.expected
echo "D epsilon/psi/mu" >> $testroot/stdout.expected
- echo "A epsilon/mu" >> $testroot/stdout.expected
echo "A epsilon/psi/tau" >> $testroot/stdout.expected
echo -n "Updated to commit " >> $testroot/stdout.expected
git_show_head $testroot/repo >> $testroot/stdout.expected
@@ -570,6 +570,44 @@ function test_update_creates_missing_parent_with_subdir {
test_done "$testroot" "0"
}
+function test_update_file_in_subsubdir {
+ local testroot=`test_init update_fle_in_subsubdir no_tree`
+
+ touch $testroot/repo/Makefile
+ mkdir -p $testroot/repo/altq
+ touch $testroot/repo/altq/if_altq.h
+ mkdir -p $testroot/repo/arch/alpha
+ touch $testroot/repo/arch/alpha/Makefile
+ (cd $testroot/repo && git add .)
+ git_commit $testroot/repo -m "adding initial tree"
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ if [ "$?" != "0" ]; then
+ test_done "$testroot" "$?"
+ return 1
+ fi
+
+ echo change > $testroot/repo/arch/alpha/Makefile
+ (cd $testroot/repo && git add .)
+ git_commit $testroot/repo -m "changed a file"
+
+ echo "U arch/alpha/Makefile" > $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
@@ -582,3 +620,4 @@ run_test test_update_moves_files_upwards
run_test test_update_moves_files_to_new_dir
run_test test_update_creates_missing_parent
run_test test_update_creates_missing_parent_with_subdir
+run_test test_update_file_in_subsubdir