cg-clean: fix directory cleaning
PATCH 1/1 cg-clean: fix directory cleaning
cg-clean is broken by commit 3cddede46dcd24bd7c36827ca564de23b2bb3517 - it removes all subdirectories when run in the top level directory.
Although it's easy to make a one-line fix, I'd prefer a solution that makes cg-clean potentially safer. Since git-ls-files runs in subdirectories now, it's no longer needed to run anything in the top level directory and filter out the results. cg-clean now changes to $_git_relpath on startup and never uses $_git_relpath again.
cg-clean +15 -21
@@ -60,8 +60,6 @@ clean_dirs() git-ls-files --cached | sed -n 's|/[^/]*$||p' | while IFS=$'\n' read dir; do - [ x"${dir#$_git_relpath}" = x"${dir}" ] && continue - dir="${dir#$_git_relpath}" while true; do echo "$dir" updir="${dir%/*}"
@@ -74,32 +72,27 @@ clean_dirs() save_IFS="$IFS" IFS=$'\n' - fpath=${_git_relpath-./} - find "$fpath" -type d -print | - while read dir; do - echo "${dir#$fpath}" - done | - sed '/^$/d;/^\.git$/d;/^\.git\//d' | + find ./ -type d -print | + sed 's/^\.\///;/^$/d;/^\.git$/d;/^\.git\//d' | cat - "$dirlist" | sort -u | diff - "$dirlist" | sed -n 's/< //p' | - while read file; do - path="${_git_relpath}$file" - if [ ! -d "$path" ]; then + while read dir; do + if [ ! -d "$dir" ]; then # Perhaps directory was removed with its parent continue fi if [ -z "$cleandir" ]; then - echo "Not removing $file/" + echo "Not removing $dir/" continue fi - [ "$quiet" ] || echo "Removing $file/" + [ "$quiet" ] || echo "Removing $dir/" if [ "$cleandirhard" ]; then - chmod -R 700 "$path" + chmod -R 700 "$dir" fi - $rm -rf "$path" - if [ -e "$path" -o -L "$path" ]; then - echo "Cannot remove $file/" + $rm -rf "$dir" + if [ -e "$dir" -o -L "$dir" ]; then + echo "Cannot remove $dir/" fi done
@@ -117,14 +110,13 @@ clean_files() cg-status -n -s '?' "$xopt" -w | while read file; do - path="${_git_relpath}$file" - if [ -d "$path" -a ! -L "$path" ]; then + if [ -d "$file" -a ! -L "$file" ]; then # Sanity check, shouldn't happen echo "FATAL: cg-status reports directories (internal error)" >&2 exit 1 - elif [ -e "$path" -o -L "$path" ]; then + elif [ -e "$file" -o -L "$file" ]; then [ "$quiet" ] || echo "Removing $file" - $rm -f "$path" + $rm -f "$file" # rm would complain itself on failure else echo "File $file has disappeared!"
@@ -135,6 +127,8 @@ clean_files() } +cd "${_git_relpath-.}" + # Even if -d or -D is not specified, we want to tell user about # directories that are not removed if [ -z "$quiet" -o "$cleandir" ]; then
Signature · 2 lines
Regards, Pavel Roskin
Dear diary, on Mon, Dec 12, 2005 at 09:54:20PM CET, I got a letter where Pavel Roskin <[email protected]> said that...
Quoted 10 lines
cg-clean is broken by commit
3cddede46dcd24bd7c36827ca564de23b2bb3517- it removes all subdirectories when run in the top level directory.Although it's easy to make a one-line fix, I'd prefer a solution that makes cg-clean potentially safer. Since git-ls-files runs in subdirectories now, it's no longer needed to run anything in the top level directory and filter out the results. cg-clean now changes to $_git_relpath on startup and never uses $_git_relpath again.
Signed-off-by Pavel Roskin <[email protected]>
Oops! Many thanks to both this and the testcase - I was just about to write one, because those cg-clean bugs are really quite ugly. I'll release 0.16.2 tomorrow if no other bug will be found.
Signature · 4 lines
Petr "Pasky" Baudis Stuff: http://pasky.or.cz/ VI has two modes: the one in which it beeps and the one in which it doesn't.