Test commit
[cogito/jonas.git] / cg-clean
blob5bbe413bcd092d5f10477ebcbffa26871cf7270b
1 #!/usr/bin/env bash
3 # Remove untracked files from the working tree
4 # Copyright (c) Pavel Roskin, 2005
6 # Cleans file and directories that are not under version control.
7 # When run without arguments, files ignored by `cg-status` and directories
8 # are not removed.
10 # OPTIONS
11 # -------
12 # -d:: Clean directories
13 # Also clean directories and their contents.
15 # -D:: Clean directories more thoroughly
16 # Same as -d but try harder (change permissions first).
18 # -n:: Don't actually remove files, just pretend to
19 # Do not actually remove the files, just pretend to do so.
21 # -q:: Silence progress reporting
22 # Quiet - don't report what's being cleaned.
24 # -x:: Clean files ignored by cg-status
25 # Also clean files ignored by `cg-status`, such as object files.
27 # Testsuite: Complete (t9400-clean)
29 USAGE="cg-clean [-d] [-D] [-n] [-q] [-x]"
31 . "${COGITO_LIB}"cg-Xlib || exit 1
33 noexclude=
34 cleandir=
35 cleandirhard=
36 quiet=
37 rm=rm
38 while optparse; do
39 if optparse -d; then
40 cleandir=1
41 elif optparse -D; then
42 cleandir=1
43 cleandirhard=1
44 elif optparse -n; then
45 pretendrm () { echo rm "$@"; }
46 rm="pretendrm"
47 elif optparse -q; then
48 quiet=1
49 elif optparse -x; then
50 noexclude=noexclude
51 else
52 optfail
54 done
56 [ ${#ARGS[*]} = 0 ] || usage
58 cd "${_git_relpath-.}"
59 list_untracked_files "$noexclude" squashdirs | tr '\0' '\n' |
60 while read -r file; do
61 if [ -d "$file" -a ! -L "$file" ]; then
62 if [ -z "$cleandir" ]; then
63 echo "Not removing $file"
64 continue
66 [ "$quiet" ] || echo "Removing $file"
67 if [ "$cleandirhard" ]; then
68 chmod -R 700 "$file"
70 $rm -rf -- "$file"
71 if [ -e "$file" -o -L "$file" ]; then
72 echo "Cannot remove $file"
74 elif [ -e "$file" -o -L "$file" ]; then
75 [ "$quiet" ] || echo "Removing $file"
76 "$rm" -f -- "$file"
77 # rm would complain itself on failure
78 else
79 echo "File $file has disappeared!"
81 done