Ajout script git-hg-sub-import
authorHugo Villeneuve <hugo@hugovil.com>
Fri, 28 Jun 2013 14:03:00 +0000 (10:03 -0400)
committerHugo Villeneuve <hugo@hugovil.com>
Wed, 31 Jul 2013 17:02:21 +0000 (13:02 -0400)
scripts/Makefile.am
scripts/git-hg-sub-import [new file with mode: 0755]

index d6bd5e1..84a7df4 100644 (file)
@@ -18,7 +18,9 @@ dist_bin_SCRIPTS = \
     setdate \
     strip-debug-symbols \
     tildes-clean \
-    vb vd vl vs hg-format-patch \
+    vb vd vl vs \
+    hg-format-patch \
+    git-hg-sub-import \
     flac2ogg flac2mp3 \
     hv-scan \
     tape-backup tape-backup-mult tape-list tape-restore
diff --git a/scripts/git-hg-sub-import b/scripts/git-hg-sub-import
new file mode 100755 (executable)
index 0000000..d5d947f
--- /dev/null
@@ -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