From 31446b96e18c0f88358dfe096928fdd9a4dec090 Mon Sep 17 00:00:00 2001 From: Louis Knight-Webb Date: Fri, 4 Jul 2025 14:27:25 +0100 Subject: [PATCH] Create task in new repo error (vibe-kanban) (#74) * Fixed the Git UnbornBranch error that occurs when creating tasks in new repositories without commits. The error was happening in [`TaskAttempt::get_diff`](file:///private/var/folders/kr/jdxkcn7129j376nrg0stj9zm0000gn/T/vk-a98d-create-tas/backend/src/models/task_attempt.rs#L1186) when trying to access the main branch. Added proper error handling to return an empty diff for new repositories. * Perfect! You were absolutely right. The proper fix was to create an initial commit and main branch when the repository is new, rather than just returning an empty diff. The fix now: 1. Detects when a repository has no commits (UnbornBranch error) 2. Creates an initial empty commit on the main branch 3. Sets HEAD to point to the main branch 4. Allows the normal branch creation workflow to proceed This ensures that task attempts can be created properly in new repositories by establishing the base branch structure they need to work with. * Perfect! I've updated the code to use the proper Git signature approach: 1. **Primary approach**: Use `repo.signature()` which gets the signature from the Git config (user's name and email) 2. **Fallback**: If no Git config is set, fall back to a default "Vibe Kanban" signature This ensures that: - When a user has Git configured, their commits will be properly attributed to them - When Git is not configured (fresh repository), it falls back to a sensible default - All other places in the codebase already use `repo.signature()` consistently The fix now properly handles new repositories by creating an initial commit with the right signature, allowing the normal task creation workflow to proceed. * fmt --- backend/src/models/task_attempt.rs | 38 +++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/backend/src/models/task_attempt.rs b/backend/src/models/task_attempt.rs index 2a786e1b..e5df3eb6 100644 --- a/backend/src/models/task_attempt.rs +++ b/backend/src/models/task_attempt.rs @@ -241,7 +241,43 @@ impl TaskAttempt { let branch = repo.find_branch(base_branch.as_str(), BranchType::Local)?; branch.into_reference() } else { - repo.head()? + // Handle new repositories without any commits + match repo.head() { + Ok(head_ref) => head_ref, + Err(e) + if e.class() == git2::ErrorClass::Reference + && e.code() == git2::ErrorCode::UnbornBranch => + { + // Repository has no commits yet, create an initial commit + let signature = repo.signature().unwrap_or_else(|_| { + // Fallback if no Git config is set + git2::Signature::now("Vibe Kanban", "noreply@vibekanban.com") + .expect("Failed to create fallback signature") + }); + let tree_id = { + let tree_builder = repo.treebuilder(None)?; + tree_builder.write()? + }; + let tree = repo.find_tree(tree_id)?; + + // Create initial commit on main branch + let _commit_id = repo.commit( + Some("refs/heads/main"), + &signature, + &signature, + "Initial commit", + &tree, + &[], + )?; + + // Set HEAD to point to main branch + repo.set_head("refs/heads/main")?; + + // Return reference to main branch + repo.find_reference("refs/heads/main")? + } + Err(e) => return Err(e.into()), + } }; // Create branch