git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: New script: cg-clean

From
PRPavel Roskin <proski@gnu.org>
Date
Aug 12, 2005, 03:59 UTC
Message-ID
<1123819140.4248.55.camel@dv>
In-Reply-To
<20050812010811.GM25280@pasky.ji.cz>
On Fri, 2005-08-12 at 03:08 +0200, Petr Baudis wrote:
Show 13 quoted lines
> Dear diary, on Fri, Aug 12, 2005 at 02:54:13AM CEST, I got a letter
> where Pavel Roskin <proski@gnu.org> told me that...
> > Hi, Petr!
> 
> Hi,
> 
> > Unfortunately, my latest revision of cg-clean has "committed suicide"
> > just when I was about to post it.  Anyway, I would prefer to wait until
> > you apply my patch to cg-status to ignore all ignores.  That would allow
> > me to reuse cg-status.
> 
> well, I did quite a while ago. Unless the kernel.org mirroring system
> broke, it should be already public.
Yes, it's there.  Thank you.

This is another attempt at cg-clean. Verbose mode is now default (i.e. everything that's removed is reported), "-q" makes the script quiet. I've reinstated the "hard" option, it's now "-D".

Signed-off-by: Pavel Roskin <proski@gnu.org>

#!/usr/bin/env bash # # Clean unknown 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:: # Also clean directories and their contents. # # -D:: # Same as -d but try harder (change permissions first). # # -q:: # Quiet - don't report what's being cleaned. # # -x:: # Also clean files ignored by cg-status, such as object files.

USAGE="cg-clean [-d] [-D] [-q] [-x]"
. ${COGITO_LIB}cg-Xlib || exit 1
noexclude=
cleandir=
cleandirhard=
quiet=
while optparse; do
	if optparse -d; then
		cleandir=1
	elif optparse -D; then
		cleandir=1
		cleandirhard=1
	elif optparse -q; then
		quiet=1
	elif optparse -x; then
		noexclude=1
	else
		optfail
	fi
done
[ "$ARGS" ] && usage
clean_dirs()
{
	dirlist=$(mktemp -t gitlsfiles.XXXXXX)
	git-ls-files --cached | sed -n 's|^'"$_git_relpath"'||p' |
	sed -n 's|/[^/]*$||p' | sort -u >"$dirlist"
	save_IFS="$IFS"
	IFS=$'\n'
	fpath=${_git_relpath-./}
	find "$fpath" -type d -print | \
	sed 's|^'"$fpath"'||;/^$/d;/^\.git$/d;/^\.git\//d' |
	cat - "$dirlist" | sort -u | diff - "$dirlist" |
	sed -n 's/< //p' |
	for file in $(cat); do
		path="${_git_relpath}$file"
		if [ -z "$cleandir" ]; then
			echo "Not removing $file/"
			continue
		fi
		if [ ! -d "$path" ]; then
			# Perhaps directory was removed with its parent
			continue
		fi
		[ "$quiet" ] || echo "Removing $file/"
		if [ "$cleandirhard" ]; then
			chmod -R 700 "$path"
		fi
		rm -rf "$path"
		if [ -e "$path" -o -L "$path" ]; then
			echo "Cannot remove $file/"
		fi
	done
	IFS="$save_IFS"
	rm -f "$dirlist"
}
clean_files()
{
	xopt=
	if [ "$noexclude" ]; then
		xopt="-x"
	fi
	save_IFS="$IFS"
	IFS=$'\n'
	cg-status "$xopt" -w | sed -n 's/^? //p' |
	for file in $(cat); do
		path="${_git_relpath}$file"
		if [ -d "$path" ]; then
			# Sanity check, shouldn't happen
			echo "FATAL: cg-status reports directories" >&2
			exit 1
		elif [ -e "$path" -o -L "$path" ]; then
			[ "$quiet" ] || echo "Removing $file"
			rm -f "$path"
			if [ -e "$path" -o -L "$path" ]; then
				echo "Cannot remove $file"
			fi
		else
			echo "File $file has disappeared!"
		fi
	done
	IFS="$save_IFS"
}
# 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
	clean_dirs
fi
clean_files
-- 
Regards,
Pavel Roskin
Previous: Petr Baudis
Message 8 of 8 in “New script: cg-clean”
  1. Pavel RoskinJul 8, 2005
  2. Wolfgang DenkJul 8, 2005
  3. Petr BaudisJul 10, 2005
  4. Pavel RoskinAug 6, 2005
  5. Petr BaudisAug 11, 2005
  6. Pavel RoskinAug 12, 2005
  7. Petr BaudisAug 12, 2005
  8. Pavel RoskinAug 12, 2005

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.