--- /dev/null
+#!/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