Skip to content

Commit

Permalink
Merge pull request #1079 from st3iny/repo/commondir
Browse files Browse the repository at this point in the history
repo: implement git_repository_commondir
  • Loading branch information
ehuss authored Aug 21, 2024
2 parents 7285832 + cb4ea6e commit 2e0fcda
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions libgit2-sys/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2148,6 +2148,7 @@ extern "C" {
pub fn git_repository_is_empty(repo: *mut git_repository) -> c_int;
pub fn git_repository_is_shallow(repo: *mut git_repository) -> c_int;
pub fn git_repository_path(repo: *const git_repository) -> *const c_char;
pub fn git_repository_commondir(repo: *const git_repository) -> *const c_char;
pub fn git_repository_state(repo: *mut git_repository) -> c_int;
pub fn git_repository_workdir(repo: *const git_repository) -> *const c_char;
pub fn git_repository_set_workdir(
Expand Down
30 changes: 30 additions & 0 deletions src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,18 @@ impl Repository {
}
}

/// Returns the path of the shared common directory for this repository.
///
/// If the repository is bare, it is the root directory for the repository.
/// If the repository is a worktree, it is the parent repo's gitdir.
/// Otherwise, it is the gitdir.
pub fn commondir(&self) -> &Path {
unsafe {
let ptr = raw::git_repository_commondir(self.raw);
util::bytes2path(crate::opt_bytes(self, ptr).unwrap())
}
}

/// Returns the current state of this repository
pub fn state(&self) -> RepositoryState {
let state = unsafe { raw::git_repository_state(self.raw) };
Expand Down Expand Up @@ -4304,4 +4316,22 @@ Committer Name <committer.proper@email> <committer@email>"#,
.unwrap();
assert_eq!(tag.id(), found_tag.id());
}

#[test]
fn smoke_commondir() {
let (td, repo) = crate::test::repo_init();
assert_eq!(
crate::test::realpath(repo.path()).unwrap(),
crate::test::realpath(repo.commondir()).unwrap()
);

let worktree = repo
.worktree("test", &td.path().join("worktree"), None)
.unwrap();
let worktree_repo = Repository::open_from_worktree(&worktree).unwrap();
assert_eq!(
crate::test::realpath(repo.path()).unwrap(),
crate::test::realpath(worktree_repo.commondir()).unwrap()
);
}
}

0 comments on commit 2e0fcda

Please sign in to comment.