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

Add Repository::set_head_bytes(); like set_head() but bytes-only. #931

Merged
merged 1 commit into from
Feb 24, 2023
Merged
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
29 changes: 29 additions & 0 deletions src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,22 @@ impl Repository {
/// Otherwise, the HEAD will be detached and will directly point to the
/// commit.
pub fn set_head(&self, refname: &str) -> Result<(), Error> {
self.set_head_bytes(refname.as_bytes())
}

/// Make the repository HEAD point to the specified reference as a byte array.
///
/// If the provided reference points to a tree or a blob, the HEAD is
/// unaltered and an error is returned.
///
/// If the provided reference points to a branch, the HEAD will point to
/// that branch, staying attached, or become attached if it isn't yet. If
/// the branch doesn't exist yet, no error will be returned. The HEAD will
/// then be attached to an unborn branch.
///
/// Otherwise, the HEAD will be detached and will directly point to the
/// commit.
pub fn set_head_bytes(&self, refname: &[u8]) -> Result<(), Error> {
let refname = CString::new(refname)?;
unsafe {
try_call!(raw::git_repository_set_head(self.raw, refname));
Expand Down Expand Up @@ -3602,6 +3618,19 @@ mod tests {
assert!(repo.set_head("*").is_err());
}

#[test]
fn smoke_set_head_bytes() {
let (_td, repo) = crate::test::repo_init();

assert!(repo.set_head_bytes(b"refs/heads/does-not-exist").is_ok());
assert!(repo.head().is_err());

assert!(repo.set_head_bytes(b"refs/heads/main").is_ok());
assert!(repo.head().is_ok());

assert!(repo.set_head_bytes(b"*").is_err());
}

#[test]
fn smoke_set_head_detached() {
let (_td, repo) = crate::test::repo_init();
Expand Down