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
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
33while test $# != 0
34do
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
69done
70
71if [ "$disabled" = true ]; then
72 echo "clean.requireForce set and -n or -f not given; refusing to clean"
73 exit 1
74fi
75
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
90git ls-files --others --directory $excl ${excl_info:+"$excl_info"} -- "$@" |
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