Skip to content

Commit 8fe7dbe

Browse files
feat: Updated 2 files
1 parent 76aecb2 commit 8fe7dbe

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

sweepai/handlers/on_merge_conflict.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from sweepai.utils.chat_logger import ChatLogger, discord_log_error
1515
from sweepai.utils.diff import generate_diff
1616
from sweepai.utils.event_logger import posthog
17-
from sweepai.utils.github_utils import ClonedRepo, get_github_client
17+
from sweepai.utils.github_utils import ClonedRepo, get_github_client, rebase_branch
1818
from sweepai.utils.progress import (
1919
PaymentContext,
2020
TicketContext,
@@ -171,25 +171,24 @@ def edit_comment(body):
171171
new_pull_request.branch_name += "_" + str(i)
172172
break
173173

174-
# Merge into base branch from cloned_repo.repo_dir to pr.base.ref
174+
# Rebase new branch onto base branch
175175
git_repo = cloned_repo.git_repo
176176
old_head_branch = git_repo.branches[branch]
177177
head_branch = git_repo.create_head(
178178
new_pull_request.branch_name,
179179
commit=old_head_branch.commit,
180180
)
181181
head_branch.checkout()
182+
git_repo = rebase_branch(git_repo, new_pull_request.branch_name, pr.base.ref)
182183
try:
183184
git_repo.config_writer().set_value(
184185
"user", "name", "sweep-nightly[bot]"
185186
).release()
186187
git_repo.config_writer().set_value(
187188
"user", "email", "[email protected]"
188189
).release()
189-
git_repo.git.merge("origin/" + pr.base.ref)
190-
except GitCommandError:
191-
# Assume there are merge conflicts
192-
pass
190+
except Exception as e:
191+
print("Exception occurred while configuring git user", e)
193192

194193
git_repo.git.add(update=True)
195194
# -m and message are needed otherwise exception is thrown

sweepai/utils/github_utils.py

+16
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,22 @@ def convert_pr_draft_field(pr: PullRequest, is_draft: bool = False):
658658
return True
659659

660660

661+
def rebase_branch(git_repo: git.Repo, source_branch: str, target_branch: str) -> git.Repo:
662+
git_repo.git.checkout(source_branch)
663+
try:
664+
git_repo.git.rebase(target_branch)
665+
except git.GitCommandError:
666+
# Handle rebase conflicts
667+
for conflicted_file in git_repo.index.conflicts:
668+
# Take the version from the target branch
669+
git_repo.git.checkout("--theirs", conflicted_file[0].a_path)
670+
git_repo.git.add(conflicted_file[0].a_path)
671+
672+
git_repo.git.rebase("--continue")
673+
674+
return git_repo
675+
676+
661677
try:
662678
g = Github(os.environ.get("GITHUB_PAT"))
663679
CURRENT_USERNAME = g.get_user().login

0 commit comments

Comments
 (0)