diff --git a/crates/gitbutler-branch-actions/tests/virtual_branches/init.rs b/crates/gitbutler-branch-actions/tests/virtual_branches/init.rs index acc3a107f8..09c198b65c 100644 --- a/crates/gitbutler-branch-actions/tests/virtual_branches/init.rs +++ b/crates/gitbutler-branch-actions/tests/virtual_branches/init.rs @@ -9,7 +9,7 @@ fn twice() { { let project = projects - .add(test_project.path()) + .add(test_project.path(), None, None) .expect("failed to add project"); let ctx = CommandContext::open(&project, AppSettings::default()).unwrap(); @@ -27,7 +27,7 @@ fn twice() { } { - let project = projects.add(test_project.path()).unwrap(); + let project = projects.add(test_project.path(), None, None).unwrap(); let ctx = CommandContext::open(&project, AppSettings::default()).unwrap(); gitbutler_branch_actions::set_base_branch( &ctx, diff --git a/crates/gitbutler-branch-actions/tests/virtual_branches/mod.rs b/crates/gitbutler-branch-actions/tests/virtual_branches/mod.rs index f312aef153..15e8949556 100644 --- a/crates/gitbutler-branch-actions/tests/virtual_branches/mod.rs +++ b/crates/gitbutler-branch-actions/tests/virtual_branches/mod.rs @@ -34,7 +34,7 @@ impl Default for Test { let test_project = TestProject::default(); let project = projects - .add(test_project.path()) + .add(test_project.path(), None, None) .expect("failed to add project"); let ctx = CommandContext::open(&project, AppSettings::default()).unwrap(); diff --git a/crates/gitbutler-cli/src/command/project.rs b/crates/gitbutler-cli/src/command/project.rs index 6c9a8fec67..55ddc5bcc2 100644 --- a/crates/gitbutler-cli/src/command/project.rs +++ b/crates/gitbutler-cli/src/command/project.rs @@ -30,7 +30,7 @@ pub fn add( .context("Only non-bare repositories can be added")? .to_owned() .canonicalize()?; - let project = ctrl.add(path)?; + let project = ctrl.add(path, None, None)?; let ctx = CommandContext::open(&project, AppSettings::default())?; if let Some(refname) = refname { gitbutler_branch_actions::set_base_branch(&ctx, &refname)?; diff --git a/crates/gitbutler-project/src/controller.rs b/crates/gitbutler-project/src/controller.rs index ec1af7a196..04e6b1c7f4 100644 --- a/crates/gitbutler-project/src/controller.rs +++ b/crates/gitbutler-project/src/controller.rs @@ -21,7 +21,12 @@ impl Controller { } } - pub fn add>(&self, path: P) -> Result { + pub fn add>( + &self, + path: P, + name: Option, + email: Option, + ) -> Result { let path = path.as_ref(); let all_projects = self .projects_storage @@ -86,6 +91,21 @@ impl Controller { tracing::error!(project_id = %project.id, ?error, "failed to create {:?} on project add", project.gb_dir()); } + let repo = git2::Repository::open(&project.path)?; + let config = repo.config()?; + + let name_key = "user.name"; + if config.get_entry(name_key).map_or(true, |e| !e.has_value()) { + let mut local = config.open_level(git2::ConfigLevel::Local)?; + local.set_str(name_key, &name.unwrap_or("Firstname Lastname".to_string()))?; + } + + let email_key = "user.email"; + if config.get_entry(email_key).map_or(true, |e| !e.has_value()) { + let mut local = config.open_level(git2::ConfigLevel::Local)?; + local.set_str(email_key, &email.unwrap_or("name@example.com".to_string()))?; + } + Ok(project) } diff --git a/crates/gitbutler-project/tests/projects/main.rs b/crates/gitbutler-project/tests/projects/main.rs index 84e4d16ccb..f220829227 100644 --- a/crates/gitbutler-project/tests/projects/main.rs +++ b/crates/gitbutler-project/tests/projects/main.rs @@ -16,7 +16,7 @@ mod add { let (controller, _tmp) = new(); let repository = gitbutler_testsupport::TestProject::default(); let path = repository.path(); - let project = controller.add(path).unwrap(); + let project = controller.add(path, None, None).unwrap(); assert_eq!(project.path, path); assert_eq!( project.title, @@ -32,7 +32,7 @@ mod add { fn non_bare_without_worktree() { let (controller, _tmp) = new(); let root = repo_path_at("non-bare-without-worktree"); - let err = controller.add(root).unwrap_err(); + let err = controller.add(root, None, None).unwrap_err(); assert_eq!( err.to_string(), "Cannot add non-bare repositories without a workdir" @@ -43,7 +43,7 @@ mod add { fn submodule() { let (controller, _tmp) = new(); let root = repo_path_at("with-submodule").join("submodule"); - let err = controller.add(root).unwrap_err(); + let err = controller.add(root, None, None).unwrap_err(); assert_eq!( err.to_string(), "A git-repository without a `.git` directory cannot currently be added" @@ -56,7 +56,7 @@ mod add { let tmp = tempfile::tempdir().unwrap(); assert_eq!( controller - .add(tmp.path().join("missing")) + .add(tmp.path().join("missing"), None, None) .unwrap_err() .to_string(), "path not found" @@ -70,7 +70,7 @@ mod add { let path = tmp.path(); std::fs::write(path.join("file.txt"), "hello world").unwrap(); assert_eq!( - controller.add(path).unwrap_err().to_string(), + controller.add(path, None, None).unwrap_err().to_string(), "must be a Git repository" ); } @@ -79,7 +79,7 @@ mod add { fn empty() { let (controller, _tmp) = new(); let tmp = tempfile::tempdir().unwrap(); - let err = controller.add(tmp.path()).unwrap_err(); + let err = controller.add(tmp.path(), None, None).unwrap_err(); assert_eq!(err.to_string(), "must be a Git repository"); } @@ -88,9 +88,9 @@ mod add { let (controller, _tmp) = new(); let repository = gitbutler_testsupport::TestProject::default(); let path = repository.path(); - controller.add(path).unwrap(); + controller.add(path, None, None).unwrap(); assert_eq!( - controller.add(path).unwrap_err().to_string(), + controller.add(path, None, None).unwrap_err().to_string(), "project already exists" ); } @@ -104,7 +104,7 @@ mod add { let repo = git2::Repository::init_bare(&repo_dir).unwrap(); create_initial_commit(&repo); - let err = controller.add(repo_dir).unwrap_err(); + let err = controller.add(repo_dir, None, None).unwrap_err(); assert_eq!(err.to_string(), "bare repositories are unsupported"); } @@ -119,7 +119,7 @@ mod add { create_initial_commit(&repo); let worktree = repo.worktree("feature", &worktree_dir, None).unwrap(); - let err = controller.add(worktree.path()).unwrap_err(); + let err = controller.add(worktree.path(), None, None).unwrap_err(); assert_eq!(err.to_string(), "can only work in main worktrees"); } @@ -157,7 +157,7 @@ mod delete { let (controller, _tmp) = new(); let repository = gitbutler_testsupport::TestProject::default(); let path = repository.path(); - let project = controller.add(path).unwrap(); + let project = controller.add(path, None, None).unwrap(); assert!(controller.delete(project.id).is_ok()); assert!(controller.delete(project.id).is_ok()); // idempotent assert!(controller.get(project.id).is_err()); diff --git a/crates/gitbutler-stack/src/stack.rs b/crates/gitbutler-stack/src/stack.rs index 455d138c70..ea2a8f5896 100644 --- a/crates/gitbutler-stack/src/stack.rs +++ b/crates/gitbutler-stack/src/stack.rs @@ -368,8 +368,10 @@ impl Stack { self.name.clone() } else if let Some(refname) = self.upstream.as_ref() { refname.branch().to_string() + } else if let Ok((author, _committer)) = ctx.repo().signatures() { + generate_branch_name(author)? } else { - let (author, _committer) = ctx.repo().signatures()?; + let author = git2::Signature::now("Firstname Lastname", "name@example.com")?; generate_branch_name(author)? }; diff --git a/crates/gitbutler-tauri/src/projects.rs b/crates/gitbutler-tauri/src/projects.rs index 557c4920fa..4a980515aa 100644 --- a/crates/gitbutler-tauri/src/projects.rs +++ b/crates/gitbutler-tauri/src/projects.rs @@ -21,12 +21,16 @@ pub mod commands { } #[tauri::command(async)] - #[instrument(skip(projects), err(Debug))] + #[instrument(skip(projects, users), err(Debug))] pub fn add_project( projects: State<'_, Controller>, + users: State<'_, gitbutler_user::Controller>, path: &path::Path, ) -> Result { - Ok(projects.add(path)?) + let user = users.get_user()?; + let name = user.as_ref().and_then(|u| u.name.clone()); + let email = user.as_ref().and_then(|u| u.email.clone()); + Ok(projects.add(path, name, email)?) } #[tauri::command(async)] diff --git a/crates/gitbutler-testsupport/src/lib.rs b/crates/gitbutler-testsupport/src/lib.rs index 1f709df88e..8d4051735c 100644 --- a/crates/gitbutler-testsupport/src/lib.rs +++ b/crates/gitbutler-testsupport/src/lib.rs @@ -201,7 +201,11 @@ pub mod read_only { // Assure the project is valid the first time. let project = if was_inserted { let tmp = tempfile::TempDir::new()?; - gitbutler_project::Controller::from_path(tmp.path()).add(project_worktree_dir)? + gitbutler_project::Controller::from_path(tmp.path()).add( + project_worktree_dir, + None, + None, + )? } else { Project { id: ProjectId::generate(), diff --git a/crates/gitbutler-testsupport/src/suite.rs b/crates/gitbutler-testsupport/src/suite.rs index d064d3e1c0..f0ab0a8f8f 100644 --- a/crates/gitbutler-testsupport/src/suite.rs +++ b/crates/gitbutler-testsupport/src/suite.rs @@ -72,7 +72,7 @@ impl Suite { ( self.projects - .add(repository.path().parent().unwrap()) + .add(repository.path().parent().unwrap(), None, None) .expect("failed to add project"), tmp, ) diff --git a/crates/gitbutler-workspace/tests/branch_trees.rs b/crates/gitbutler-workspace/tests/branch_trees.rs index 35c2c318cf..ad6a833f66 100644 --- a/crates/gitbutler-workspace/tests/branch_trees.rs +++ b/crates/gitbutler-workspace/tests/branch_trees.rs @@ -44,7 +44,7 @@ mod compute_updated_branch_head { let data_dir = gitbutler_testsupport::paths::data_dir(); let projects = gitbutler_project::Controller::from_path(data_dir.path()); let project = projects - .add(test_repository.repository.path()) + .add(test_repository.repository.path(), None, None) .expect("failed to add project"); let ctx = CommandContext::open(&project, but_settings::AppSettings::default()).unwrap(); set_test_target(&ctx).unwrap(); @@ -77,7 +77,7 @@ mod compute_updated_branch_head { let data_dir = gitbutler_testsupport::paths::data_dir(); let projects = gitbutler_project::Controller::from_path(data_dir.path()); let project = projects - .add(test_repository.repository.path()) + .add(test_repository.repository.path(), None, None) .expect("failed to add project"); let ctx = CommandContext::open(&project, but_settings::AppSettings::default()).unwrap(); set_test_target(&ctx).unwrap(); @@ -115,7 +115,7 @@ mod compute_updated_branch_head { let data_dir = gitbutler_testsupport::paths::data_dir(); let projects = gitbutler_project::Controller::from_path(data_dir.path()); let project = projects - .add(test_repository.repository.path()) + .add(test_repository.repository.path(), None, None) .expect("failed to add project"); let ctx = CommandContext::open(&project, but_settings::AppSettings::default()).unwrap(); set_test_target(&ctx).unwrap(); diff --git a/crates/gitbutler-workspace/tests/mod.rs b/crates/gitbutler-workspace/tests/mod.rs index 58d8eb6396..08b4daad38 100644 --- a/crates/gitbutler-workspace/tests/mod.rs +++ b/crates/gitbutler-workspace/tests/mod.rs @@ -22,7 +22,7 @@ mod checkout_branch_trees { let projects = gitbutler_project::Controller::from_path(data_dir.path()); let project = projects - .add(test_project.path()) + .add(test_project.path(), None, None) .expect("failed to add project"); let ctx = CommandContext::open(&project, AppSettings::default()).unwrap();