fix 'got status' recursion problems
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
diff --git a/lib/fileindex.c b/lib/fileindex.c
index 8ff4674..0e62e71 100644
--- a/lib/fileindex.c
+++ b/lib/fileindex.c
@@ -759,8 +759,66 @@ got_fileindex_diff_tree(struct got_fileindex *fileindex,
static const struct got_error *
diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **, DIR *,
- const char *, const char *, struct got_repository *,
- struct got_fileindex_diff_dir_cb *, void *);
+ struct got_pathlist_head *, const char *, const char *,
+ struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
+
+static const struct got_error *
+read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
+{
+ const struct got_error *err = NULL;
+ struct got_pathlist_entry *new = NULL;
+ struct dirent *dep = NULL;
+ struct dirent *de = NULL;
+
+ for (;;) {
+ de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
+ if (de == NULL) {
+ err = got_error_from_errno("malloc");
+ break;
+ }
+
+ if (readdir_r(dir, de, &dep) != 0) {
+ err = got_error_from_errno("readdir_r");
+ free(de);
+ break;
+ }
+ if (dep == NULL) {
+ free(de);
+ break;
+ }
+
+ if (strcmp(de->d_name, ".") == 0 ||
+ strcmp(de->d_name, "..") == 0 ||
+ (path[0] == '\0' &&
+ strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
+ free(de);
+ continue;
+ }
+
+ err = got_pathlist_insert(&new, dirlist, de->d_name, de);
+ if (err) {
+ free(de);
+ break;
+ }
+ if (new == NULL) {
+ err = got_error(GOT_ERR_DIR_DUP_ENTRY);
+ free(de);
+ break;
+ }
+ }
+
+ return err;
+}
+
+void
+free_dirlist(struct got_pathlist_head *dirlist)
+{
+ struct got_pathlist_entry *dle;
+
+ TAILQ_FOREACH(dle, dirlist, entry)
+ free(dle->data);
+ got_pathlist_free(dirlist);
+}
static const struct got_error *
walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
@@ -778,6 +836,9 @@ walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
char *subpath;
char *subdirpath;
DIR *subdir;
+ struct got_pathlist_head subdirlist;
+
+ TAILQ_INIT(&subdirlist);
if (asprintf(&subpath, "%s%s%s", path,
path[0] == '\0' ? "" : "/", de->d_name) == -1)
@@ -795,11 +856,19 @@ walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
return got_error_from_errno2("opendir", subdirpath);
}
- err = diff_fileindex_dir(fileindex, ie, subdir, rootpath,
- subpath, repo, cb, cb_arg);
+ err = read_dirlist(&subdirlist, subdir, subdirpath);
+ if (err) {
+ free(subpath);
+ free(subdirpath);
+ closedir(subdir);
+ return err;
+ }
+ err = diff_fileindex_dir(fileindex, ie, subdir, &subdirlist,
+ rootpath, subpath, repo, cb, cb_arg);
free(subpath);
free(subdirpath);
closedir(subdir);
+ free_dirlist(&subdirlist);
if (err)
return err;
}
@@ -810,60 +879,17 @@ walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
static const struct got_error *
diff_fileindex_dir(struct got_fileindex *fileindex,
- struct got_fileindex_entry **ie, DIR *dir, const char *rootpath,
- const char *path, struct got_repository *repo,
- struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
+ struct got_fileindex_entry **ie, DIR *dir,
+ struct got_pathlist_head *dirlist, const char *rootpath, const char *path,
+ struct got_repository *repo, struct got_fileindex_diff_dir_cb *cb,
+ void *cb_arg)
{
const struct got_error *err = NULL;
struct dirent *de = NULL;
size_t path_len = strlen(path);
- struct got_fileindex_entry *next;
- struct got_pathlist_head dirlist;
struct got_pathlist_entry *dle;
- TAILQ_INIT(&dirlist);
-
- for (;;) {
- struct got_pathlist_entry *new = NULL;
- struct dirent *dep = NULL;
-
- de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
- if (de == NULL) {
- err = got_error_from_errno("malloc");
- goto done;
- }
-
- if (readdir_r(dir, de, &dep) != 0) {
- err = got_error_from_errno("readdir_r");
- free(de);
- goto done;
- }
- if (dep == NULL) {
- free(de);
- break;
- }
-
- if (strcmp(de->d_name, ".") == 0 ||
- strcmp(de->d_name, "..") == 0 ||
- (path[0] == '\0' &&
- strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
- free(de);
- continue;
- }
-
- err = got_pathlist_insert(&new, &dirlist, de->d_name, de);
- if (err) {
- free(de);
- goto done;
- }
- if (new == NULL) {
- err = got_error(GOT_ERR_DIR_DUP_ENTRY);
- free(de);
- goto done;
- }
- }
-
- dle = TAILQ_FIRST(&dirlist);
+ dle = TAILQ_FIRST(dirlist);
while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
if (dle && *ie) {
char *de_path;
@@ -884,11 +910,10 @@ diff_fileindex_dir(struct got_fileindex *fileindex,
err = walk_dir(&dle, fileindex, ie, dle, path,
dir, rootpath, repo, cb, cb_arg);
} else if (cmp < 0 ) {
- next = walk_fileindex(fileindex, *ie);
err = cb->diff_old(cb_arg, *ie, path);
if (err)
break;
- *ie = next;
+ *ie = walk_fileindex(fileindex, *ie);
} else {
err = cb->diff_new(cb_arg, de, path);
if (err)
@@ -899,11 +924,10 @@ diff_fileindex_dir(struct got_fileindex *fileindex,
if (err)
break;
} else if (*ie) {
- next = walk_fileindex(fileindex, *ie);
err = cb->diff_old(cb_arg, *ie, path);
if (err)
break;
- *ie = next;
+ *ie = walk_fileindex(fileindex, *ie);
} else if (dle) {
de = dle->data;
err = cb->diff_new(cb_arg, de, path);
@@ -915,10 +939,7 @@ diff_fileindex_dir(struct got_fileindex *fileindex,
break;
}
}
-done:
- TAILQ_FOREACH(dle, &dirlist, entry)
- free(dle->data);
- got_pathlist_free(&dirlist);
+
return err;
}
@@ -927,10 +948,21 @@ got_fileindex_diff_dir(struct got_fileindex *fileindex, DIR *rootdir,
const char *rootpath, const char *path, struct got_repository *repo,
struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
{
- struct got_fileindex_entry *min;
- min = RB_MIN(got_fileindex_tree, &fileindex->entries);
- return diff_fileindex_dir(fileindex, &min, rootdir, rootpath, path,
- repo, cb, cb_arg);
+ const struct got_error *err;
+ struct got_fileindex_entry *ie;
+ struct got_pathlist_head dirlist;
+
+ TAILQ_INIT(&dirlist);
+ err = read_dirlist(&dirlist, rootdir, path);
+ if (err)
+ return err;
+ ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
+ while (ie && !got_path_is_child(ie->path, path, strlen(path)))
+ ie = walk_fileindex(fileindex, ie);
+ err = diff_fileindex_dir(fileindex, &ie, rootdir, &dirlist, rootpath,
+ path, repo, cb, cb_arg);
+ free_dirlist(&dirlist);
+ return err;
}
RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);
diff --git a/lib/worktree.c b/lib/worktree.c
index 80edb4d..686a315 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -2198,7 +2198,7 @@ status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
return got_error(GOT_ERR_CANCELLED);
- if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
+ if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
return NULL;
memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
@@ -2228,9 +2228,6 @@ status_new(void *arg, struct dirent *de, const char *parent_path)
if (de->d_type == DT_LNK)
return NULL;
- if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
- return NULL;
-
if (parent_path[0]) {
if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
return got_error_from_errno("asprintf");
@@ -2238,8 +2235,9 @@ status_new(void *arg, struct dirent *de, const char *parent_path)
path = de->d_name;
}
- err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
- NULL, NULL);
+ if (got_path_is_child(path, a->status_path, a->status_path_len))
+ err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
+ path, NULL, NULL);
if (parent_path[0])
free(path);
return err;
diff --git a/regress/cmdline/status.sh b/regress/cmdline/status.sh
index fa78fd8..caf49ba 100755
--- a/regress/cmdline/status.sh
+++ b/regress/cmdline/status.sh
@@ -392,6 +392,56 @@ function test_status_shows_conflict {
test_done "$testroot" "$ret"
}
+function test_status_empty_dir {
+ local testroot=`test_init status_empty_dir`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ rm $testroot/wt/epsilon/zeta
+
+ echo '! epsilon/zeta' > $testroot/stdout.expected
+
+ (cd $testroot/wt && got status epsilon > $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"
+}
+
+function test_status_empty_dir_unversioned_file {
+ local testroot=`test_init status_empty_dir_unversioned_file`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ rm $testroot/wt/epsilon/zeta
+ touch $testroot/wt/epsilon/unversioned
+
+ echo '? epsilon/unversioned' > $testroot/stdout.expected
+ echo '! epsilon/zeta' >> $testroot/stdout.expected
+
+ (cd $testroot/wt && got status epsilon > $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_status_basic
run_test test_status_subdir_no_mods
run_test test_status_subdir_no_mods2
@@ -401,3 +451,5 @@ run_test test_status_unversioned_subdirs
run_test test_status_ignores_symlink
run_test test_status_shows_no_mods_after_complete_merge
run_test test_status_shows_conflict
+run_test test_status_empty_dir
+run_test test_status_empty_dir_unversioned_file