From 85df39e199fa146e22d9cf8ccd635277fe066d12 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 24 Jun 2021 21:18:07 +0200 Subject: main::update(): Change HEAD branch if default branch changed If the default branch on GitHub changed, change the local mirror's HEAD to match the new default. Need to store the default branch in the database now so we can find out whether it changed. --- src/git.rs | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'src/git.rs') diff --git a/src/git.rs b/src/git.rs index 753137c..ccc0922 100644 --- a/src/git.rs +++ b/src/git.rs @@ -76,9 +76,7 @@ pub fn mirror>( remote.fetch(&refspecs, None, None)?; if default_branch != "master" { - repo.set_head( - &format!("refs/heads/{}", default_branch), - )?; + repo_change_current_branch(&repo, default_branch)?; } Ok(()) @@ -133,3 +131,28 @@ pub fn update_description>( Ok(()) } + +/// Change the current branch of the repository at `repo_path` to +/// `default_branch`. +pub fn change_current_branch>( + repo_path: P, + default_branch: &str, +) -> Result<(), Error> { + let repo = git2::Repository::open_bare(repo_path)?; + + Ok( + repo_change_current_branch(&repo, default_branch)? + ) +} + +/// Change `repo`'s current branch to `default_branch`. +fn repo_change_current_branch( + repo: &git2::Repository, + default_branch: &str, +) -> Result<(), Error> { + Ok( + repo.set_head( + &format!("refs/heads/{}", default_branch), + )? + ) +} -- cgit v1.2.3