Skip to content

Commit

Permalink
Let's fix this one
Browse files Browse the repository at this point in the history
  • Loading branch information
malfet committed Jan 5, 2024
1 parent e579a45 commit b7e520a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
14 changes: 11 additions & 3 deletions .github/scripts/gitutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,14 +396,22 @@ def _shasum(value: str) -> str:
m.update(value.encode("utf-8"))
return m.hexdigest()

def is_commit_hash(ref: str) -> bool:
"True if ref is hexidecimal number, else false"
try:
int(ref, 16)
except ValueError:
return False
return True

def are_ghstack_branches_in_sync(repo: GitRepo, head_ref: str) -> bool:
def are_ghstack_branches_in_sync(repo: GitRepo, head_ref: str, base_ref: Optional[str] = None) -> bool:
"""Checks that diff between base and head is the same as diff between orig and its parent"""
orig_ref = re.sub(r"/head$", "/orig", head_ref)
base_ref = re.sub(r"/head$", "/base", head_ref)
if base_ref is None:
base_ref = re.sub(r"/head$", "/base", head_ref)
orig_diff_sha = _shasum(repo.diff(f"{repo.remote}/{orig_ref}"))
head_diff_sha = _shasum(
repo.diff(f"{repo.remote}/{base_ref}", f"{repo.remote}/{head_ref}")
repo.diff(base_ref if is_commit_hash(base_ref) else f"{repo.remote}/{base_ref}", f"{repo.remote}/{head_ref}")
)
return orig_diff_sha == head_diff_sha

Expand Down
7 changes: 5 additions & 2 deletions .github/scripts/trymerge.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,10 +674,13 @@ def skip_func(idx: int, candidate: "GitHubPR") -> bool:
for stacked_pr, rev in entire_stack:
if stacked_pr.is_closed():
continue
if not are_ghstack_branches_in_sync(repo, stacked_pr.head_ref()):
base_ref = stacked_pr.base_ref()
if base_ref == pr.default_branch():
base_ref = repo.get_merge_base(f"{repo.remote}/{base_ref}", f"{repo.remote}/{stacked_pr.head_ref()}")
if not are_ghstack_branches_in_sync(repo, stacked_pr.head_ref(), base_ref):
raise RuntimeError(
f"PR {stacked_pr.pr_num} is out of sync with the corresponding revision {rev} on "
+ f"branch {orig_ref} that would be merged into main. "
+ f"branch {stacked_pr.get_ghstack_orig_ref()} that would be merged into {stacked_pr.default_branch()}. "
+ "This usually happens because there is a non ghstack change in the PR. "
+ f"Please sync them and try again (ex. make the changes on {orig_ref} and run ghstack)."
)
Expand Down

0 comments on commit b7e520a

Please sign in to comment.