]> git.ipfire.org Git - thirdparty/git.git/blame - git-clean.sh
autoconf: Add tests for memmem, strtoumax and mkdtemp functions
[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=
7484529d
DS
29rmf="rm -f --"
30rmrf="rm -rf --"
c3b831bd
PR
31rm_refuse="echo Not removing"
32echo1="echo"
33
f192c5d0 34disabled=$(git config --bool clean.requireForce)
562ca192 35
822f7c73 36while test $# != 0
c3b831bd
PR
37do
38 case "$1" in
39 -d)
40 cleandir=1
41 ;;
2122591b 42 -f)
f192c5d0 43 disabled=false
2122591b 44 ;;
c3b831bd 45 -n)
f192c5d0 46 disabled=false
c3b831bd
PR
47 rmf="echo Would remove"
48 rmrf="echo Would remove"
49 rm_refuse="echo Would not remove"
50 echo1=":"
51 ;;
52 -q)
e6d7b2f6 53 echo1=":"
c3b831bd
PR
54 ;;
55 -x)
56 ignored=1
57 ;;
58 -X)
59 ignoredonly=1
60 ;;
393e3b19
JH
61 --)
62 shift
63 break
64 ;;
393e3b19 65 *)
27c0d1c7
PH
66 usage # should not happen
67 ;;
c3b831bd
PR
68 esac
69 shift
70done
71
f192c5d0
JS
72# requireForce used to default to false but now it defaults to true.
73# IOW, lack of explicit "clean.requireForce = false" is taken as
74# "clean.requireForce = true".
75case "$disabled" in
76"")
77 die "clean.requireForce not set and -n or -f not given; refusing to clean"
78 ;;
79"true")
80 die "clean.requireForce set and -n or -f not given; refusing to clean"
81 ;;
82esac
2122591b 83
27c0d1c7
PH
84if [ "$ignored,$ignoredonly" = "1,1" ]; then
85 die "-x and -X cannot be set together"
86fi
c3b831bd
PR
87
88if [ -z "$ignored" ]; then
89 excl="--exclude-per-directory=.gitignore"
b57321f5 90 excl_info= excludes_file=
c3b831bd
PR
91 if [ -f "$GIT_DIR/info/exclude" ]; then
92 excl_info="--exclude-from=$GIT_DIR/info/exclude"
93 fi
b57321f5
JH
94 if cfg_excl=$(git config core.excludesfile) && test -f "$cfg_excl"
95 then
96 excludes_file="--exclude-from=$cfg_excl"
97 fi
c3b831bd
PR
98 if [ "$ignoredonly" ]; then
99 excl="$excl --ignored"
100 fi
101fi
102
b57321f5
JH
103git ls-files --others --directory \
104 $excl ${excl_info:+"$excl_info"} ${excludes_file:+"$excludes_file"} \
105 -- "$@" |
c3b831bd
PR
106while read -r file; do
107 if [ -d "$file" -a ! -L "$file" ]; then
108 if [ -z "$cleandir" ]; then
109 $rm_refuse "$file"
110 continue
111 fi
112 $echo1 "Removing $file"
113 $rmrf "$file"
114 else
115 $echo1 "Removing $file"
116 $rmf "$file"
117 fi
118done