git-next/crates/repo-actor/src/branch.rs

97 lines
2.7 KiB
Rust
Raw Normal View History

//
use actix::prelude::*;
use git_next_config as config;
use git_next_git as git;
use tracing::{info, warn};
use crate::{MessageToken, ValidateRepo};
use std::time::Duration;
// advance next to the next commit towards the head of the dev branch
#[tracing::instrument(fields(next), skip_all)]
pub async fn advance_next(
next: git::Commit,
dev_commit_history: Vec<git::Commit>,
repo_details: git::RepoDetails,
repo_config: config::RepoConfig,
open_repository: &dyn git::repository::OpenRepositoryLike,
addr: Addr<super::RepoActor>,
message_token: MessageToken,
) {
let next_commit = find_next_commit_on_dev(next, dev_commit_history);
let Some(commit) = next_commit else {
warn!("No commits to advance next to");
return;
};
if let Some(problem) = validate_commit_message(commit.message()) {
warn!("Can't advance next to commit '{}': {}", commit, problem);
return;
}
2024-04-12 17:41:09 +01:00
info!("Advancing next to commit '{}'", commit);
if let Err(err) = git::push::reset(
open_repository,
&repo_details,
&repo_config.branches().next(),
&commit.into(),
&git::push::Force::No,
) {
warn!(?err, "Failed")
}
tokio::time::sleep(Duration::from_secs(10)).await;
addr.do_send(ValidateRepo { message_token })
}
#[tracing::instrument]
fn validate_commit_message(message: &git::commit::Message) -> Option<String> {
let message = &message.to_string();
if message.to_ascii_lowercase().starts_with("wip") {
return Some("Is Work-In-Progress".to_string());
}
match git_conventional::Commit::parse(message) {
Ok(commit) => {
info!(?commit, "Pass");
None
}
Err(err) => {
warn!(?err, "Fail");
Some(err.kind().to_string())
}
}
}
2024-05-14 07:59:31 +01:00
pub fn find_next_commit_on_dev(
next: git::Commit,
dev_commit_history: Vec<git::Commit>,
) -> Option<git::Commit> {
let mut next_commit: Option<git::Commit> = None;
for commit in dev_commit_history.into_iter() {
if commit == next {
break;
};
next_commit.replace(commit);
}
next_commit
}
// advance main branch to the commit 'next'
#[tracing::instrument(fields(next), skip_all)]
pub async fn advance_main(
next: git::Commit,
repo_details: &git::RepoDetails,
repo_config: &config::RepoConfig,
open_repository: &dyn git::repository::OpenRepositoryLike,
) {
2024-04-12 17:41:09 +01:00
info!("Advancing main to next");
if let Err(err) = git::push::reset(
open_repository,
repo_details,
&repo_config.branches().main(),
&next.into(),
&git::push::Force::No,
) {
2024-04-12 19:43:21 +01:00
warn!(?err, "Failed")
};
}