2025-07-23 15:40:00 -07:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
2025-07-11 13:30:11 -04:00
|
|
|
use clap::Parser;
|
|
|
|
|
use codex_common::CliConfigOverrides;
|
|
|
|
|
use codex_core::config::Config;
|
|
|
|
|
use codex_core::config::ConfigOverrides;
|
|
|
|
|
|
|
|
|
|
use crate::chatgpt_token::init_chatgpt_token_from_auth;
|
|
|
|
|
use crate::get_task::GetTaskResponse;
|
|
|
|
|
use crate::get_task::OutputItem;
|
|
|
|
|
use crate::get_task::PrOutputItem;
|
|
|
|
|
use crate::get_task::get_task;
|
|
|
|
|
|
|
|
|
|
/// Applies the latest diff from a Codex agent task.
|
|
|
|
|
#[derive(Debug, Parser)]
|
|
|
|
|
pub struct ApplyCommand {
|
|
|
|
|
pub task_id: String,
|
|
|
|
|
|
|
|
|
|
#[clap(flatten)]
|
|
|
|
|
pub config_overrides: CliConfigOverrides,
|
|
|
|
|
}
|
2025-07-23 15:40:00 -07:00
|
|
|
pub async fn run_apply_command(
|
|
|
|
|
apply_cli: ApplyCommand,
|
|
|
|
|
cwd: Option<PathBuf>,
|
|
|
|
|
) -> anyhow::Result<()> {
|
2025-07-11 13:30:11 -04:00
|
|
|
let config = Config::load_with_cli_overrides(
|
|
|
|
|
apply_cli
|
|
|
|
|
.config_overrides
|
|
|
|
|
.parse_overrides()
|
|
|
|
|
.map_err(anyhow::Error::msg)?,
|
|
|
|
|
ConfigOverrides::default(),
|
2025-10-03 13:02:26 -07:00
|
|
|
)
|
|
|
|
|
.await?;
|
2025-07-11 13:30:11 -04:00
|
|
|
|
2025-09-09 14:23:23 -07:00
|
|
|
init_chatgpt_token_from_auth(&config.codex_home).await?;
|
2025-07-11 13:30:11 -04:00
|
|
|
|
|
|
|
|
let task_response = get_task(&config, apply_cli.task_id).await?;
|
2025-07-23 15:40:00 -07:00
|
|
|
apply_diff_from_task(task_response, cwd).await
|
2025-07-11 13:30:11 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-23 15:40:00 -07:00
|
|
|
pub async fn apply_diff_from_task(
|
|
|
|
|
task_response: GetTaskResponse,
|
|
|
|
|
cwd: Option<PathBuf>,
|
|
|
|
|
) -> anyhow::Result<()> {
|
2025-07-11 13:30:11 -04:00
|
|
|
let diff_turn = match task_response.current_diff_task_turn {
|
|
|
|
|
Some(turn) => turn,
|
|
|
|
|
None => anyhow::bail!("No diff turn found"),
|
|
|
|
|
};
|
|
|
|
|
let output_diff = diff_turn.output_items.iter().find_map(|item| match item {
|
|
|
|
|
OutputItem::Pr(PrOutputItem { output_diff }) => Some(output_diff),
|
|
|
|
|
_ => None,
|
|
|
|
|
});
|
|
|
|
|
match output_diff {
|
2025-07-23 15:40:00 -07:00
|
|
|
Some(output_diff) => apply_diff(&output_diff.diff, cwd).await,
|
2025-07-11 13:30:11 -04:00
|
|
|
None => anyhow::bail!("No PR output item found"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-23 15:40:00 -07:00
|
|
|
async fn apply_diff(diff: &str, cwd: Option<PathBuf>) -> anyhow::Result<()> {
|
2025-09-30 03:10:33 -07:00
|
|
|
let cwd = cwd.unwrap_or(std::env::current_dir().unwrap_or_else(|_| std::env::temp_dir()));
|
|
|
|
|
let req = codex_git_apply::ApplyGitRequest {
|
|
|
|
|
cwd,
|
|
|
|
|
diff: diff.to_string(),
|
|
|
|
|
revert: false,
|
|
|
|
|
preflight: false,
|
|
|
|
|
};
|
|
|
|
|
let res = codex_git_apply::apply_git_patch(&req)?;
|
|
|
|
|
if res.exit_code != 0 {
|
2025-07-11 13:30:11 -04:00
|
|
|
anyhow::bail!(
|
2025-09-30 03:10:33 -07:00
|
|
|
"Git apply failed (applied={}, skipped={}, conflicts={})\nstdout:\n{}\nstderr:\n{}",
|
|
|
|
|
res.applied_paths.len(),
|
|
|
|
|
res.skipped_paths.len(),
|
|
|
|
|
res.conflicted_paths.len(),
|
|
|
|
|
res.stdout,
|
|
|
|
|
res.stderr
|
2025-07-11 13:30:11 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
println!("Successfully applied diff");
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|