Skip to content

feat(repository): support repository discovery from non-existent subpaths#7217

Open
yassine-safraoui wants to merge 1 commit intolibgit2:mainfrom
yassine-safraoui:feat-find-repo-non-existant-path
Open

feat(repository): support repository discovery from non-existent subpaths#7217
yassine-safraoui wants to merge 1 commit intolibgit2:mainfrom
yassine-safraoui:feat-find-repo-non-existant-path

Conversation

@yassine-safraoui
Copy link

This PR fixes repository discovery when the input path includes non-existent descendants inside an existing repository tree (issue #6383).

Problem

git_repository_discover currently fails early when start_path cannot be prettified (for example, /path/to/repo/missing1/missing2), even if a valid repository exists in an ancestor directory.

What changed

src/libgit2/repository.c

In find_repo_traverse:

  • Updated the git_fs_path_prettify(&path, start_path, NULL) handling:
    • If it fails with GIT_ENOTFOUND, discovery now continues using the raw start_path instead of returning immediately.
    • Non-GIT_ENOTFOUND errors are still returned.
  • Added a stop condition for relative traversal at "." (when no minimum iterations remain), to avoid walking past the effective root in that case.
  • Kept existing traversal/validation behavior for .git dir/file checks while allowing discovery from non-existent subpaths.
  • Added a final git_fs_path_prettify_dir(&out->gitdir, out->gitdir.ptr, NULL) after successful discovery.

Why the final prettify_dir call is needed

If the initial prettify fails because the input path is invalid/non-existent, we may still successfully discover a valid repo by walking ancestors. In that case, we should still normalize the discovered gitdir before returning.

That is why git_fs_path_prettify_dir is called at the end with the known-valid discovered gitdir path.

I considered tracking this with a boolean (set when initial prettify fails) and only calling prettify_dir conditionally, but the extra call is low-cost and keeping it unconditional keeps control flow simpler.

tests/libgit2/repo/discover.c

Added tests covering:

  • Success when discovering from a non-existent subpath inside an existing repository.
  • Failure when starting from a non-existent branch whose parent chain does not reach a repository.
  • Failure for a relative non-repo path with "." as ceiling.

Also added test fixture setup for a non-repository folder subtree used by the new cases.

Validation

  • Added/updated tests for the new behavior.
  • Ran the full test suite; all tests pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant