Move traversal of reachable objects into a separate library.
[git/dscho.git] / git-clean.sh
blob071b974f496b8deff3a2d1b869c35d4f556dc17f
1 #!/bin/sh
3 # Copyright (c) 2005-2006 Pavel Roskin
6 USAGE="[-d] [-n] [-q] [-x | -X] [--] <paths>..."
7 LONG_USAGE='Clean untracked files from the working directory
8 -d remove directories as well
9 -n don'\''t remove anything, just show what would be done
10 -q be quiet, only report errors
11 -x remove ignored files as well
12 -X remove only ignored files
13 When optional <paths>... arguments are given, the paths
14 affected are further limited to those that match them.'
15 SUBDIRECTORY_OK=Yes
16 . git-sh-setup
18 ignored=
19 ignoredonly=
20 cleandir=
21 rmf="rm -f --"
22 rmrf="rm -rf --"
23 rm_refuse="echo Not removing"
24 echo1="echo"
26 while case "$#" in 0) break ;; esac
28 case "$1" in
29 -d)
30 cleandir=1
32 -n)
33 rmf="echo Would remove"
34 rmrf="echo Would remove"
35 rm_refuse="echo Would not remove"
36 echo1=":"
38 -q)
39 echo1=":"
41 -x)
42 ignored=1
44 -X)
45 ignoredonly=1
47 --)
48 shift
49 break
51 -*)
52 usage
55 break
56 esac
57 shift
58 done
60 case "$ignored,$ignoredonly" in
61 1,1) usage;;
62 esac
64 if [ -z "$ignored" ]; then
65 excl="--exclude-per-directory=.gitignore"
66 if [ -f "$GIT_DIR/info/exclude" ]; then
67 excl_info="--exclude-from=$GIT_DIR/info/exclude"
69 if [ "$ignoredonly" ]; then
70 excl="$excl --ignored"
74 git-ls-files --others --directory $excl ${excl_info:+"$excl_info"} -- "$@" |
75 while read -r file; do
76 if [ -d "$file" -a ! -L "$file" ]; then
77 if [ -z "$cleandir" ]; then
78 $rm_refuse "$file"
79 continue
81 $echo1 "Removing $file"
82 $rmrf "$file"
83 else
84 $echo1 "Removing $file"
85 $rmf "$file"
87 done