+# Replace the given path with either entries from PROJRC or SUBPATHS
+# Arg 1: path
+apply_substitutions() {
+ # Repo name can have spaces in it, hence the use of ${*}
+ local path="${*}"
+
+ if [ ${PROJRC_COUNT} -ne 0 ]; then
+ for s in $(seq 0 $((${PROJRC_COUNT} - 1))); do
+ local pattern=${PROJRC_KEY[${s}]}
+ local replacement=${PROJRC_VAL[${s}]}
+
+ path=${path/${pattern}/${replacement}}
+ done
+ fi
+
+ if [ ${SUBPATHS_COUNT} -ne 0 ]; then
+ for s in $(seq 0 $((${SUBPATHS_COUNT} - 1))); do
+ local pattern=${SUBPATHS_KEY[${s}]}
+ local replacement=${SUBPATHS_VAL[${s}]}
+
+ path=${path/${pattern}/${replacement}}
+ done
+ fi
+
+ echo "${path}"
+}
+
+# git-remote-hg doesn't work with Mercurial projrc extension,
+# so use it manually.
+# We parse the projrc file in the assembly (on the server), to check
+# for any projrc substitutions.
+#
+# Arg 1: subproject (path and name)
+#
+# Return the new substitution name if found, and original subrepo name if not
+# (with the path stripped)
+hg_check_projrc_config() {
+ if [ ${#} -lt 1 ]; then
+ echo "Missing subrepos name"
+ fi
+
+ # Repo name can have spaces in it, hence the use of ${*}
+ local subrepo_orig="${*}"
+ local subrepo="${*}"
+
+ # Quote spaces
+ subrepo=${subrepo// /\\\\ }
+
+ asm=$(cat .git/config | grep "hg::" | sed "s/.*url = hg::\(.*\)/\1/")
+
+ local baserepo=$(basename "${subrepo}")
+
+ if cat "${asm}/.hg/projrc" | grep -q "${baserepo}" ; then
+ projrc=$(cat "${asm}/.hg/projrc" | grep "${baserepo}" | sed "s/.*= \(.*\)/\1/")
+ else
+ # No substitution, return same name as original
+ projrc=$(basename "${subrepo_orig}")
+ fi
+
+ echo "${projrc}"
+}
+