contrib / examples / git-clean.shon commit Merge branch 'sb/submodule-helper' (65e1449)
   1#!/bin/sh
   2#
   3# Copyright (c) 2005-2006 Pavel Roskin
   4#
   5
   6OPTIONS_KEEPDASHDASH=
   7OPTIONS_SPEC="\
   8git-clean [options] <paths>...
   9
  10Clean untracked files from the working directory
  11
  12When optional <paths>... arguments are given, the paths
  13affected are further limited to those that match them.
  14--
  15d remove directories as well
  16f override clean.requireForce and clean anyway
  17n don't remove anything, just show what would be done
  18q be quiet, only report errors
  19x remove ignored files as well
  20X remove only ignored files"
  21
  22SUBDIRECTORY_OK=Yes
  23. git-sh-setup
  24require_work_tree
  25
  26ignored=
  27ignoredonly=
  28cleandir=
  29rmf="rm -f --"
  30rmrf="rm -rf --"
  31rm_refuse="echo Not removing"
  32echo1="echo"
  33
  34disabled=$(git config --bool clean.requireForce)
  35
  36while test $# != 0
  37do
  38        case "$1" in
  39        -d)
  40                cleandir=1
  41                ;;
  42        -f)
  43                disabled=false
  44                ;;
  45        -n)
  46                disabled=false
  47                rmf="echo Would remove"
  48                rmrf="echo Would remove"
  49                rm_refuse="echo Would not remove"
  50                echo1=":"
  51                ;;
  52        -q)
  53                echo1=":"
  54                ;;
  55        -x)
  56                ignored=1
  57                ;;
  58        -X)
  59                ignoredonly=1
  60                ;;
  61        --)
  62                shift
  63                break
  64                ;;
  65        *)
  66                usage # should not happen
  67                ;;
  68        esac
  69        shift
  70done
  71
  72# requireForce used to default to false but now it defaults to true.
  73# IOW, lack of explicit "clean.requireForce = false" is taken as
  74# "clean.requireForce = true".
  75case "$disabled" in
  76"")
  77        die "clean.requireForce not set and -n or -f not given; refusing to clean"
  78        ;;
  79"true")
  80        die "clean.requireForce set and -n or -f not given; refusing to clean"
  81        ;;
  82esac
  83
  84if [ "$ignored,$ignoredonly" = "1,1" ]; then
  85        die "-x and -X cannot be set together"
  86fi
  87
  88if [ -z "$ignored" ]; then
  89        excl="--exclude-per-directory=.gitignore"
  90        excl_info= excludes_file=
  91        if [ -f "$GIT_DIR/info/exclude" ]; then
  92                excl_info="--exclude-from=$GIT_DIR/info/exclude"
  93        fi
  94        if cfg_excl=$(git config core.excludesfile) && test -f "$cfg_excl"
  95        then
  96                excludes_file="--exclude-from=$cfg_excl"
  97        fi
  98        if [ "$ignoredonly" ]; then
  99                excl="$excl --ignored"
 100        fi
 101fi
 102
 103git ls-files --others --directory \
 104        $excl ${excl_info:+"$excl_info"} ${excludes_file:+"$excludes_file"} \
 105        -- "$@" |
 106while read -r file; do
 107        if [ -d "$file" -a ! -L "$file" ]; then
 108                if [ -z "$cleandir" ]; then
 109                        $rm_refuse "$file"
 110                        continue
 111                fi
 112                $echo1 "Removing $file"
 113                $rmrf "$file"
 114        else
 115                $echo1 "Removing $file"
 116                $rmf "$file"
 117        fi
 118done