Skip to content

On project add set git name and git email if not present #8065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion crates/gitbutler-cli/src/command/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
22 changes: 21 additions & 1 deletion crates/gitbutler-project/src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ impl Controller {
}
}

pub fn add<P: AsRef<Path>>(&self, path: P) -> Result<Project> {
pub fn add<P: AsRef<Path>>(
&self,
path: P,
name: Option<String>,
email: Option<String>,
) -> Result<Project> {
let path = path.as_ref();
let all_projects = self
.projects_storage
Expand Down Expand Up @@ -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("[email protected]".to_string()))?;
}

Ok(project)
}

Expand Down
22 changes: 11 additions & 11 deletions crates/gitbutler-project/tests/projects/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
);
}
Expand All @@ -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");
}

Expand All @@ -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"
);
}
Expand All @@ -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");
}

Expand All @@ -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");
}

Expand Down Expand Up @@ -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());
Expand Down
4 changes: 3 additions & 1 deletion crates/gitbutler-stack/src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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", "[email protected]")?;
generate_branch_name(author)?
};

Expand Down
8 changes: 6 additions & 2 deletions crates/gitbutler-tauri/src/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<projects::Project, Error> {
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)]
Expand Down
6 changes: 5 additions & 1 deletion crates/gitbutler-testsupport/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion crates/gitbutler-testsupport/src/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
6 changes: 3 additions & 3 deletions crates/gitbutler-workspace/tests/branch_trees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion crates/gitbutler-workspace/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading