#!/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/ # 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...]" } 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 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 # Remove CR (DOS) sub="${sub//$'\r'/}" if [ "${sub}" != "" ]; then # Get subrepository URL src="${sub//*= /}" # 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// =*}" # 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: ${repo}" echo " path: ${srcpath}" echo " id: ${id}" echo " local: ${dest}" echo " rev: ${rev}" fi if [ ! -d "${dest}" ]; then subrepo_find_branch "${src}" ${id} ${rev} # git-remote-hg seems to replace # "BRANCH - NAME" # with # "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 # Make tracking branch git checkout -f "branches/${branch}" # Adjusting git tree to specific commit specified in # .hgsubstate: # The SHA from hg and git are not the same, therefore, we must # find the commit sequence in hg and map this # 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} fi cd .. if ! grep -q "${dest}" .gitignore ; then # Ignore subrepo in top-level git repository echo "${dest}" >> .gitignore fi fi fi done < ${HGSUB_TMP} rm ${HGSUB_TMP}