Skip to content

Commit 6c06327

Browse files
committed
Add Repository::set_head_bytes(); like set_head() but bytes-only.
This allows setting `HEAD` to non-utf8 reference names.
1 parent 49d8c92 commit 6c06327

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/repo.rs

+29
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,22 @@ impl Repository {
817817
/// Otherwise, the HEAD will be detached and will directly point to the
818818
/// commit.
819819
pub fn set_head(&self, refname: &str) -> Result<(), Error> {
820+
self.set_head_bytes(refname.as_bytes())
821+
}
822+
823+
/// Make the repository HEAD point to the specified reference as a byte array.
824+
///
825+
/// If the provided reference points to a tree or a blob, the HEAD is
826+
/// unaltered and an error is returned.
827+
///
828+
/// If the provided reference points to a branch, the HEAD will point to
829+
/// that branch, staying attached, or become attached if it isn't yet. If
830+
/// the branch doesn't exist yet, no error will be returned. The HEAD will
831+
/// then be attached to an unborn branch.
832+
///
833+
/// Otherwise, the HEAD will be detached and will directly point to the
834+
/// commit.
835+
pub fn set_head_bytes(&self, refname: &[u8]) -> Result<(), Error> {
820836
let refname = CString::new(refname)?;
821837
unsafe {
822838
try_call!(raw::git_repository_set_head(self.raw, refname));
@@ -3602,6 +3618,19 @@ mod tests {
36023618
assert!(repo.set_head("*").is_err());
36033619
}
36043620

3621+
#[test]
3622+
fn smoke_set_head_bytes() {
3623+
let (_td, repo) = crate::test::repo_init();
3624+
3625+
assert!(repo.set_head_bytes(b"refs/heads/does-not-exist").is_ok());
3626+
assert!(repo.head().is_err());
3627+
3628+
assert!(repo.set_head_bytes(b"refs/heads/main").is_ok());
3629+
assert!(repo.head().is_ok());
3630+
3631+
assert!(repo.set_head_bytes(b"*").is_err());
3632+
}
3633+
36053634
#[test]
36063635
fn smoke_set_head_detached() {
36073636
let (_td, repo) = crate::test::repo_init();

0 commit comments

Comments
 (0)