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