From 3df013597d7a2b5e400839e6625c05bd47de4dca Mon Sep 17 00:00:00 2001 From: Linus Heckemann Date: Thu, 30 May 2024 23:20:42 +0200 Subject: [PATCH] libfetchers: handle nonexistent refs in GitLab repos more gracefully MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before: $ nix flake lock --override-input nixpkgs gitlab:simple-nixos-mailserver/nixos-mailserver/nonexistent fetching git input 'git+file:///home/linus/projects/lix' fetching gitlab input 'gitlab:simple-nixos-mailserver/nixos-mailserver/nonexistent' error: [json.exception.type_error.302] type must be string, but is null After: $ outputs/out/bin/nix flake lock --override-input nixpkgs gitlab:simple-nixos-mailserver/nixos-mailserver/nonexistent fetching git input 'git+file:///home/linus/projects/lix' fetching gitlab input 'gitlab:simple-nixos-mailserver/nixos-mailserver/nonexistent' error: … while updating the lock file of flake 'git+file:///home/linus/projects/lix?ref=refs/heads/fix-gitlab-nonexistent&rev=915f16a619a36237a099b9aa9afed6d14ff613b4' … while updating the flake input 'nixpkgs' … while fetching the input 'gitlab:simple-nixos-mailserver/nixos-mailserver/nonexistent' error: No commits returned by GitLab API -- does the ref really exist? Change-Id: Id9bc79d98348500e152ed519bb3ac79a3d15c38d --- src/libfetchers/github.cc | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/libfetchers/github.cc b/src/libfetchers/github.cc index 6f997885d..3129e0d73 100644 --- a/src/libfetchers/github.cc +++ b/src/libfetchers/github.cc @@ -322,9 +322,15 @@ struct GitLabInputScheme : GitArchiveInputScheme readFile( store->toRealPath( downloadFile(store, url, "source", false, headers).storePath))); - auto rev = Hash::parseAny(std::string(json[0]["id"]), htSHA1); - debug("HEAD revision for '%s' is %s", url, rev.gitRev()); - return rev; + if (json.is_array() && json.size() == 1 && json[0]["id"] != nullptr) { + auto rev = Hash::parseAny(std::string(json[0]["id"]), htSHA1); + debug("HEAD revision for '%s' is %s", url, rev.gitRev()); + return rev; + } else if (json.is_array() && json.size() == 0) { + throw Error("No commits returned by GitLab API -- does the ref really exist?"); + } else { + throw Error("Didn't know what to do with response from GitLab: %s", json); + } } DownloadUrl getDownloadUrl(const Input & input) const override