Add iso8859-to-utf8.sh
[hvutilities.git] / scripts / git-hg-sub-import
index 9789578..0180d4f 100755 (executable)
@@ -1,6 +1,10 @@
 #!/bin/bash
 set -o errexit
 
+SCRIPT="`readlink -e $0`"
+SCRIPTPATH=$(dirname ${SCRIPT})
+source ${SCRIPTPATH}/hv-hg-functions.sh
+
 # for use with git-remote-hg:
 #   http://felipec.wordpress.com/2012/11/13/git-remote-hg-bzr-2/
 
@@ -13,56 +17,6 @@ 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.
-#
-# Arg 1: subrepository URL
-# Arg 2: id
-# Arg 3: revision
-subrepo_find_branch() {
-    local src="${1}"
-    local id="${2}"
-    local rev="${3}"
-
-    pushd "${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
-
-    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
-}
-
 if [ "x${1}" = "x--help" ]; then
     print_usage
     exit 1
@@ -78,7 +32,15 @@ if [ ! -f .gitignore ]; then
     echo ".gitignore" > .gitignore
 fi
 
-hg_subpaths_config
+asm=$(cat .git/config | grep "hg::" | sed "s/.*url = hg::\(.*\)/\1/")
+hg_parse_projrc ${asm}
+
+hg_parse_subpaths
+
+HGSUB_TMP="/tmp/$(basename $0).hgsub-$$.tmp"
+
+# Create temporary copy to replace backslashes with slashes.
+cat .hgsub | sed 's/\\/\//g' > ${HGSUB_TMP}
 
 # Read lines from .hgsub
 while read sub; do
@@ -89,8 +51,12 @@ while read sub; do
         # Get subrepository URL
         src="${sub//*= /}"
 
-        # Replace using subpaths extension content
-        src=${src/${sp_src}/${sp_dst}}
+        # Replace using Mercurial subpaths substitutions (either projrc or from hgrc)
+        src=$(apply_substitutions ${src})
+
+        # Original name
+        repo=$(basename "${src}")
+        srcpath=$(dirname "${src}")
 
         # Get subrepository local alias or label
         dest="${sub// =*}"
@@ -102,11 +68,11 @@ while read sub; do
         rev=$(cat .hgsubstate | tr -d '\r' | grep "${id}" | sed "s/ .*//")
 
         if [ -n "${debug}" ]; then
-            echo "repo: ${src}"
-            echo "  id:     ${id}"
-            echo "  rev:    ${rev}"
-            echo "  src:    ${src}"
-            echo "  dest:   ${dest}"
+            echo "repo: ${repo}"
+            echo "  path:  ${srcpath}"
+            echo "  id:    ${id}"
+            echo "  local: ${dest}"
+            echo "  rev:   ${rev}"
         fi
 
         if [ ! -d "${dest}" ]; then
@@ -118,11 +84,22 @@ while read sub; do
             #   "BRANCH___-___NAME"
             branch="${branch// /___}"
 
+            if [ -n "${debug}" ]; then
+                echo "branch: ${branch}"
+            fi
+
             git clone "hg::${src}" "${dest}"
 
+            cd "${dest}"
+
+            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
+
             if [ "x${branch}" != "xdefault" ]; then
-                cd "${dest}"
-                git checkout "branches/${branch}"
+                # Make tracking branch
+                git checkout -f "branches/${branch}"
 
                 # Adjusting git tree to specific commit specified in
                 # .hgsubstate:
@@ -131,18 +108,16 @@ while read sub; do
                 # 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
 
+            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
+done < ${HGSUB_TMP}
+
+rm ${HGSUB_TMP}