Skip to content

Commit aef85f8

Browse files
committed
fix(cli): /diff shows clear error when not in a git repo
Previously claw --resume <session> /diff would produce: 'git diff --cached failed: error: unknown option `cached\'' when the CWD was not inside a git project, because git falls back to --no-index mode which does not support --cached. Two fixes: 1. render_diff_report_for() checks 'git rev-parse --is-inside-work-tree' before running git diff, and returns a human-readable message if not in a git repo: 'Diff\n Result no git repository\n Detail <cwd> is not inside a git project' 2. resume /diff now uses std::env::current_dir() instead of the session file's parent directory as the CWD for the diff (session parent dir is the .claw/sessions/<id>/ directory, never a git repo). 159 CLI tests pass, fmt clean.
1 parent 3ed27d5 commit aef85f8

File tree

1 file changed

+16
-1
lines changed
  • rust/crates/rusty-claude-cli/src

1 file changed

+16
-1
lines changed

rust/crates/rusty-claude-cli/src/main.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2708,7 +2708,7 @@ fn run_resume_command(
27082708
SlashCommand::Diff => Ok(ResumeCommandOutcome {
27092709
session: session.clone(),
27102710
message: Some(render_diff_report_for(
2711-
session_path.parent().unwrap_or_else(|| Path::new(".")),
2711+
&std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")),
27122712
)?),
27132713
json: None,
27142714
}),
@@ -5242,6 +5242,21 @@ fn render_diff_report() -> Result<String, Box<dyn std::error::Error>> {
52425242
}
52435243

52445244
fn render_diff_report_for(cwd: &Path) -> Result<String, Box<dyn std::error::Error>> {
5245+
// Verify we are inside a git repository before calling `git diff`.
5246+
// Running `git diff --cached` outside a git tree produces a misleading
5247+
// "unknown option `cached`" error because git falls back to --no-index mode.
5248+
let in_git_repo = std::process::Command::new("git")
5249+
.args(["rev-parse", "--is-inside-work-tree"])
5250+
.current_dir(cwd)
5251+
.output()
5252+
.map(|o| o.status.success())
5253+
.unwrap_or(false);
5254+
if !in_git_repo {
5255+
return Ok(format!(
5256+
"Diff\n Result no git repository\n Detail {} is not inside a git project",
5257+
cwd.display()
5258+
));
5259+
}
52455260
let staged = run_git_diff_command_in(cwd, &["diff", "--cached"])?;
52465261
let unstaged = run_git_diff_command_in(cwd, &["diff"])?;
52475262
if staged.trim().is_empty() && unstaged.trim().is_empty() {

0 commit comments

Comments
 (0)