Ajout script git-hg-sub-update
authorHugo Villeneuve <hugo@hugovil.com>
Wed, 31 Jul 2013 16:52:26 +0000 (12:52 -0400)
committerHugo Villeneuve <hugo@hugovil.com>
Wed, 31 Jul 2013 18:07:19 +0000 (14:07 -0400)
scripts/Makefile.am
scripts/git-hg-sub-update [new file with mode: 0755]

index d506774..8d297bb 100644 (file)
@@ -20,7 +20,7 @@ dist_bin_SCRIPTS = \
     tildes-clean \
     vb vd vl vs \
     hg-format-patch hg-update-subrepos \
-    git-hg-sub-import \
+    git-hg-sub-import git-hg-sub-update \
     flac2ogg flac2mp3 \
     hv-scan \
     tape-backup tape-backup-mult tape-list tape-restore
diff --git a/scripts/git-hg-sub-update b/scripts/git-hg-sub-update
new file mode 100755 (executable)
index 0000000..897fb0a
--- /dev/null
@@ -0,0 +1,118 @@
+#!/bin/bash
+set -o errexit
+
+# for use with git-remote-hg:
+#   http://felipec.wordpress.com/2012/11/13/git-remote-hg-bzr-2/
+
+# Uncomment to have verbose debug output
+#debug=1
+
+print_usage()
+{
+    echo "$(basename $0) -- HG subrepository importer for git-remote-hg"
+    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}" 1> /dev/null
+
+    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 1> /dev/null
+}
+
+if [ "x${1}" = "x--help" ]; then
+    print_usage
+    exit 1
+fi
+
+if [ ! -f .hgsub ]; then
+    echo "No Mercurial subrepositories found"
+    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)
+    sub="${sub//$'\r'/}"
+
+    if [ "${sub}" != "" ]; then
+        # Get subrepository URL
+        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/.*\(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 "  src:    ${src}"
+            echo "  dest:   ${dest}"
+        fi
+
+        pushd "${dest}" 1> /dev/null
+        echo "============================"
+        echo "subrepo: ${dest}"
+        git pull
+        popd 1> /dev/null
+    fi
+done < .hgsub