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