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/database.rs | 14 +++++++++++--- src/git.rs | 29 ++++++++++++++++++++++++++--- src/main.rs | 9 +++++++++ 3 files changed, 46 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/database.rs b/src/database.rs index 107a6d6..ed5d327 100644 --- a/src/database.rs +++ b/src/database.rs @@ -30,6 +30,7 @@ pub struct Repo { id: i64, name: Option, description: Option, + pub default_branch: Option, updated_at: Option, } @@ -47,6 +48,7 @@ impl From<&github::Repo> for Repo { id: repo.id, name: Some(repo.name.clone()), description: repo.description.clone(), + default_branch: Some(repo.default_branch.clone()), updated_at: Some(repo.updated_at.clone()), } } @@ -95,6 +97,7 @@ impl Db { id INTEGER PRIMARY KEY, name TEXT NOT NULL, description TEXT, + default_branch TEXT, updated_at TEXT NOT NULL ); "#, @@ -128,6 +131,7 @@ impl Db { id, name, description, + default_branch, updated_at FROM repositories WHERE id = ? @@ -139,7 +143,8 @@ impl Db { id: row.get(0)?, name: Some(row.get(1)?), description: row.get(2)?, - updated_at: Some(row.get(3)?), + default_branch: row.get(3)?, + updated_at: Some(row.get(4)?), } ) }, @@ -158,14 +163,15 @@ impl Db { tx.execute( r#" INSERT INTO repositories - (id, name, description, updated_at) + (id, name, description, default_branch, updated_at) VALUES - (?, ?, ?, ?) + (?, ?, ?, ?, ?) "#, rusqlite::params![ repo.id, &repo.name, &repo.description, + &repo.default_branch, &repo.updated_at, ], )?; @@ -222,12 +228,14 @@ impl Db { SET name = ?, description = ?, + default_branch = ?, updated_at = ? WHERE id = ? "#, rusqlite::params![ &repo.name, &repo.description, + &repo.default_branch, &repo.updated_at, repo.id, ], 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), + )? + ) +} diff --git a/src/main.rs b/src/main.rs index 0fdc715..878bd73 100644 --- a/src/main.rs +++ b/src/main.rs @@ -278,6 +278,15 @@ fn update>( git::update_description(&repo_path, remote_description)?; } + if let Some(default_branch) = ¤t_repo.default_branch { + if default_branch != &updated_repo.default_branch { + git::change_current_branch( + &repo_path, + &updated_repo.default_branch, + )?; + } + } + update_mtime(&repo_path, &updated_repo)?; Ok(()) -- cgit v1.2.3