#!/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