commit_wizard/core/usecases/commit/
push.rs1use std::time::Instant;
2
3use crate::{
4 core::{Context, CoreResult},
5 engine::{
6 capabilities::commit::{check::validate_commits, push::evaluate_push},
7 error::ErrorCode,
8 models::policy::commit::CommitModel,
9 },
10};
11
12pub fn run(
13 ctx: &Context,
14 from: Option<String>,
15 to: String,
16 remote: String,
17 branch: Option<String>,
18 allow_empty: bool,
19) -> CoreResult<()> {
20 let ui = ctx.ui();
21 let start = Instant::now();
22
23 let resolved_config = ctx.config().ok_or_else(|| {
24 ErrorCode::ConfigUnreadable
25 .error()
26 .with_context("context", "Config not resolved")
27 })?;
28 let policy = CommitModel::from_config(resolved_config);
29 let push_policy = ctx.policy();
30 let git = ctx.git();
31
32 let current_branch = git.current_branch()?;
33 let push_branch = branch.unwrap_or_else(|| current_branch.clone());
34
35 let raw_commits = git.list_commits(from.as_deref(), &to)?;
36 let checked = validate_commits(raw_commits, &policy);
37 let mut report = evaluate_push(push_policy, &push_branch, checked.commits);
38 let forced = report.blocked && ctx.force();
40 if forced {
41 report.blocked = false;
42 }
43
44 let duration_ms = start.elapsed().as_millis() as u64;
45
46 let mut content = ctx
47 .ui()
48 .new_output_content()
49 .title("Push Validation")
50 .subtitle("Validate push against active rules before pushing")
51 .data("current_branch", current_branch.clone())
52 .data("push_branch", push_branch.clone())
53 .data("remote", remote.clone())
54 .data("range_to", to.clone())
55 .data("protected_branch", report.protected_branch.to_string())
56 .data("total_checked", report.total_checked.to_string())
57 .data("invalid_count", report.invalid_count.to_string())
58 .data("blocked", report.blocked.to_string())
59 .data("allow_empty", allow_empty.to_string());
60
61 if let Some(from_ref) = from.clone() {
62 content = content.data("range_from", from_ref);
63 }
64
65 if !report.block_reasons.is_empty() {
66 content = content.section(
67 "Block Reasons",
68 report.block_reasons.join("\n"),
69 "sh".to_string(),
70 );
71 }
72
73 let invalid_lines = report
74 .commits
75 .iter()
76 .filter(|c| !c.valid)
77 .map(|c| format!("{} {}", c.hash, c.summary))
78 .collect::<Vec<_>>();
79
80 if !invalid_lines.is_empty() {
81 content = content.section(
82 "Invalid Commits",
83 invalid_lines.join("\n"),
84 "sh".to_string(),
85 );
86 }
87
88 let meta = ui
89 .new_output_meta()
90 .with_duration_ms(duration_ms)
91 .with_timestamp(chrono::Utc::now().to_string())
92 .with_command("push".to_string())
93 .with_dry_run(ctx.dry_run());
94
95 if report.blocked {
96 ui.print_with_meta(&content, Some(&meta), false)?;
97 return Err(ErrorCode::GitPushBlocked
98 .error()
99 .with_context("branch", push_branch)
100 .with_context("reasons", report.block_reasons.join("; ")));
101 }
102
103 if ctx.dry_run() {
104 content = content
105 .title("Push Preview")
106 .subtitle("Dry run: no push was performed");
107 return ui.print_with_meta(&content, Some(&meta), true);
108 }
109
110 git.push_branch(&remote, &push_branch)?;
111
112 content = content
113 .title("Push Completed")
114 .subtitle("Push completed successfully");
115
116 ui.print_with_meta(&content, Some(&meta), true)
117}