#!/bin/bash
#
# HG wrappers
## Run `hg ...` within $REPO.
function hg {
local repo=$1; shift
cmd $HG -R "$repo" "${HG_ARGS[@]}" "$@"
}
## Does the repo have local modifications?
function hg_modified {
hg $1 id -i | grep -q '+'
}
## Get the date for the current commit as an unix timestamp
function hg_time {
local repo=$1
local hg_unix=
local hg_tz=
local hg_date=$(hg $repo log -r . --template '{date|hgdate}')
local hg_unix=${hg_date% *}
local hg_tz=${hg_date#* }
[ -n "$hg_unix" ] || fail "failed to read hg time"
echo "$hg_unix"
}
## Show changes in repo
# hg_diff [path ...]
function hg_diff {
local repo=$1; shift
hg $repo diff "$@"
}
## Commit changes in repo, with given message:
#
# hg_commit .../etc $msg
#
# Automatically determines possible -u to use when running with sudo.
function hg_commit {
local repo="$1"
local msg="$2"
local user_opt=
local msg_opt=
if [ ${SUDO_USER:-} ]; then
user_opt=('-u' "$SUDO_USER")
elif [ $HOME ] && [ -e $HOME/.hgrc ]; then
debug "using .hgrc user"
user_opt=( )
else
user_opt=('-u' "$USER")
fi
if [ "$msg" ]; then
msg_opt=('-m' "$msg")
fi
# XXX: there's something about bash arrays that I don't like... empty arrays behave badly
# mercurial does not like it if you pass it '' as an argument
if [ -n "${user_opt:-}" -a -n "${msg_opt:-}" ]; then
hg $repo commit "${user_opt[@]}" "${msg_opt[@]}"
elif [ -n "${user_opt:-}" ]; then
hg $repo commit "${user_opt[@]}"
elif [ -n "${msg_opt:-}" ]; then
hg $repo commit "${msg_opt[@]}"
else
hg $repo commit
fi
}