diff --git a/src/branch_handler.py b/src/branch_handler.py index 96e827e..e5f4aae 100644 --- a/src/branch_handler.py +++ b/src/branch_handler.py @@ -179,6 +179,90 @@ def back_to_master_branch(repo_path: str) -> None: print(f"Checked out {main_branch} branch") +def merge_master(repo_path: str, issue=None) -> bool: + """ + Merge the main/master branch into the current branch with error handling. + + Args: + repo_path: Path to local git repository + issue: The GitHub issue to log errors to (optional) + + Returns: + True if merge was successful, False otherwise + """ + from src.git_utils import write_issue_response, add_signature_to_comment + import traceback + + repo = git.Repo(repo_path) + current_branch = repo.active_branch.name + + # Determine main branch (main or master) + if 'main' in repo.heads: + main_branch = 'main' + elif 'master' in repo.heads: + main_branch = 'master' + else: + error_msg = "Neither 'main' nor 'master' branch found in repository" + print(error_msg) + if issue: + try: + from response_agent import llm_config + error_msg_with_signature = add_signature_to_comment( + error_msg, llm_config['model']) + except (ImportError, KeyError): + error_msg_with_signature = error_msg + \ + "\n\n---\n*This response was automatically generated by blech_bot*" + write_issue_response(issue, error_msg_with_signature) + return False + + # Don't try to merge if we're already on the main branch + if current_branch == main_branch: + print(f"Already on {main_branch} branch, no merge needed") + return True + + try: + # Make sure main branch is up to date + repo.git.checkout(main_branch) + repo.git.pull('origin', main_branch) + + # Go back to feature branch + repo.git.checkout(current_branch) + + # Merge main into current branch + repo.git.merge(main_branch) + print(f"Successfully merged {main_branch} into {current_branch}") + return True + except Exception as e: + error_msg = f"Error merging {main_branch} into {current_branch}: {str(e)}\n\n```\n{traceback.format_exc()}\n```" + print(error_msg) + + # Try to abort the merge if it's in progress + try: + repo.git.merge('--abort') + print("Merge aborted") + except: + print("Could not abort merge, it may not have been in progress") + + # Log error to issue if provided + if issue: + try: + from response_agent import llm_config + error_msg_with_signature = add_signature_to_comment( + error_msg, llm_config['model']) + except (ImportError, KeyError): + error_msg_with_signature = error_msg + \ + "\n\n---\n*This response was automatically generated by blech_bot*" + write_issue_response(issue, error_msg_with_signature) + + # Make sure we're back on the feature branch + try: + repo.git.checkout(current_branch) + except: + print(f"Could not checkout {current_branch} after failed merge") + + return False + + def push_changes(repo_path: str, branch_name: Optional[str] = None, force: bool = False) -> None: """ Push local changes to remote repository diff --git a/src/git_utils.py b/src/git_utils.py index b4edb82..c01eaf1 100644 --- a/src/git_utils.py +++ b/src/git_utils.py @@ -233,6 +233,7 @@ def update_repository(repo_path: str) -> None: git_repo = git.Repo(repo_path) origin = git_repo.remotes.origin origin.pull() + print("Repository updated with the latest changes from remote") def get_pr_branch(pr: PullRequest) -> str: @@ -265,6 +266,8 @@ def get_development_branch(issue: Issue, repo_path: str, create: bool = False) - ValueError: If gh CLI is not installed RuntimeError: If multiple branches exist for the issue """ + # Update repository before any branch operations + update_repository(repo_path) try: # Check for existing branches related to this issue related_branches = get_issue_related_branches(repo_path, issue) @@ -470,8 +473,12 @@ def push_changes_with_authentication( Args: repo_path: Path to the local git repository + out_thread: The issue or PR to log errors to branch_name: Name of the branch to push (default: current branch) """ + # Try to merge master/main branch first + from src.branch_handler import merge_master + merge_master(repo_path, out_thread) load_dotenv() token = os.getenv('GITHUB_TOKEN')