From 325d110e9a33d88e83af95191c75b67a3dad4eb3 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Sat, 9 Apr 2022 11:04:44 +0530 Subject: [PATCH] feat: switch to user configured branch even when configuration is changed DESCRIPTION Branch switching wasn't possible in earlier versions. If user deploys "master" branch first and then wants to switch to "pages" branch, they would have to delete the Pages-managed copy of the repository to re-initialize the repository. This patch introduces changes to switch to user configured branch on startup and at each repository update. --- src/deploy.rs | 2 +- src/page.rs | 60 +++++++++++++++++++++++++++++++++---------------- src/settings.rs | 2 +- 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/src/deploy.rs b/src/deploy.rs index 5e75d9f..c1a5bc0 100644 --- a/src/deploy.rs +++ b/src/deploy.rs @@ -45,7 +45,7 @@ async fn update(payload: web::Json) -> impl Responder { for page in SETTINGS.pages.iter() { if page.secret == payload.secret { web::block(|| { - page.fetch_upstream(&page.branch); + page.update(); }) .await .unwrap(); diff --git a/src/page.rs b/src/page.rs index a219cc5..939081e 100644 --- a/src/page.rs +++ b/src/page.rs @@ -38,25 +38,27 @@ impl Page { }; let repo = Repository::open(&self.path).unwrap(); - { - self._fetch_upstream(&repo, &self.branch); - let branch = repo - .find_branch(&format!("origin/{}", &self.branch), BranchType::Remote) - .unwrap(); - - let mut checkout_options = CheckoutBuilder::new(); - checkout_options.force(); - - let tree = branch.get().peel(ObjectType::Tree).unwrap(); - - repo.checkout_tree(&tree, Some(&mut checkout_options)) - .unwrap(); - - repo.set_head(branch.get().name().unwrap()).unwrap(); - } + self._fetch_upstream(&repo, &self.branch); + self.deploy_branch(&repo); repo } + pub fn deploy_branch(&self, repo: &Repository) { + let branch = repo + .find_branch(&format!("origin/{}", &self.branch), BranchType::Remote) + .unwrap(); + + let mut checkout_options = CheckoutBuilder::new(); + checkout_options.force(); + + let tree = branch.get().peel(ObjectType::Tree).unwrap(); + + repo.checkout_tree(&tree, Some(&mut checkout_options)) + .unwrap(); + repo.set_head(branch.get().name().unwrap()).unwrap(); + info!("Deploying branch {}", self.branch); + } + fn _fetch_upstream(&self, repo: &Repository, branch: &str) { let mut remote = repo.find_remote("origin").unwrap(); remote.connect(Direction::Fetch).unwrap(); @@ -65,9 +67,10 @@ impl Page { remote.disconnect().unwrap(); } - pub fn fetch_upstream(&self, branch: &str) { + pub fn update(&self) { let repo = self.create_repo(); - self._fetch_upstream(&repo, branch); + self._fetch_upstream(&repo, &self.branch); + self.deploy_branch(&repo); } } @@ -75,13 +78,22 @@ impl Page { mod tests { use super::*; + use git2::Branch; + use git2::Repository; use mktemp::Temp; + impl Page { + fn get_tree<'a>(&self, repo: &'a Repository) -> Branch<'a> { + repo.find_branch(&format!("origin/{}", &self.branch), BranchType::Remote) + .unwrap() + } + } + #[actix_rt::test] async fn pages_works() { let tmp_dir = Temp::new_dir().unwrap(); assert!(tmp_dir.exists(), "tmp directory successully created"); - let page = Page { + let mut page = Page { secret: String::default(), repo: "https://github.com/mcaptcha/website".to_owned(), path: tmp_dir.to_str().unwrap().to_string(), @@ -100,5 +112,15 @@ mod tests { Repository::open(tmp_dir.as_path()).is_ok(), "repository exists yet" ); + + let gh_pages = page.get_tree(&repo); + assert_eq!( + gh_pages.name().unwrap().as_ref().unwrap(), + &"origin/gh-pages" + ); + page.branch = "master".to_string(); + page.update(); + let master = page.get_tree(&repo); + assert_eq!(master.name().unwrap().as_ref().unwrap(), &"origin/master"); } } diff --git a/src/settings.rs b/src/settings.rs index 1857d0b..1d0a0e3 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -104,7 +104,7 @@ impl Settings { panic!("duplicate page onfiguration {:?} and {:?}", page, page2); } } - page.fetch_upstream(&page.branch); + page.update(); } Ok(settings)