guix-clean raw

   1  #!/usr/bin/env bash
   2  export LC_ALL=C
   3  set -e -o pipefail
   4  
   5  # Source the common prelude, which:
   6  #   1. Checks if we're at the top directory of the Bitcoin Core repository
   7  #   2. Defines a few common functions and variables
   8  #
   9  # shellcheck source=libexec/prelude.bash
  10  source "$(dirname "${BASH_SOURCE[0]}")/libexec/prelude.bash"
  11  
  12  # Parse supported args
  13  FORCE=0
  14  if [[ $* == "--force" ]]; then
  15      FORCE=1
  16  elif [ $# != 0 ]; then
  17      echo "Script only takes optional --force arg."
  18      exit 1
  19  fi
  20  
  21  ###################
  22  ## Sanity Checks ##
  23  ###################
  24  
  25  ################
  26  # Required non-builtin commands should be invokable
  27  ################
  28  
  29  check_tools cat mkdir make git guix
  30  
  31  
  32  #############
  33  ##  Clean  ##
  34  #############
  35  
  36  # Usage: under_dir MAYBE_PARENT MAYBE_CHILD
  37  #
  38  # If MAYBE_CHILD is a subdirectory of MAYBE_PARENT, print the relative path
  39  # from MAYBE_PARENT to MAYBE_CHILD. Otherwise, return 1 as the error code.
  40  #
  41  # NOTE: This does not perform any symlink-resolving or path canonicalization.
  42  #
  43  under_dir() {
  44      local path_residue
  45      path_residue="${2##"${1}"}"
  46      if [ -z "$path_residue" ] || [ "$path_residue" = "$2" ]; then
  47          return 1
  48      else
  49          echo "$path_residue"
  50      fi
  51  }
  52  
  53  # Usage: dir_under_git_root MAYBE_CHILD
  54  #
  55  # If MAYBE_CHILD is under the current git repository and exists, print the
  56  # relative path from the git repository's top-level directory to MAYBE_CHILD,
  57  # otherwise, exit with an error code.
  58  #
  59  dir_under_git_root() {
  60      local rv
  61      rv="$(under_dir "$(git_root)" "$1")"
  62      [ -n "$rv" ] && echo "$rv"
  63  }
  64  
  65  shopt -s nullglob
  66  found_precious_dirs_files=( "${version_base_prefix}"*/"${var_base_basename}/precious_dirs" ) # This expands to an array of directories...
  67  shopt -u nullglob
  68  
  69  exclude_flags=()
  70  
  71  for precious_dirs_file in "${found_precious_dirs_files[@]}"; do
  72      # Make sure the precious directories (e.g. SOURCES_PATH, BASE_CACHE, SDK_PATH)
  73      # are excluded from git-clean
  74      echo "Found precious_dirs file: '${precious_dirs_file}'"
  75  
  76      # Exclude the precious_dirs file itself
  77      if dirs_file_exclude_fragment=$(dir_under_git_root "$(dirname "$precious_dirs_file")"); then
  78          exclude_flags+=( --exclude="${dirs_file_exclude_fragment}/precious_dirs" )
  79      fi
  80  
  81      # Read each 'name=dir' pair from the precious_dirs file
  82      while IFS='=' read -r name dir; do
  83          # Add an exclusion flag if the precious directory is under the git root.
  84          if under=$(dir_under_git_root "$dir"); then
  85              echo "Avoiding ${name}: ${under}"
  86              exclude_flags+=( --exclude="$under" )
  87          fi
  88      done < "$precious_dirs_file"
  89  done
  90  
  91  if [[ $FORCE == 0 ]]; then
  92      git clean -nxdff "${exclude_flags[@]}"
  93  
  94      read -p "Proceed? (y/n) " -r
  95      if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  96          echo "Aborted."
  97          exit 1
  98      fi
  99  fi
 100  
 101  git clean -xdff "${exclude_flags[@]}"
 102