2024-05-26 08:35:45 +01:00
|
|
|
//
|
2024-05-23 16:50:36 +01:00
|
|
|
use crate as git;
|
2024-05-26 08:35:45 +01:00
|
|
|
use git_next_config as config;
|
2024-05-23 16:50:36 +01:00
|
|
|
|
2024-05-26 08:35:45 +01:00
|
|
|
use tracing::{debug, error, info, warn};
|
|
|
|
|
|
|
|
pub type Result<T> = core::result::Result<T, Error>;
|
2024-05-23 16:50:36 +01:00
|
|
|
|
|
|
|
pub struct Positions {
|
|
|
|
pub main: git::Commit,
|
|
|
|
pub next: git::Commit,
|
|
|
|
pub dev: git::Commit,
|
|
|
|
pub dev_commit_history: Vec<git::Commit>,
|
|
|
|
}
|
|
|
|
|
2024-05-26 08:35:45 +01:00
|
|
|
#[allow(clippy::cognitive_complexity)] // TODO: (#83) reduce complexity
|
|
|
|
pub fn validate_positions(
|
|
|
|
repository: &git::OpenRepository,
|
|
|
|
repo_details: &git::RepoDetails,
|
|
|
|
repo_config: config::RepoConfig,
|
|
|
|
) -> Result<Positions> {
|
|
|
|
// Collect Commit Histories for `main`, `next` and `dev` branches
|
|
|
|
repository.fetch()?;
|
|
|
|
let commit_histories = get_commit_histories(repository, &repo_config);
|
|
|
|
let commit_histories = match commit_histories {
|
|
|
|
Ok(commit_histories) => commit_histories,
|
|
|
|
Err(err) => {
|
|
|
|
error!(?err, "Failed to get commit histories");
|
2024-05-26 08:56:01 +01:00
|
|
|
return Err(git::validation::positions::Error::CommitLog(err));
|
2024-05-26 08:35:45 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
// Validations
|
|
|
|
let Some(main) = commit_histories.main.first().cloned() else {
|
|
|
|
warn!(
|
|
|
|
"No commits on main branch '{}'",
|
|
|
|
repo_config.branches().main()
|
|
|
|
);
|
2024-05-26 08:56:01 +01:00
|
|
|
return Err(git::validation::positions::Error::BranchHasNoCommits(
|
2024-05-26 08:35:45 +01:00
|
|
|
repo_config.branches().main(),
|
|
|
|
));
|
|
|
|
};
|
|
|
|
// Dev must be on main branch, or user must rebase it
|
|
|
|
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 '{}' - user should rebase onto main branch '{}'",
|
|
|
|
repo_config.branches().dev(),
|
|
|
|
repo_config.branches().main(),
|
|
|
|
repo_config.branches().main(),
|
|
|
|
);
|
2024-06-03 08:04:48 +01:00
|
|
|
return Err(git::validation::positions::Error::DevBranchNotBasedOn {
|
|
|
|
dev: repo_config.branches().dev(),
|
|
|
|
other: repo_config.branches().main(),
|
|
|
|
});
|
2024-05-26 08:35:45 +01:00
|
|
|
}
|
|
|
|
// verify that next is an ancestor of dev, and force update to it main if it isn't
|
|
|
|
let Some(next) = commit_histories.next.first().cloned() else {
|
|
|
|
warn!(
|
|
|
|
"No commits on next branch '{}",
|
|
|
|
repo_config.branches().next()
|
|
|
|
);
|
2024-05-26 08:56:01 +01:00
|
|
|
return Err(git::validation::positions::Error::BranchHasNoCommits(
|
2024-05-26 08:35:45 +01:00
|
|
|
repo_config.branches().next(),
|
|
|
|
));
|
|
|
|
};
|
|
|
|
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");
|
2024-06-08 20:43:20 +01:00
|
|
|
if let Err(err) = git::push::reset(
|
2024-05-26 08:35:45 +01:00
|
|
|
repository,
|
|
|
|
repo_details,
|
2024-06-08 21:13:31 +01:00
|
|
|
&repo_config.branches().next(),
|
|
|
|
&main.into(),
|
|
|
|
&git::push::Force::From(next.clone().into()),
|
2024-05-26 08:35:45 +01:00
|
|
|
) {
|
|
|
|
warn!(?err, "Failed to reset next to main");
|
2024-05-26 08:56:01 +01:00
|
|
|
return Err(git::validation::positions::Error::FailedToResetBranch {
|
2024-05-26 08:35:45 +01:00
|
|
|
branch: repo_config.branches().next(),
|
|
|
|
commit: next,
|
|
|
|
});
|
|
|
|
}
|
2024-06-03 08:04:48 +01:00
|
|
|
return Err(git::validation::positions::Error::NextBranchResetRequired(
|
2024-05-26 08:35:45 +01:00
|
|
|
repo_config.branches().next(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
let next_commits = commit_histories
|
|
|
|
.next
|
|
|
|
.into_iter()
|
|
|
|
.take(2) // next should never be more than one commit ahead of main, so this should be
|
|
|
|
// either be next and main on two adjacent commits, or next and main on the same commit,
|
|
|
|
// plus the parent of main.
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
if !next_commits.contains(&main) {
|
|
|
|
warn!(
|
|
|
|
"Main branch '{}' is not on the same commit as next branch '{}', or it's parent - resetting next to main",
|
|
|
|
repo_config.branches().main(),
|
|
|
|
repo_config.branches().next()
|
|
|
|
);
|
2024-06-08 20:43:20 +01:00
|
|
|
if let Err(err) = git::push::reset(
|
2024-05-26 08:35:45 +01:00
|
|
|
repository,
|
|
|
|
repo_details,
|
2024-06-08 21:13:31 +01:00
|
|
|
&repo_config.branches().next(),
|
|
|
|
&main.into(),
|
|
|
|
&git::push::Force::From(next.clone().into()),
|
2024-05-26 08:35:45 +01:00
|
|
|
) {
|
|
|
|
warn!(?err, "Failed to reset next to main");
|
2024-05-26 08:56:01 +01:00
|
|
|
return Err(git::validation::positions::Error::FailedToResetBranch {
|
2024-05-26 08:35:45 +01:00
|
|
|
branch: repo_config.branches().next(),
|
|
|
|
commit: next,
|
|
|
|
});
|
|
|
|
}
|
2024-06-03 08:04:48 +01:00
|
|
|
return Err(git::validation::positions::Error::NextBranchResetRequired(
|
2024-05-26 08:35:45 +01:00
|
|
|
repo_config.branches().next(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
let Some(next) = next_commits.first().cloned() else {
|
|
|
|
warn!(
|
|
|
|
"No commits on next branch '{}'",
|
|
|
|
repo_config.branches().next()
|
|
|
|
);
|
2024-05-26 08:56:01 +01:00
|
|
|
return Err(git::validation::positions::Error::BranchHasNoCommits(
|
2024-05-26 08:35:45 +01:00
|
|
|
repo_config.branches().next(),
|
|
|
|
));
|
|
|
|
};
|
|
|
|
let dev_has_next = commit_histories
|
|
|
|
.dev
|
|
|
|
.iter()
|
|
|
|
.any(|commit| commit == &next_commits[0]);
|
|
|
|
if !dev_has_next {
|
|
|
|
warn!(
|
|
|
|
"Dev branch '{}' is not based on next branch '{}' - next branch will be updated shortly",
|
|
|
|
repo_config.branches().dev(),
|
|
|
|
repo_config.branches().next()
|
|
|
|
);
|
2024-06-03 08:04:48 +01:00
|
|
|
return Err(git::validation::positions::Error::DevBranchNotBasedOn {
|
|
|
|
dev: repo_config.branches().dev(),
|
|
|
|
other: repo_config.branches().next(),
|
|
|
|
}); // dev is not based on next
|
2024-05-26 08:35:45 +01:00
|
|
|
}
|
|
|
|
let Some(dev) = commit_histories.dev.first().cloned() else {
|
|
|
|
warn!(
|
|
|
|
"No commits on dev branch '{}'",
|
|
|
|
repo_config.branches().dev()
|
|
|
|
);
|
2024-05-26 08:56:01 +01:00
|
|
|
return Err(git::validation::positions::Error::BranchHasNoCommits(
|
2024-05-26 08:35:45 +01:00
|
|
|
repo_config.branches().dev(),
|
|
|
|
));
|
|
|
|
};
|
2024-05-26 08:56:01 +01:00
|
|
|
Ok(git::validation::positions::Positions {
|
2024-05-26 08:35:45 +01:00
|
|
|
main,
|
|
|
|
next,
|
|
|
|
dev,
|
|
|
|
dev_commit_history: commit_histories.dev,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_commit_histories(
|
|
|
|
repository: &git::repository::OpenRepository,
|
|
|
|
repo_config: &config::RepoConfig,
|
|
|
|
) -> git::commit::log::Result<git::commit::Histories> {
|
|
|
|
let main = (repository.commit_log(&repo_config.branches().main(), &[]))?;
|
|
|
|
let main_head = [main[0].clone()];
|
|
|
|
let next = repository.commit_log(&repo_config.branches().next(), &main_head)?;
|
|
|
|
let dev = repository.commit_log(&repo_config.branches().dev(), &main_head)?;
|
|
|
|
debug!(
|
|
|
|
main = main.len(),
|
|
|
|
next = next.len(),
|
|
|
|
dev = dev.len(),
|
|
|
|
"Commit histories"
|
|
|
|
);
|
|
|
|
let histories = git::commit::Histories { main, next, dev };
|
|
|
|
Ok(histories)
|
|
|
|
}
|
2024-06-03 08:04:48 +01:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
2024-05-23 16:50:36 +01:00
|
|
|
pub enum Error {
|
2024-06-03 08:04:48 +01:00
|
|
|
#[error("fetch: {0}")]
|
|
|
|
Fetch(#[from] git::fetch::Error),
|
2024-05-25 11:29:08 +01:00
|
|
|
|
2024-06-03 08:04:48 +01:00
|
|
|
#[error("commit log: {0}")]
|
|
|
|
CommitLog(#[from] git::commit::log::Error),
|
2024-05-26 07:42:47 +01:00
|
|
|
|
2024-06-03 08:04:48 +01:00
|
|
|
#[error("failed to reset branch '{branch}' to {commit}")]
|
2024-05-23 16:50:36 +01:00
|
|
|
FailedToResetBranch {
|
2024-05-26 08:35:45 +01:00
|
|
|
branch: config::BranchName,
|
2024-05-23 16:50:36 +01:00
|
|
|
commit: git::Commit,
|
|
|
|
},
|
2024-05-25 11:29:08 +01:00
|
|
|
|
2024-06-03 08:04:48 +01:00
|
|
|
#[error("next branch '{0}' needs to be reset")]
|
|
|
|
NextBranchResetRequired(config::BranchName),
|
2024-05-25 11:29:08 +01:00
|
|
|
|
2024-06-03 08:04:48 +01:00
|
|
|
#[error("branch '{0}' has no commits")]
|
2024-05-26 08:35:45 +01:00
|
|
|
BranchHasNoCommits(config::BranchName),
|
2024-05-25 11:29:08 +01:00
|
|
|
|
2024-06-03 08:04:48 +01:00
|
|
|
#[error("dev branch '{dev}' not based on branch '{other}' ")]
|
|
|
|
DevBranchNotBasedOn {
|
|
|
|
dev: config::BranchName,
|
|
|
|
other: config::BranchName,
|
|
|
|
},
|
2024-05-25 11:29:08 +01:00
|
|
|
}
|