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

New script: cg-clean

From
PRPavel Roskin <proski@gnu.org>
Date
Jul 8, 2005, 22:34 UTC
Message-ID
<1120862084.17812.6.camel@dv>
Hello, Petr!
Please consider this script for Cogito.
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. # Only regular files that are not ignored by cg-status are cleaned # by default. # # OPTIONS # ------- # -i:: # Clean files ignored by cg-status, such as object files. # # -s:: # Clean symlinks, fifos, sockets and other special files. # # -r:: # Clean directories. # # -R:: # Clean directories, try harder. Make directories writeable # recursively before removing. # # -a:: # Clean all the above. # # If any other arguments are specified, they will be the only files # considered for removal. It will also imply the `-a' option.

USAGE="cg-clean [-i] [-s] [-r] [-R] [-a] [FILE]..."
. ${COGITO_LIB}cg-Xlib
cleanexclude=
cleanspecial=
cleandir=
cleandirhard=
while optparse; do
	if optparse -i; then
		cleanexclude=1
	elif optparse -s; then
		cleanspecial=1
	elif optparse -r; then
		cleandir=1
	elif optparse -R; then
		cleandirhard=1
	elif optparse -a; then
		cleanexclude=1
		cleandirhard=1
	else
		optfail
	fi
done
if [ "$ARGS" ]; then
	cleanexclude=1
	cleandirhard=1
fi
# Good candidate for cg-Xlib
# Put exclude options for git-ls-files to EXCLUDE
set_exclude() {
	stdignores=('*.[ao]' '.*' tags '*~' '#*' ',,merge*')
	for ign in "${stdignores[@]}"; do
		EXCLUDE="$EXCLUDE --exclude=$ign"
	done
	EXCLUDEFILE=$_git/exclude
	if [ -f "$EXCLUDEFILE" ]; then
		EXCLUDE="$EXCLUDE --exclude-from=$EXCLUDEFILE"
	fi
}
EXCLUDE=
if [ -z "$cleanexclude" ]; then
	set_exclude
fi	
git-update-cache --refresh > /dev/null
do_clean() {
	# FIXME - very suboptimal
	if [ "$ARGS" ]; then
		local found=
		for arg in "${ARGS[@]}"; do
			if [ "$arg" = "$file" ]; then
				found=1
				break
			fi
		done
		[ "$found" ] || return
	fi
	if [ "$cleandirhard" ]; then
		chmod -R 700 "$file"
		rm -rf "$file"
		return
	fi
	if [ "$cleandir" ]; then
		rm -rf "$file"
		return
	fi
	if [ "$cleanspecial" ]; then
		[ -d "$file" ] && return
		rm -f "$file"
		return
	fi
	[ -f "$file" ] && rm -f "$file"
}
# Need to use temporary file so that changing IFS doesn't affect $EXCLUDE
# expansion.
filelist=$(mktemp -t gitlsfiles.XXXXXX)
git-ls-files --others $EXCLUDE >"$filelist"
IFS=$'\n'
for file in $(cat "$filelist"); do
	do_clean "$file"
done
rm -f "$filelist"
-- 
Regards,
Pavel Roskin
Next: Wolfgang Denk
Message 1 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.