threads / patch / 2815

patchcg-clean: fix directory cleaning

Subject: [PATCH] cg-clean: fix directory cleaning

## tl;dr

2 messages between Dec 12, 2005 and Dec 13, 2005. Diffs are folded; open one to read it.

replies: 1people: 2as markdown or json

Pavel Roskin· Dec 12, 2005, 20:54 UTC · lore

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 <proski@gnu.org>
Show changes to cg-clean +15 −21
diff --git a/cg-clean b/cg-clean
index 92fc48e..6909076 100755
--- a/cg-clean
+++ b/cg-clean
@@ -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
-- 
Regards,
Pavel Roskin
Petr Baudis· Dec 13, 2005, 00:42 UTC · re: Pavel Roskin · lore

Re: [PATCH] cg-clean: fix directory cleaning

Dear diary, on Mon, Dec 12, 2005 at 09:54:20PM CET, I got a letter where Pavel Roskin <proski@gnu.org> said that...

Show 10 quoted 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 <proski@gnu.org>

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.

-- 
				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.

← back to recent threads