Skip to content

Commit

Permalink
fetch{gitlab,gitiles}: add tag argument (NixOS#367322)
Browse files Browse the repository at this point in the history
  • Loading branch information
philiptaron authored Dec 24, 2024
2 parents ce1a731 + e64344c commit c7b408f
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 36 deletions.
18 changes: 15 additions & 3 deletions pkgs/build-support/fetchgitiles/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,36 @@
lib.makeOverridable (
{
url,
rev,
rev ? null,
tag ? null,
name ? "source",
...
}@args:

assert (
lib.assertMsg (lib.xor (tag == null) (
rev == null
)) "fetchFromGitiles requires one of either `rev` or `tag` to be provided (not both)."
);

let
realrev = (if tag != null then "refs/tags/" + tag else rev);
in

fetchzip (
{
inherit name;
url = "${url}/+archive/${rev}.tar.gz";
url = "${url}/+archive/${realrev}.tar.gz";
stripRoot = false;
meta.homepage = url;
}
// removeAttrs args [
"url"
"tag"
"rev"
]
)
// {
inherit rev;
inherit rev tag;
}
)
133 changes: 100 additions & 33 deletions pkgs/build-support/fetchgitlab/default.nix
Original file line number Diff line number Diff line change
@@ -1,36 +1,103 @@
{ lib, fetchgit, fetchzip }:
{
lib,
fetchgit,
fetchzip,
}:

lib.makeOverridable (
# gitlab example
{ owner, repo, rev, protocol ? "https", domain ? "gitlab.com", name ? "source", group ? null
, fetchSubmodules ? false, leaveDotGit ? false
, deepClone ? false, forceFetchGit ? false
, sparseCheckout ? []
, ... # For hash agility
} @ args:

let
slug = lib.concatStringsSep "/" ((lib.optional (group != null) group) ++ [ owner repo ]);
escapedSlug = lib.replaceStrings [ "." "/" ] [ "%2E" "%2F" ] slug;
escapedRev = lib.replaceStrings [ "+" "%" "/" ] [ "%2B" "%25" "%2F" ] rev;
passthruAttrs = removeAttrs args [ "protocol" "domain" "owner" "group" "repo" "rev" "fetchSubmodules" "forceFetchGit" "leaveDotGit" "deepClone" ];

useFetchGit = fetchSubmodules || leaveDotGit || deepClone || forceFetchGit || (sparseCheckout != []);
fetcher = if useFetchGit then fetchgit else fetchzip;

gitRepoUrl = "${protocol}://${domain}/${slug}.git";

fetcherArgs = (if useFetchGit then {
inherit rev deepClone fetchSubmodules sparseCheckout leaveDotGit;
url = gitRepoUrl;
} else {
url = "${protocol}://${domain}/api/v4/projects/${escapedSlug}/repository/archive.tar.gz?sha=${escapedRev}";

passthru = {
inherit gitRepoUrl;
};
}) // passthruAttrs // { inherit name; };
in

fetcher fetcherArgs // { meta.homepage = "${protocol}://${domain}/${slug}/"; inherit rev owner repo; }
# gitlab example
{
owner,
repo,
rev ? null,
tag ? null,
protocol ? "https",
domain ? "gitlab.com",
name ? "source",
group ? null,
fetchSubmodules ? false,
leaveDotGit ? false,
deepClone ? false,
forceFetchGit ? false,
sparseCheckout ? [ ],
... # For hash agility
}@args:

assert (
lib.assertMsg (lib.xor (tag == null) (
rev == null
)) "fetchFromGitLab requires one of either `rev` or `tag` to be provided (not both)."
);

let
slug = lib.concatStringsSep "/" (
(lib.optional (group != null) group)
++ [
owner
repo
]
);
escapedSlug = lib.replaceStrings [ "." "/" ] [ "%2E" "%2F" ] slug;
escapedRev = lib.replaceStrings [ "+" "%" "/" ] [ "%2B" "%25" "%2F" ] (
if tag != null then "refs/tags/" + tag else rev
);
passthruAttrs = removeAttrs args [
"protocol"
"domain"
"owner"
"group"
"repo"
"rev"
"tag"
"fetchSubmodules"
"forceFetchGit"
"leaveDotGit"
"deepClone"
];

useFetchGit =
fetchSubmodules || leaveDotGit || deepClone || forceFetchGit || (sparseCheckout != [ ]);
fetcher = if useFetchGit then fetchgit else fetchzip;

gitRepoUrl = "${protocol}://${domain}/${slug}.git";

fetcherArgs =
(
if useFetchGit then
{
inherit
rev
deepClone
tag
fetchSubmodules
sparseCheckout
leaveDotGit
;
url = gitRepoUrl;
}
else
{
url = "${protocol}://${domain}/api/v4/projects/${escapedSlug}/repository/archive.tar.gz?sha=${escapedRev}";

passthru = {
inherit gitRepoUrl;
};
}
)
// passthruAttrs
// {
inherit name;
};
in

fetcher fetcherArgs
// {
meta.homepage = "${protocol}://${domain}/${slug}/";
inherit
tag
rev
owner
repo
;
}
)

0 comments on commit c7b408f

Please sign in to comment.