@@ -186,6 +186,44 @@ void git_repository_free(git_repository *repo)
186186 git__free (repo );
187187}
188188
189+ /* Check if we have a separate commondir (e.g. we have a worktree) */
190+ static int lookup_commondir (git_buf * out , git_buf * repository_path )
191+ {
192+ git_buf common_link = GIT_BUF_INIT ;
193+ int error ;
194+
195+ /*
196+ * If there's no commondir file, the repository path is the
197+ * common path, but it needs a trailing slash.
198+ */
199+ if (!git_path_contains_file (repository_path , GIT_COMMONDIR_FILE )) {
200+ if ((error = git_buf_set (out , repository_path -> ptr , repository_path -> size )) == 0 )
201+ error = git_path_to_dir (out );
202+
203+ goto done ;
204+ }
205+
206+ if ((error = git_buf_joinpath (& common_link , repository_path -> ptr , GIT_COMMONDIR_FILE )) < 0 ||
207+ (error = git_futils_readbuffer (& common_link , common_link .ptr )) < 0 )
208+ goto done ;
209+
210+ git_buf_rtrim (& common_link );
211+ if (git_path_is_relative (common_link .ptr )) {
212+ if ((error = git_buf_joinpath (out , repository_path -> ptr , common_link .ptr )) < 0 )
213+ goto done ;
214+ } else {
215+ git_buf_swap (out , & common_link );
216+ }
217+
218+ git_buf_dispose (& common_link );
219+
220+ /* Make sure the commondir path always has a trailing slash */
221+ error = git_path_prettify_dir (out , out -> ptr , NULL );
222+
223+ done :
224+ return error ;
225+ }
226+
189227/*
190228 * Git repository open methods
191229 *
@@ -197,38 +235,13 @@ static int is_valid_repository_path(bool *out, git_buf *repository_path, git_buf
197235
198236 * out = false;
199237
200- /* Check if we have a separate commondir (e.g. we have a
201- * worktree) */
202- if (git_path_contains_file (repository_path , GIT_COMMONDIR_FILE )) {
203- git_buf common_link = GIT_BUF_INIT ;
204-
205- if ((error = git_buf_joinpath (& common_link , repository_path -> ptr , GIT_COMMONDIR_FILE )) < 0 ||
206- (error = git_futils_readbuffer (& common_link , common_link .ptr )) < 0 )
207- return error ;
208-
209- git_buf_rtrim (& common_link );
210- if (git_path_is_relative (common_link .ptr )) {
211- if ((error = git_buf_joinpath (common_path , repository_path -> ptr , common_link .ptr )) < 0 )
212- return error ;
213- } else {
214- git_buf_swap (common_path , & common_link );
215- }
216-
217- git_buf_dispose (& common_link );
218- }
219- else {
220- if ((error = git_buf_set (common_path , repository_path -> ptr , repository_path -> size )) < 0 )
221- return error ;
222- }
223-
224- /* Make sure the commondir path always has a trailing * slash */
225- if (git_buf_rfind (common_path , '/' ) != (ssize_t )common_path -> size - 1 )
226- if ((error = git_buf_putc (common_path , '/' )) < 0 )
227- return error ;
238+ if ((error = lookup_commondir (common_path , repository_path )) < 0 )
239+ return error ;
228240
229241 /* Ensure HEAD file exists */
230242 if (git_path_contains_file (repository_path , GIT_HEAD_FILE ) == false)
231243 return 0 ;
244+
232245 /* Check files in common dir */
233246 if (git_path_contains_dir (common_path , GIT_OBJECTS_DIR ) == false)
234247 return 0 ;
0 commit comments