@@ -42,23 +42,24 @@ bool git_repository__fsync_gitdir = false;
4242
4343static const struct {
4444 git_repository_item_t parent ;
45+ git_repository_item_t fallback ;
4546 const char * name ;
4647 bool directory ;
4748} items [] = {
48- { GIT_REPOSITORY_ITEM_GITDIR , NULL , true },
49- { GIT_REPOSITORY_ITEM_WORKDIR , NULL , true },
50- { GIT_REPOSITORY_ITEM_COMMONDIR , NULL , true },
51- { GIT_REPOSITORY_ITEM_GITDIR , "index" , false },
52- { GIT_REPOSITORY_ITEM_COMMONDIR , "objects" , true },
53- { GIT_REPOSITORY_ITEM_COMMONDIR , "refs" , true },
54- { GIT_REPOSITORY_ITEM_COMMONDIR , "packed-refs" , false },
55- { GIT_REPOSITORY_ITEM_COMMONDIR , "remotes" , true },
56- { GIT_REPOSITORY_ITEM_COMMONDIR , "config" , false },
57- { GIT_REPOSITORY_ITEM_COMMONDIR , "info" , true },
58- { GIT_REPOSITORY_ITEM_COMMONDIR , "hooks" , true },
59- { GIT_REPOSITORY_ITEM_COMMONDIR , "logs" , true },
60- { GIT_REPOSITORY_ITEM_GITDIR , "modules" , true },
61- { GIT_REPOSITORY_ITEM_COMMONDIR , "worktrees" , true }
49+ { GIT_REPOSITORY_ITEM_GITDIR , GIT_REPOSITORY_ITEM__LAST , NULL , true },
50+ { GIT_REPOSITORY_ITEM_WORKDIR , GIT_REPOSITORY_ITEM__LAST , NULL , true },
51+ { GIT_REPOSITORY_ITEM_COMMONDIR , GIT_REPOSITORY_ITEM__LAST , NULL , true },
52+ { GIT_REPOSITORY_ITEM_GITDIR , GIT_REPOSITORY_ITEM__LAST , "index" , false },
53+ { GIT_REPOSITORY_ITEM_COMMONDIR , GIT_REPOSITORY_ITEM_GITDIR , "objects" , true },
54+ { GIT_REPOSITORY_ITEM_COMMONDIR , GIT_REPOSITORY_ITEM_GITDIR , "refs" , true },
55+ { GIT_REPOSITORY_ITEM_COMMONDIR , GIT_REPOSITORY_ITEM_GITDIR , "packed-refs" , false },
56+ { GIT_REPOSITORY_ITEM_COMMONDIR , GIT_REPOSITORY_ITEM_GITDIR , "remotes" , true },
57+ { GIT_REPOSITORY_ITEM_COMMONDIR , GIT_REPOSITORY_ITEM_GITDIR , "config" , false },
58+ { GIT_REPOSITORY_ITEM_COMMONDIR , GIT_REPOSITORY_ITEM_GITDIR , "info" , true },
59+ { GIT_REPOSITORY_ITEM_COMMONDIR , GIT_REPOSITORY_ITEM_GITDIR , "hooks" , true },
60+ { GIT_REPOSITORY_ITEM_COMMONDIR , GIT_REPOSITORY_ITEM_GITDIR , "logs" , true },
61+ { GIT_REPOSITORY_ITEM_GITDIR , GIT_REPOSITORY_ITEM__LAST , "modules" , true },
62+ { GIT_REPOSITORY_ITEM_COMMONDIR , GIT_REPOSITORY_ITEM_GITDIR , "worktrees" , true }
6263};
6364
6465static int check_repositoryformatversion (git_config * config );
@@ -2308,11 +2309,11 @@ int git_repository_is_empty(git_repository *repo)
23082309 return is_empty ;
23092310}
23102311
2311- int git_repository_item_path ( git_buf * out , const git_repository * repo , git_repository_item_t item )
2312+ static const char * resolved_parent_path ( const git_repository * repo , git_repository_item_t item , git_repository_item_t fallback )
23122313{
23132314 const char * parent ;
23142315
2315- switch (items [ item ]. parent ) {
2316+ switch (item ) {
23162317 case GIT_REPOSITORY_ITEM_GITDIR :
23172318 parent = git_repository_path (repo );
23182319 break ;
@@ -2324,9 +2325,17 @@ int git_repository_item_path(git_buf *out, const git_repository *repo, git_repos
23242325 break ;
23252326 default :
23262327 git_error_set (GIT_ERROR_INVALID , "invalid item directory" );
2327- return -1 ;
2328+ return NULL ;
23282329 }
2330+ if (!parent && fallback != GIT_REPOSITORY_ITEM__LAST )
2331+ return resolved_parent_path (repo , fallback , GIT_REPOSITORY_ITEM__LAST );
2332+
2333+ return parent ;
2334+ }
23292335
2336+ int git_repository_item_path (git_buf * out , const git_repository * repo , git_repository_item_t item )
2337+ {
2338+ const char * parent = resolved_parent_path (repo , items [item ].parent , items [item ].fallback );
23302339 if (parent == NULL ) {
23312340 git_error_set (GIT_ERROR_INVALID , "path cannot exist in repository" );
23322341 return GIT_ENOTFOUND ;
0 commit comments