Unverified Commit 5c2d20cf authored by Jasi's avatar Jasi
Browse files

fetchFromBitbucket: implement fetchgit codepath

This allows `fetchFromBitbucket` to interface with `fetchgit` if
git-related arguments are given.
parent cfae051f
Loading
Loading
Loading
Loading
+93 −12
Original line number Diff line number Diff line
{
  lib,
  repoRevToNameMaybe,
  fetchgit,
  fetchzip,
}:

@@ -8,23 +9,103 @@ lib.makeOverridable (
  {
    owner,
    repo,
    rev,
    name ? repoRevToNameMaybe repo rev "bitbucket",
    tag ? null,
    rev ? null,
    name ? repoRevToNameMaybe repo (lib.revOrTag rev tag) "bitbucket",
    fetchSubmodules ? false,
    leaveDotGit ? false,
    deepClone ? false,
    forceFetchGit ? false,
    fetchLFS ? false,
    rootDir ? "",
    sparseCheckout ? lib.optional (rootDir != "") rootDir,
    meta ? { },
    ... # For hash agility
  }@args:
  fetchzip (
    {
      inherit name;
      url = "https://bitbucket.org/${owner}/${repo}/get/${lib.strings.escapeURL rev}.tar.gz";
      meta.homepage = "https://bitbucket.org/${owner}/${repo}/";

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

  let
    position = (
      if args.meta.description or null != null then
        builtins.unsafeGetAttrPos "description" args.meta
      else if tag != null then
        builtins.unsafeGetAttrPos "tag" args
      else
        builtins.unsafeGetAttrPos "rev" args
    );
    baseUrl = "https://bitbucket.org/${owner}/${repo}";
    newMeta =
      meta
      // {
        homepage = meta.homepage or baseUrl;
      }
    // removeAttrs args [
      // lib.optionalAttrs (position != null) {
        # to indicate where derivation originates, similar to make-derivation.nix's mkDerivation
        position = "${position.file}:${toString position.line}";
      };
    gitRepoUrl = "${baseUrl}.git";

    # the tag is escaped to support mercurial-based tags as bitbucket supports them
    revWithTag = if tag != null then "refs/tags/${lib.strings.escapeURL tag}" else rev;

    passthruAttrs = removeAttrs args [
      "owner"
      "repo"
      "rev"
    ]
      "tag"
      "fetchSubmodules"
      "forceFetchGit"
    ];

    useFetchGit =
      fetchSubmodules
      || (leaveDotGit == true)
      || deepClone
      || forceFetchGit
      || fetchLFS
      || (rootDir != "")
      || (sparseCheckout != [ ]);

    fetcher = if useFetchGit then fetchgit else fetchzip;

    fetcherArgs =
      (
        if useFetchGit then
          {
            inherit
              rev
              tag
              deepClone
              fetchSubmodules
              sparseCheckout
              fetchLFS
              ;
            url = gitRepoUrl;
          }
          // lib.optionalAttrs (leaveDotGit != null) { inherit leaveDotGit; }
        else
          {
            url = "https://bitbucket.org/${owner}/${repo}/get/${revWithTag}.tar.gz";
            extension = "tar.gz";
            passthru = {
              inherit gitRepoUrl;
            };
          }
      )
      // passthruAttrs
      // {
        inherit name;
      };
  in
  fetcher fetcherArgs
  // {
    inherit rev;
    meta = newMeta;
    inherit owner repo tag;
    rev = revWithTag;
  }
)