mirror of
https://github.com/openai/codex.git
synced 2026-11-26 23:40:43 +00:00
fix(worktree): preserve remote dirty transfers
This commit is contained in:
@@ -187,14 +187,30 @@ pub(crate) async fn ensure_worktree(
|
||||
.await?;
|
||||
}
|
||||
|
||||
apply_dirty_policy_after_create(
|
||||
let prepared_transfer = match prepare_dirty_policy_after_create(
|
||||
runner,
|
||||
request_handle,
|
||||
&repo.root,
|
||||
&worktree_git_root,
|
||||
req.dirty_policy,
|
||||
)
|
||||
.await?;
|
||||
.await
|
||||
{
|
||||
Ok(prepared_transfer) => prepared_transfer,
|
||||
Err(err) => {
|
||||
return Err(rollback_failed_create(
|
||||
runner,
|
||||
&repo.root,
|
||||
&worktree_git_root,
|
||||
&branch,
|
||||
/*delete_branch*/ !branch_exists,
|
||||
err,
|
||||
)
|
||||
.await);
|
||||
}
|
||||
};
|
||||
finalize_dirty_policy_after_create(runner, request_handle, &repo.root, prepared_transfer)
|
||||
.await?;
|
||||
let dirty = dirty_state(runner, &worktree_git_root).await?;
|
||||
let head = git_stdout_result(runner, &worktree_git_root, &["rev-parse", "HEAD"])
|
||||
.await?
|
||||
@@ -473,57 +489,125 @@ async fn validate_dirty_policy_before_create(
|
||||
}
|
||||
}
|
||||
|
||||
async fn apply_dirty_policy_after_create(
|
||||
async fn prepare_dirty_policy_after_create(
|
||||
runner: &WorkspaceCommandRunner,
|
||||
request_handle: &AppServerRequestHandle,
|
||||
source_root: &Path,
|
||||
worktree_root: &Path,
|
||||
policy: DirtyPolicy,
|
||||
) -> Result<()> {
|
||||
) -> Result<PreparedRemoteDirtyTransfer> {
|
||||
let state = dirty_state(runner, source_root).await?;
|
||||
if !state.is_dirty() {
|
||||
return Ok(());
|
||||
return Ok(PreparedRemoteDirtyTransfer { move_plan: None });
|
||||
}
|
||||
let plan = RemoteTransferPlan::capture(runner, source_root).await?;
|
||||
match policy {
|
||||
DirtyPolicy::Fail | DirtyPolicy::Ignore => Ok(()),
|
||||
DirtyPolicy::Fail | DirtyPolicy::Ignore => {
|
||||
Ok(PreparedRemoteDirtyTransfer { move_plan: None })
|
||||
}
|
||||
DirtyPolicy::CopyTracked => {
|
||||
plan.apply_tracked_diff(runner, request_handle, worktree_root)
|
||||
.await
|
||||
.await?;
|
||||
Ok(PreparedRemoteDirtyTransfer { move_plan: None })
|
||||
}
|
||||
DirtyPolicy::CopyAll => {
|
||||
plan.apply_tracked_diff(runner, request_handle, worktree_root)
|
||||
.await?;
|
||||
plan.copy_untracked(request_handle, source_root, worktree_root)
|
||||
.await
|
||||
.await?;
|
||||
Ok(PreparedRemoteDirtyTransfer { move_plan: None })
|
||||
}
|
||||
DirtyPolicy::MoveTracked => {
|
||||
plan.apply_tracked_diff(runner, request_handle, worktree_root)
|
||||
.await?;
|
||||
plan.clean_source_after_move(
|
||||
runner,
|
||||
request_handle,
|
||||
source_root,
|
||||
/*move_untracked*/ false,
|
||||
)
|
||||
.await
|
||||
Ok(PreparedRemoteDirtyTransfer {
|
||||
move_plan: Some(RemoteMovePlan {
|
||||
transfer: plan,
|
||||
move_untracked: false,
|
||||
}),
|
||||
})
|
||||
}
|
||||
DirtyPolicy::MoveAll => {
|
||||
plan.apply_tracked_diff(runner, request_handle, worktree_root)
|
||||
.await?;
|
||||
plan.copy_untracked(request_handle, source_root, worktree_root)
|
||||
.await?;
|
||||
plan.clean_source_after_move(
|
||||
runner,
|
||||
request_handle,
|
||||
source_root,
|
||||
/*move_untracked*/ true,
|
||||
)
|
||||
.await
|
||||
Ok(PreparedRemoteDirtyTransfer {
|
||||
move_plan: Some(RemoteMovePlan {
|
||||
transfer: plan,
|
||||
move_untracked: true,
|
||||
}),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn finalize_dirty_policy_after_create(
|
||||
runner: &WorkspaceCommandRunner,
|
||||
request_handle: &AppServerRequestHandle,
|
||||
source_root: &Path,
|
||||
prepared: PreparedRemoteDirtyTransfer,
|
||||
) -> Result<()> {
|
||||
let Some(move_plan) = prepared.move_plan else {
|
||||
return Ok(());
|
||||
};
|
||||
move_plan
|
||||
.transfer
|
||||
.clean_source_after_move(
|
||||
runner,
|
||||
request_handle,
|
||||
source_root,
|
||||
move_plan.move_untracked,
|
||||
)
|
||||
.await
|
||||
.with_context(|| {
|
||||
"worktree already contains transferred changes, but failed to clean the source checkout after move"
|
||||
})
|
||||
}
|
||||
|
||||
async fn rollback_failed_create(
|
||||
runner: &WorkspaceCommandRunner,
|
||||
repo_root: &Path,
|
||||
worktree_git_root: &Path,
|
||||
branch: &str,
|
||||
delete_branch: bool,
|
||||
err: anyhow::Error,
|
||||
) -> anyhow::Error {
|
||||
let mut rollback_errors = Vec::new();
|
||||
let worktree_arg = worktree_git_root.to_string_lossy().to_string();
|
||||
if let Err(rollback_err) = git_status(
|
||||
runner,
|
||||
repo_root,
|
||||
&["worktree", "remove", "--force", &worktree_arg],
|
||||
)
|
||||
.await
|
||||
{
|
||||
rollback_errors.push(rollback_err.to_string());
|
||||
}
|
||||
if delete_branch
|
||||
&& let Err(rollback_err) = git_status(runner, repo_root, &["branch", "-D", branch]).await
|
||||
{
|
||||
rollback_errors.push(rollback_err.to_string());
|
||||
}
|
||||
if rollback_errors.is_empty() {
|
||||
err
|
||||
} else {
|
||||
anyhow::anyhow!(
|
||||
"{err}; additionally failed to roll back newly-created worktree: {}",
|
||||
rollback_errors.join("; ")
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
struct PreparedRemoteDirtyTransfer {
|
||||
move_plan: Option<RemoteMovePlan>,
|
||||
}
|
||||
|
||||
struct RemoteMovePlan {
|
||||
transfer: RemoteTransferPlan,
|
||||
move_untracked: bool,
|
||||
}
|
||||
|
||||
struct RemoteTransferPlan {
|
||||
staged_diff: String,
|
||||
unstaged_diff: String,
|
||||
@@ -534,8 +618,14 @@ struct RemoteTransferPlan {
|
||||
impl RemoteTransferPlan {
|
||||
async fn capture(runner: &WorkspaceCommandRunner, source_root: &Path) -> Result<Self> {
|
||||
Ok(Self {
|
||||
staged_diff: git_stdout(runner, source_root, &["diff", "--cached", "--binary"]).await?,
|
||||
unstaged_diff: git_stdout(runner, source_root, &["diff", "--binary"]).await?,
|
||||
staged_diff: git_stdout_raw_uncapped(
|
||||
runner,
|
||||
source_root,
|
||||
&["diff", "--cached", "--binary"],
|
||||
)
|
||||
.await?,
|
||||
unstaged_diff: git_stdout_raw_uncapped(runner, source_root, &["diff", "--binary"])
|
||||
.await?,
|
||||
tracked_paths: tracked_paths(runner, source_root).await?,
|
||||
untracked_paths: untracked_paths(runner, source_root).await?,
|
||||
})
|
||||
@@ -750,6 +840,23 @@ async fn git_stdout(runner: &WorkspaceCommandRunner, cwd: &Path, args: &[&str])
|
||||
Ok(output.stdout.trim_end().to_string())
|
||||
}
|
||||
|
||||
async fn git_stdout_raw_uncapped(
|
||||
runner: &WorkspaceCommandRunner,
|
||||
cwd: &Path,
|
||||
args: &[&str],
|
||||
) -> Result<String> {
|
||||
let argv = std::iter::once("git")
|
||||
.chain(args.iter().copied())
|
||||
.collect::<Vec<_>>();
|
||||
let output = runner
|
||||
.run(WorkspaceCommand::new(argv).cwd(cwd).disable_output_cap())
|
||||
.await?;
|
||||
if !output.success() {
|
||||
anyhow::bail!("git {} failed: {}", args.join(" "), output.stderr.trim());
|
||||
}
|
||||
Ok(output.stdout)
|
||||
}
|
||||
|
||||
async fn git_stdout_result(
|
||||
runner: &WorkspaceCommandRunner,
|
||||
cwd: &Path,
|
||||
@@ -866,3 +973,70 @@ async fn fs_remove(request_handle: &AppServerRequestHandle, path: &Path) -> Resu
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::future::Future;
|
||||
use std::path::PathBuf;
|
||||
use std::pin::Pin;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
use super::git_stdout_raw_uncapped;
|
||||
use crate::workspace_command::WorkspaceCommand;
|
||||
use crate::workspace_command::WorkspaceCommandError;
|
||||
use crate::workspace_command::WorkspaceCommandExecutor;
|
||||
use crate::workspace_command::WorkspaceCommandOutput;
|
||||
use crate::workspace_command::WorkspaceCommandRunner;
|
||||
|
||||
struct FakeRunner {
|
||||
seen: Mutex<Vec<WorkspaceCommand>>,
|
||||
output: WorkspaceCommandOutput,
|
||||
}
|
||||
|
||||
impl WorkspaceCommandExecutor for FakeRunner {
|
||||
fn run(
|
||||
&self,
|
||||
command: WorkspaceCommand,
|
||||
) -> Pin<
|
||||
Box<
|
||||
dyn Future<Output = Result<WorkspaceCommandOutput, WorkspaceCommandError>>
|
||||
+ Send
|
||||
+ '_,
|
||||
>,
|
||||
> {
|
||||
self.seen.lock().expect("seen lock").push(command);
|
||||
Box::pin(async move { Ok(self.output.clone()) })
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn raw_git_stdout_preserves_patch_newline_and_disables_output_cap() {
|
||||
let fake_runner = Arc::new(FakeRunner {
|
||||
seen: Mutex::new(Vec::new()),
|
||||
output: WorkspaceCommandOutput {
|
||||
exit_code: 0,
|
||||
stdout: "diff --git a/file b/file\n".to_string(),
|
||||
stderr: String::new(),
|
||||
},
|
||||
});
|
||||
let runner: WorkspaceCommandRunner = fake_runner.clone();
|
||||
|
||||
let output = git_stdout_raw_uncapped(
|
||||
&runner,
|
||||
&PathBuf::from("/repo"),
|
||||
&["diff", "--cached", "--binary"],
|
||||
)
|
||||
.await
|
||||
.expect("raw git stdout");
|
||||
|
||||
assert_eq!(output, "diff --git a/file b/file\n");
|
||||
let seen = fake_runner.seen.lock().expect("seen lock");
|
||||
assert_eq!(seen.len(), 1);
|
||||
assert_eq!(seen[0].argv, vec!["git", "diff", "--cached", "--binary"]);
|
||||
assert_eq!(seen[0].cwd, Some(PathBuf::from("/repo")));
|
||||
assert!(seen[0].disable_output_cap);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,17 @@ struct TransferPlan {
|
||||
untracked_paths: Vec<PathBuf>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct PreparedDirtyTransfer {
|
||||
move_plan: Option<MovePlan>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct MovePlan {
|
||||
transfer: TransferPlan,
|
||||
move_untracked: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct DirtyState {
|
||||
@@ -82,50 +93,68 @@ pub fn validate_dirty_policy_before_create(
|
||||
}
|
||||
}
|
||||
|
||||
pub fn apply_dirty_policy_after_create(
|
||||
pub(crate) fn prepare_dirty_policy_after_create(
|
||||
source_root: &Path,
|
||||
worktree_root: &Path,
|
||||
policy: DirtyPolicy,
|
||||
) -> Result<()> {
|
||||
) -> Result<PreparedDirtyTransfer> {
|
||||
let state = dirty_state(source_root)?;
|
||||
if !state.is_dirty() {
|
||||
return Ok(());
|
||||
return Ok(PreparedDirtyTransfer { move_plan: None });
|
||||
}
|
||||
|
||||
match policy {
|
||||
DirtyPolicy::Fail | DirtyPolicy::Ignore => Ok(()),
|
||||
DirtyPolicy::Fail | DirtyPolicy::Ignore => Ok(PreparedDirtyTransfer { move_plan: None }),
|
||||
DirtyPolicy::CopyTracked => {
|
||||
let plan = TransferPlan::capture(source_root)?;
|
||||
plan.apply_tracked_diff(worktree_root)
|
||||
plan.apply_tracked_diff(worktree_root)?;
|
||||
Ok(PreparedDirtyTransfer { move_plan: None })
|
||||
}
|
||||
DirtyPolicy::CopyAll => {
|
||||
let plan = TransferPlan::capture(source_root)?;
|
||||
plan.apply_tracked_diff(worktree_root)?;
|
||||
copy_untracked_files_at_paths(source_root, worktree_root, &plan.untracked_paths)?;
|
||||
Ok(())
|
||||
Ok(PreparedDirtyTransfer { move_plan: None })
|
||||
}
|
||||
DirtyPolicy::MoveTracked => {
|
||||
let plan = TransferPlan::capture(source_root)?;
|
||||
plan.apply_tracked_diff(worktree_root)?;
|
||||
plan.clean_source_after_move(source_root, /*move_untracked*/ false)
|
||||
.with_context(|| {
|
||||
"worktree already contains transferred changes, but failed to clean the source checkout after move"
|
||||
})?;
|
||||
Ok(())
|
||||
Ok(PreparedDirtyTransfer {
|
||||
move_plan: Some(MovePlan {
|
||||
transfer: plan,
|
||||
move_untracked: false,
|
||||
}),
|
||||
})
|
||||
}
|
||||
DirtyPolicy::MoveAll => {
|
||||
let plan = TransferPlan::capture(source_root)?;
|
||||
plan.apply_tracked_diff(worktree_root)?;
|
||||
copy_untracked_files_at_paths(source_root, worktree_root, &plan.untracked_paths)?;
|
||||
plan.clean_source_after_move(source_root, /*move_untracked*/ true)
|
||||
.with_context(|| {
|
||||
"worktree already contains transferred changes, but failed to clean the source checkout after move"
|
||||
})?;
|
||||
Ok(())
|
||||
Ok(PreparedDirtyTransfer {
|
||||
move_plan: Some(MovePlan {
|
||||
transfer: plan,
|
||||
move_untracked: true,
|
||||
}),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn finalize_dirty_policy_after_create(
|
||||
source_root: &Path,
|
||||
prepared: PreparedDirtyTransfer,
|
||||
) -> Result<()> {
|
||||
let Some(move_plan) = prepared.move_plan else {
|
||||
return Ok(());
|
||||
};
|
||||
move_plan
|
||||
.transfer
|
||||
.clean_source_after_move(source_root, move_plan.move_untracked)
|
||||
.with_context(|| {
|
||||
"worktree already contains transferred changes, but failed to clean the source checkout after move"
|
||||
})
|
||||
}
|
||||
|
||||
fn bail_for_dirty_source<T>() -> Result<T> {
|
||||
anyhow::bail!(
|
||||
"source checkout has uncommitted changes; use --worktree-dirty ignore, copy-tracked, copy-all, move-tracked, or move-all"
|
||||
|
||||
@@ -59,13 +59,14 @@ pub fn ensure_worktree(req: WorktreeRequest) -> Result<WorktreeResolution> {
|
||||
}
|
||||
|
||||
let warnings = dirty::validate_dirty_policy_before_create(&repo.root, req.dirty_policy)?;
|
||||
let branch_exists = branch_exists(&repo.root, &branch);
|
||||
let has_head = git::status(&repo.root, &["rev-parse", "--verify", "HEAD"]).is_ok();
|
||||
fs::create_dir_all(
|
||||
worktree_git_root
|
||||
.parent()
|
||||
.context("managed worktree path has no parent")?,
|
||||
)?;
|
||||
if branch_exists(&repo.root, &branch) {
|
||||
if branch_exists {
|
||||
git::status(
|
||||
&repo.root,
|
||||
&[
|
||||
@@ -102,7 +103,23 @@ pub fn ensure_worktree(req: WorktreeRequest) -> Result<WorktreeResolution> {
|
||||
)?;
|
||||
}
|
||||
|
||||
dirty::apply_dirty_policy_after_create(&repo.root, &worktree_git_root, req.dirty_policy)?;
|
||||
let prepared_transfer = match dirty::prepare_dirty_policy_after_create(
|
||||
&repo.root,
|
||||
&worktree_git_root,
|
||||
req.dirty_policy,
|
||||
) {
|
||||
Ok(prepared_transfer) => prepared_transfer,
|
||||
Err(err) => {
|
||||
return Err(rollback_failed_create(
|
||||
&repo.root,
|
||||
&worktree_git_root,
|
||||
&branch,
|
||||
/*delete_branch*/ !branch_exists,
|
||||
err,
|
||||
));
|
||||
}
|
||||
};
|
||||
dirty::finalize_dirty_policy_after_create(&repo.root, prepared_transfer)?;
|
||||
let dirty = dirty::dirty_state(&worktree_git_root)?;
|
||||
let head = git::stdout(&worktree_git_root, &["rev-parse", "HEAD"]).ok();
|
||||
let mut info = WorktreeInfo {
|
||||
@@ -137,6 +154,33 @@ pub fn ensure_worktree(req: WorktreeRequest) -> Result<WorktreeResolution> {
|
||||
})
|
||||
}
|
||||
|
||||
fn rollback_failed_create(
|
||||
repo_root: &Path,
|
||||
worktree_git_root: &Path,
|
||||
branch: &str,
|
||||
delete_branch: bool,
|
||||
err: anyhow::Error,
|
||||
) -> anyhow::Error {
|
||||
let mut rollback_errors = Vec::new();
|
||||
let worktree_arg = worktree_git_root.to_string_lossy();
|
||||
if let Err(rollback_err) =
|
||||
git::status(repo_root, &["worktree", "remove", "--force", &worktree_arg])
|
||||
{
|
||||
rollback_errors.push(rollback_err.to_string());
|
||||
}
|
||||
if delete_branch && let Err(rollback_err) = git::status(repo_root, &["branch", "-D", branch]) {
|
||||
rollback_errors.push(rollback_err.to_string());
|
||||
}
|
||||
if rollback_errors.is_empty() {
|
||||
err
|
||||
} else {
|
||||
anyhow::anyhow!(
|
||||
"{err}; additionally failed to roll back newly-created worktree: {}",
|
||||
rollback_errors.join("; ")
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn resolve_worktree(codex_home: &Path, cwd: &Path) -> Result<Option<WorktreeInfo>> {
|
||||
let Ok(root) = git::stdout(cwd, &["rev-parse", "--show-toplevel"]) else {
|
||||
return Ok(None);
|
||||
|
||||
@@ -250,6 +250,72 @@ fn move_tracked_transfers_tracked_state_and_leaves_untracked_files() -> anyhow::
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_transfer_rolls_back_new_worktree_and_branch() -> anyhow::Result<()> {
|
||||
let fixture = GitFixture::new_with_base_before_tracked_file()?;
|
||||
fs::write(fixture.repo.path().join("tracked.txt"), "changed\n")?;
|
||||
|
||||
let err = codex_worktree::ensure_worktree(WorktreeRequest {
|
||||
codex_home: fixture.codex_home.path().to_path_buf(),
|
||||
source_cwd: fixture.repo.path().to_path_buf(),
|
||||
branch: "rollback-created".to_string(),
|
||||
base_ref: Some("HEAD~1".to_string()),
|
||||
dirty_policy: DirtyPolicy::CopyTracked,
|
||||
})
|
||||
.expect_err("tracked patch should not apply against an older base");
|
||||
|
||||
assert!(
|
||||
err.to_string()
|
||||
.contains("failed to apply unstaged changes to worktree"),
|
||||
"{err:#}"
|
||||
);
|
||||
assert!(
|
||||
!fixture
|
||||
.repo
|
||||
.path()
|
||||
.with_file_name(format!(
|
||||
"{}.rollback-created",
|
||||
fixture.repo.path().file_name().unwrap().to_string_lossy()
|
||||
))
|
||||
.exists()
|
||||
);
|
||||
assert!(!branch_exists(fixture.repo.path(), "rollback-created")?);
|
||||
assert_eq!(
|
||||
git(fixture.repo.path(), &["status", "--short"])?,
|
||||
" M tracked.txt"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_transfer_preserves_preexisting_branch() -> anyhow::Result<()> {
|
||||
let fixture = GitFixture::new_with_base_before_tracked_file()?;
|
||||
run_git(fixture.repo.path(), &["branch", "keep-me", "HEAD~1"])?;
|
||||
fs::write(fixture.repo.path().join("tracked.txt"), "changed\n")?;
|
||||
|
||||
codex_worktree::ensure_worktree(WorktreeRequest {
|
||||
codex_home: fixture.codex_home.path().to_path_buf(),
|
||||
source_cwd: fixture.repo.path().to_path_buf(),
|
||||
branch: "keep-me".to_string(),
|
||||
base_ref: /*base_ref*/ None,
|
||||
dirty_policy: DirtyPolicy::CopyTracked,
|
||||
})
|
||||
.expect_err("tracked patch should not apply against the preexisting branch");
|
||||
|
||||
assert!(branch_exists(fixture.repo.path(), "keep-me")?);
|
||||
assert!(
|
||||
!fixture
|
||||
.repo
|
||||
.path()
|
||||
.with_file_name(format!(
|
||||
"{}.keep-me",
|
||||
fixture.repo.path().file_name().unwrap().to_string_lossy()
|
||||
))
|
||||
.exists()
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn creates_orphan_worktree_from_unborn_repo_without_base_ref() -> anyhow::Result<()> {
|
||||
let fixture = GitFixture::new_unborn()?;
|
||||
@@ -397,6 +463,19 @@ impl GitFixture {
|
||||
run_git(repo.path(), &["config", "user.name", "Codex"])?;
|
||||
Ok(Self { repo, codex_home })
|
||||
}
|
||||
|
||||
fn new_with_base_before_tracked_file() -> anyhow::Result<Self> {
|
||||
let repo = TempDir::new()?;
|
||||
let codex_home = TempDir::new()?;
|
||||
run_git(repo.path(), &["init", "-b", "main"])?;
|
||||
run_git(repo.path(), &["config", "user.email", "codex@example.com"])?;
|
||||
run_git(repo.path(), &["config", "user.name", "Codex"])?;
|
||||
run_git(repo.path(), &["commit", "--allow-empty", "-m", "base"])?;
|
||||
fs::write(repo.path().join("tracked.txt"), "original\n")?;
|
||||
run_git(repo.path(), &["add", "tracked.txt"])?;
|
||||
run_git(repo.path(), &["commit", "-m", "add tracked file"])?;
|
||||
Ok(Self { repo, codex_home })
|
||||
}
|
||||
}
|
||||
|
||||
fn run_git(cwd: &Path, args: &[&str]) -> anyhow::Result<()> {
|
||||
@@ -422,3 +501,16 @@ fn git(cwd: &Path, args: &[&str]) -> anyhow::Result<String> {
|
||||
}
|
||||
Ok(String::from_utf8(output.stdout)?.trim_end().to_string())
|
||||
}
|
||||
|
||||
fn branch_exists(cwd: &Path, branch: &str) -> anyhow::Result<bool> {
|
||||
let output = Command::new("git")
|
||||
.args([
|
||||
"show-ref",
|
||||
"--verify",
|
||||
"--quiet",
|
||||
&format!("refs/heads/{branch}"),
|
||||
])
|
||||
.current_dir(cwd)
|
||||
.output()?;
|
||||
Ok(output.status.success())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user