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