Skip to content

Commit

Permalink
Merge pull request #931 from drmikehenry/set_head_bytes
Browse files Browse the repository at this point in the history
Add `Repository::set_head_bytes()`; like `set_head()` but bytes-only.
  • Loading branch information
ehuss authored Feb 24, 2023
2 parents 49d8c92 + 6c06327 commit 7292b00
Showing 1 changed file with 29 additions and 0 deletions.
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

0 comments on commit 7292b00

Please sign in to comment.