fix: is_inside_git_repo should take the directory as a param (#809)
https://github.com/openai/codex/pull/800 made `cwd` a property of `Config` and made it so the `cwd` is not necessarily `std::env::current_dir()`. As such, `is_inside_git_repo()` should check `Config.cwd` rather than `std::env::current_dir()`. This PR updates `is_inside_git_repo()` to take `Config` instead of an arbitrary `PathBuf` to force the check to operate on a `Config` where `cwd` has been resolved to what the user specified.
This commit is contained in:
@@ -5,6 +5,8 @@ use rand::Rng;
|
|||||||
use tokio::sync::Notify;
|
use tokio::sync::Notify;
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
|
||||||
|
use crate::config::Config;
|
||||||
|
|
||||||
const INITIAL_DELAY_MS: u64 = 200;
|
const INITIAL_DELAY_MS: u64 = 200;
|
||||||
const BACKOFF_FACTOR: f64 = 1.3;
|
const BACKOFF_FACTOR: f64 = 1.3;
|
||||||
|
|
||||||
@@ -33,26 +35,20 @@ pub(crate) fn backoff(attempt: u64) -> Duration {
|
|||||||
Duration::from_millis((base as f64 * jitter) as u64)
|
Duration::from_millis((base as f64 * jitter) as u64)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return `true` if the current working directory is inside a Git repository.
|
/// Return `true` if the project folder specified by the `Config` is inside a
|
||||||
|
/// Git repository.
|
||||||
///
|
///
|
||||||
/// The check walks up the directory hierarchy looking for a `.git` folder. This
|
/// The check walks up the directory hierarchy looking for a `.git` file or
|
||||||
|
/// directory (note `.git` can be a file that contains a `gitdir` entry). This
|
||||||
/// approach does **not** require the `git` binary or the `git2` crate and is
|
/// approach does **not** require the `git` binary or the `git2` crate and is
|
||||||
/// therefore fairly lightweight. It intentionally only looks for the
|
/// therefore fairly lightweight.
|
||||||
/// presence of a *directory* named `.git` – this is good enough for regular
|
|
||||||
/// work‑trees and bare repos that live inside a work‑tree (common for
|
|
||||||
/// developers running Codex locally).
|
|
||||||
///
|
///
|
||||||
/// Note that this does **not** detect *work‑trees* created with
|
/// Note that this does **not** detect *work‑trees* created with
|
||||||
/// `git worktree add` where the checkout lives outside the main repository
|
/// `git worktree add` where the checkout lives outside the main repository
|
||||||
/// directory. If you need Codex to work from such a checkout simply pass the
|
/// directory. If you need Codex to work from such a checkout simply pass the
|
||||||
/// `--allow-no-git-exec` CLI flag that disables the repo requirement.
|
/// `--allow-no-git-exec` CLI flag that disables the repo requirement.
|
||||||
pub fn is_inside_git_repo() -> bool {
|
pub fn is_inside_git_repo(config: &Config) -> bool {
|
||||||
// Best‑effort: any IO error is treated as "not a repo" – the caller can
|
let mut dir = config.cwd.to_path_buf();
|
||||||
// decide what to do with the result.
|
|
||||||
let mut dir = match std::env::current_dir() {
|
|
||||||
Ok(d) => d,
|
|
||||||
Err(_) => return false,
|
|
||||||
};
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if dir.join(".git").exists() {
|
if dir.join(".git").exists() {
|
||||||
|
|||||||
@@ -47,23 +47,6 @@ pub async fn run_main(cli: Cli) -> anyhow::Result<()> {
|
|||||||
|
|
||||||
assert_api_key(stderr_with_ansi);
|
assert_api_key(stderr_with_ansi);
|
||||||
|
|
||||||
if !skip_git_repo_check && !is_inside_git_repo() {
|
|
||||||
eprintln!("Not inside a Git repo and --skip-git-repo-check was not specified.");
|
|
||||||
std::process::exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO(mbolin): Take a more thoughtful approach to logging.
|
|
||||||
let default_level = "error";
|
|
||||||
let _ = tracing_subscriber::fmt()
|
|
||||||
.with_env_filter(
|
|
||||||
EnvFilter::try_from_default_env()
|
|
||||||
.or_else(|_| EnvFilter::try_new(default_level))
|
|
||||||
.unwrap(),
|
|
||||||
)
|
|
||||||
.with_ansi(stderr_with_ansi)
|
|
||||||
.with_writer(std::io::stderr)
|
|
||||||
.try_init();
|
|
||||||
|
|
||||||
let sandbox_policy = if full_auto {
|
let sandbox_policy = if full_auto {
|
||||||
Some(SandboxPolicy::new_full_auto_policy())
|
Some(SandboxPolicy::new_full_auto_policy())
|
||||||
} else {
|
} else {
|
||||||
@@ -85,6 +68,24 @@ pub async fn run_main(cli: Cli) -> anyhow::Result<()> {
|
|||||||
cwd: cwd.map(|p| p.canonicalize().unwrap_or(p)),
|
cwd: cwd.map(|p| p.canonicalize().unwrap_or(p)),
|
||||||
};
|
};
|
||||||
let config = Config::load_with_overrides(overrides)?;
|
let config = Config::load_with_overrides(overrides)?;
|
||||||
|
|
||||||
|
if !skip_git_repo_check && !is_inside_git_repo(&config) {
|
||||||
|
eprintln!("Not inside a Git repo and --skip-git-repo-check was not specified.");
|
||||||
|
std::process::exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO(mbolin): Take a more thoughtful approach to logging.
|
||||||
|
let default_level = "error";
|
||||||
|
let _ = tracing_subscriber::fmt()
|
||||||
|
.with_env_filter(
|
||||||
|
EnvFilter::try_from_default_env()
|
||||||
|
.or_else(|_| EnvFilter::try_new(default_level))
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.with_ansi(stderr_with_ansi)
|
||||||
|
.with_writer(std::io::stderr)
|
||||||
|
.try_init();
|
||||||
|
|
||||||
let (codex_wrapper, event, ctrl_c) = codex_wrapper::init_codex(config).await?;
|
let (codex_wrapper, event, ctrl_c) = codex_wrapper::init_codex(config).await?;
|
||||||
let codex = Arc::new(codex_wrapper);
|
let codex = Arc::new(codex_wrapper);
|
||||||
info!("Codex initialized with event: {event:?}");
|
info!("Codex initialized with event: {event:?}");
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ pub fn run_main(cli: Cli) -> std::io::Result<()> {
|
|||||||
// modal. The flag is shown when the current working directory is *not*
|
// modal. The flag is shown when the current working directory is *not*
|
||||||
// inside a Git repository **and** the user did *not* pass the
|
// inside a Git repository **and** the user did *not* pass the
|
||||||
// `--allow-no-git-exec` flag.
|
// `--allow-no-git-exec` flag.
|
||||||
let show_git_warning = !cli.skip_git_repo_check && !is_inside_git_repo();
|
let show_git_warning = !cli.skip_git_repo_check && !is_inside_git_repo(&config);
|
||||||
|
|
||||||
try_run_ratatui_app(cli, config, show_git_warning, log_rx);
|
try_run_ratatui_app(cli, config, show_git_warning, log_rx);
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Reference in New Issue
Block a user