X-Git-Url: http://gitweb.hugovil.com/?a=blobdiff_plain;f=scripts%2Fgit-hg-sub-import;h=38f862187d3c6199d675f63dc9cc18d156702b60;hb=37ec648edcf6dc34bedb64f4b595c32d7ea2e32f;hp=ecd7757ac0381573e98c79b354e3895190e5de94;hpb=a3d197ba5982637e448f1f89201bca4d99e01602;p=hvutilities.git diff --git a/scripts/git-hg-sub-import b/scripts/git-hg-sub-import index ecd7757..38f8621 100755 --- a/scripts/git-hg-sub-import +++ b/scripts/git-hg-sub-import @@ -5,7 +5,7 @@ set -o errexit # http://felipec.wordpress.com/2012/11/13/git-remote-hg-bzr-2/ # Uncomment to have verbose debug output -#debug=1 +debug=1 print_usage() { @@ -16,7 +16,9 @@ print_usage() # git-remote-hg doesn't work with Mercurial subpaths extension, # so use it manually hg_subpaths_config() { - subpaths=$(hg showconfig | grep "subpaths") + subpaths=$(hg showconfig | grep "subpaths" | grep "HG_REPO") + + echo "subpaths = ${subpaths}" if [ -n ${subpaths} ]; then sp_src=${subpaths//subpaths./} @@ -24,9 +26,46 @@ hg_subpaths_config() { sp_src=${sp_src//\\/} # Remove windows separator (LSI) sp_dst=${subpaths//*=/} - echo "sp_src = $sp_src" - echo "sp_dst = $sp_dst" + if [ -n "${debug}" ]; then + echo "sp_src = $sp_src" + echo "sp_dst = $sp_dst" + fi + fi +} + +# git-remote-hg doesn't work with Mercurial projrc extension, +# so use it manually. +# We parse the projrc file in the assembly (on the server), to check +# for any projrc substitutions. +# +# Arg 1: subproject (path and name) +# +# Return the new substitution name if found, and original subrepo name if not +# (with the path stripped) +hg_check_projrc_config() { + if [ ${#} -lt 1 ]; then + echo "Missing subrepos name" + fi + + # Repo name can have spaces in it, hence the use of ${*} + local subrepo_orig="${*}" + local subrepo="${*}" + + # Quote spaces + subrepo=${subrepo// /\\\\ } + + asm=$(cat .git/config | grep "hg::" | sed "s/.*url = hg::\(.*\)/\1/") + + local baserepo=$(basename "${subrepo}") + + if cat "${asm}/.hg/projrc" | grep -q "${baserepo}" ; then + projrc=$(cat "${asm}/.hg/projrc" | grep "${baserepo}" | sed "s/.*= \(.*\)/\1/") + else + # No substitution, return same name as original + projrc=$(basename "${subrepo_orig}") fi + + echo "${projrc}" } # Map a revision to a branch name in HG subrepository @@ -41,7 +80,7 @@ subrepo_find_branch() { local id="${2}" local rev="${3}" - pushd "${src}" + pushd "${src}" 1> /dev/null branch=$(hg log -r ${rev} | grep "branch:" | sed "s/branch:\ *//") @@ -50,11 +89,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 @@ -95,12 +138,20 @@ while read sub; do # Get revision of subrepository (remove CR from .hgsubstate) rev=$(cat .hgsubstate | tr -d '\r' | grep "${id}" | sed "s/ .*//") + # Original name + repo=$(basename "${src}") + srcpath=$(dirname "${src}") + + projrc_sub=$(hg_check_projrc_config "${src}") + src="${srcpath}/${projrc_sub}" + if [ -n "${debug}" ]; then - echo "repo: ${src}" + echo "repo: ${repo}" + echo " path: ${srcpath}" echo " id: ${id}" - echo " rev: ${rev}" - echo " src: ${src}" + echo " src: ${projrc_sub}" echo " dest: ${dest}" + echo " rev: ${rev}" fi if [ ! -d "${dest}" ]; then @@ -112,27 +163,38 @@ while read sub; do # "BRANCH___-___NAME" branch="${branch// /___}" + if [ -n "${debug}" ]; then + echo "branch: ${branch}" + fi + git clone "hg::${src}" "${dest}" - if [ "x${branch}" != "xdefault" ]; then - cd "${dest}" - git checkout "branches/${branch}" + cd "${dest}" - # This does not work: the SHA from hg and git - # are not the same... - #git reset --hard ${rev} + if [ -x ${HOME}/scripts/git-set-local-author.sh ]; then + # Make sure commits have correct author for LSI + ${HOME}/scripts/git-set-local-author.sh + fi - cd .. + if [ "x${branch}" != "xdefault" ]; then + # Make tracking branch + git checkout -f "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} fi + cd .. + if ! grep -q "${dest}" .gitignore ; then # Ignore subrepo in top-level git repository echo "${dest}" >> .gitignore fi - else - pushd "${dest}" - git pull - popd fi fi done < .hgsub