Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repo: implement git_repository_commondir #1079

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
);
}
}