Merge branch 'jk/ref-filter-colors-fix'
[git] / contrib / examples / git-clean.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005-2006 Pavel Roskin
4 #
5
6 OPTIONS_KEEPDASHDASH=
7 OPTIONS_SPEC="\
8 git-clean [options] <paths>...
9
10 Clean untracked files from the working directory
11
12 When optional <paths>... arguments are given, the paths
13 affected are further limited to those that match them.
14 --
15 d remove directories as well
16 f override clean.requireForce and clean anyway
17 n don't remove anything, just show what would be done
18 q be quiet, only report errors
19 x remove ignored files as well
20 X remove only ignored files"
21
22 SUBDIRECTORY_OK=Yes
23 . git-sh-setup
24 require_work_tree
25
26 ignored=
27 ignoredonly=
28 cleandir=
29 rmf="rm -f --"
30 rmrf="rm -rf --"
31 rm_refuse="echo Not removing"
32 echo1="echo"
33
34 disabled=$(git config --bool clean.requireForce)
35
36 while test $# != 0
37 do
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
70 done
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".
75 case "$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         ;;
82 esac
83
84 if [ "$ignored,$ignoredonly" = "1,1" ]; then
85         die "-x and -X cannot be set together"
86 fi
87
88 if [ -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
101 fi
102
103 git ls-files --others --directory \
104         $excl ${excl_info:+"$excl_info"} ${excludes_file:+"$excludes_file"} \
105         -- "$@" |
106 while 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
118 done