clean: require -f to do damage by default
[git/git.git] / git-clean.sh
CommitLineData
c3b831bd
PR
1#!/bin/sh
2#
3# Copyright (c) 2005-2006 Pavel Roskin
4#
5
2122591b 6USAGE="[-d] [-f] [-n] [-q] [-x | -X] [--] <paths>..."
c3b831bd
PR
7LONG_USAGE='Clean untracked files from the working directory
8 -d remove directories as well
2122591b 9 -f override clean.requireForce and clean anyway
c3b831bd
PR
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
393e3b19
JH
13 -X remove only ignored files
14When optional <paths>... arguments are given, the paths
15affected are further limited to those that match them.'
c3b831bd
PR
16SUBDIRECTORY_OK=Yes
17. git-sh-setup
7eff28a9 18require_work_tree
c3b831bd
PR
19
20ignored=
21ignoredonly=
22cleandir=
7484529d
DS
23rmf="rm -f --"
24rmrf="rm -rf --"
c3b831bd
PR
25rm_refuse="echo Not removing"
26echo1="echo"
27
562ca192
JH
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".
31disabled=$(git config --bool clean.requireForce || echo true)
32
822f7c73 33while test $# != 0
c3b831bd
PR
34do
35 case "$1" in
36 -d)
37 cleandir=1
38 ;;
2122591b
JT
39 -f)
40 disabled=
41 ;;
c3b831bd 42 -n)
2122591b 43 disabled=
c3b831bd
PR
44 rmf="echo Would remove"
45 rmrf="echo Would remove"
46 rm_refuse="echo Would not remove"
47 echo1=":"
48 ;;
49 -q)
e6d7b2f6 50 echo1=":"
c3b831bd
PR
51 ;;
52 -x)
53 ignored=1
54 ;;
55 -X)
56 ignoredonly=1
57 ;;
393e3b19
JH
58 --)
59 shift
60 break
61 ;;
62 -*)
c3b831bd 63 usage
393e3b19
JH
64 ;;
65 *)
66 break
c3b831bd
PR
67 esac
68 shift
69done
70
2122591b
JT
71if [ "$disabled" = true ]; then
72 echo "clean.requireForce set and -n or -f not given; refusing to clean"
73 exit 1
74fi
75
c3b831bd
PR
76case "$ignored,$ignoredonly" in
77 1,1) usage;;
78esac
79
80if [ -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
88fi
89
5be60078 90git ls-files --others --directory $excl ${excl_info:+"$excl_info"} -- "$@" |
c3b831bd
PR
91while 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
103done