Make sure new projects have a main branch (vibe-kanban) (#450)

* Commit changes from coding agent for task attempt 55feb9b4-1b54-4181-b23e-a495886c39eb

* Cleanup script changes for task attempt 55feb9b4-1b54-4181-b23e-a495886c39eb

* Commit changes from coding agent for task attempt 55feb9b4-1b54-4181-b23e-a495886c39eb
This commit is contained in:
Louis Knight-Webb
2025-08-11 23:04:56 +01:00
committed by GitHub
parent 6f39cca4e6
commit 59c977e235
2 changed files with 54 additions and 24 deletions

View File

@@ -13,7 +13,7 @@ use db::models::project::{
};
use deployment::Deployment;
use ignore::WalkBuilder;
use services::services::git::{GitBranch, GitService};
use services::services::git::GitBranch;
use utils::response::ApiResponse;
use uuid::Uuid;
@@ -34,8 +34,9 @@ pub async fn get_project(
pub async fn get_project_branches(
Extension(project): Extension<Project>,
State(deployment): State<DeploymentImpl>,
) -> Result<ResponseJson<ApiResponse<Vec<GitBranch>>>, ApiError> {
let branches = GitService::new().get_all_branches(&project.git_repo_path)?;
let branches = deployment.git().get_all_branches(&project.git_repo_path)?;
Ok(ResponseJson(ApiResponse::success(branches)))
}
@@ -84,6 +85,15 @@ pub async fn create_project(
"The specified directory is not a git repository",
)));
}
// Ensure existing repo has a main branch if it's empty
if let Err(e) = deployment.git().ensure_main_branch_exists(path) {
tracing::error!("Failed to ensure main branch exists: {}", e);
return Ok(ResponseJson(ApiResponse::error(&format!(
"Failed to ensure main branch exists: {}",
e
))));
}
} else {
// For new repos, create directory and initialize git
@@ -100,28 +110,12 @@ pub async fn create_project(
// Check if it's already a git repo, if not initialize it
if !path.join(".git").exists() {
match std::process::Command::new("git")
.arg("init")
.current_dir(path)
.output()
{
Ok(output) => {
if !output.status.success() {
let error_msg = String::from_utf8_lossy(&output.stderr);
tracing::error!("Git init failed: {}", error_msg);
return Ok(ResponseJson(ApiResponse::error(&format!(
"Git init failed: {}",
error_msg
))));
}
}
Err(e) => {
tracing::error!("Failed to run git init: {}", e);
return Ok(ResponseJson(ApiResponse::error(&format!(
"Failed to run git init: {}",
e
))));
}
if let Err(e) = deployment.git().initialize_repo_with_main_branch(path) {
tracing::error!("Failed to initialize git repository: {}", e);
return Ok(ResponseJson(ApiResponse::error(&format!(
"Failed to initialize git repository: {}",
e
))));
}
}
}

View File

@@ -93,6 +93,42 @@ impl GitService {
Repository::open(repo_path).map_err(GitServiceError::from)
}
/// Initialize a new git repository with a main branch and initial commit
pub fn initialize_repo_with_main_branch(
&self,
repo_path: &Path,
) -> Result<(), GitServiceError> {
// Create directory if it doesn't exist
if !repo_path.exists() {
std::fs::create_dir_all(repo_path)?;
}
// Initialize git repository with main branch
let repo = Repository::init_opts(
repo_path,
git2::RepositoryInitOptions::new()
.initial_head("main")
.mkdir(true),
)?;
// Create initial commit
self.create_initial_commit(&repo)?;
Ok(())
}
/// Ensure an existing repository has a main branch (for empty repos)
pub fn ensure_main_branch_exists(&self, repo_path: &Path) -> Result<(), GitServiceError> {
let repo = self.open_repo(repo_path)?;
// Only create initial commit if repository is empty
if repo.is_empty()? {
self.create_initial_commit(&repo)?;
}
Ok(())
}
pub fn create_initial_commit(&self, repo: &Repository) -> Result<(), GitServiceError> {
let signature = repo.signature().unwrap_or_else(|_| {
// Fallback if no Git config is set