mod plan;
use serde::{Deserialize, Serialize};
use super::{Fields, FinishMode, MergeStrategy};
pub use plan::{branch_title, plan_flow, FlowFacts};
fn is_false(b: &bool) -> bool {
!*b
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FlowAction {
#[default]
Start,
Finish,
Sync,
Promote,
SetParent,
PickUp,
}
impl FlowAction {
pub fn word(self) -> &'static str {
match self {
FlowAction::Start => "start",
FlowAction::Finish => "finish",
FlowAction::Sync => "sync",
FlowAction::Promote => "promote",
FlowAction::SetParent => "set-parent",
FlowAction::PickUp => "pick-up",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FlowResume {
Continue,
Abort,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SyncMethod {
Merge,
Rebase,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct FlowRequest {
pub action: FlowAction,
pub topic: Option<String>,
pub fields: Fields,
pub from: Option<String>,
pub publish: bool,
pub branch: Option<String>,
pub parent: Option<String>,
pub clear: bool,
pub stack: bool,
pub method: Option<SyncMethod>,
pub finish_mode: Option<FinishMode>,
pub title: Option<String>,
pub delete: Option<bool>,
pub delete_remote: bool,
pub remote: Option<String>,
pub run_hooks: bool,
pub allow_behind: bool,
pub resume: Option<FlowResume>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "op", rename_all = "snake_case")]
pub enum FlowStep {
Checkout {
branch: String,
},
CreateBranch {
name: String,
from: String,
},
SetUpstream {
branch: String,
upstream: String,
},
RecordParent {
branch: String,
parent: Option<String>,
},
Push {
remote: String,
branch: String,
set_upstream: bool,
},
Merge {
branch: String,
into: String,
strategy: MergeStrategy,
#[serde(default, skip_serializing_if = "is_false")]
no_ff: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
message: Option<String>,
},
Rebase {
branch: String,
onto: String,
},
Tag {
name: String,
at: String,
message: String,
},
DeleteBranch {
name: String,
},
DeleteRemoteBranch {
remote: String,
name: String,
},
OpenMergeRequest {
source: String,
target: String,
title: String,
},
Hook {
phase: String,
command: String,
#[serde(default, skip_serializing_if = "is_false")]
personal: bool,
},
}
impl FlowStep {
pub fn describe(&self) -> String {
match self {
FlowStep::Checkout { branch } => format!("Check out {branch}"),
FlowStep::CreateBranch { name, from } => format!("Create branch {name} from {from}"),
FlowStep::SetUpstream { branch, upstream } => {
format!("Make {branch} track {upstream}")
}
FlowStep::RecordParent { branch, parent } => match parent {
Some(p) => format!("Record {p} as the parent of {branch}"),
None => format!("Forget the recorded parent of {branch}"),
},
FlowStep::Push {
remote,
branch,
set_upstream,
} => format!(
"Push {branch} to {remote}{}",
if *set_upstream { " and track it" } else { "" }
),
FlowStep::Merge {
branch,
into,
strategy,
no_ff,
..
} => match strategy {
MergeStrategy::Merge if *no_ff => {
format!("Merge {branch} into {into} with a merge commit")
}
MergeStrategy::Merge => format!("Merge {branch} into {into}"),
MergeStrategy::Squash => format!("Squash {branch} into {into} as one commit"),
MergeStrategy::Rebase | MergeStrategy::FastForward => {
format!("Fast-forward {into} to {branch}")
}
},
FlowStep::Rebase { branch, onto } => format!("Rebase {branch} onto {onto}"),
FlowStep::Tag { name, at, .. } => format!("Tag {at} as {name}"),
FlowStep::DeleteBranch { name } => format!("Delete the local branch {name}"),
FlowStep::DeleteRemoteBranch { remote, name } => {
format!("Delete {name} on {remote}")
}
FlowStep::OpenMergeRequest { source, target, .. } => {
format!("Open a merge request from {source} into {target} on the host")
}
FlowStep::Hook { phase, command, .. } => format!("Run the {phase} hook: {command}"),
}
}
pub fn git(&self) -> Vec<String> {
match self {
FlowStep::Checkout { branch } => vec![format!("git checkout {branch}")],
FlowStep::CreateBranch { name, from } => vec![format!("git branch {name} {from}")],
FlowStep::SetUpstream { branch, upstream } => {
vec![format!("git branch --set-upstream-to={upstream} {branch}")]
}
FlowStep::RecordParent { branch, parent } => match parent {
Some(p) => vec![format!(
"git config branch.{branch}.{} {p}",
super::RECORDED_PARENT_VARIABLE
)],
None => vec![format!(
"git config --unset branch.{branch}.{}",
super::RECORDED_PARENT_VARIABLE
)],
},
FlowStep::Push {
remote,
branch,
set_upstream,
} => vec![format!(
"git push{} {remote} {branch}",
if *set_upstream { " -u" } else { "" }
)],
FlowStep::Merge {
branch,
strategy,
no_ff,
message,
..
} => match strategy {
MergeStrategy::Merge if *no_ff => vec![format!("git merge --no-ff {branch}")],
MergeStrategy::Merge => vec![format!("git merge {branch}")],
MergeStrategy::Squash => vec![
format!("git merge --squash {branch}"),
format!(
"git commit -m \"{}\"",
message.as_deref().unwrap_or("Squashed commit")
),
],
MergeStrategy::Rebase | MergeStrategy::FastForward => {
vec![format!("git merge --ff-only {branch}")]
}
},
FlowStep::Rebase { branch, onto } => {
vec![
format!("git checkout {branch}"),
format!("git rebase {onto}"),
]
}
FlowStep::Tag { name, at, message } => {
vec![format!("git tag -a {name} {at} -m \"{message}\"")]
}
FlowStep::DeleteBranch { name } => vec![format!("git branch -d {name}")],
FlowStep::DeleteRemoteBranch { remote, name } => {
vec![format!("git push {remote} --delete {name}")]
}
FlowStep::OpenMergeRequest { .. } => Vec::new(),
FlowStep::Hook { command, .. } => vec![command.clone()],
}
}
pub fn irreversible(&self) -> bool {
matches!(
self,
FlowStep::Push { .. } | FlowStep::OpenMergeRequest { .. } | FlowStep::Hook { .. }
)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MergeRequestSpec {
pub source: String,
pub target: String,
pub title: String,
pub remote: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct HookInfo {
pub phase: String,
pub command: String,
#[serde(default, skip_serializing_if = "is_false")]
pub personal: bool,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum HooksTrust {
Personal,
#[default]
Trusted,
Untrusted {
hash: String,
previous: Option<Vec<HookInfo>>,
},
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct TrustHooksArgs {
pub hash: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct HooksReport {
pub hash: String,
pub hooks: Vec<HookInfo>,
pub trusted: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct FlowPlan {
pub action: FlowAction,
pub title: String,
pub branch: Option<String>,
pub steps: Vec<FlowStep>,
pub hooks: Vec<HookInfo>,
pub hooks_run: bool,
#[serde(default)]
pub hooks_trust: HooksTrust,
pub warnings: Vec<String>,
pub blockers: Vec<String>,
pub dirty: bool,
pub undoable: bool,
pub irreversible: Vec<String>,
pub merge_request: Option<MergeRequestSpec>,
#[serde(default, skip_serializing_if = "is_false")]
pub resumed: bool,
}
impl FlowPlan {
pub fn can_run(&self) -> bool {
self.blockers.is_empty()
}
pub fn lines(&self) -> Vec<String> {
self.steps
.iter()
.enumerate()
.map(|(i, s)| format!("{}. {}", i + 1, s.describe()))
.collect()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FlowStatus {
Done,
Conflicted,
Failed,
Aborted,
NothingToDo,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum StepStatus {
Done,
Skipped,
Failed,
Conflicted,
Pending,
NotRun,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct StepReport {
pub step: FlowStep,
pub status: StepStatus,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub output: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub exit_code: Option<i32>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FlowOutcome {
pub action: FlowAction,
pub status: FlowStatus,
pub branch: Option<String>,
pub steps: Vec<StepReport>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub conflicts: Vec<String>,
pub message: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub merge_request: Option<MergeRequestSpec>,
#[serde(default, skip_serializing_if = "is_false")]
pub resumable: bool,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub remote_deleted: Vec<crate::journal::RemoteRefChange>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FlowState {
pub request: FlowRequest,
pub steps: Vec<FlowStep>,
pub next: usize,
#[serde(default)]
pub conflict: bool,
pub original_head: Option<String>,
#[serde(default)]
pub squash_message: Option<String>,
#[serde(default)]
pub merge_request: Option<MergeRequestSpec>,
}