From 33cbf685994a49f927e652c4df0232dbe07b1ff2 Mon Sep 17 00:00:00 2001 From: nicos_backbase Date: Mon, 13 Oct 2025 16:01:01 +0200 Subject: [PATCH] fix: https://github.com/codcod/repos/issues/3 --- src/commands/init.rs | 34 +++++++++++++++++----------------- src/git.rs | 34 ++++++++++++++++++++++++++++++++++ src/github/api.rs | 12 ++++++------ src/util.rs | 19 ++++++++++--------- 4 files changed, 67 insertions(+), 32 deletions(-) diff --git a/src/commands/init.rs b/src/commands/init.rs index 82428b5..7e8f76b 100644 --- a/src/commands/init.rs +++ b/src/commands/init.rs @@ -34,23 +34,23 @@ impl Command for InitCommand { .into_iter() .filter_map(|e| e.ok()) { - if entry.file_name() == ".git" && entry.file_type().is_dir() { - if let Some(repo_dir) = entry.path().parent() { - if let Some(name) = repo_dir.file_name().and_then(|n| n.to_str()) { - // Try to get remote URL - if let Ok(url) = get_git_remote_url(repo_dir) { - let repo = RepositoryBuilder::new(name.to_string(), url) - .with_path( - repo_dir - .strip_prefix(¤t_dir) - .unwrap_or(repo_dir) - .to_string_lossy() - .to_string(), - ) - .build(); - repositories.push(repo); - } - } + if entry.file_name() == ".git" + && entry.file_type().is_dir() + && let Some(repo_dir) = entry.path().parent() + && let Some(name) = repo_dir.file_name().and_then(|n| n.to_str()) + { + // Try to get remote URL + if let Ok(url) = get_git_remote_url(repo_dir) { + let repo = RepositoryBuilder::new(name.to_string(), url) + .with_path( + repo_dir + .strip_prefix(¤t_dir) + .unwrap_or(repo_dir) + .to_string_lossy() + .to_string(), + ) + .build(); + repositories.push(repo); } } } diff --git a/src/git.rs b/src/git.rs index b12d735..aaf0133 100644 --- a/src/git.rs +++ b/src/git.rs @@ -180,3 +180,37 @@ pub fn push_branch(repo_path: &str, branch_name: &str) -> Result<()> { Ok(()) } + +pub fn get_default_branch(repo_path: &str) -> Result { + // Try to get the default branch using git symbolic-ref + let output = Command::new("git") + .args(["symbolic-ref", "refs/remotes/origin/HEAD"]) + .current_dir(repo_path) + .output(); + + if let Ok(output) = output + && output.status.success() + { + let branch_ref = String::from_utf8_lossy(&output.stdout).trim().to_string(); + if let Some(branch) = branch_ref.strip_prefix("refs/remotes/origin/") { + return Ok(branch.to_string()); + } + } + + // Fallback: try to get the current branch + let output = Command::new("git") + .args(["branch", "--show-current"]) + .current_dir(repo_path) + .output() + .context("Failed to execute git branch command")?; + + if output.status.success() { + let current_branch = String::from_utf8_lossy(&output.stdout).trim().to_string(); + if !current_branch.is_empty() { + return Ok(current_branch); + } + } + + // Final fallback to "main" + Ok("main".to_string()) +} diff --git a/src/github/api.rs b/src/github/api.rs index 9dd6a66..378a6ab 100644 --- a/src/github/api.rs +++ b/src/github/api.rs @@ -11,7 +11,6 @@ use uuid::Uuid; // Constants for maintainability const DEFAULT_BRANCH_PREFIX: &str = "automated-changes"; const UUID_LENGTH: usize = 6; -const DEFAULT_BASE_BRANCH: &str = "main"; /// Create a pull request for a repository pub async fn create_pull_request(repo: &Repository, options: &PrOptions) -> Result<()> { @@ -66,11 +65,12 @@ async fn create_github_pr(repo: &Repository, branch_name: &str, options: &PrOpti // Extract owner and repo name from URL let (owner, repo_name) = client.parse_github_url(&repo.url)?; - // Determine base branch - let base_branch = options - .base_branch - .clone() - .unwrap_or_else(|| DEFAULT_BASE_BRANCH.to_string()); + // Determine base branch - get actual default branch if not specified + let base_branch = if let Some(ref base) = options.base_branch { + base.clone() + } else { + git::get_default_branch(&repo.get_target_dir())? + }; let result = client .create_pull_request(PullRequestParams::new( diff --git a/src/util.rs b/src/util.rs index 56de4cf..14f5951 100644 --- a/src/util.rs +++ b/src/util.rs @@ -17,10 +17,11 @@ pub fn find_git_repositories(start_path: &str) -> Result> { let path = entry.path(); // Check if this directory contains a .git folder - if path.is_dir() && path.join(".git").exists() { - if let Some(repo) = create_repository_from_path(path)? { - repositories.push(repo); - } + if path.is_dir() + && path.join(".git").exists() + && let Some(repo) = create_repository_from_path(path)? + { + repositories.push(repo); } } @@ -67,11 +68,11 @@ fn get_remote_url(repo_path: &Path) -> Result> { .current_dir(repo_path) .output(); - if let Ok(output) = output { - if output.status.success() { - let url = String::from_utf8_lossy(&output.stdout).trim().to_string(); - return Ok(Some(url)); - } + if let Ok(output) = output + && output.status.success() + { + let url = String::from_utf8_lossy(&output.stdout).trim().to_string(); + return Ok(Some(url)); } Ok(None)