Skip to content

Commit

Permalink
feat: Updated 2 files
Browse files Browse the repository at this point in the history
  • Loading branch information
sweep-nightly[bot] committed Apr 8, 2024
1 parent 76aecb2 commit 8fe7dbe
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
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

0 comments on commit 8fe7dbe

Please sign in to comment.