X-Git-Url: http://gitweb.hugovil.com/?a=blobdiff_plain;f=scripts%2Fgit-hg-sub-import;fp=scripts%2Fgit-hg-sub-import;h=d5d947fb3906d85cc7c60113c5d951ea27201391;hb=bfb50df5b671e5de95fccd9ceaefe5b15ef04041;hp=0000000000000000000000000000000000000000;hpb=48a944a21b629ce72d2008a18900e8ab626ea969;p=hvutilities.git diff --git a/scripts/git-hg-sub-import b/scripts/git-hg-sub-import new file mode 100755 index 0000000..d5d947f --- /dev/null +++ b/scripts/git-hg-sub-import @@ -0,0 +1,103 @@ +#!/bin/bash +set -o errexit + +# Uncomment to have verboswe debug output +#debug=1 + +# for use with git-remote-hg: +# http://felipec.wordpress.com/2012/11/13/git-remote-hg-bzr-2/ + +LSI_SRV=/mnt/server/research/Projects/HG_Repo + +print_usage() +{ + echo "$(basename $0) -- HG subrepository importer for git-remote-hg" + echo "Usage: $(basename $0) [OPTIONS...]" +} + +# Map a revision to a branch name in HG subrepository +# Use hg log (in original repo) to get branch name corresponding to that +# revision. +# +# Arg 1: subrepository URL +# Arg 2: id +# Arg 3: revision +subrepo_find_branch() { + local src="${1}" + local id="${2}" + local rev="${3}" + + pushd "${LSI_SRV}/${src}" + + branch=$(hg log -r ${rev} | grep "branch:" | sed "s/branch:\ *//") + + if [ -z "${branch}" ]; then + # If "branch:" is null, this indicate we are on the default branch + branch=default + fi + + if [ -n "${debug}" ]; then + echo " branch: ${branch}" + fi + + popd +} + +if [ "x${1}" = "x--help" ]; then + print_usage + exit 1 +fi + +if [ ! -f .hgsub ]; then + echo "No Mercurial subrepositories found" + exit 1 +fi + +# Read lines from .hgsub +while read sub; do + # Remove CR (DOS) + sub="${sub//$'\r'/}" + + if [ "${sub}" != "" ]; then + # Get subrepository URL + src="${sub//*\/HG_REPO/}" + + # Get subrepository local alias or label + dest="${sub// =*}" + + # Get project ID (example: S0289) + id=$(echo ${sub} | sed "s/.*HG_REPO\(S[0-9][0-9][0-9][0-9]\).*/\1/") + + # Get revision of subrepository (remove CR from .hgsubstate) + rev=$(cat .hgsubstate | tr -d '\r' | grep "${id}" | sed "s/ .*//") + + if [ -n "${debug}" ]; then + echo "repo: ${src}" + echo " id: ${id}" + echo " rev: ${rev}" + echo " dest: ${dest}" + fi + + if [ ! -d "${dest}" ]; then + subrepo_find_branch "${src}" ${id} ${rev} + + # git-remote-hg seems to replace + # "BRANCH - NAME" + # with + # "BRANCH___-___NAME" + branch="${branch// /___}" + + git clone "hg::${LSI_SRV}/${src}" "${dest}" + + if [ "x${branch}" != "xdefault" ]; then + cd "${dest}" + git checkout "branches/${branch}" + cd .. + fi + else + pushd "${dest}" + git pull + popd + fi + fi +done < .hgsub