Skip to content

Commit 7b7b4da

Browse files
authored
Merge pull request #528 from Mic92/git-author
Always set git author/committer
2 parents 0715e46 + ec51871 commit 7b7b4da

File tree

7 files changed

+79
-28
lines changed

7 files changed

+79
-28
lines changed

nixpkgs_review/builddir.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
from tempfile import TemporaryDirectory
66
from typing import Any, Union
77

8+
from . import git
89
from .overlay import Overlay
9-
from .utils import sh, warn
10+
from .utils import warn
1011

1112

1213
class DisableKeyboardInterrupt:
@@ -89,7 +90,7 @@ def __exit__(
8990

9091
with DisableKeyboardInterrupt():
9192
if Path.exists(self.worktree_dir / ".git"):
92-
res = sh(["git", "worktree", "remove", "-f", str(self.worktree_dir)])
93+
res = git.run(["worktree", "remove", "-f", str(self.worktree_dir)])
9394
if res.returncode != 0:
9495
warn(
9596
f"Failed to remove worktree at {self.worktree_dir}. Please remove it manually. Git failed with: {res.returncode}"

nixpkgs_review/cli/rev.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import argparse
22
from pathlib import Path
33

4+
from nixpkgs_review import git
45
from nixpkgs_review.allow import AllowedFeatures
56
from nixpkgs_review.buildenv import Buildenv
67
from nixpkgs_review.review import review_local_revision
7-
from nixpkgs_review.utils import verify_commit_hash
88

99

1010
def rev_command(args: argparse.Namespace) -> Path:
1111
allow = AllowedFeatures(args.allow)
1212
with Buildenv(allow.aliases, args.extra_nixpkgs_config) as nixpkgs_config:
13-
commit = verify_commit_hash(args.commit)
13+
commit = git.verify_commit_hash(args.commit)
1414
return review_local_revision(
1515
f"rev-{commit}",
1616
args,

nixpkgs_review/cli/wip.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import argparse
22
from pathlib import Path
33

4+
from nixpkgs_review import git
45
from nixpkgs_review.allow import AllowedFeatures
56
from nixpkgs_review.buildenv import Buildenv
67
from nixpkgs_review.review import review_local_revision
7-
from nixpkgs_review.utils import verify_commit_hash
88

99

1010
def wip_command(args: argparse.Namespace) -> Path:
1111
allow = AllowedFeatures(args.allow)
1212
with Buildenv(allow.aliases, args.extra_nixpkgs_config) as nixpkgs_config:
1313
return review_local_revision(
14-
f"rev-{verify_commit_hash('HEAD')}-dirty",
14+
f"rev-{git.verify_commit_hash('HEAD')}-dirty",
1515
args,
1616
allow,
1717
nixpkgs_config,

nixpkgs_review/git.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import subprocess
2+
from pathlib import Path
3+
4+
from .errors import NixpkgsReviewError
5+
from .utils import sh
6+
7+
8+
def run(
9+
command: list[str],
10+
cwd: Path | str | None = None,
11+
stdin: str | None = None,
12+
stdout: int | None = None,
13+
stderr: int | None = None,
14+
quiet: bool = False,
15+
) -> subprocess.CompletedProcess[str]:
16+
"""Run a git command with nixpkgs-review identity."""
17+
env = {
18+
"GIT_AUTHOR_NAME": "nixpkgs-review",
19+
"GIT_AUTHOR_EMAIL": "nixpkgs-review@example.com",
20+
"GIT_COMMITTER_NAME": "nixpkgs-review",
21+
"GIT_COMMITTER_EMAIL": "nixpkgs-review@example.com",
22+
}
23+
return sh(
24+
["git", *command],
25+
cwd=cwd,
26+
env=env,
27+
stdin=stdin,
28+
stdout=stdout,
29+
stderr=stderr,
30+
quiet=quiet,
31+
)
32+
33+
34+
def verify_commit_hash(commit: str) -> str:
35+
"""Verify and return full commit hash."""
36+
cmd = ["rev-parse", "--verify", commit]
37+
proc = run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, quiet=True)
38+
if proc.returncode != 0:
39+
error_message = f"Failed to verify commit hash '{commit}': {proc.stderr or 'Invalid commit'}"
40+
raise NixpkgsReviewError(error_message)
41+
return proc.stdout.strip()

nixpkgs_review/review.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from typing import IO
1919
from xml.etree import ElementTree as ET
2020

21+
from . import git
2122
from .allow import AllowedFeatures
2223
from .builddir import Builddir
2324
from .errors import NixpkgsReviewError
@@ -276,41 +277,36 @@ def _display_pr_info(self, pr: dict, pr_number: int) -> None:
276277
print(f"{'=' * 80}\n")
277278

278279
def git_merge(self, commit: str) -> None:
279-
res = sh(
280-
["git", "merge", "--no-commit", "--no-ff", commit], cwd=self.worktree_dir()
280+
res = git.run(
281+
["merge", "--no-commit", "--no-ff", commit], cwd=self.worktree_dir()
281282
)
282283
if res.returncode != 0:
283284
msg = f"Failed to merge {commit} into {self.worktree_dir()}. git merge failed with exit code {res.returncode}"
284285
raise NixpkgsReviewError(msg)
285286

286287
def git_checkout(self, commit: str) -> None:
287-
res = sh(["git", "checkout", commit], cwd=self.worktree_dir())
288+
res = git.run(["checkout", commit], cwd=self.worktree_dir())
288289
if res.returncode != 0:
289290
msg = f"Failed to checkout {commit} in {self.worktree_dir()}. git checkout failed with exit code {res.returncode}"
290291
raise NixpkgsReviewError(msg)
291292

292293
def apply_unstaged(self, staged: bool = False) -> None:
293294
args = [
294-
"git",
295295
"--no-pager",
296296
"diff",
297297
"--no-ext-diff",
298298
"--src-prefix=a/",
299299
"--dst-prefix=b/",
300300
]
301301
args.extend(["--staged"] if staged else [])
302-
with subprocess.Popen(args, stdout=subprocess.PIPE) as diff_proc:
303-
assert diff_proc.stdout
304-
diff = diff_proc.stdout.read()
302+
diff = git.run(args, stdout=subprocess.PIPE).stdout
305303

306304
if not diff:
307305
info("No diff detected, stopping review...")
308306
sys.exit(0)
309307

310308
info("Applying `nixpkgs` diff...")
311-
result = subprocess.run(
312-
["git", "apply"], cwd=self.worktree_dir(), input=diff, check=False
313-
)
309+
result = git.run(["apply"], cwd=self.worktree_dir(), stdin=diff)
314310

315311
if result.returncode != 0:
316312
warn(f"Failed to apply diff in {self.worktree_dir()}")
@@ -386,7 +382,7 @@ def build_commit(
386382
return self.build(changed_attrs, self.build_args)
387383

388384
def git_worktree(self, commit: str) -> None:
389-
res = sh(["git", "worktree", "add", self.worktree_dir(), commit])
385+
res = git.run(["worktree", "add", self.worktree_dir(), commit])
390386
if res.returncode != 0:
391387
msg = f"Failed to add worktree for {commit} in {self.worktree_dir()}. git worktree failed with exit code {res.returncode}"
392388
raise NixpkgsReviewError(msg)

nixpkgs_review/utils.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import functools
2+
import os
23
import shlex
34
import shutil
45
import subprocess
@@ -36,16 +37,30 @@ def to_link(uri: str, text: str) -> str:
3637

3738

3839
def sh(
39-
command: list[str], cwd: Path | str | None = None
40+
command: list[str],
41+
cwd: Path | str | None = None,
42+
env: dict[str, str] | None = None,
43+
stdin: str | None = None,
44+
stdout: int | None = None,
45+
stderr: int | None = None,
46+
quiet: bool = False,
4047
) -> "subprocess.CompletedProcess[str]":
41-
info("$ " + shlex.join(command))
42-
return subprocess.run(command, cwd=cwd, text=True, check=False)
43-
44-
45-
def verify_commit_hash(commit: str) -> str:
46-
cmd = ["git", "rev-parse", "--verify", commit]
47-
proc = subprocess.run(cmd, check=True, stdout=subprocess.PIPE, text=True)
48-
return proc.stdout.strip()
48+
if not quiet:
49+
info("$ " + shlex.join(command))
50+
if env is not None:
51+
full_env = os.environ.copy()
52+
full_env.update(env)
53+
env = full_env
54+
return subprocess.run(
55+
command,
56+
cwd=cwd,
57+
text=True,
58+
check=False,
59+
env=env,
60+
input=stdin,
61+
stdout=stdout,
62+
stderr=stderr,
63+
)
4964

5065

5166
def escape_attr(attr: str) -> str:

pyproject.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ lint.ignore = [
4343
# fixmes
4444
"FIX",
4545

46-
# Missing type annotation for `self` in method
47-
"ANN101",
4846
# Dynamically typed expressions (typing.Any)
4947
"ANN401",
5048
# Trailing comma missing

0 commit comments

Comments
 (0)