Skip to content

Commit 6581afb

Browse files
tspythonclaude
andcommitted
Prompt for a repo on startup instead of crashing when none is found
Launching the .app from Finder runs with the working directory set to `/`, so `GitModel::open()` finds nothing and — on a machine with no recent-repo history — the app exited with a console-only error the user never sees. Now fall back to a native folder picker (and an error dialog for non-repo selections); cancelling exits quietly. Also let a bogus repo path argument fall through to the picker rather than aborting. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 76dbf62 commit 6581afb

1 file changed

Lines changed: 61 additions & 19 deletions

File tree

src/main.rs

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -92,29 +92,71 @@ fn apply_theme_selection() -> anyhow::Result<()> {
9292

9393
fn open_startup_repo() -> anyhow::Result<GitModel> {
9494
if let Some(path) = repo_arg() {
95-
return open_and_remember(path);
95+
match open_and_remember(&path) {
96+
Ok(git) => return Ok(git),
97+
Err(err) => {
98+
eprintln!("{err}");
99+
// Fall through to the picker rather than aborting — the
100+
// path arg may be bogus (e.g. a Finder `-psn` token).
101+
}
102+
}
96103
}
97104

98-
match GitModel::open() {
99-
Ok(git) => {
100-
let _ = repo_store::remember_repo(git.repo_root());
101-
Ok(git)
105+
if let Ok(git) = GitModel::open() {
106+
let _ = repo_store::remember_repo(git.repo_root());
107+
return Ok(git);
108+
}
109+
110+
for path in repo_store::recent_repos().unwrap_or_default() {
111+
if let Ok(git) = open_and_remember(&path) {
112+
eprintln!(
113+
"Opened recent repository {} (current directory is not a git repo)",
114+
git.repo_root().display()
115+
);
116+
return Ok(git);
102117
}
103-
Err(current_dir_err) => {
104-
let recent = repo_store::recent_repos().unwrap_or_default();
105-
for path in recent {
106-
if let Ok(git) = open_and_remember(path.clone()) {
107-
eprintln!(
108-
"Opened recent repository {} after current directory lookup failed: {}",
109-
git.repo_root().display(),
110-
current_dir_err
111-
);
112-
return Ok(git);
113-
}
114-
}
118+
}
119+
120+
// No repo from CWD or history — common when launched from Finder,
121+
// where the working directory is `/`. Ask the user to pick one
122+
// instead of crashing with a console-only error.
123+
prompt_for_repo()
124+
}
115125

116-
Err(current_dir_err)
117-
.context("not in a git repository and no recent repository could be reopened")
126+
/// Show a native folder picker until the user selects a valid git
127+
/// repository, or cancels (in which case we exit quietly).
128+
fn prompt_for_repo() -> anyhow::Result<GitModel> {
129+
let start_dir = env::var_os("HOME")
130+
.map(PathBuf::from)
131+
.unwrap_or_else(|| PathBuf::from("/"));
132+
133+
loop {
134+
let picked = rfd::FileDialog::new()
135+
.set_title("wgit — Open a Git Repository")
136+
.set_directory(&start_dir)
137+
.pick_folder();
138+
139+
let Some(path) = picked else {
140+
// User cancelled — nothing to open.
141+
std::process::exit(0);
142+
};
143+
144+
match GitModel::open_at(&path) {
145+
Ok(git) => {
146+
let _ = repo_store::remember_repo(git.repo_root());
147+
return Ok(git);
148+
}
149+
Err(err) => {
150+
rfd::MessageDialog::new()
151+
.set_level(rfd::MessageLevel::Warning)
152+
.set_title("Not a Git repository")
153+
.set_description(format!(
154+
"{} couldn't be opened as a git repository.\n\n{err}",
155+
path.display()
156+
))
157+
.set_buttons(rfd::MessageButtons::Ok)
158+
.show();
159+
}
118160
}
119161
}
120162
}

0 commit comments

Comments
 (0)