2024-05-26 08:35:45 +01:00
|
|
|
//
|
2024-06-19 07:03:08 +01:00
|
|
|
use crate::messages::MessageToken;
|
2024-05-23 17:56:47 +01:00
|
|
|
use git_next_config as config;
|
2024-05-11 19:46:20 +01:00
|
|
|
use git_next_git as git;
|
2024-04-09 18:18:19 +01:00
|
|
|
|
2024-06-19 07:03:08 +01:00
|
|
|
use derive_more::Display;
|
|
|
|
use tracing::{info, warn};
|
2024-05-26 08:35:45 +01:00
|
|
|
|
2024-04-09 22:43:23 +01:00
|
|
|
// advance next to the next commit towards the head of the dev branch
|
2024-04-11 17:58:30 +01:00
|
|
|
#[tracing::instrument(fields(next), skip_all)]
|
2024-06-19 07:03:08 +01:00
|
|
|
pub fn advance_next(
|
|
|
|
next: &git::Commit,
|
|
|
|
dev_commit_history: &[git::Commit],
|
2024-05-26 08:35:45 +01:00
|
|
|
repo_details: git::RepoDetails,
|
2024-05-23 17:56:47 +01:00
|
|
|
repo_config: config::RepoConfig,
|
2024-06-19 16:40:10 +01:00
|
|
|
open_repository: &dyn git::repository::OpenRepositoryLike,
|
2024-05-23 08:30:58 +01:00
|
|
|
message_token: MessageToken,
|
2024-06-19 07:03:08 +01:00
|
|
|
) -> Result<MessageToken> {
|
|
|
|
let commit =
|
|
|
|
find_next_commit_on_dev(next, dev_commit_history).ok_or_else(|| Error::NextAtDev)?;
|
|
|
|
validate_commit_message(commit.message())?;
|
2024-04-12 17:41:09 +01:00
|
|
|
info!("Advancing next to commit '{}'", commit);
|
2024-06-19 07:03:08 +01:00
|
|
|
git::push::reset(
|
2024-06-19 16:40:10 +01:00
|
|
|
open_repository,
|
2024-05-26 08:35:45 +01:00
|
|
|
&repo_details,
|
2024-06-08 21:13:31 +01:00
|
|
|
&repo_config.branches().next(),
|
|
|
|
&commit.into(),
|
|
|
|
&git::push::Force::No,
|
2024-06-19 07:03:08 +01:00
|
|
|
)?;
|
|
|
|
Ok(message_token)
|
2024-04-10 15:18:48 +01:00
|
|
|
}
|
|
|
|
|
2024-04-12 16:21:45 +01:00
|
|
|
#[tracing::instrument]
|
2024-06-19 07:03:08 +01:00
|
|
|
fn validate_commit_message(message: &git::commit::Message) -> Result<()> {
|
2024-04-12 16:21:45 +01:00
|
|
|
let message = &message.to_string();
|
|
|
|
if message.to_ascii_lowercase().starts_with("wip") {
|
2024-06-19 07:03:08 +01:00
|
|
|
return Err(Error::IsWorkInProgress);
|
2024-04-12 16:21:45 +01:00
|
|
|
}
|
|
|
|
match git_conventional::Commit::parse(message) {
|
|
|
|
Ok(commit) => {
|
|
|
|
info!(?commit, "Pass");
|
2024-06-19 07:03:08 +01:00
|
|
|
Ok(())
|
2024-04-12 16:21:45 +01:00
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
warn!(?err, "Fail");
|
2024-06-19 07:03:08 +01:00
|
|
|
Err(Error::InvalidCommitMessage {
|
|
|
|
reason: err.kind().to_string(),
|
|
|
|
})
|
2024-04-12 16:21:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-14 07:59:31 +01:00
|
|
|
pub fn find_next_commit_on_dev(
|
2024-06-19 07:03:08 +01:00
|
|
|
next: &git::Commit,
|
|
|
|
dev_commit_history: &[git::Commit],
|
2024-05-11 19:46:20 +01:00
|
|
|
) -> Option<git::Commit> {
|
2024-06-19 07:03:08 +01:00
|
|
|
let mut next_commit: Option<&git::Commit> = None;
|
|
|
|
for commit in dev_commit_history.iter() {
|
2024-04-10 15:18:48 +01:00
|
|
|
if commit == next {
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
next_commit.replace(commit);
|
|
|
|
}
|
2024-06-19 07:03:08 +01:00
|
|
|
next_commit.cloned()
|
2024-04-09 22:43:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// advance main branch to the commit 'next'
|
2024-04-11 17:58:30 +01:00
|
|
|
#[tracing::instrument(fields(next), skip_all)]
|
2024-06-19 07:03:08 +01:00
|
|
|
pub fn advance_main(
|
2024-05-11 19:46:20 +01:00
|
|
|
next: git::Commit,
|
2024-05-26 08:35:45 +01:00
|
|
|
repo_details: &git::RepoDetails,
|
2024-05-23 17:56:47 +01:00
|
|
|
repo_config: &config::RepoConfig,
|
2024-06-19 16:40:10 +01:00
|
|
|
open_repository: &dyn git::repository::OpenRepositoryLike,
|
2024-06-19 07:03:08 +01:00
|
|
|
) -> Result<()> {
|
2024-04-12 17:41:09 +01:00
|
|
|
info!("Advancing main to next");
|
2024-06-19 07:03:08 +01:00
|
|
|
git::push::reset(
|
2024-06-19 16:40:10 +01:00
|
|
|
open_repository,
|
2024-05-26 08:35:45 +01:00
|
|
|
repo_details,
|
2024-06-08 21:13:31 +01:00
|
|
|
&repo_config.branches().main(),
|
|
|
|
&next.into(),
|
|
|
|
&git::push::Force::No,
|
2024-06-19 07:03:08 +01:00
|
|
|
)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type Result<T> = core::result::Result<T, Error>;
|
|
|
|
#[derive(Debug, thiserror::Error, Display)]
|
|
|
|
pub enum Error {
|
|
|
|
#[display("push: {}", 0)]
|
|
|
|
Push(#[from] git::push::Error),
|
|
|
|
|
|
|
|
#[display("no commits to advance next to")]
|
|
|
|
NextAtDev,
|
|
|
|
|
|
|
|
#[display("commit is a Work-in-progress")]
|
|
|
|
IsWorkInProgress,
|
|
|
|
|
|
|
|
#[display("commit message is not in conventional commit format: {reason}")]
|
|
|
|
InvalidCommitMessage { reason: String },
|
2024-04-09 19:30:05 +01:00
|
|
|
}
|