From f19a101a73fcd447f6cfbaf7a8b01583ae6caf12 Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Fri, 14 Feb 2020 06:05:43 +0100 Subject: [PATCH 1/2] nix-prefetch-git: report deepClone & leaveDotGit Just like in https://github.com/NixOS/nixpkgs/commit/be30ba8e0eabbc2483122ddf9d5911484df3a578 we want `fetchgit (builtins.fromJSON (builtins.readFile ./src.json))` to work. --- pkgs/build-support/fetchgit/nix-prefetch-git | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pkgs/build-support/fetchgit/nix-prefetch-git b/pkgs/build-support/fetchgit/nix-prefetch-git index 8020ba46f3f..38c00d9fd9c 100755 --- a/pkgs/build-support/fetchgit/nix-prefetch-git +++ b/pkgs/build-support/fetchgit/nix-prefetch-git @@ -25,7 +25,7 @@ commitDateStrict8601= if test -n "$deepClone"; then deepClone=true else - deepClone=false + deepClone= fi if test "$leaveDotGit" != 1; then @@ -65,7 +65,7 @@ for arg; do --branch-name) argfun=set_branchName;; --deepClone) deepClone=true;; --quiet) QUIET=true;; - --no-deepClone) deepClone=false;; + --no-deepClone) deepClone=;; --leave-dotGit) leaveDotGit=true;; --fetch-submodules) fetchSubmodules=true;; --builder) builder=true;; @@ -150,7 +150,7 @@ checkout_ref(){ local hash="$1" local ref="$2" - if "$deepClone"; then + if [[ -n "$deepClone" ]]; then # The caller explicitly asked for a deep clone. Deep clones # allow "git describe" and similar tools to work. See # https://marc.info/?l=nix-dev&m=139641582514772 @@ -369,7 +369,9 @@ print_results() { "rev": "$(json_escape "$fullRev")", "date": "$(json_escape "$commitDateStrict8601")", "$(json_escape "$hashType")": "$(json_escape "$hash")", - "fetchSubmodules": $([[ -n "$fetchSubmodules" ]] && echo true || echo false) + "fetchSubmodules": $([[ -n "$fetchSubmodules" ]] && echo true || echo false), + "deepClone": $([[ -n "$deepClone" ]] && echo true || echo false), + "leaveDotGit": $([[ -n "$leaveDotGit" ]] && echo true || echo false) } EOF fi From 28faf5bc8622f4fec12321cac9bd7950856f1d91 Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Fri, 14 Feb 2020 21:27:47 +0100 Subject: [PATCH 2/2] nix-prefetch-git: Fix output mangling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `git repack` and `git gc` sometimes print “Nothing new to pack.” to stdout, which breaks redirecting output to JSON file. Let’s move the stdout of all git calls where it is not used to stderr so that we still receive the info but it does not pollute our output. --- pkgs/build-support/fetchgit/nix-prefetch-git | 31 ++++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/pkgs/build-support/fetchgit/nix-prefetch-git b/pkgs/build-support/fetchgit/nix-prefetch-git index 38c00d9fd9c..abba76bd1ac 100755 --- a/pkgs/build-support/fetchgit/nix-prefetch-git +++ b/pkgs/build-support/fetchgit/nix-prefetch-git @@ -53,6 +53,11 @@ Options: exit 1 } +# some git commands print to stdout, which would contaminate our JSON output +clean_git(){ + git "$@" >&2 +} + argi=0 argfun="" for arg; do @@ -98,9 +103,9 @@ fi init_remote(){ local url=$1 - git init - git remote add origin "$url" - ( [ -n "$http_proxy" ] && git config http.proxy "$http_proxy" ) || true + clean_git init + clean_git remote add origin "$url" + ( [ -n "$http_proxy" ] && clean_git config http.proxy "$http_proxy" ) || true } # Return the reference of an hash if it exists on the remote repository. @@ -141,8 +146,8 @@ checkout_hash(){ hash=$(hash_from_ref "$ref") fi - git fetch -t ${builder:+--progress} origin || return 1 - git checkout -b "$branchName" "$hash" || return 1 + clean_git fetch -t ${builder:+--progress} origin || return 1 + clean_git checkout -b "$branchName" "$hash" || return 1 } # Fetch only a branch/tag and checkout it. @@ -164,8 +169,8 @@ checkout_ref(){ if test -n "$ref"; then # --depth option is ignored on http repository. - git fetch ${builder:+--progress} --depth 1 origin +"$ref" || return 1 - git checkout -b "$branchName" FETCH_HEAD || return 1 + clean_git fetch ${builder:+--progress} --depth 1 origin +"$ref" || return 1 + clean_git checkout -b "$branchName" FETCH_HEAD || return 1 else return 1 fi @@ -174,7 +179,7 @@ checkout_ref(){ # Update submodules init_submodules(){ # Add urls into .git/config file - git submodule init + clean_git submodule init # list submodule directories and their hashes git submodule status | @@ -248,7 +253,7 @@ make_deterministic_repo(){ # Remove all remote branches. git branch -r | while read -r branch; do - git branch -rD "$branch" >&2 + clean_git branch -rD "$branch" done # Remove tags not reachable from HEAD. If we're exactly on a tag, don't @@ -256,19 +261,19 @@ make_deterministic_repo(){ maybe_tag=$(git tag --points-at HEAD) git tag --contains HEAD | while read -r tag; do if [ "$tag" != "$maybe_tag" ]; then - git tag -d "$tag" >&2 + clean_git tag -d "$tag" fi done # Do a full repack. Must run single-threaded, or else we lose determinism. - git config pack.threads 1 - git repack -A -d -f + clean_git config pack.threads 1 + clean_git repack -A -d -f rm -f .git/config # Garbage collect unreferenced objects. # Note: --keep-largest-pack prevents non-deterministic ordering of packs # listed in .git/objects/info/packs by only using a single pack - git gc --prune=all --keep-largest-pack + clean_git gc --prune=all --keep-largest-pack ) }