Skip to content
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
34 changes: 17 additions & 17 deletions src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&current_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(&current_dir)
.unwrap_or(repo_dir)
.to_string_lossy()
.to_string(),
)
.build();
repositories.push(repo);
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
// 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())
}
12 changes: 6 additions & 6 deletions src/github/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand Down Expand Up @@ -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(
Expand Down
19 changes: 10 additions & 9 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ pub fn find_git_repositories(start_path: &str) -> Result<Vec<Repository>> {
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);
}
}

Expand Down Expand Up @@ -67,11 +68,11 @@ fn get_remote_url(repo_path: &Path) -> Result<Option<String>> {
.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)
Expand Down