X-Git-Url: http://gitweb.hugovil.com/?a=blobdiff_plain;ds=sidebyside;f=scripts%2Fgit-hg-sub-import;h=41617d1058c81431031f3ff655d9edf5c522bbad;hb=20cb6a4a1b260f18c0cc1414431080ecab3f3da4;hp=d5d947fb3906d85cc7c60113c5d951ea27201391;hpb=bfb50df5b671e5de95fccd9ceaefe5b15ef04041;p=hvutilities.git diff --git a/scripts/git-hg-sub-import b/scripts/git-hg-sub-import index d5d947f..41617d1 100755 --- a/scripts/git-hg-sub-import +++ b/scripts/git-hg-sub-import @@ -1,13 +1,11 @@ #!/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 +# Uncomment to have verbose debug output +#debug=1 print_usage() { @@ -15,6 +13,24 @@ print_usage() echo "Usage: $(basename $0) [OPTIONS...]" } +# git-remote-hg doesn't work with Mercurial subpaths extension, +# so use it manually +hg_subpaths_config() { + subpaths=$(hg showconfig | grep "subpaths") + + if [ -n ${subpaths} ]; then + sp_src=${subpaths//subpaths./} + sp_src=${sp_src//=*/} + sp_src=${sp_src//\\/} # Remove windows separator (LSI) + sp_dst=${subpaths//*=/} + + if [ -n "${debug}" ]; then + echo "sp_src = $sp_src" + echo "sp_dst = $sp_dst" + fi + fi +} + # Map a revision to a branch name in HG subrepository # Use hg log (in original repo) to get branch name corresponding to that # revision. @@ -27,7 +43,7 @@ subrepo_find_branch() { local id="${2}" local rev="${3}" - pushd "${LSI_SRV}/${src}" + pushd "${src}" 1> /dev/null branch=$(hg log -r ${rev} | grep "branch:" | sed "s/branch:\ *//") @@ -36,11 +52,15 @@ subrepo_find_branch() { branch=default fi + num=$(hg log --branch "${branch}" --template '{node}\n' | \ + grep -n ${rev} | awk -F ':' '{print $1}') + if [ -n "${debug}" ]; then echo " branch: ${branch}" + echo " num: ${num}" fi - popd + popd 1> /dev/null } if [ "x${1}" = "x--help" ]; then @@ -53,6 +73,13 @@ if [ ! -f .hgsub ]; then exit 1 fi +if [ ! -f .gitignore ]; then + # We do not want to track .gitignore itself + echo ".gitignore" > .gitignore +fi + +hg_subpaths_config + # Read lines from .hgsub while read sub; do # Remove CR (DOS) @@ -60,13 +87,16 @@ while read sub; do if [ "${sub}" != "" ]; then # Get subrepository URL - src="${sub//*\/HG_REPO/}" + src="${sub//*= /}" + + # Replace using subpaths extension content + src=${src/${sp_src}/${sp_dst}} # 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/") + id=$(echo ${sub} | sed "s/.*\(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/ .*//") @@ -75,6 +105,7 @@ while read sub; do echo "repo: ${src}" echo " id: ${id}" echo " rev: ${rev}" + echo " src: ${src}" echo " dest: ${dest}" fi @@ -87,17 +118,27 @@ while read sub; do # "BRANCH___-___NAME" branch="${branch// /___}" - git clone "hg::${LSI_SRV}/${src}" "${dest}" + git clone "hg::${src}" "${dest}" if [ "x${branch}" != "xdefault" ]; then cd "${dest}" git checkout "branches/${branch}" + + # Adjusting git tree to specific commit specified in + # .hgsubstate: + # The SHA from hg and git are not the same, therefore, we must + # find the commit sequence in hg and map this + # to a hash in git using the ${num} variable: + git_rev=$(git log --oneline | sed -n "${num}p" | awk '{print $1}') + git reset --hard ${git_rev} + cd .. fi - else - pushd "${dest}" - git pull - popd + + if ! grep -q "${dest}" .gitignore ; then + # Ignore subrepo in top-level git repository + echo "${dest}" >> .gitignore + fi fi fi done < .hgsub