Skip to content
Open
84 changes: 84 additions & 0 deletions src/branch_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,90 @@
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

Check warning on line 194 in src/branch_handler.py

View check run for this annotation

Codecov / codecov/patch

src/branch_handler.py#L193-L194

Added lines #L193 - L194 were not covered by tests

repo = git.Repo(repo_path)
current_branch = repo.active_branch.name

Check warning on line 197 in src/branch_handler.py

View check run for this annotation

Codecov / codecov/patch

src/branch_handler.py#L196-L197

Added lines #L196 - L197 were not covered by tests

# Determine main branch (main or master)
if 'main' in repo.heads:
main_branch = 'main'

Check warning on line 201 in src/branch_handler.py

View check run for this annotation

Codecov / codecov/patch

src/branch_handler.py#L201

Added line #L201 was not covered by tests
elif 'master' in repo.heads:
main_branch = 'master'

Check warning on line 203 in src/branch_handler.py

View check run for this annotation

Codecov / codecov/patch

src/branch_handler.py#L203

Added line #L203 was not covered by tests
else:
error_msg = "Neither 'main' nor 'master' branch found in repository"
print(error_msg)

Check warning on line 206 in src/branch_handler.py

View check run for this annotation

Codecov / codecov/patch

src/branch_handler.py#L205-L206

Added lines #L205 - L206 were not covered by tests
if issue:
try:
from response_agent import llm_config
error_msg_with_signature = add_signature_to_comment(

Check warning on line 210 in src/branch_handler.py

View check run for this annotation

Codecov / codecov/patch

src/branch_handler.py#L208-L210

Added lines #L208 - L210 were not covered by tests
error_msg, llm_config['model'])
except (ImportError, KeyError):
error_msg_with_signature = error_msg + \

Check warning on line 213 in src/branch_handler.py

View check run for this annotation

Codecov / codecov/patch

src/branch_handler.py#L212-L213

Added lines #L212 - L213 were not covered by tests
"\n\n---\n*This response was automatically generated by blech_bot*"
write_issue_response(issue, error_msg_with_signature)
return False

Check warning on line 216 in src/branch_handler.py

View check run for this annotation

Codecov / codecov/patch

src/branch_handler.py#L215-L216

Added lines #L215 - L216 were not covered by tests

# 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

Check warning on line 221 in src/branch_handler.py

View check run for this annotation

Codecov / codecov/patch

src/branch_handler.py#L220-L221

Added lines #L220 - L221 were not covered by tests

try:

Check warning on line 223 in src/branch_handler.py

View check run for this annotation

Codecov / codecov/patch

src/branch_handler.py#L223

Added line #L223 was not covered by tests
# Make sure main branch is up to date
repo.git.checkout(main_branch)
repo.git.pull('origin', main_branch)

Check warning on line 226 in src/branch_handler.py

View check run for this annotation

Codecov / codecov/patch

src/branch_handler.py#L225-L226

Added lines #L225 - L226 were not covered by tests

# Go back to feature branch
repo.git.checkout(current_branch)

Check warning on line 229 in src/branch_handler.py

View check run for this annotation

Codecov / codecov/patch

src/branch_handler.py#L229

Added line #L229 was not covered by tests

# 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)

Check warning on line 237 in src/branch_handler.py

View check run for this annotation

Codecov / codecov/patch

src/branch_handler.py#L232-L237

Added lines #L232 - L237 were not covered by tests

# 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")

Check warning on line 244 in src/branch_handler.py

View check run for this annotation

Codecov / codecov/patch

src/branch_handler.py#L240-L244

Added lines #L240 - L244 were not covered by tests

# Log error to issue if provided
if issue:
try:
from response_agent import llm_config
error_msg_with_signature = add_signature_to_comment(

Check warning on line 250 in src/branch_handler.py

View check run for this annotation

Codecov / codecov/patch

src/branch_handler.py#L248-L250

Added lines #L248 - L250 were not covered by tests
error_msg, llm_config['model'])
except (ImportError, KeyError):
error_msg_with_signature = error_msg + \

Check warning on line 253 in src/branch_handler.py

View check run for this annotation

Codecov / codecov/patch

src/branch_handler.py#L252-L253

Added lines #L252 - L253 were not covered by tests
"\n\n---\n*This response was automatically generated by blech_bot*"
write_issue_response(issue, error_msg_with_signature)

Check warning on line 255 in src/branch_handler.py

View check run for this annotation

Codecov / codecov/patch

src/branch_handler.py#L255

Added line #L255 was not covered by tests

# 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")

Check warning on line 261 in src/branch_handler.py

View check run for this annotation

Codecov / codecov/patch

src/branch_handler.py#L258-L261

Added lines #L258 - L261 were not covered by tests

return False

Check warning on line 263 in src/branch_handler.py

View check run for this annotation

Codecov / codecov/patch

src/branch_handler.py#L263

Added line #L263 was not covered by tests


def push_changes(repo_path: str, branch_name: Optional[str] = None, force: bool = False) -> None:
"""
Push local changes to remote repository
Expand Down
7 changes: 7 additions & 0 deletions src/git_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@
git_repo = git.Repo(repo_path)
origin = git_repo.remotes.origin
origin.pull()
print("Repository updated with the latest changes from remote")

Check warning on line 236 in src/git_utils.py

View check run for this annotation

Codecov / codecov/patch

src/git_utils.py#L236

Added line #L236 was not covered by tests


def get_pr_branch(pr: PullRequest) -> str:
Expand Down Expand Up @@ -265,6 +266,8 @@
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)
Expand Down Expand Up @@ -470,8 +473,12 @@

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')

Expand Down