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

  • Calendars

    Created a bunch of calendars in Google Calendar, to celebrate JY's last day of preschool, prepare for the coming weeks. Google builtin…

  • 猪头了吧 T. Rowe Price

  • (no subject)

    昨晚上十二点了还在唠叨,跟孩子讲什么总有一天妈妈会离开啊,所以赶紧去跟爸爸睡吧,诸如此类。非要把人惹毛了才行。若是理智的人,断然不会给小孩子打这种预防针。徒然给小宝增加心理阴影。嘴上说说很容易,完全不想想后果。当初可意被关进电梯,后来不管是谁说一句“不要她了”都会让她大哭不止,这心理阴影多久才平复的…

  • 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