Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sweep: allow for rebase. #3498

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
11 changes: 5 additions & 6 deletions sweepai/handlers/on_merge_conflict.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from sweepai.utils.chat_logger import ChatLogger, discord_log_error
from sweepai.utils.diff import generate_diff
from sweepai.utils.event_logger import posthog
from sweepai.utils.github_utils import ClonedRepo, get_github_client
from sweepai.utils.github_utils import ClonedRepo, get_github_client, rebase_branch
from sweepai.utils.progress import (
PaymentContext,
TicketContext,
Expand Down Expand Up @@ -171,25 +171,24 @@ def edit_comment(body):
new_pull_request.branch_name += "_" + str(i)
break

# Merge into base branch from cloned_repo.repo_dir to pr.base.ref
# Rebase new branch onto base branch
git_repo = cloned_repo.git_repo
old_head_branch = git_repo.branches[branch]
head_branch = git_repo.create_head(
new_pull_request.branch_name,
commit=old_head_branch.commit,
)
head_branch.checkout()
git_repo = rebase_branch(git_repo, new_pull_request.branch_name, pr.base.ref)
try:
git_repo.config_writer().set_value(
"user", "name", "sweep-nightly[bot]"
).release()
git_repo.config_writer().set_value(
"user", "email", "team@sweep.dev"
).release()
git_repo.git.merge("origin/" + pr.base.ref)
except GitCommandError:
# Assume there are merge conflicts
pass
except Exception as e:
print("Exception occurred while configuring git user", e)

git_repo.git.add(update=True)
# -m and message are needed otherwise exception is thrown
Expand Down
16 changes: 16 additions & 0 deletions sweepai/utils/github_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,22 @@ def convert_pr_draft_field(pr: PullRequest, is_draft: bool = False):
return True


def rebase_branch(git_repo: git.Repo, source_branch: str, target_branch: str) -> git.Repo:
git_repo.git.checkout(source_branch)
try:
git_repo.git.rebase(target_branch)
except git.GitCommandError:
# Handle rebase conflicts
for conflicted_file in git_repo.index.conflicts:
# Take the version from the target branch
git_repo.git.checkout("--theirs", conflicted_file[0].a_path)
git_repo.git.add(conflicted_file[0].a_path)

git_repo.git.rebase("--continue")

return git_repo


try:
g = Github(os.environ.get("GITHUB_PAT"))
CURRENT_USERNAME = g.get_user().login
Expand Down
Loading