From 0622e6092bf05da949903c6c6159e3fd67b099aa Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 13 Apr 2024 15:26:45 +0100 Subject: [PATCH] feat(repo/branch): update next branch when not based on main Closes kemitix/git-next#41 --- src/server/actors/repo/branch.rs | 38 +++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/server/actors/repo/branch.rs b/src/server/actors/repo/branch.rs index 2b450319..a67b5101 100644 --- a/src/server/actors/repo/branch.rs +++ b/src/server/actors/repo/branch.rs @@ -47,15 +47,7 @@ pub async fn validate_positions( let next_is_ancestor_of_dev = commit_histories.dev.iter().any(|dev| dev == &next); if !next_is_ancestor_of_dev { info!("Next is not an ancestor of dev - resetting next to main"); - let reset = git.reset( - &config.branches().next(), - main.into(), - ResetForce::Force(next.into()), - &repo_details, - ); - if let Err(err) = reset { - warn!(?err, "Failed"); - } + reset_next_to_main(next, main, &repo_details, &config, &git); revalidate_positions(addr).await; return; } @@ -67,10 +59,11 @@ pub async fn validate_positions( .collect::>(); if !next_commits.contains(&main) { warn!( - "Main branch '{}' is not on the same commit as next branch '{}', or it's parent", + "Main branch '{}' is not on the same commit as next branch '{}', or it's parent - resetting next to main", config.branches().main(), config.branches().next() ); + reset_next_to_main(next, main, &repo_details, &config, &git); revalidate_positions(addr).await; return; } @@ -85,7 +78,7 @@ pub async fn validate_positions( .any(|commit| commit == &next_commits[0]); if !dev_has_next { warn!( - "Dev branch '{}' is not based on next branch '{}'", + "Dev branch '{}' is not based on next branch '{}' - next branch will be updated shortly", config.branches().dev(), config.branches().next() ); @@ -95,9 +88,10 @@ pub async fn validate_positions( let dev_has_main = commit_histories.dev.iter().any(|commit| commit == &main); if !dev_has_main { warn!( - "Dev branch '{}' is not based on main branch '{}'", + "Dev branch '{}' is not based on main branch '{}' - user should rebase onto main branch '{}'", config.branches().dev(), - config.branches().main() + config.branches().main(), + config.branches().main(), ); revalidate_positions(addr).await; return; @@ -132,6 +126,24 @@ async fn get_commit_histories( } } +fn reset_next_to_main( + next: forge::Commit, + main: forge::Commit, + repo_details: &RepoDetails, + repo_config: &RepoConfig, + git: &Git, +) { + let reset = git.reset( + &repo_config.branches().next(), + main.into(), + ResetForce::Force(next.into()), + repo_details, + ); + if let Err(err) = reset { + warn!(?err, "Failed"); + } +} + async fn revalidate_positions(addr: Addr) { // TODO : (#18) sleep and restart while we don't have webhooks tokio::time::sleep(std::time::Duration::from_secs(10)).await;