Yuan Yijun (bbbush) wrote,
Yuan Yijun
bbbush

Binary or Text

Dear LazyWeb:

Simply speaking, I need a way to fast convert files, so files from VSS can be committed to a Git repo (Why? I like it. http://wiki.freebsd.org/LocalGit and http://wiki.freebsd.org/LocalMercurial .)

Files containing trailing blanks can only be committed with "git commit -n", but I don't like that. Since the VSS=>Git operation is not done frequently, I was using dos2unix then sed to trim the blanks, but sed cannot recognize a file is binary or text, and my existing method to probe that is too slow.

Suggest me to use Hg or bzr please. Any speed benchmark on win32? Repo size is 13k files, 250M (single snapshot).



Thanks!



----

update: I got it with git's own pre-commit hook (use diff to find text file or not). Commit first, then trim trailing blanks, then commit again: diff will be faster, and object can be reused I think (so add can be faster). Code like this, how to use xargs?


#!/bin/sh

function do_trim_text_files_and_commit()
{
		if [ $# == 0 ]; then
				return
		fi

		chmod +w "$@"
		dos2unix "$@"
		sed -i -e "s,\( \|\t\)\+$,," "$@"
		git add -f "$@"
}

# from git "pre-commit", use "diff" to find text files
function trim_text_files_and_commit_again()
{
		git-diff-index -p -M --cached HEAD -- |
		perl -e '
		my $filename;
		my $reported_filename = "";
		while(<>){
				if (m|^diff --git a/(.*) b/\1$|) {
						$filename = $1;
						next;
				}
				if (/^@@ -\S+ \+(\d+)/) {
						next;
				}
				if (/^ /) {
						next;
				}
				if (s/^\+//) {
						$lineno++;
						chomp;
						if (/\s$/) {
								if ($reported_filename ne $filename){
										print "$filename\n";
										$reported_filename = $filename;
								}
						}
				}
		}
		' |
		tr '\n' '\0' | xargs -0 trim-text-files
}

function add_files()
{
	if [ $# == 0 ]; then
		echo "no files specified" > /dev/stderr
		return
	fi

	git add -f "$@" &&

	# trim-text-files can only be run in repo TOP folder.
	while :; do
		if [ -d .git ]; then
			trim_text_files_and_commit_again
			break
		fi
		cd ..
	done
}

if [ "$0" == "/usr/local/bin/git-add-project" ]; then
	git ls-files "$@" |
	tr '\n' '\0' | xargs -0 ~/git-add.sh
elif [ "$0" == "/usr/local/bin/trim-text-files" ]; then
	do_trim_text_files_and_commit "$@"
else
	add_files "$@"
fi







Tags: fedora, 工作
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments