Skip to content

Commit a9c31a5

Browse files
authored
Merge pull request #4 from codcod/3-pull-requests-are-not-created-for-repos-with-default-branches-other-than-main
fix: create prs for branches other than main #3
2 parents 7484fdb + 33cbf68 commit a9c31a5

File tree

4 files changed

+67
-32
lines changed

4 files changed

+67
-32
lines changed

src/commands/init.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,23 @@ impl Command for InitCommand {
3434
.into_iter()
3535
.filter_map(|e| e.ok())
3636
{
37-
if entry.file_name() == ".git" && entry.file_type().is_dir() {
38-
if let Some(repo_dir) = entry.path().parent() {
39-
if let Some(name) = repo_dir.file_name().and_then(|n| n.to_str()) {
40-
// Try to get remote URL
41-
if let Ok(url) = get_git_remote_url(repo_dir) {
42-
let repo = RepositoryBuilder::new(name.to_string(), url)
43-
.with_path(
44-
repo_dir
45-
.strip_prefix(&current_dir)
46-
.unwrap_or(repo_dir)
47-
.to_string_lossy()
48-
.to_string(),
49-
)
50-
.build();
51-
repositories.push(repo);
52-
}
53-
}
37+
if entry.file_name() == ".git"
38+
&& entry.file_type().is_dir()
39+
&& let Some(repo_dir) = entry.path().parent()
40+
&& let Some(name) = repo_dir.file_name().and_then(|n| n.to_str())
41+
{
42+
// Try to get remote URL
43+
if let Ok(url) = get_git_remote_url(repo_dir) {
44+
let repo = RepositoryBuilder::new(name.to_string(), url)
45+
.with_path(
46+
repo_dir
47+
.strip_prefix(&current_dir)
48+
.unwrap_or(repo_dir)
49+
.to_string_lossy()
50+
.to_string(),
51+
)
52+
.build();
53+
repositories.push(repo);
5454
}
5555
}
5656
}

src/git.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,37 @@ pub fn push_branch(repo_path: &str, branch_name: &str) -> Result<()> {
180180

181181
Ok(())
182182
}
183+
184+
pub fn get_default_branch(repo_path: &str) -> Result<String> {
185+
// Try to get the default branch using git symbolic-ref
186+
let output = Command::new("git")
187+
.args(["symbolic-ref", "refs/remotes/origin/HEAD"])
188+
.current_dir(repo_path)
189+
.output();
190+
191+
if let Ok(output) = output
192+
&& output.status.success()
193+
{
194+
let branch_ref = String::from_utf8_lossy(&output.stdout).trim().to_string();
195+
if let Some(branch) = branch_ref.strip_prefix("refs/remotes/origin/") {
196+
return Ok(branch.to_string());
197+
}
198+
}
199+
200+
// Fallback: try to get the current branch
201+
let output = Command::new("git")
202+
.args(["branch", "--show-current"])
203+
.current_dir(repo_path)
204+
.output()
205+
.context("Failed to execute git branch command")?;
206+
207+
if output.status.success() {
208+
let current_branch = String::from_utf8_lossy(&output.stdout).trim().to_string();
209+
if !current_branch.is_empty() {
210+
return Ok(current_branch);
211+
}
212+
}
213+
214+
// Final fallback to "main"
215+
Ok("main".to_string())
216+
}

src/github/api.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use uuid::Uuid;
1111
// Constants for maintainability
1212
const DEFAULT_BRANCH_PREFIX: &str = "automated-changes";
1313
const UUID_LENGTH: usize = 6;
14-
const DEFAULT_BASE_BRANCH: &str = "main";
1514

1615
/// Create a pull request for a repository
1716
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
6665
// Extract owner and repo name from URL
6766
let (owner, repo_name) = client.parse_github_url(&repo.url)?;
6867

69-
// Determine base branch
70-
let base_branch = options
71-
.base_branch
72-
.clone()
73-
.unwrap_or_else(|| DEFAULT_BASE_BRANCH.to_string());
68+
// Determine base branch - get actual default branch if not specified
69+
let base_branch = if let Some(ref base) = options.base_branch {
70+
base.clone()
71+
} else {
72+
git::get_default_branch(&repo.get_target_dir())?
73+
};
7474

7575
let result = client
7676
.create_pull_request(PullRequestParams::new(

src/util.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ pub fn find_git_repositories(start_path: &str) -> Result<Vec<Repository>> {
1717
let path = entry.path();
1818

1919
// Check if this directory contains a .git folder
20-
if path.is_dir() && path.join(".git").exists() {
21-
if let Some(repo) = create_repository_from_path(path)? {
22-
repositories.push(repo);
23-
}
20+
if path.is_dir()
21+
&& path.join(".git").exists()
22+
&& let Some(repo) = create_repository_from_path(path)?
23+
{
24+
repositories.push(repo);
2425
}
2526
}
2627

@@ -67,11 +68,11 @@ fn get_remote_url(repo_path: &Path) -> Result<Option<String>> {
6768
.current_dir(repo_path)
6869
.output();
6970

70-
if let Ok(output) = output {
71-
if output.status.success() {
72-
let url = String::from_utf8_lossy(&output.stdout).trim().to_string();
73-
return Ok(Some(url));
74-
}
71+
if let Ok(output) = output
72+
&& output.status.success()
73+
{
74+
let url = String::from_utf8_lossy(&output.stdout).trim().to_string();
75+
return Ok(Some(url));
7576
}
7677

7778
Ok(None)

0 commit comments

Comments
 (0)