@@ -456,6 +456,18 @@ impl Repository {
456456 }
457457 }
458458
459+ /// Returns the path of the shared common directory for this repository.
460+ ///
461+ /// If the repository is bare, it is the root directory for the repository.
462+ /// If the repository is a worktree, it is the parent repo's gitdir.
463+ /// Otherwise, it is the gitdir.
464+ pub fn commondir ( & self ) -> & Path {
465+ unsafe {
466+ let ptr = raw:: git_repository_commondir ( self . raw ) ;
467+ util:: bytes2path ( crate :: opt_bytes ( self , ptr) . unwrap ( ) )
468+ }
469+ }
470+
459471 /// Returns the current state of this repository
460472 pub fn state ( & self ) -> RepositoryState {
461473 let state = unsafe { raw:: git_repository_state ( self . raw ) } ;
@@ -4304,4 +4316,22 @@ Committer Name <committer.proper@email> <committer@email>"#,
43044316 . unwrap ( ) ;
43054317 assert_eq ! ( tag. id( ) , found_tag. id( ) ) ;
43064318 }
4319+
4320+ #[ test]
4321+ fn smoke_commondir ( ) {
4322+ let ( td, repo) = crate :: test:: repo_init ( ) ;
4323+ assert_eq ! (
4324+ crate :: test:: realpath( repo. path( ) ) . unwrap( ) ,
4325+ crate :: test:: realpath( repo. commondir( ) ) . unwrap( )
4326+ ) ;
4327+
4328+ let worktree = repo
4329+ . worktree ( "test" , & td. path ( ) . join ( "worktree" ) , None )
4330+ . unwrap ( ) ;
4331+ let worktree_repo = Repository :: open_from_worktree ( & worktree) . unwrap ( ) ;
4332+ assert_eq ! (
4333+ crate :: test:: realpath( repo. path( ) ) . unwrap( ) ,
4334+ crate :: test:: realpath( worktree_repo. commondir( ) ) . unwrap( )
4335+ ) ;
4336+ }
43074337}
0 commit comments