Skip to content

Commit 055465e

Browse files
authored
Merge pull request #426 from azuwis/push-snpnlpltsnsk
Support nixpkgs shallow clone and make use of github pull/<pr>/merge ref
2 parents 6bcf207 + a218ae9 commit 055465e

File tree

2 files changed

+39
-24
lines changed

2 files changed

+39
-24
lines changed

nixpkgs_review/review.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ def git_merge(self, commit: str) -> None:
155155
f"Failed to merge {commit} into {self.worktree_dir()}. git merge failed with exit code {res.returncode}"
156156
)
157157

158+
def git_checkout(self, commit: str) -> None:
159+
res = sh(["git", "checkout", commit], cwd=self.worktree_dir())
160+
if res.returncode != 0:
161+
raise NixpkgsReviewError(
162+
f"Failed to checkout {commit} in {self.worktree_dir()}. git checkout failed with exit code {res.returncode}"
163+
)
164+
158165
def apply_unstaged(self, staged: bool = False) -> None:
159166
args = ["git", "--no-pager", "diff", "--no-ext-diff"]
160167
args.extend(["--staged"] if staged else [])
@@ -191,6 +198,8 @@ def build_commit(
191198

192199
if reviewed_commit is None:
193200
self.apply_unstaged(staged)
201+
elif self.checkout == CheckoutOption.MERGE:
202+
self.git_checkout(reviewed_commit)
194203
else:
195204
self.git_merge(reviewed_commit)
196205

@@ -228,13 +237,6 @@ def git_worktree(self, commit: str) -> None:
228237
f"Failed to add worktree for {commit} in {self.worktree_dir()}. git worktree failed with exit code {res.returncode}"
229238
)
230239

231-
def checkout_pr(self, base_rev: str, pr_rev: str) -> None:
232-
if self.checkout == CheckoutOption.MERGE:
233-
self.git_worktree(base_rev)
234-
self.git_merge(pr_rev)
235-
else:
236-
self.git_worktree(pr_rev)
237-
238240
def build(
239241
self, packages_per_system: dict[System, set[str]], args: str
240242
) -> dict[System, list[Attr]]:
@@ -272,15 +274,19 @@ def build_pr(self, pr_number: int) -> dict[System, list[Attr]]:
272274
packages_per_system = self.github_client.get_borg_eval_gist(pr)
273275
else:
274276
packages_per_system = None
275-
merge_rev, pr_rev = fetch_refs(
276-
self.remote,
277-
pr["base"]["ref"],
278-
f"pull/{pr['number']}/head",
279-
)
280277

281278
if self.checkout == CheckoutOption.MERGE:
282-
base_rev = merge_rev
279+
base_rev, pr_rev = fetch_refs(
280+
self.remote,
281+
pr["base"]["ref"],
282+
f"pull/{pr['number']}/merge",
283+
)
283284
else:
285+
merge_rev, pr_rev = fetch_refs(
286+
self.remote,
287+
pr["base"]["ref"],
288+
f"pull/{pr['number']}/head",
289+
)
284290
run = subprocess.run(
285291
["git", "merge-base", merge_rev, pr_rev],
286292
stdout=subprocess.PIPE,
@@ -295,7 +301,7 @@ def build_pr(self, pr_number: int) -> dict[System, list[Attr]]:
295301
if packages_per_system is None:
296302
return self.build_commit(base_rev, pr_rev)
297303

298-
self.checkout_pr(base_rev, pr_rev)
304+
self.git_worktree(pr_rev)
299305

300306
for system in list(packages_per_system.keys()):
301307
if system not in self.systems:
@@ -589,6 +595,15 @@ def filter_packages(
589595

590596
def fetch_refs(repo: str, *refs: str) -> list[str]:
591597
cmd = ["git", "-c", "fetch.prune=false", "fetch", "--no-tags", "--force", repo]
598+
shallow = subprocess.run(
599+
["git", "rev-parse", "--is-shallow-repository"],
600+
text=True,
601+
stdout=subprocess.PIPE,
602+
)
603+
if shallow.returncode != 0:
604+
raise NixpkgsReviewError(f"Failed to detect if {repo} is shallow repository")
605+
if shallow.stdout.strip() == "true":
606+
cmd.append("--depth=1")
592607
for i, ref in enumerate(refs):
593608
cmd.append(f"{ref}:refs/nixpkgs-review/{i}")
594609
res = sh(cmd)

tests/test_pr.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def test_pr_local_eval(helpers: Helpers, capfd: pytest.CaptureFixture) -> None:
2626
f.write("foo")
2727
subprocess.run(["git", "add", "."])
2828
subprocess.run(["git", "commit", "-m", "example-change"])
29-
subprocess.run(["git", "checkout", "-b", "pull/1/head"])
30-
subprocess.run(["git", "push", str(nixpkgs.remote), "pull/1/head"])
29+
subprocess.run(["git", "checkout", "-b", "pull/1/merge"])
30+
subprocess.run(["git", "push", str(nixpkgs.remote), "pull/1/merge"])
3131

3232
path = main(
3333
"nixpkgs-review",
@@ -54,8 +54,8 @@ def test_pr_local_eval_missing_nom(
5454
f.write("foo")
5555
subprocess.run(["git", "add", "."])
5656
subprocess.run(["git", "commit", "-m", "example-change"])
57-
subprocess.run(["git", "checkout", "-b", "pull/1/head"])
58-
subprocess.run(["git", "push", str(nixpkgs.remote), "pull/1/head"])
57+
subprocess.run(["git", "checkout", "-b", "pull/1/merge"])
58+
subprocess.run(["git", "push", str(nixpkgs.remote), "pull/1/merge"])
5959

6060
path = main(
6161
"nixpkgs-review",
@@ -82,8 +82,8 @@ def test_pr_local_eval_without_nom(
8282
f.write("foo")
8383
subprocess.run(["git", "add", "."])
8484
subprocess.run(["git", "commit", "-m", "example-change"])
85-
subprocess.run(["git", "checkout", "-b", "pull/1/head"])
86-
subprocess.run(["git", "push", str(nixpkgs.remote), "pull/1/head"])
85+
subprocess.run(["git", "checkout", "-b", "pull/1/merge"])
86+
subprocess.run(["git", "push", str(nixpkgs.remote), "pull/1/merge"])
8787

8888
path = main(
8989
"nixpkgs-review",
@@ -110,8 +110,8 @@ def test_pr_local_eval_with_sandbox(helpers: Helpers) -> None:
110110
f.write("foo")
111111
subprocess.run(["git", "add", "."])
112112
subprocess.run(["git", "commit", "-m", "example-change"])
113-
subprocess.run(["git", "checkout", "-b", "pull/1/head"])
114-
subprocess.run(["git", "push", str(nixpkgs.remote), "pull/1/head"])
113+
subprocess.run(["git", "checkout", "-b", "pull/1/merge"])
114+
subprocess.run(["git", "push", str(nixpkgs.remote), "pull/1/merge"])
115115

116116
path = main(
117117
"nixpkgs-review",
@@ -135,8 +135,8 @@ def test_pr_ofborg_eval(mock_urlopen: MagicMock, helpers: Helpers) -> None:
135135
f.write("foo")
136136
subprocess.run(["git", "add", "."])
137137
subprocess.run(["git", "commit", "-m", "example-change"])
138-
subprocess.run(["git", "checkout", "-b", "pull/37200/head"])
139-
subprocess.run(["git", "push", str(nixpkgs.remote), "pull/37200/head"])
138+
subprocess.run(["git", "checkout", "-b", "pull/37200/merge"])
139+
subprocess.run(["git", "push", str(nixpkgs.remote), "pull/37200/merge"])
140140

141141
mock_urlopen.side_effect = [
142142
mock_open(read_data=helpers.read_asset("github-pull-37200.json"))(),

0 commit comments

Comments
 (0)