Skip to content

Commit cd0e2c1

Browse files
committed
refactor
1 parent 06ec50d commit cd0e2c1

File tree

5 files changed

+32
-90
lines changed

5 files changed

+32
-90
lines changed

apps/desktop/src/lib/error/knownErrors.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ export enum Code {
88
ProjectMissing = 'errors.projects.missing',
99
SecretKeychainNotFound = 'errors.secret.keychain_notfound',
1010
MissingLoginKeychain = 'errors.secret.missing_login_keychain',
11-
GitHubTokenExpired = 'errors.github.expired_token',
12-
NonGitRepository = 'errors.projects.not_git_repository'
11+
GitHubTokenExpired = 'errors.github.expired_token'
1312
}
1413

1514
export const KNOWN_ERRORS: Record<string, string> = {
@@ -35,8 +34,5 @@ With \`seahorse\` or equivalent, create a \`Login\` password store, right click
3534
`,
3635
[Code.GitHubTokenExpired]: `
3736
Your GitHub token appears expired, please check your settings!
38-
`,
39-
[Code.NonGitRepository]: `
40-
The selected directory is not a Git repository. Would you like to initialize a Git repository in this directory?
4137
`
4238
};

apps/desktop/src/lib/project/project.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { goto } from '$app/navigation';
2-
import { showToast } from '$lib/notifications/toasts';
2+
import { showError, showToast } from '$lib/notifications/toasts';
33
import { projectPath } from '$lib/routes/routes.svelte';
44
import { TestId } from '@gitbutler/ui';
55
import type { ForgeName } from '$lib/forge/interface/forge';
@@ -162,10 +162,32 @@ export function handleAddProjectOutcome(
162162
return true;
163163
case 'notAGitRepository':
164164
showToast({
165-
testId: TestId.AddProjectNotAGitRepoModal,
166-
style: 'warning',
167165
title: 'Not a Git repository',
168-
message: `Unable to add project: ${outcome.subject}`
166+
message:
167+
'The selected directory is not a Git repository. Would you like to initialize one?',
168+
style: 'warning',
169+
extraAction: {
170+
label: 'Initialize Repository',
171+
onClick: async (dismiss) => {
172+
try {
173+
const projectPath = 'get this - needs a refactor probably';
174+
await projectsService.initGitRepository(projectPath);
175+
dismiss();
176+
showToast({
177+
title: 'Repository Initialized',
178+
message: `Git repository has been successfully initialized at ${projectPath}. Loading project...`,
179+
style: 'info'
180+
});
181+
const projectId = 'TODO: get this from somewhere';
182+
// Retry setting the project active
183+
await setActiveProjectOrRedirect(projectId);
184+
} catch (initError) {
185+
console.error('Failed to initialize repository:', initError);
186+
dismiss();
187+
showError('Failed to initialize repository', initError);
188+
}
189+
}
190+
}
169191
});
170192
return true;
171193
}

apps/desktop/src/routes/[projectId]/+layout.svelte

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import ReduxResult from '$components/ReduxResult.svelte';
1313
import { OnboardingEvent, POSTHOG_WRAPPER } from '$lib/analytics/posthog';
1414
import { BACKEND } from '$lib/backend';
15-
import { getUserErrorCode, Code } from '$lib/backend/ipc';
1615
import { BASE_BRANCH_SERVICE } from '$lib/baseBranch/baseBranchService.svelte';
1716
import { BRANCH_SERVICE } from '$lib/branches/branchService.svelte';
1817
import { SETTINGS_SERVICE } from '$lib/config/appSettingsV2';
@@ -23,7 +22,7 @@ import { getUserErrorCode, Code } from '$lib/backend/ipc';
2322
import { GITLAB_STATE } from '$lib/forge/gitlab/gitlabState.svelte';
2423
import { GIT_SERVICE } from '$lib/git/gitService';
2524
import { MODE_SERVICE } from '$lib/mode/modeService';
26-
import { showError, showInfo, showWarning, showToast } from '$lib/notifications/toasts';
25+
import { showError, showInfo, showWarning } from '$lib/notifications/toasts';
2726
import { PROJECTS_SERVICE } from '$lib/project/projectsService';
2827
import { FILE_SELECTION_MANAGER } from '$lib/selection/fileSelectionManager.svelte';
2928
import { UNCOMMITTED_SERVICE } from '$lib/selection/uncommittedService.svelte';
@@ -321,44 +320,7 @@ import { getUserErrorCode, Code } from '$lib/backend/ipc';
321320
}
322321
} catch (error: unknown) {
323322
posthog.captureOnboarding(OnboardingEvent.SetProjectActiveFailed);
324-
325-
// Check if this is a non-git repository error
326-
const errorCode = getUserErrorCode(error);
327-
if (errorCode === Code.NonGitRepository) {
328-
// Show special toast with git init option
329-
showToast({
330-
title: 'Not a Git repository',
331-
message: 'The selected directory is not a Git repository. Would you like to initialize one?',
332-
style: 'warning',
333-
extraAction: {
334-
label: 'Initialize Repository',
335-
onClick: async (dismiss) => {
336-
try {
337-
const currentProject = projects?.find((p) => p.id === projectId);
338-
if (currentProject?.path) {
339-
await projectsService.initGitRepository(currentProject.path);
340-
dismiss();
341-
showToast({
342-
title: 'Repository Initialized',
343-
message: `Git repository has been successfully initialized at ${currentProject.path}. Loading project...`,
344-
style: 'info'
345-
});
346-
// Retry setting the project active
347-
await setActiveProjectOrRedirect(projectId);
348-
} else {
349-
throw new Error('Could not find project path');
350-
}
351-
} catch (initError) {
352-
console.error('Failed to initialize repository:', initError);
353-
dismiss();
354-
showError('Failed to initialize repository', initError);
355-
}
356-
}
357-
}
358-
});
359-
} else {
360-
showError('Failed to set the project active', error);
361-
}
323+
showError('Failed to set the project active', error);
362324
}
363325
}
364326

crates/gitbutler-error/src/error.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,6 @@ pub enum Code {
139139
SecretKeychainNotFound,
140140
MissingLoginKeychain,
141141
GitForcePushProtection,
142-
/// When trying to open a project that is not in a git repository,
143-
/// this error code is used to trigger special handling in the frontend.
144-
/// The frontend will show a modal offering to initialize a git repository.
145-
NonGitRepository,
146142
}
147143

148144
impl std::fmt::Display for Code {
@@ -161,7 +157,6 @@ impl std::fmt::Display for Code {
161157
Code::SecretKeychainNotFound => "errors.secret.keychain_notfound",
162158
Code::MissingLoginKeychain => "errors.secret.missing_login_keychain",
163159
Code::GitForcePushProtection => "errors.git.force_push_protection",
164-
Code::NonGitRepository => "errors.projects.not_git_repository",
165160
};
166161
f.write_str(code)
167162
}

crates/gitbutler-tauri/src/projects.rs

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,6 @@ pub fn set_project_active(
6969
let err = anyhow::Error::from(err);
7070
if code == git2::ErrorCode::Owner {
7171
err.context(gitbutler_error::error::Code::RepoOwnership)
72-
} else if code == git2::ErrorCode::NotFound ||
73-
code == git2::ErrorCode::Invalid ||
74-
code == git2::ErrorCode::Config {
75-
// Common error codes when a directory is not a git repository:
76-
// - NotFound: .git directory or repository not found
77-
// - Invalid: Invalid repository structure
78-
// - Config: Repository configuration issues
79-
err.context(gitbutler_error::error::Code::NonGitRepository)
8072
} else {
8173
err
8274
}
@@ -225,33 +217,8 @@ Ensure these aren't touched by GitButler or avoid using it in this repository.",
225217
#[tauri::command]
226218
#[instrument(err(Debug))]
227219
pub fn init_git_repository(path: String) -> Result<(), Error> {
228-
let repo_path = std::path::Path::new(&path);
229-
230-
// Validate path
231-
if !repo_path.exists() {
232-
return Err(anyhow::anyhow!("Path does not exist: {}", path).into());
233-
}
234-
if !repo_path.is_dir() {
235-
return Err(anyhow::anyhow!("Path is not a directory: {}", path).into());
236-
}
237-
238-
// Check if it's already a git repository
239-
if git2::Repository::open(repo_path).is_ok() {
240-
return Err(anyhow::anyhow!("Directory is already a Git repository").into());
241-
}
242-
243-
// Check if directory is writable
244-
let temp_file = repo_path.join(".gitbutler_write_test");
245-
if let Err(_) = std::fs::write(&temp_file, "test") {
246-
return Err(anyhow::anyhow!("Directory is not writable: {}", path).into());
247-
}
248-
// Clean up test file
249-
let _ = std::fs::remove_file(&temp_file);
250-
251-
// Initialize the repository
252-
git2::Repository::init(repo_path)
253-
.with_context(|| format!("Failed to initialize Git repository at {}", path))?;
254-
255-
tracing::info!("Successfully initialized Git repository at {}", path);
220+
let path: PathBuf = path.into();
221+
git2::Repository::init(&path)
222+
.with_context(|| format!("Failed to initialize Git repository at {}", path.display()))?;
256223
Ok(())
257224
}

0 commit comments

Comments
 (0)