diff options
author | Teddy Wing | 2021-06-24 21:18:07 +0200 |
---|---|---|
committer | Teddy Wing | 2021-06-24 21:18:07 +0200 |
commit | 85df39e199fa146e22d9cf8ccd635277fe066d12 (patch) | |
tree | ebed651a7cdb1059ca92ca0916196c2dc19c27f3 /src/git.rs | |
parent | 98ec0eb9370bd12225fde1d8c2ff8b8ace693609 (diff) | |
download | reflectub-85df39e199fa146e22d9cf8ccd635277fe066d12.tar.bz2 |
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.
Diffstat (limited to 'src/git.rs')
-rw-r--r-- | src/git.rs | 29 |
1 files changed, 26 insertions, 3 deletions
@@ -76,9 +76,7 @@ pub fn mirror<P: AsRef<Path>>( 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<P: AsRef<Path>>( Ok(()) } + +/// Change the current branch of the repository at `repo_path` to +/// `default_branch`. +pub fn change_current_branch<P: AsRef<Path>>( + 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), + )? + ) +} |