aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2021-06-24 21:18:07 +0200
committerTeddy Wing2021-06-24 21:18:07 +0200
commit85df39e199fa146e22d9cf8ccd635277fe066d12 (patch)
treeebed651a7cdb1059ca92ca0916196c2dc19c27f3
parent98ec0eb9370bd12225fde1d8c2ff8b8ace693609 (diff)
downloadreflectub-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.
-rw-r--r--src/database.rs14
-rw-r--r--src/git.rs29
-rw-r--r--src/main.rs9
3 files changed, 46 insertions, 6 deletions
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<String>,
description: Option<String>,
+ pub default_branch: Option<String>,
updated_at: Option<String>,
}
@@ -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<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),
+ )?
+ )
+}
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<P: AsRef<Path>>(
git::update_description(&repo_path, remote_description)?;
}
+ if let Some(default_branch) = &current_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(())