Skip to content

Instantly share code, notes, and snippets.

@ermshiperete
Last active December 27, 2015 16:39
Show Gist options
  • Select an option

  • Save ermshiperete/7356850 to your computer and use it in GitHub Desktop.

Select an option

Save ermshiperete/7356850 to your computer and use it in GitHub Desktop.
My script to normalize line endings and cleanup whitespace, and the wrapper script that has all the steps to convert the Bloom repo.
bloom-desktop/
bloom-desktop_hg/
bloom-desktop_hg.hatton/
chorus/
chorus_hg/
chorushelp/
chorushelp_hg/
flexbridge/
fwbridge_hg/
gafaws/
gafaws_hg/
hgresume/
hgresume_hg/
icu-dotnet/
icu-dotnet_hg/
l10nsharp/
l10nsharp_hg/
lfbase/
lfbase_hg/
palaso/
palaso_hg/
wesay/
wesay_hg/
wesay-tx/
wesay-tx_hg/
libgdiplus/
libgdiplus_hg/
lfmerge/
lfmerge_hg/
authors.txt
*.bak
#!/bin/bash
# Add .gitattributes file to all branches if missing. Convert .hgignore to .gitignore.
# Submit as a new commit
echo "****** Add .gitattributes file and convert .hgignore ******"
. $(dirname $0)/cleanupLineEndingsAndWhiteSpace.sh
LOGFILE=/tmp/multibranch-$(basename $(pwd))
IFS=$'\n'
for BRANCH in $(hg branches); do
COMMIT=0
BRANCH="$(expr match "$BRANCH" '\([^ ]*\)')"
if [ $(hg heads --template '{branches} {rev}:{node}\n' "$BRANCH" | wc -l) -gt 1 ]; then
echo "Found branch with multiple heads:" >> $LOGFILE
echo "$(hg heads --template '{branches} {rev}:{node}\n' $BRANCH)" >> $LOGFILE
echo "**** Found branch with multiple heads in $BRANCH"
fi
echo "Processing $BRANCH"
hg update "branch($BRANCH)"
if [ ! -f .gitattributes ]; then
add_gitattributes
hg add .gitattributes
COMMIT=1
fi
for HGIGNORE in $(find . -name .hgignore); do
GITIGNORE="$(dirname $HGIGNORE)/.gitignore"
if [ ! -f "$GITIGNORE" ]; then
echo "Converting $HGIGNORE file"
convert_hgignore "$HGIGNORE" "$GITIGNORE"
hg add "$GITIGNORE"
COMMIT=1
fi
done
if [ $COMMIT -gt 0 ] ; then
hg commit -m "Adding .gitattributes and .gitignore files
This is in preparation for the migration to git."
fi
done
#!/bin/bash
CleanupGitIgnore()
{
COMMIT=0
for GITIGNORE in $(find . -name .gitignore); do
echo "Cleaning $GITIGNORE file"
sed -i 's/^\(output\|obj\|.bzr\|bin\|test-results\|config\|packages\)$/\1\//g' $GITIGNORE
git add $GITIGNORE
COMMIT=1
done
if [ $COMMIT -gt 0 ] ; then
git commit -m "Fix .gitignore file"
fi
}
IFS=$'\n'
for rbranch in $(git branch -r); do
if [[ $rbranch = *origin/HEAD* ]]; then
continue
fi
branch=${rbranch# origin/}
echo Processing branch $branch
git checkout $branch
CleanupGitIgnore
done
#!/bin/bash -e
# Rewrite git repo to normalize line endings and cleanup whitespace
normalize_line_endings()
{
echo "****** Normalize line endings ******"
# Normalize line endings
# see http://christoph.ruegg.name/blog/cleaning-up-after-migrating-from-hg-to-git.html
# Note: the dos2unix in Ubuntu Precise automatically skips binary files. We can't use fromdos
# instead of dos2unix because that can't skip binary files.
AUTOCRLF=$(git config --local core.autocrlf) || true
# Unset whitespace options, otherwise the conversion doesn't work because the files get
# checked out with normalized line endings, resulting in an (always) dirty tree.
[ -f .git/info/attributes ] && mv .git/info/attributes{,.bak}
echo "* !text !whitespace" > .git/info/attributes
git config core.autocrlf false
git reset --hard
git filter-branch -f --prune-empty --tree-filter 'git ls-files -z | xargs -0 dos2unix' --tag-name-filter cat -- --all
if [ -z "$AUTOCRLF" ]; then
git config --unset core.autocrlf
else
git config core.autocrlf "$AUTOCRLF"
fi
rm .git/info/attributes
[ -f .git/info/attributes.bak ] && mv .git/info/attributes{.bak,} || true
}
fix_whitespace()
{
# Fix whitespace
echo "****** Fix whitespace ******"
[ -f .git/info/attributes ] && mv .git/info/attributes{,.bak}
add_gitattributes .git/info/attributes
# following lines based on https://github.com/npp-community/nppcr_repo_scripts/blob/master/git/wsfix.sh
WS_FILTER='filter_whitespace()
{
if git rev-parse --quiet --verify $GIT_COMMIT^ >/dev/null
then
against=$(map $(git rev-parse $GIT_COMMIT^))
git reset -q $against -- .
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
git rm --cached -rfq --ignore-unmatch '*'
fi
git diff --full-index $against $GIT_COMMIT | git apply --cached --whitespace=fix || true
}'
git filter-branch -f --index-filter "$WS_FILTER; filter_whitespace" --tag-name-filter cat -- --all || true
rm .git/info/attributes
[ -f .git/info/attributes.bak ] && mv .git/info/attributes{.bak,} || true
}
add_gitattributes()
{
GITATTR=$1
[ -z $GITATTR ] && GITATTR=.gitattributes
cat > $GITATTR <<-'ENDOFFILE'
* text=auto whitespace=space-before-tab,indent-with-non-tab,trailing-space,tabwidth=4
*.cs diff=csharp
*.cpp diff=cpp
*.h diff=cpp
*.idh diff=cpp
# Different settings for javascript files
*.js whitespace=-indent-with-non-tab,tab-in-indent,trailing-space,tabwidth=2
*.ts whitespace=-indent-with-non-tab,tab-in-indent,trailing-space,tabwidth=2
*.htm* whitespace=-indent-with-non-tab,tab-in-indent,trailing-space,tabwidth=2
*.tmx whitespace=-indent-with-non-tab,tab-in-indent,trailing-space,tabwidth=2
# Don't check (nor fix) whitespace for generated files
*.csproj -whitespace
*.resx -whitespace
*.Designer.cs -whitespace
*.vdproj -whitespace
*.config -whitespace
*.settings -whitespace
*.ReSharper -whitespace
*.vcxproj -whitespace
*.vcxproj.filters -whitespace
*.patch -whitespace
*.svg -whitespace
*.xml -whitespace
*.bmml -whitespace
*.DotSettings -whitespace
*.mdpolicy -whitespace
changelog -whitespace
ENDOFFILE
}
convert_hgignore()
{
HGIGNORE=$1
GITIGNORE=$2
sed 's/^glob://g;s/^relglob://g;s/^syntax:\s*glob//g;s/^syntax:\s*relglob//g;s/^re://g;s/^relre://g;s/^regexp://g;s/^syntax:\s*re//g;s/^syntax:\s*relre//g;s/^syntax:\s*regexp//g;s/^\(output\|obj\|.bzr\|bin\|test-results\|config\|packages\)$/\1\//g;s#\\#/#g' < "$HGIGNORE" > "$GITIGNORE"
}
add_missing_files()
{
# Add .gitattributes file to all branches if missing. Convert .hgignore to .gitignore.
# Submit as a new commit
echo "****** Add .gitattributes file and convert .hgignore ******"
IFS=$'\n'
for BRANCH in $(git branch); do
COMMIT=0
BRANCH="${BRANCH#"${BRANCH%%[![:space:]]*}"}"
BRANCH="${BRANCH#* }"
echo "************ Adding missing files for $BRANCH"
git checkout -f ${BRANCH#origin/}
if [ ! -f .gitattributes ]; then
add_gitattributes
git add .gitattributes
COMMIT=1
fi
for HGIGNORE in $(find . -name .hgignore); do
GITIGNORE="$(dirname $HGIGNORE)/.gitignore"
if [ ! -f "$GITIGNORE" ]; then
echo "Converting $HGIGNORE file"
convert_hgignore "$HGIGNORE" "$GITIGNORE"
git add "$GITIGNORE"
COMMIT=1
fi
done
if [ $COMMIT -gt 0 ] ; then
git commit -m "Adding .gitattributes and .gitignore files"
fi
done
}
cleanup()
{
echo "****** Cleanup ******"
git fsck --full
git prune
git gc --aggressive
}
if [ ! -d .git ]; then
echo "Need to be in root of git repo"
exit
fi
#!/bin/bash
# Convert Bloom repo from Mercurial to Git
NAME=bloom-desktop
HGREPO=https://bitbucket.org/hatton/$NAME
#HGREPO=https://bitbucket.org/yautokes/$NAME
GITNAME=BloomDesktop
$(dirname "$0")/ConvertCommon.sh $NAME $HGREPO --strip-inactive
cd $NAME
git remote add origin [email protected]:BloomBooks/$GITNAME.git
#git push origin --force --all --prune
#git push origin --force --tags --prune
git push origin --force --mirror
#!/bin/bash
# Convert repo from Mercurial to Git
NAME=chorus
HGREPO=http://[email protected]/$NAME
GITNAME=$NAME
set -e
$(dirname "$0")/ConvertCommon.sh $NAME $HGREPO #--add-files-to-hg
echo "****** Pushing to remote repo ******"
cd $NAME
git remote add origin [email protected]:palaso/$GITNAME.git
#git push origin --prune --all
#git push origin --force --tags
#!/bin/bash
# Convert repo from Mercurial to Git
NAME=chorusai
HGREPO=http://[email protected]/$NAME
GITNAME=$NAME
set -e
$(dirname "$0")/ConvertCommon.sh $NAME $HGREPO #--add-files-to-hg
echo "****** Pushing to remote repo ******"
cd $NAME
git remote add origin [email protected]:palaso/$GITNAME.git
#git push origin --prune --all
#git push origin --force --tags
#!/bin/bash
# Convert repo from Mercurial to Git
NAME=chorushelp
HGREPO=http://[email protected]/$NAME
GITNAME=$NAME
set -e
$(dirname "$0")/ConvertCommon.sh $NAME $HGREPO #--add-files-to-hg
echo "****** Pushing to remote repo ******"
cd $NAME
git remote add origin [email protected]:sillsdev/$GITNAME.git
#git push origin --prune --all
#git push origin --force --tags
#!/bin/bash -e
# Convert $NAME repo from Mercurial to Git
NAME=$1
HGREPO=$2
[ -z "$NAME" ] && echo "Missing parameter!" && exit 1
[ -z "HGREPO" ] && echo "Missing parameter!" && exit 1
DIR=$(dirname $(readlink -e $0))
WHERE=$(pwd)
if [ -d ${NAME}_hg ]; then
cd ${NAME}_hg
hg pull
hg update
cd $WHERE
else
hg clone $HGREPO ${NAME}_hg
fi
if [ "$3" = "--add-files-to-hg" ]; then
cd ${NAME}_hg
$DIR/AddMissingFilesToHgBranches.sh
hg push || true
cd $WHERE
shift
fi
echo "****** Preparing git repo ******"
[ -d $NAME ] && rm -rf $NAME
$DIR/hg-to-git.sh --authors=$DIR/authors.txt --force $3 $WHERE/${NAME}_hg $WHERE/${NAME}
cd $WHERE/${NAME}
. $DIR/cleanupLineEndingsAndWhiteSpace.sh
normalize_line_endings
fix_whitespace
if [ "$3" != "--add-files-to-hg" ]; then
add_missing_files
fi
cleanup
#!/bin/bash
# Convert repo from Mercurial to Git
NAME=fwbridge
HGREPO=http://[email protected]/$NAME
GITNAME=flexbridge
set -e
$(dirname "$0")/ConvertCommon.sh $NAME $HGREPO --add-files-to-hg
echo "****** Some extra cleaning ******"
cd $NAME
branch=LT-12877_Reusing_menu_command_should_restore_window_not_cause_problem
git tag -am "Mercurial branch that was closed" "closed/$branch" "$branch" && git branch -D "$branch"
echo "****** Pushing to remote repo ******"
git remote add origin [email protected]:palaso/$GITNAME.git
git push origin --all
git push origin --force --tags
#!/bin/bash
# Convert repo from Mercurial to Git
NAME=gafaws
HGREPO=http://[email protected]/$NAME
GITNAME=$NAME
set -e
$(dirname "$0")/ConvertCommon.sh $NAME $HGREPO #--add-files-to-hg
echo "****** Pushing to remote repo ******"
cd $NAME
git remote add origin [email protected]:sillsdev/$GITNAME.git
#git push origin --prune --all
#git push origin --force --tags
#!/bin/bash
ConvertHgIgnore()
{
COMMIT=0
for HGIGNORE in $(find . -name .hgignore); do
GITIGNORE="$(dirname $HGIGNORE)/.gitignore"
if [ ! -f "$GITIGNORE" ]; then
echo "Converting $HGIGNORE file"
sed 's/^glob://g;s/^relglob://g;s/^syntax:\s*glob//g;s/^syntax:\s*relglob//g;s/^re://g;s/^relre://g;s/^regexp://g;s/^syntax:\s*re//g;s/^syntax:\s*relre//g;s/^syntax:\s*regexp//g;s/^\(output\|obj\|.bzr\|\bin\|test-results\|config\|packages\)$/\1\//g' < "$HGIGNORE" > "$GITIGNORE"
git add "$GITIGNORE"
COMMIT=1
fi
done
if [ $COMMIT -gt 0 ] ; then
git commit -m "Adding .gitignore file"
fi
}
IFS=$'\n'
for rbranch in $(git branch -r); do
if [[ $rbranch = *origin/HEAD* ]]; then
continue
fi
branch=${rbranch# origin/}
echo Processing branch $branch
git checkout $branch
git rebase origin/$branch
ConvertHgIgnore
done
#!/bin/bash
# Convert repo from Mercurial to Git
NAME=hgresume
HGREPO=http://[email protected]/$NAME
GITNAME=$NAME
set -e
$(dirname "$0")/ConvertCommon.sh $NAME $HGREPO #--add-files-to-hg
echo "****** Pushing to remote repo ******"
cd $NAME
git remote add origin [email protected]:sillsdev/$GITNAME.git
#git push origin --prune --all
#git push origin --force --tags
#!/bin/bash
# Convert repo from Mercurial to Git
NAME=icu-dotnet
HGREPO=http://[email protected]/$NAME
GITNAME=$NAME
set -e
$(dirname "$0")/ConvertCommon.sh $NAME $HGREPO --add-files-to-hg
echo "****** Pushing to remote repo ******"
cd $NAME
git remote add origin [email protected]:palaso/$GITNAME.git
git push origin --prune --all
git push origin --tags
#!/bin/bash
# Convert repo from Mercurial to Git
NAME=l10nsharp
HGREPO=https://bitbucket.org/hatton/$NAME
GITNAME=$NAME
set -e
$(dirname "$0")/ConvertCommon.sh $NAME $HGREPO --add-files-to-hg
echo "****** Pushing to remote repo ******"
cd $NAME
git remote add origin [email protected]:palaso/$GITNAME.git
git push origin --prune --all
git push origin --force --tags
#!/bin/bash
# Convert repo from Mercurial to Git
NAME=lfbase
HGREPO=http://[email protected]/$NAME
GITNAME=$NAME
set -e
$(dirname "$0")/ConvertCommon.sh $NAME $HGREPO #--add-files-to-hg
echo "****** Pushing to remote repo ******"
cd $NAME
git remote add origin [email protected]:sillsdev/$GITNAME.git
#git push origin --prune --all
#git push origin --force --tags
#!/bin/bash
# Convert repo from Mercurial to Git
NAME=lfmerge
HGREPO=http://[email protected]/$NAME
GITNAME=LFMerge
set -e
$(dirname "$0")/ConvertCommon.sh $NAME $HGREPO --add-files-to-hg
echo "****** Pushing to remote repo ******"
cd $NAME
git remote add origin [email protected]:sillsdev/$GITNAME.git
git push origin --prune --all
git push origin --force --tags
#!/bin/bash
# Convert Palaso's libgdiplus repo from Mercurial to Git
NAME=libgdiplus
HGREPO=http://hg.palaso.org/$NAME
GITNAME=libgdiplus
set -e
$(dirname "$0")/ConvertCommon.sh $NAME $HGREPO
#!/bin/bash
# Convert Palaso repo from Mercurial to Git
NAME=palaso
HGREPO=http://[email protected]/$NAME
GITNAME=libpalaso
set -e
$(dirname "$0")/ConvertCommon.sh $NAME $HGREPO --add-files-to-hg
cd $NAME
#git remote add origin [email protected]:palaso/$GITNAME.git
#git push origin --all
#git push origin --force --tags
git remote add sillsdev https://github.com/sillsdev/$GITNAME.git
git config remote.sillsdev.pushurl ssh://$(git config fwinit.gerrituser)@gerrit.lsdev.sil.org:59418/$GITNAME.git
git push sillsdev --all
git push sillsdev --force --tags
#!/bin/bash
# Convert repo from Mercurial to Git
NAME=wesay-tx
HGREPO=http://[email protected]/$NAME
GITNAME=$NAME
set -e
$(dirname "$0")/ConvertCommon.sh $NAME $HGREPO
echo "****** Pushing to remote repo ******"
cd $NAME
git remote add origin [email protected]:sillsdev/$GITNAME.git
git push origin --prune --all
git push origin --force --tags
#!/bin/bash
# Convert repo from Mercurial to Git
NAME=wesay
HGREPO=http://[email protected]/$NAME
GITNAME=$NAME
set -e
$(dirname "$0")/ConvertCommon.sh $NAME $HGREPO --add-files-to-hg
echo "****** Pushing to remote repo ******"
cd $NAME
git remote add origin [email protected]:palaso/$GITNAME.git
git push origin --prune --all
git push origin --force --tags
#!/bin/bash -e
# Adapted from http://www.manas.com.ar/mgarcia/2013/10/09/introducing-hg-to-git/
usage()
{
exec >&2
echo "Usage: $(basename $0) [OPTIONS] /path/to/hg/repo /path/to/new/repo"
echo ""
echo "OPTIONS:"
echo " -a, --authors=FILE File of author mappings"
echo " -f, --force Passed on to hg-fast-export"
echo " -h, --help Display this help"
exit ${1-0}
}
abort()
{
exec >&2
echo "$1"
echo ""
usage 1
}
abs()
{
readlink -f "$1"
}
# Use getopt to parse options into a manageable form
PROGNAME=$(basename "$0")
PARSEDARGS=$(getopt -n "$PROGNAME" \
-o a:fh --long authors:,force,help,strip-inactive \
-- "$@") || usage $?
eval set -- "$PARSEDARGS"
# Process options
AUTHORS=
while true
do
case "$1" in
-a|--authors) AUTHORS=$(abs $2); shift;;
-f|--force) FORCE=--force;;
--help) usage ;;
--strip-inactive) STRIP_INACTIVE_BRANCHES=true ;;
--) shift; break ;;
*) echo "Internal error: option '$1' not handled"; exit 1;;
esac
shift
done
if [ $# -ne 2 ]
then
echo "Incorrect number of arguments:" "$@" >&2
echo "" >&2
usage 1
fi
[ -d "$1" ] || abort "$1 should be an existing hg repository"
[ -d "$1/.hg" ] || abort "$1 should be an existing hg repository ($1/.hg must be a directory)"
[ -d "$2" ] && abort "$2 should not exist"
SOURCE=$(cd "$1" && pwd)
git init "$2" && cd "$2"
LOG=$(mktemp)
SANITIZE=$(mktemp)
trap "rm -f $LOG $SANITIZE" 0
hg-fast-export -r "$SOURCE" $FORCE ${AUTHORS:+-A $AUTHORS} |& tee "$LOG"
git checkout
sed -n '/Warning: sanitized branch/s/.*\[\([^]]*\)\] to \[\([^]]*\)\].*/s@^\1$@\2@/p' "$LOG" |
sort -u -o "$SANITIZE"
(cd "$SOURCE" && hg branches --closed) |
sed -n '/(closed)/s/ *[0-9]*:.*//p' |
sed -f "$SANITIZE" |
while read branch
do
git tag -am "Mercurial branch that was closed" "closed/$branch" "$branch" &&
git branch -D "$branch"
done
if [ -n "$STRIP_INACTIVE_BRANCHES" ]; then
(cd "$SOURCE" && hg branches --closed) |
sed -n '/(inactive)/s/ *[0-9]*:.*//p' |
sed -f "$SANITIZE" |
while read branch
do
git tag -am "Mercurial branch that was inactive" "inactive/$branch" "$branch" &&
git branch -D "$branch"
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment