]> git.ipfire.org Git - thirdparty/git.git/blame - git-clean.sh
sh-setup: don't let eval output to be shell-expanded.
[thirdparty/git.git] / git-clean.sh
CommitLineData
c3b831bd
PR
1#!/bin/sh
2#
3# Copyright (c) 2005-2006 Pavel Roskin
4#
5
27c0d1c7
PH
6OPTIONS_KEEPDASHDASH=
7OPTIONS_SPEC="\
8git-clean [options] <paths>...
9
10Clean untracked files from the working directory
11
393e3b19 12When optional <paths>... arguments are given, the paths
27c0d1c7
PH
13affected are further limited to those that match them.
14--
15d remove directories as well
16f override clean.requireForce and clean anyway
17n don't remove anything, just show what would be done
18q be quiet, only report errors
19x remove ignored files as well
20X remove only ignored files"
21
c3b831bd
PR
22SUBDIRECTORY_OK=Yes
23. git-sh-setup
7eff28a9 24require_work_tree
c3b831bd
PR
25
26ignored=
27ignoredonly=
28cleandir=
5be60078 29disabled="`git config --bool clean.requireForce`"
7484529d
DS
30rmf="rm -f --"
31rmrf="rm -rf --"
c3b831bd
PR
32rm_refuse="echo Not removing"
33echo1="echo"
34
822f7c73 35while test $# != 0
c3b831bd
PR
36do
37 case "$1" in
38 -d)
39 cleandir=1
40 ;;
2122591b
JT
41 -f)
42 disabled=
43 ;;
c3b831bd 44 -n)
2122591b 45 disabled=
c3b831bd
PR
46 rmf="echo Would remove"
47 rmrf="echo Would remove"
48 rm_refuse="echo Would not remove"
49 echo1=":"
50 ;;
51 -q)
e6d7b2f6 52 echo1=":"
c3b831bd
PR
53 ;;
54 -x)
55 ignored=1
56 ;;
57 -X)
58 ignoredonly=1
59 ;;
393e3b19
JH
60 --)
61 shift
62 break
63 ;;
393e3b19 64 *)
27c0d1c7
PH
65 usage # should not happen
66 ;;
c3b831bd
PR
67 esac
68 shift
69done
70
2122591b 71if [ "$disabled" = true ]; then
27c0d1c7 72 die "clean.requireForce set and -n or -f not given; refusing to clean"
2122591b
JT
73fi
74
27c0d1c7
PH
75if [ "$ignored,$ignoredonly" = "1,1" ]; then
76 die "-x and -X cannot be set together"
77fi
c3b831bd
PR
78
79if [ -z "$ignored" ]; then
80 excl="--exclude-per-directory=.gitignore"
81 if [ -f "$GIT_DIR/info/exclude" ]; then
82 excl_info="--exclude-from=$GIT_DIR/info/exclude"
83 fi
84 if [ "$ignoredonly" ]; then
85 excl="$excl --ignored"
86 fi
87fi
88
5be60078 89git ls-files --others --directory $excl ${excl_info:+"$excl_info"} -- "$@" |
c3b831bd
PR
90while read -r file; do
91 if [ -d "$file" -a ! -L "$file" ]; then
92 if [ -z "$cleandir" ]; then
93 $rm_refuse "$file"
94 continue
95 fi
96 $echo1 "Removing $file"
97 $rmrf "$file"
98 else
99 $echo1 "Removing $file"
100 $rmf "$file"
101 fi
102done