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