diff --git a/README.md b/README.md index 1f9066d..78c0470 100644 --- a/README.md +++ b/README.md @@ -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] @@ -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}`. @@ -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 @@ -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: diff --git a/flake.lock b/flake.lock index 4f3a5cf..a5a9b85 100644 --- a/flake.lock +++ b/flake.lock @@ -7,11 +7,11 @@ ] }, "locked": { - "lastModified": 1730504689, - "narHash": "sha256-hgmguH29K2fvs9szpq2r3pz2/8cJd2LPS+b4tfNFCwE=", + "lastModified": 1733312601, + "narHash": "sha256-4pDvzqnegAfRkPwO3wmwBhVi/Sye1mzps0zHWYnP88c=", "owner": "hercules-ci", "repo": "flake-parts", - "rev": "506278e768c2a08bec68eb62932193e341f55c90", + "rev": "205b12d8b7cd4802fbcb8e8ef6a0f1408781a4f9", "type": "github" }, "original": { @@ -22,11 +22,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1731816655, - "narHash": "sha256-55e1JMAuYvHZs9EICprWgJ4RmaWwDuSjzJ5K7S7zb6w=", + "lastModified": 1734820311, + "narHash": "sha256-YsLK4ZiGY5CZmmgzsfU76OHVUTDeZJgirKzNO+et0UQ=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "0a14706530dcb90acecb81ce0da219d88baaae75", + "rev": "7e4a1594489d41bf8e16046b28e14a0e264c9baa", "type": "github" }, "original": { @@ -50,11 +50,11 @@ ] }, "locked": { - "lastModified": 1730321837, - "narHash": "sha256-vK+a09qq19QNu2MlLcvN4qcRctJbqWkX7ahgPZ/+maI=", + "lastModified": 1734704479, + "narHash": "sha256-MMi74+WckoyEWBRcg/oaGRvXC9BVVxDZNRMpL+72wBI=", "owner": "numtide", "repo": "treefmt-nix", - "rev": "746901bb8dba96d154b66492a29f5db0693dbfcc", + "rev": "65712f5af67234dad91a5a4baee986a8b62dbf8f", "type": "github" }, "original": { diff --git a/nix_update/eval.py b/nix_update/eval.py index b4a553f..f3fc220 100644 --- a/nix_update/eval.py +++ b/nix_update/eval.py @@ -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 @@ -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; diff --git a/nix_update/update.py b/nix_update/update.py index b4e6e8d..2265985 100644 --- a/nix_update/update.py +++ b/nix_update/update.py @@ -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: @@ -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 @@ -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 @@ -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) @@ -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 diff --git a/nix_update/version/__init__.py b/nix_update/version/__init__.py index 11dcb71..9e94458 100644 --- a/nix_update/version/__init__.py +++ b/nix_update/version/__init__.py @@ -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] = [] @@ -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] diff --git a/nix_update/version/version.py b/nix_update/version/version.py index c10b01f..061a573 100644 --- a/nix_update/version/version.py +++ b/nix_update/version/version.py @@ -7,6 +7,7 @@ class Version: number: str prerelease: bool | None = None rev: str | None = None + tag: str | None = None class VersionPreference(StrEnum): diff --git a/tests/test_github.py b/tests/test_github.py index 3a39c46..f55e799 100644 --- a/tests/test_github.py +++ b/tests/test_github.py @@ -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() diff --git a/tests/testpkgs/default.nix b/tests/testpkgs/default.nix index 1a798f6..d1dc90f 100644 --- a/tests/testpkgs/default.nix +++ b/tests/testpkgs/default.nix @@ -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 { }; diff --git a/tests/testpkgs/github-tag.nix b/tests/testpkgs/github-tag.nix new file mode 100644 index 0000000..4eb593b --- /dev/null +++ b/tests/testpkgs/github-tag.nix @@ -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="; + }; +}