fetchFromGitLab: align with fetchFromGitHub

This commit is contained in:
Julie Tyché Bettens 2024-05-21 10:52:45 +02:00
parent 7ec6274c37
commit afb8fbbbb6

View file

@ -1,36 +1,67 @@
{ lib, fetchgit, fetchzip }: { lib, fetchgit, fetchzip }:
lib.makeOverridable ( lib.makeOverridable (
# gitlab example { owner, repo, rev
{ owner, repo, rev, protocol ? "https", domain ? "gitlab.com", name ? "source", group ? null , name ? null # Override with null to use the default value
, fetchSubmodules ? false, leaveDotGit ? false , pname ? lib.concatStringsSep "-" ([ "source" domain ] ++ (lib.optional (group != null) group) ++ [ owner repo ])
, fetchSubmodules ? false, leaveDotGit ? null
, deepClone ? false, forceFetchGit ? false , deepClone ? false, forceFetchGit ? false
, sparseCheckout ? [] , sparseCheckout ? []
, protocol ? "https", domain ? "gitlab.com", group ? null
, passthru ? { }
, meta ? { }
, ... # For hash agility , ... # For hash agility
} @ args: }@args:
let let
name = if args.name or null != null then args.name
else "${pname}-${rev}";
position = (if args.meta.description or null != null
then builtins.unsafeGetAttrPos "description" args.meta
else builtins.unsafeGetAttrPos "rev" args
);
baseUrl = "${protocol}://${domain}/${slug}";
newPassthru = passthru // {
inherit rev owner repo;
};
newMeta = meta // {
homepage = meta.homepage or baseUrl;
} // lib.optionalAttrs (position != null) {
# to indicate where derivation originates, similar to make-derivation.nix's mkDerivation
position = "${position.file}:${toString position.line}";
};
passthruAttrs = removeAttrs args [ "protocol" "domain" "owner" "group" "repo" "rev" "fetchSubmodules" "forceFetchGit" "leaveDotGit" "deepClone" ];
slug = lib.concatStringsSep "/" ((lib.optional (group != null) group) ++ [ owner repo ]); slug = lib.concatStringsSep "/" ((lib.optional (group != null) group) ++ [ owner repo ]);
escapedSlug = lib.replaceStrings [ "." "/" ] [ "%2E" "%2F" ] slug; escapedSlug = lib.replaceStrings [ "." "/" ] [ "%2E" "%2F" ] slug;
escapedRev = lib.replaceStrings [ "+" "%" "/" ] [ "%2B" "%25" "%2F" ] rev; escapedRev = lib.replaceStrings [ "+" "%" "/" ] [ "%2B" "%25" "%2F" ] rev;
passthruAttrs = removeAttrs args [ "protocol" "domain" "owner" "group" "repo" "rev" "fetchSubmodules" "forceFetchGit" "leaveDotGit" "deepClone" ]; useFetchGit = fetchSubmodules || (leaveDotGit == true) || deepClone || forceFetchGit || (sparseCheckout != []);
# We prefer fetchzip in cases we don't need submodules as the hash
# is more stable in that case.
fetcher =
if useFetchGit then fetchgit
# fetchzip may not be overridable when using external tools, for example nix-prefetch
else if fetchzip ? override then fetchzip.override { withUnzip = false; }
else fetchzip;
useFetchGit = fetchSubmodules || leaveDotGit || deepClone || forceFetchGit || (sparseCheckout != []); gitRepoUrl = "${baseUrl}.git";
fetcher = if useFetchGit then fetchgit else fetchzip;
gitRepoUrl = "${protocol}://${domain}/${slug}.git"; fetcherArgs = (if useFetchGit
then {
inherit rev deepClone fetchSubmodules sparseCheckout; url = gitRepoUrl;
passthru = newPassthru;
} // lib.optionalAttrs (leaveDotGit != null) { inherit leaveDotGit; }
else {
url = "${protocol}://${domain}/api/v4/projects/${escapedSlug}/repository/archive.tar.gz?sha=${escapedRev}";
fetcherArgs = (if useFetchGit then { passthru = newPassthru // {
inherit rev deepClone fetchSubmodules sparseCheckout leaveDotGit; inherit gitRepoUrl;
url = gitRepoUrl; };
} else { }
url = "${protocol}://${domain}/api/v4/projects/${escapedSlug}/repository/archive.tar.gz?sha=${escapedRev}"; ) // passthruAttrs // { inherit name; };
passthru = {
inherit gitRepoUrl;
};
}) // passthruAttrs // { inherit name; };
in in
fetcher fetcherArgs // { meta.homepage = "${protocol}://${domain}/${slug}/"; inherit rev owner repo; } (fetcher fetcherArgs).overrideAttrs (finalAttrs: previousAttrs: {
meta = newMeta;
})
) )