Skip to content

Commit 5ee8605

Browse files
unhappychoiceclaude
andcommitted
fix: detect git repository from subdirectories
Add find_git_root helper to traverse parent directories when looking for .git directory. This fixes the issue where running gitlogue from a subdirectory would fail with "Not a Git repository" error. Closes #75 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 64f170c commit 5ee8605

1 file changed

Lines changed: 31 additions & 13 deletions

File tree

src/main.rs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use anyhow::{Context, Result};
1111
use clap::{Parser, Subcommand, ValueEnum};
1212
use config::Config;
1313
use git::GitRepository;
14-
use std::path::PathBuf;
14+
use std::path::{Path, PathBuf};
1515
use theme::Theme;
1616
use ui::UI;
1717

@@ -118,23 +118,41 @@ pub enum ThemeCommands {
118118

119119
impl Args {
120120
pub fn validate(&self) -> Result<PathBuf> {
121-
let repo_path = self.path.clone().unwrap_or_else(|| PathBuf::from("."));
121+
let start_path = self.path.clone().unwrap_or_else(|| PathBuf::from("."));
122122

123-
if !repo_path.exists() {
124-
anyhow::bail!("Path does not exist: {}", repo_path.display());
123+
if !start_path.exists() {
124+
anyhow::bail!("Path does not exist: {}", start_path.display());
125125
}
126126

127-
let git_dir = repo_path.join(".git");
128-
if !git_dir.exists() {
129-
anyhow::bail!(
127+
let canonical_path = start_path
128+
.canonicalize()
129+
.context("Failed to resolve path")?;
130+
131+
let repo_path = Self::find_git_root(&canonical_path).ok_or_else(|| {
132+
anyhow::anyhow!(
130133
"Not a Git repository: {} (or any parent directories)",
131-
repo_path.display()
132-
);
133-
}
134+
start_path.display()
135+
)
136+
})?;
134137

135-
repo_path
136-
.canonicalize()
137-
.context("Failed to resolve repository path")
138+
Ok(repo_path)
139+
}
140+
141+
fn find_git_root(start_path: &Path) -> Option<PathBuf> {
142+
let mut current = if start_path.is_file() {
143+
start_path.parent()?.to_path_buf()
144+
} else {
145+
start_path.to_path_buf()
146+
};
147+
148+
loop {
149+
if current.join(".git").exists() {
150+
return Some(current);
151+
}
152+
if !current.pop() {
153+
return None;
154+
}
155+
}
138156
}
139157
}
140158

0 commit comments

Comments
 (0)