<commit_before>#! @FEATURE_SH@

# bk.sh - BitKeeper scripts
# @(#)%K%

# Functions intended to be accessed as bk whatever by the user are
# named _whatever.  Functions for internal use only are named
# __whatever.  Don't name any functions without leading underscores,
# they can clash with commands or shell builtins.

# PORTABILITY NOTE: Never use shift or getopt inside a function that
# might be called from inside another function that uses shift or
# getopt.  Every shell I've tried except AT&T ksh gets it wrong.  Most
# people don't have AT&T ksh.

# Utility function
# if you want to add a -q option to a function here, just set Q=-q
# in your getopts loop and use qecho in every place that should be
# conditional.
qecho() {
	[ "$Q" != "-q" ] && echo $@
}

__cd2root() {
	while [ ! -d "BitKeeper/etc" ]
	do	cd ..
		if [ `pwd` = "/" ]
		then	echo "bk: can not find package root."
			exit 1
		fi
	done
}

# shorthand to dig out renames
_renames() {
	case "X$1" in
	    X-r[0-9]*)
		;;
	    *)
	    	echo 'usage renames -r<rev> OR renames -r<rev>,<rev>'
		exit 1
		;;
	esac
	__cd2root
	bk rset -h "$1" | awk -F@ '{ if ($1 != $2) print $2 " -> " $1 }'
}

# shorthand
_inode() {		# /* undoc? 2.0 */
	bk prs -hr+ -nd':ROOTKEY:' "$@"
}

# shorthand
_flags() {		# /* undoc? 2.0 */
	bk prs -hr+ -nd':GFILE: :FLAGS:' "$@"
}

# This removes the tag graph.  Use with care.
_striptags() {
	__cd2root
	_BK_STRIPTAGS=Y bk admin -z ChangeSet
}

# Hard link based clone.
# Usage: lclone from [to]
_lclone() {
	HERE=`pwd`
	while getopts q opt
	do
		case "$opt" in
		q) Q="-q";;
		*)	echo "Usage: lclone [-q] from to"
			exit 1;;
		esac
	done
	shift `expr $OPTIND - 1`
	if [ "X$1" = X ]
	then	echo Usage: $0 from to
		exit 1
	else	cd $1 || exit 1
		FROM=`pwd`
		cd $HERE
	fi
	if [ "X$2" = X ]
	then	TO=`basename $FROM`
	else	if [ "X$3" != X ]; then	echo Usage: $0 from to; exit 1; fi
		TO="$2"
	fi
	test -d "$TO" && { echo $2 exists; exit 1; }
	cd $FROM
	qecho Finding SCCS directories...
	bk sfiles -d > /tmp/dirs$$
	cd $HERE
	mkdir $TO
	cd $TO
	while read x
	do
		if [ "$x" != "." -a -d $FROM/$x/BitKeeper ]
		then
			qecho "Skipping $x ..."
			continue
		fi
		qecho Linking $x ...
		mkdir -p $x/SCCS
		ln $FROM/$x/SCCS/s.* $x/SCCS
	done < /tmp/dirs$$
	bk sane
	qecho Looking for and removing any uncommitted deltas
	bk sfiles -pA | bk stripdel -
	qecho Running a sanity check
	bk -r check -ac || {
		echo lclone failed
		exit 1
	}
	bk parent $Q $FROM
	rm -f /tmp/dir$$
	exit 0
}

# Show what would be sent
_keysync() {
	if [ "X$1" = X -o "X$2" = X -o "X$3" != X ]
	then	echo usage root1 root2
		exit 1
	fi
	test -d "$1" -a -d "$2" || {
		echo usage root1 root2
		exit 1
	}
	HERE=`pwd`
	__keysync "$1" "$2" > /tmp/sync$$
	__keysync "$2" "$1" >> /tmp/sync$$
	if [ X$PAGER != X ]
	then	$PAGER /tmp/sync$$<commit_msg>lclone bugfixes<commit_after>#! @FEATURE_SH@

# bk.sh - BitKeeper scripts
# @(#)%K%

# Functions intended to be accessed as bk whatever by the user are
# named _whatever.  Functions for internal use only are named
# __whatever.  Don't name any functions without leading underscores,
# they can clash with commands or shell builtins.

# PORTABILITY NOTE: Never use shift or getopt inside a function that
# might be called from inside another function that uses shift or
# getopt.  Every shell I've tried except AT&T ksh gets it wrong.  Most
# people don't have AT&T ksh.

# Utility function
# if you want to add a -q option to a function here, just set Q=-q
# in your getopts loop and use qecho in every place that should be
# conditional.
qecho() {
	[ "$Q" != "-q" ] && echo $@
}

__cd2root() {
	while [ ! -d "BitKeeper/etc" ]
	do	cd ..
		if [ `pwd` = "/" ]
		then	echo "bk: can not find package root."
			exit 1
		fi
	done
}

# shorthand to dig out renames
_renames() {
	case "X$1" in
	    X-r[0-9]*)
		;;
	    *)
	    	echo 'usage renames -r<rev> OR renames -r<rev>,<rev>'
		exit 1
		;;
	esac
	__cd2root
	bk rset -h "$1" | awk -F@ '{ if ($1 != $2) print $2 " -> " $1 }'
}

# shorthand
_inode() {		# /* undoc? 2.0 */
	bk prs -hr+ -nd':ROOTKEY:' "$@"
}

# shorthand
_flags() {		# /* undoc? 2.0 */
	bk prs -hr+ -nd':GFILE: :FLAGS:' "$@"
}

# This removes the tag graph.  Use with care.
_striptags() {
	__cd2root
	_BK_STRIPTAGS=Y bk admin -z ChangeSet
}

# Hard link based clone.
# Usage: lclone from [to]
_lclone() {
	HERE=`pwd`
	while getopts q opt
	do
		case "$opt" in
		q) Q="-q";;
		*)	echo "Usage: lclone [-q] from to"
			exit 1;;
		esac
	done
	shift `expr $OPTIND - 1`
	if [ "X$1" = X ]
	then	echo Usage: $0 from to
		exit 1
	else	cd $1 || exit 1
		FROM=`pwd`
		cd $HERE
	fi
	if [ "X$2" = X ]
	then	TO=`basename $FROM`
	else	if [ "X$3" != X ]; then	echo Usage: $0 from to; exit 1; fi
		TO="$2"
	fi
	test -d "$TO" && { echo $2 exists; exit 1; }
	cd $FROM
	qecho Finding SCCS directories...
	bk sfiles -d > /tmp/dirs$$
	cd $HERE
	mkdir $TO
	cd $TO
	while read x
	do
		if [ "$x" != "." -a -d $FROM/$x/BitKeeper ]
		then
			qecho "Skipping $x ..."
			continue
		fi
		qecho Linking $x ...
		mkdir -p $x/SCCS
		# This loop is to prevent arg list too long errors
		# and avoid running ln for every individual file.
		# Instead we group them up five at a time.
		# The if block after the loop is to pick up any
		# remaing sfiles.
		# All this replaces: ln $FROM/$x/SCCS/s.* $x/SCCS
		for sfile in $FROM/$x/SCCS/s.*
		do
			sfiles="$sfiles $sfile"
			count=${count}X
			if [ "$count" = "XXXXX" ]
			then
				ln $sfiles $x/SCCS
				unset sfiles count
			fi
		done
		if [ "$sfiles" ]
		then
			ln $sfiles $x/SCCS
			unset sfiles count
		fi
	done < /tmp/dirs$$
	bk sane
	qecho Looking for and removing any uncommitted deltas
	bk sfiles -pA | bk stripdel -
	qecho Running a sanity check
	bk -r check -ac || {
		echo lclone failed
		exit 1
	}
	bk parent $Q $FROM
	rm -f /tmp/dirs$$
	exit 0
}

# Show what would be sent
_keysync() {
	if [ "X$1" = X -o "X$2" = X -o "X$3" != X ]
	then	echo usage root1 root2
		exit 1
	fi
	test -d "$1" -a -d "$2" || {
		echo usage root1 root2
		exit 1
	}
	HERE=`pwd`
	__keysync "$1" "$2" > /tmp/sync$$
	__keysync "$2" "$1" >> /tmp/sync$$
	if [ X$PAGER != X ]
	then	$PAGER /tmp/sync$$
{'fraction_non_alphanumeric': 0.14869949288401768, 'fraction_numerical': 0.01570423687223949, 'mean_word_length': 2.6698679471788718, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1134315', 'n_tokens_mistral': 2716, 'n_tokens_neox': 2506, 'n_words': 903}