@@ -69,9 +69,13 @@ pub fn set_project_active(
69
69
let err = anyhow:: Error :: from ( err) ;
70
70
if code == git2:: ErrorCode :: Owner {
71
71
err. context ( gitbutler_error:: error:: Code :: RepoOwnership )
72
- } else if code == git2:: ErrorCode :: NotFound {
73
- // When git2::Repository::open fails with NotFound, it likely means the directory
74
- // is not a git repository, so we attach our custom error code.
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
75
79
err. context ( gitbutler_error:: error:: Code :: NonGitRepository )
76
80
} else {
77
81
err
@@ -223,7 +227,7 @@ Ensure these aren't touched by GitButler or avoid using it in this repository.",
223
227
pub fn init_git_repository ( path : String ) -> Result < ( ) , Error > {
224
228
let repo_path = std:: path:: Path :: new ( & path) ;
225
229
226
- // Check if path exists and is a directory
230
+ // Validate path
227
231
if !repo_path. exists ( ) {
228
232
return Err ( anyhow:: anyhow!( "Path does not exist: {}" , path) . into ( ) ) ;
229
233
}
@@ -236,6 +240,14 @@ pub fn init_git_repository(path: String) -> Result<(), Error> {
236
240
return Err ( anyhow:: anyhow!( "Directory is already a Git repository" ) . into ( ) ) ;
237
241
}
238
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
+
239
251
// Initialize the repository
240
252
git2:: Repository :: init ( repo_path)
241
253
. with_context ( || format ! ( "Failed to initialize Git repository at {}" , path) ) ?;
0 commit comments