git-svn: extract reusable code into utility functions
[git/jnareb-git.git] / git-clean.sh
blob01c95e9fe8a19afcf331ed5ffd47eea478886213
1 #!/bin/sh
3 # Copyright (c) 2005-2006 Pavel Roskin
6 OPTIONS_KEEPDASHDASH=
7 OPTIONS_SPEC="\
8 git-clean [options] <paths>...
10 Clean untracked files from the working directory
12 When optional <paths>... arguments are given, the paths
13 affected are further limited to those that match them.
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"
22 SUBDIRECTORY_OK=Yes
23 . git-sh-setup
24 require_work_tree
26 ignored=
27 ignoredonly=
28 cleandir=
29 rmf="rm -f --"
30 rmrf="rm -rf --"
31 rm_refuse="echo Not removing"
32 echo1="echo"
34 disabled=$(git config --bool clean.requireForce)
36 while test $# != 0
38 case "$1" in
39 -d)
40 cleandir=1
42 -f)
43 disabled=false
45 -n)
46 disabled=false
47 rmf="echo Would remove"
48 rmrf="echo Would remove"
49 rm_refuse="echo Would not remove"
50 echo1=":"
52 -q)
53 echo1=":"
55 -x)
56 ignored=1
58 -X)
59 ignoredonly=1
61 --)
62 shift
63 break
66 usage # should not happen
68 esac
69 shift
70 done
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"
79 "true")
80 die "clean.requireForce set and -n or -f not given; refusing to clean"
82 esac
84 if [ "$ignored,$ignoredonly" = "1,1" ]; then
85 die "-x and -X cannot be set together"
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"
94 if cfg_excl=$(git config core.excludesfile) && test -f "$cfg_excl"
95 then
96 excludes_file="--exclude-from=$cfg_excl"
98 if [ "$ignoredonly" ]; then
99 excl="$excl --ignored"
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
112 $echo1 "Removing $file"
113 $rmrf "$file"
114 else
115 $echo1 "Removing $file"
116 $rmf "$file"
118 done