#!/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"
}
## Output possible -u flag for commit.
function hg_user {
if [ ${SUDO_USER:-} ]; then
echo '-u' "$SUDO_USER"
elif [ $HOME ] && [ -e $HOME/.hgrc ]; then
debug "using .hgrc user"
echo ''
else
echo '-u' "$USER"
fi
}
## 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 $msg
#
# Automatically determines possible -u to use when running with sudo.
function hg_commit {
local repo=$1
local msg=$2
local user_opt=$(hg_user)
local msg_opt=
[ $msg ] && msg_opt=('-m' "$msg")
debug "$user_opt: $msg"
hg $repo commit ${user_opt[@]} ${msg_opt[@]}
}