commit a9606f2c0c0ed83d0ceef604a996d4ee56cd561fcda0717079850d372d3cf07a from: Omar Polo date: Sat Jul 18 17:45:12 2026 UTC sanitize path-prefix; avoids failures when there's a trailing slash if there's a trailing slash in the path-prefix, we end up creating bad paths that break the logic in multiple places. Make sure to have it cleared at creation and parsing time. While here, change got_worktree_match_path_prefix() to use got_path_cmp() instead of strcmp(). ok/improvements stsp@ commit - d6b5af4a568fa12a795821b40b8c3c80350cfd9d53754be1527bff020d350b4e commit + a9606f2c0c0ed83d0ceef604a996d4ee56cd561fcda0717079850d372d3cf07a blob - 480f5328fc1e9156e8e7bbeb7add0cf4b7b76fea8916810cfcf3591b98255cd5 blob + 7c4d5f5c17619d7fc886f0c70a4bcb408c0842b5e5c445efbdf556f940acccb9 --- lib/worktree.c +++ lib/worktree.c @@ -180,7 +180,13 @@ got_worktree_init(const char *path, struct got_referen if (!got_path_is_absolute(prefix)) { if (asprintf(&absprefix, "/%s", prefix) == -1) return got_error_from_errno("asprintf"); + } else { + absprefix = strdup(prefix); + if (absprefix == NULL) + return got_error_from_errno("strdup"); } + if (!got_path_is_root_dir(absprefix)) + got_path_strip_trailing_slashes(absprefix); /* Create top-level directory (may already exist). */ if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) { @@ -229,8 +235,7 @@ got_worktree_init(const char *path, struct got_referen goto done; /* Store in-repository path prefix. */ - err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX, - absprefix ? absprefix : prefix); + err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX, absprefix); if (err) goto done; @@ -273,13 +278,21 @@ got_worktree_match_path_prefix(int *match, struct got_ const char *path_prefix) { char *absprefix = NULL; + const char *a, *b; + size_t al, bl; if (!got_path_is_absolute(path_prefix)) { if (asprintf(&absprefix, "/%s", path_prefix) == -1) return got_error_from_errno("asprintf"); } - *match = (strcmp(absprefix ? absprefix : path_prefix, - worktree->path_prefix) == 0); + + a = absprefix ? absprefix : path_prefix; + al = strlen(a); + + b = worktree->path_prefix; + bl = strlen(b); + + *match = (got_path_cmp(a, b, al, bl) == 0); free(absprefix); return NULL; } blob - 04a0502f81345ad82fd5be245621e488e3c9609b9354926bafb7008bfe9cb7f2 blob + 56b3f6adb0e425c98b269eef35e87693e7d12335e02d83310f492baa70be95f0 --- lib/worktree_open.c +++ lib/worktree_open.c @@ -195,6 +195,8 @@ open_worktree(struct got_worktree **worktree, const ch GOT_WORKTREE_PATH_PREFIX); if (err) goto done; + if (!got_path_is_root_dir((*worktree)->path_prefix)) + got_path_strip_trailing_slashes((*worktree)->path_prefix); err = read_meta_file(&base_commit_id_str, path_meta, GOT_WORKTREE_BASE_COMMIT); blob - 14eb0f0e41ccd9a535f2e2a0c8c9bad1461be8f3cd11e36f7506c78fd96fad5d blob + f05b420cac9d5f5f83eae70b5bc3376b14e6a34a75e14ec596ee6712e0b7cf97 --- regress/cmdline/commit.sh +++ regress/cmdline/commit.sh @@ -107,6 +107,59 @@ test_commit_subdir() { test_done "$testroot" "$ret" } +test_commit_subdirs() { + local testroot=`test_init commit_subdir` + + # trailing slash for -p on purpose, we used to have a bug + # that was triggered by the presence of trailing slashes. + got checkout -p epsilon/ $testroot/repo $testroot/wt > /dev/null + ret=$? + if [ $ret -ne 0 ]; then + test_done "$testroot" "$ret" + return 1 + fi + + mkdir -p $testroot/wt/foo/bar + mkdir -p $testroot/wt/foo/baz + + echo omega > $testroot/wt/foo/bar/omega + echo kappa > $testroot/wt/foo/bar/kappa + + (cd $testroot/wt && got add -R foo >$testroot/stdout) + ret=$? + if [ $ret -ne 0 ]; then + echo "got add failed unexpectedly" >&2 + test_done "$testroot" "$ret" + return 1 + fi + + echo "A foo/bar/kappa" > $testroot/stdout.expected + echo "A foo/bar/omega" >>$testroot/stdout.expected + + cmp -s $testroot/stdout.expected $testroot/stdout + ret=$? + if [ $ret -ne 0 ]; then + diff -u $testroot/stdout.expected $testroot/stdout + test_done "$testroot" "$ret" + return 1 + fi + + (cd $testroot/wt && \ + got commit -m 'test commit_subdirs' > $testroot/stdout) + + local head_rev=`git_show_head $testroot/repo` + echo "A foo/bar/kappa" > $testroot/stdout.expected + echo "A foo/bar/omega" >>$testroot/stdout.expected + echo "Created commit $head_rev" >> $testroot/stdout.expected + + cmp -s $testroot/stdout.expected $testroot/stdout + ret=$? + if [ $ret -ne 0 ]; then + diff -u $testroot/stdout.expected $testroot/stdout + fi + test_done "$testroot" "$ret" +} + test_commit_single_file() { local testroot=`test_init commit_single_file` @@ -2117,6 +2170,7 @@ test_parseargs "$@" run_test test_commit_basic run_test test_commit_new_subdir run_test test_commit_subdir +run_test test_commit_subdirs run_test test_commit_single_file run_test test_commit_out_of_date run_test test_commit_added_subdirs