orch/engine/git/
progress.rs1use crate::store::now_stamp;
4use std::path::Path;
5use std::sync::Arc;
6
7use tokio::process::Command;
8
9use crate::engine::git::commit::When;
10use crate::engine::git::{git_ops, worktree};
11use crate::store::TaskStore;
12use crate::store::cluster::local::ProgressFn;
13use crate::store::cluster::progress::{self, Record, Report};
14
15pub fn handler() -> ProgressFn {
17 Arc::new(|store, node, report| Box::pin(async move { handle(&store, &node, report).await }))
18}
19
20async fn git_out(dir: &Path, args: &[&str]) -> Option<String> {
21 let out = Command::new("git")
22 .args(args)
23 .current_dir(dir)
24 .output()
25 .await
26 .ok()?;
27 out.status
28 .success()
29 .then(|| String::from_utf8_lossy(&out.stdout).trim().to_string())
30}
31
32pub async fn handle(store: &TaskStore, node: &str, report: Report) -> anyhow::Result<Record> {
35 let task_id = store
36 .resolve_id_by_worktree(&report.worktree)
37 .await?
38 .ok_or_else(|| anyhow::anyhow!("{} is not the worktree of a task", report.worktree))?;
39 let task = store.get(task_id).await?;
40 let dir = Path::new(&report.worktree);
41 let previous = progress::list(store, task_id).await?;
42 let mut errors: Vec<String> = report.cli_error.iter().cloned().collect();
43
44 let head_before = git_out(dir, &["rev-parse", "HEAD"])
45 .await
46 .unwrap_or_default();
47 let mut committed_by = if report.cli_committed { "cli" } else { "none" };
48 let ext = task.external_id.clone().unwrap_or_default();
49 match git_ops::auto_commit(
50 dir,
51 &ext,
52 &report.done,
53 "orch",
54 u32::try_from(task.attempts).unwrap_or(0),
55 When::Tested,
56 )
57 .await
58 {
59 Ok(true) => committed_by = "orch",
60 Ok(false) => {}
61 Err(e) => errors.push(format!("commit: {e:#}")),
62 }
63
64 if committed_by == "none"
65 && previous
66 .last()
67 .is_some_and(|last| !head_before.is_empty() && last.commit != head_before)
68 {
69 committed_by = "agent";
70 }
71
72 let branch = git_ops::get_current_branch(dir).await;
73 let commit = git_out(dir, &["rev-parse", "HEAD"])
74 .await
75 .unwrap_or_default();
76 let remote = git_out(
77 dir,
78 &["rev-parse", &format!("refs/remotes/origin/{branch}")],
79 )
80 .await;
81 let pushed_by = if remote.as_deref() == Some(commit.as_str()) {
82 "agent"
83 } else {
84 let default_branch = worktree::detect_default_branch(dir).await;
85 match git_ops::push_branch(dir, &branch, &default_branch).await {
86 Ok(true) => "orch",
87 Ok(false) => "none",
88 Err(e) => {
89 errors.push(format!("push: {e:#}"));
90 "none"
91 }
92 }
93 };
94
95 let record = Record {
96 node: node.to_string(),
97 ts: now_stamp(),
98 branch,
99 commit,
100 committed_by: committed_by.to_string(),
101 pushed_by: pushed_by.to_string(),
102 error: (!errors.is_empty()).then(|| progress::cut(&errors.join("; "))),
103 done: progress::cut(&report.done),
104 left: progress::cut(&report.left),
105 state: progress::cut(&report.state),
106 };
107 progress::append(store, task_id, &record).await?;
108 Ok(record)
109}