Skip to content

Commit 0f31609

Browse files
committed
repository: do not interpret all files as gitlinks in discovery
When trying to find a discovery, we walk up the directory structure checking if there is a ".git" file or directory and, if so, check its validity. But in the case that we've got a ".git" file, we do not want to unconditionally assume that the file is in fact a ".git" file and treat it as such, as we would error out if it is not. Fix the issue by only treating a file as a gitlink file if it ends with "/.git". This allows users of the function to discover a repository by handing in any path contained inside of a git repository.
1 parent 4dbaf3c commit 0f31609

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/repository.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ static int find_repo(
410410
break;
411411
}
412412
}
413-
else if (S_ISREG(st.st_mode)) {
413+
else if (S_ISREG(st.st_mode) && git__suffixcmp(path.ptr, "/" DOT_GIT) == 0) {
414414
error = read_gitfile(&repo_link, path.ptr);
415415
if (error < 0)
416416
break;

tests/repo/discover.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,13 @@ void test_repo_discover__ceiling_should_not_affect_gitdir_redirection(void)
189189
ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs.ptr, SUB_REPOSITORY_GITDIR);
190190
ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs.ptr, DISCOVER_FOLDER);
191191
}
192+
193+
void test_repo_discover__discovery_starting_at_file_succeeds(void)
194+
{
195+
int fd;
196+
197+
cl_assert((fd = p_creat(SUB_REPOSITORY_FOLDER "/file", 0600)) >= 0);
198+
cl_assert(p_close(fd) == 0);
199+
200+
ensure_repository_discover(SUB_REPOSITORY_FOLDER "/file", ceiling_dirs.ptr, SUB_REPOSITORY_GITDIR);
201+
}

0 commit comments

Comments
 (0)