Skip to content

Commit f4c6914

Browse files
committed
repo: implement git_repository_commondir
1 parent 7285832 commit f4c6914

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

libgit2-sys/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2148,6 +2148,7 @@ extern "C" {
21482148
pub fn git_repository_is_empty(repo: *mut git_repository) -> c_int;
21492149
pub fn git_repository_is_shallow(repo: *mut git_repository) -> c_int;
21502150
pub fn git_repository_path(repo: *const git_repository) -> *const c_char;
2151+
pub fn git_repository_commondir(repo: *const git_repository) -> *const c_char;
21512152
pub fn git_repository_state(repo: *mut git_repository) -> c_int;
21522153
pub fn git_repository_workdir(repo: *const git_repository) -> *const c_char;
21532154
pub fn git_repository_set_workdir(

src/repo.rs

+28
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,16 @@ impl Repository {
456456
}
457457
}
458458

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+
459469
/// Returns the current state of this repository
460470
pub fn state(&self) -> RepositoryState {
461471
let state = unsafe { raw::git_repository_state(self.raw) };
@@ -4304,4 +4314,22 @@ Committer Name <committer.proper@email> <committer@email>"#,
43044314
.unwrap();
43054315
assert_eq!(tag.id(), found_tag.id());
43064316
}
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+
}
43074335
}

0 commit comments

Comments
 (0)