Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: tag in fetchFromGitHub support #312

Merged
merged 9 commits into from
Jan 4, 2025
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ $ nix run github:Mic92/nix-update
## USAGE

First change to your directory containing the nix expression (Could be a nixpkgs
or your own repository). Than run `nix-update` as follows
or your own repository). Then run `nix-update` as follows

```console
$ nix-update attribute [--version version]
Expand All @@ -77,7 +77,7 @@ If your package is defined in a flake use the `--flake` flag instead:
$ nix-update attribute --flake [--version version]
```

`nix-update` will than try to update either the
`nix-update` will then try to update either the
`packages.{currentSystem}.{attribute}` or `{attribute}` output attribute of the
given flake. To update a package in `legacyPackages`, pass the full path to that
package including the platform: `legacyPackages.{platform}.{attribute}`.
Expand Down Expand Up @@ -260,10 +260,10 @@ First clone the repo to your preferred location (in the following, we assume
`~/` - your home):

```console
$ git clone https://github.com/Mic92/nix-update/ ~/nix-update
$ git clone https://github.com/Mic92/nix-update/ ~/git/nix-update
```

Than enter the dev shell:
Then enter the dev shell:

```console
$ cd ~/nix-update
Expand Down Expand Up @@ -297,7 +297,7 @@ $ nix fmt

## Known Bugs

nix-update might not work correctly if a file contain multiple packages as it
nix-update might not work correctly if a file contains multiple packages as it
performs naive search and replace to update version numbers. This might be a
problem if:

Expand Down
18 changes: 9 additions & 9 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion nix_update/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class Package:
src_homepage: str | None
changelog: str | None
maintainers: list[dict[str, str]] | None
rev: str
rev: str | None
tag: str | None
hash: str | None
go_modules: str | None
go_modules_old: str | None
Expand Down Expand Up @@ -175,6 +176,7 @@ def eval_expression(
urls = pkg.src.urls or null;
url = pkg.src.url or null;
rev = pkg.src.rev or null;
tag = pkg.src.tag or null;
hash = pkg.src.outputHash or null;
go_modules = pkg.goModules.outputHash or null;
go_modules_old = pkg.go-modules.outputHash or null;
Expand Down
26 changes: 16 additions & 10 deletions nix_update/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@

def replace_version(package: Package) -> bool:
assert package.new_version is not None
old_rev_tag = package.rev or package.tag
old_version = package.old_version
new_version = package.new_version.number
if new_version.startswith("v"):
new_version = new_version[1:]

changed = old_version != new_version or (
package.new_version.rev is not None and package.new_version.rev != package.rev
package.new_version.rev is not None and package.new_version.rev != old_rev_tag
)

if changed:
Expand All @@ -46,7 +47,7 @@ def replace_version(package: Package) -> bool:
with fileinput.FileInput(package.filename, inplace=True) as f:
for i, line in enumerate(f, 1):
if package.new_version.rev:
line = line.replace(package.rev, package.new_version.rev)
line = line.replace(old_rev_tag, package.new_version.rev)
if (
not version_string_in_version_declaration
or package.version_position.line == i
Expand Down Expand Up @@ -425,20 +426,22 @@ def update_version(
version_prefix = ""
if preference != VersionPreference.BRANCH:
branch = None
if package.rev and package.rev.endswith(package.old_version):
version_prefix = package.rev.removesuffix(package.old_version)
old_rev_tag = package.rev or package.tag
if old_rev_tag and old_rev_tag.endswith(package.old_version):
version_prefix = old_rev_tag.removesuffix(package.old_version)
elif version == "branch":
# fallback
branch = "HEAD"
else:
assert version.startswith("branch=")
branch = version[7:]
old_rev_tag = package.rev or package.tag
new_version = fetch_latest_version(
package.parsed_url,
preference,
version_regex,
branch,
package.rev,
old_rev_tag,
version_prefix,
)
package.new_version = new_version
Expand All @@ -457,20 +460,21 @@ def update_version(
package.diff_url = (
f"https://diff.rs/{parts[4]}/{package.old_version}/{new_version.number}"
)
old_rev_tag = package.rev or package.tag
if package.parsed_url.netloc == "registry.npmjs.org":
parts = package.parsed_url.path.split("/")
package.diff_url = f"https://npmdiff.dev/{parts[1]}/{package.old_version}/{new_version.number}"
elif package.parsed_url.netloc == "github.com":
_, owner, repo, *_ = package.parsed_url.path.split("/")
package.diff_url = f"https://github.com/{owner}/{repo.removesuffix('.git')}/compare/{package.rev}...{new_version.rev or new_version.number}"
package.diff_url = f"https://github.com/{owner}/{repo.removesuffix('.git')}/compare/{old_rev_tag}...{new_version.rev or new_version.number}"
elif package.parsed_url.netloc in ["codeberg.org", "gitea.com"]:
_, owner, repo, *_ = package.parsed_url.path.split("/")
package.diff_url = f"https://{package.parsed_url.netloc}/{owner}/{repo}/compare/{package.rev}...{new_version.rev or new_version.number}"
package.diff_url = f"https://{package.parsed_url.netloc}/{owner}/{repo}/compare/{old_rev_tag}...{new_version.rev or new_version.number}"
elif GITLAB_API.match(package.parsed_url.geturl()) and package.src_homepage:
package.diff_url = f"{package.src_homepage}-/compare/{package.rev}...{new_version.rev or new_version.number}"
package.diff_url = f"{package.src_homepage}-/compare/{old_rev_tag}...{new_version.rev or new_version.number}"
elif package.parsed_url.netloc in ["bitbucket.org", "bitbucket.io"]:
_, owner, repo, *_ = package.parsed_url.path.split("/")
package.diff_url = f"https://{package.parsed_url.netloc}/{owner}/{repo}/branches/compare/{new_version.rev or new_version.number}%0D{package.rev}"
package.diff_url = f"https://{package.parsed_url.netloc}/{owner}/{repo}/branches/compare/{new_version.rev or new_version.number}%0D{old_rev_tag}"

return replace_version(package)

Expand All @@ -492,7 +496,9 @@ def update(opts: Options) -> Package:
)

new_package = eval_attr(opts)
package.new_version = Version(new_package.old_version, rev=new_package.rev)
package.new_version = Version(
new_package.old_version, rev=new_package.rev, tag=new_package.tag
)

return package

Expand Down
4 changes: 2 additions & 2 deletions nix_update/version/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def fetch_latest_version(
preference: VersionPreference,
version_regex: str,
branch: str | None = None,
old_rev: str | None = None,
old_rev_tag: str | None = None,
version_prefix: str = "",
) -> Version:
unstable: list[str] = []
Expand Down Expand Up @@ -119,7 +119,7 @@ def fetch_latest_version(
None,
)

if ver is not None and ver.rev != old_rev:
if ver is not None and ver.rev != old_rev_tag:
return ver

return final[0]
Expand Down
1 change: 1 addition & 0 deletions nix_update/version/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Version:
number: str
prerelease: bool | None = None
rev: str | None = None
tag: str | None = None


class VersionPreference(StrEnum):
Expand Down
33 changes: 33 additions & 0 deletions tests/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,39 @@ def test_github_empty_fallback(helpers: conftest.Helpers) -> None:
)


def test_github_tag(helpers: conftest.Helpers) -> None:
with helpers.testpkgs(init_git=True) as path:
monkeypatch = pytest.MonkeyPatch()
monkeypatch.setenv("GITHUB_TOKEN", "invalid_token")
main(["--file", str(path), "--commit", "github-tag"])
version = subprocess.run(
[
"nix",
"eval",
"--raw",
"--extra-experimental-features",
"nix-command",
"-f",
path,
"github-tag.version",
],
check=True,
text=True,
stdout=subprocess.PIPE,
).stdout.strip()
assert tuple(map(int, version.split("."))) >= (8, 5, 2)
commit = subprocess.run(
["git", "-C", path, "log", "-1"],
text=True,
stdout=subprocess.PIPE,
check=True,
).stdout.strip()
print(commit)
assert version in commit
assert "github" in commit
assert "https://github.com/sharkdp/fd/compare/v8.0.0...v" in commit


def test_github_feed_fallback(helpers: conftest.Helpers) -> None:
with helpers.testpkgs(init_git=True) as path:
monkeypatch = pytest.MonkeyPatch()
Expand Down
1 change: 1 addition & 0 deletions tests/testpkgs/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
gitea = pkgs.callPackage ./gitea.nix { };
github = pkgs.callPackage ./github.nix { };
github-no-release = pkgs.callPackage ./github-no-release.nix { };
github-tag = pkgs.callPackage ./github-tag.nix { };
gitlab = pkgs.callPackage ./gitlab.nix { };
pypi = pkgs.python3.pkgs.callPackage ./pypi.nix { };
sourcehut = pkgs.python3.pkgs.callPackage ./sourcehut.nix { };
Expand Down
13 changes: 13 additions & 0 deletions tests/testpkgs/github-tag.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{ stdenv, fetchFromGitHub }:

stdenv.mkDerivation rec {
pname = "fd";
version = "8.0.0";

src = fetchFromGitHub {
owner = "sharkdp";
repo = pname;
tag = "v${version}";
sha256 = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
};
}
Loading