Skip to content

Commit e442ece

Browse files
1 parent 3f8d602 commit e442ece

File tree

84 files changed

+2881
-2152
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+2881
-2152
lines changed

codex-rs/Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

codex-rs/apply-patch/src/lib.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ pub enum ApplyPatchFileChange {
116116
Add {
117117
content: String,
118118
},
119-
Delete,
119+
Delete {
120+
content: String,
121+
},
120122
Update {
121123
unified_diff: String,
122124
move_path: Option<PathBuf>,
@@ -210,7 +212,18 @@ pub fn maybe_parse_apply_patch_verified(argv: &[String], cwd: &Path) -> MaybeApp
210212
changes.insert(path, ApplyPatchFileChange::Add { content: contents });
211213
}
212214
Hunk::DeleteFile { .. } => {
213-
changes.insert(path, ApplyPatchFileChange::Delete);
215+
let content = match std::fs::read_to_string(&path) {
216+
Ok(content) => content,
217+
Err(e) => {
218+
return MaybeApplyPatchVerified::CorrectnessError(
219+
ApplyPatchError::IoError(IoError {
220+
context: format!("Failed to read {}", path.display()),
221+
source: e,
222+
}),
223+
);
224+
}
225+
};
226+
changes.insert(path, ApplyPatchFileChange::Delete { content });
214227
}
215228
Hunk::UpdateFile {
216229
move_path, chunks, ..

codex-rs/core/src/apply_patch.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ pub(crate) fn convert_apply_patch_to_protocol(
109109
ApplyPatchFileChange::Add { content } => FileChange::Add {
110110
content: content.clone(),
111111
},
112-
ApplyPatchFileChange::Delete => FileChange::Delete,
112+
ApplyPatchFileChange::Delete { content } => FileChange::Delete {
113+
content: content.clone(),
114+
},
113115
ApplyPatchFileChange::Update {
114116
unified_diff,
115117
move_path,

codex-rs/core/src/git_info.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,35 @@ use tokio::process::Command;
1010
use tokio::time::Duration as TokioDuration;
1111
use tokio::time::timeout;
1212

13-
use crate::util::is_inside_git_repo;
13+
/// Return `true` if the project folder specified by the `Config` is inside a
14+
/// Git repository.
15+
///
16+
/// The check walks up the directory hierarchy looking for a `.git` file or
17+
/// directory (note `.git` can be a file that contains a `gitdir` entry). This
18+
/// approach does **not** require the `git` binary or the `git2` crate and is
19+
/// therefore fairly lightweight.
20+
///
21+
/// Note that this does **not** detect *work‑trees* created with
22+
/// `git worktree add` where the checkout lives outside the main repository
23+
/// directory. If you need Codex to work from such a checkout simply pass the
24+
/// `--allow-no-git-exec` CLI flag that disables the repo requirement.
25+
pub fn get_git_repo_root(base_dir: &Path) -> Option<PathBuf> {
26+
let mut dir = base_dir.to_path_buf();
27+
28+
loop {
29+
if dir.join(".git").exists() {
30+
return Some(dir);
31+
}
32+
33+
// Pop one component (go up one directory). `pop` returns false when
34+
// we have reached the filesystem root.
35+
if !dir.pop() {
36+
break;
37+
}
38+
}
39+
40+
None
41+
}
1442

1543
/// Timeout for git commands to prevent freezing on large repositories
1644
const GIT_COMMAND_TIMEOUT: TokioDuration = TokioDuration::from_secs(5);
@@ -94,9 +122,7 @@ pub async fn collect_git_info(cwd: &Path) -> Option<GitInfo> {
94122

95123
/// Returns the closest git sha to HEAD that is on a remote as well as the diff to that sha.
96124
pub async fn git_diff_to_remote(cwd: &Path) -> Option<GitDiffToRemote> {
97-
if !is_inside_git_repo(cwd) {
98-
return None;
99-
}
125+
get_git_repo_root(cwd)?;
100126

101127
let remotes = get_git_remotes(cwd).await?;
102128
let branches = branch_ancestry(cwd).await?;
@@ -440,7 +466,7 @@ async fn diff_against_sha(cwd: &Path, sha: &GitSha) -> Option<String> {
440466
}
441467

442468
/// Resolve the path that should be used for trust checks. Similar to
443-
/// `[utils::is_inside_git_repo]`, but resolves to the root of the main
469+
/// `[get_git_repo_root]`, but resolves to the root of the main
444470
/// repository. Handles worktrees.
445471
pub fn resolve_root_git_project_for_trust(cwd: &Path) -> Option<PathBuf> {
446472
let base = if cwd.is_dir() { cwd } else { cwd.parent()? };

0 commit comments

Comments
 (0)