lib/update.utils
changeset 52 b68b8615c512
child 55 a2d87cfd77e4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/update.utils	Tue Mar 20 14:12:11 2012 +0200
@@ -0,0 +1,94 @@
+# vim: set ft=sh :
+#
+# Utility functions
+
+
+### Command execution
+## Execute command, possibly logging its execution.
+#
+#   cmd     $cmd...
+#
+# Fails if the command returns an error exit code.
+function cmd {
+    log_cmd "$@"
+
+    "$@" || die "Failed"
+}
+
+function indent () {
+    local indent=$1; shift
+
+    log_cmd "$@"
+
+    "$@" | sed "s/^/$indent/"
+
+    return ${PIPESTATUS[0]}
+}
+
+
+### FS utils
+# Create dir in $ROOT if not exists.
+function ensure_dir {
+    local dir=$1
+
+    if [ ! -d $ROOT/$dir ]; then
+        log_warn "Creating output dir: $dir"
+        cmd mkdir $ROOT/$dir
+    fi
+}
+
+## Output absolute path from $ROOT:
+#
+#   abspath $path
+#
+function abspath () {
+    local path=$1
+
+    echo "$ROOT/$path"
+}
+
+### HG wrappers
+# Run `hg ...` within $REPO.
+function hg {
+    local repo=$REPO
+
+    cmd $HG -R $ROOT/$repo "$@"
+}
+
+# Does the repo have local modifications?
+function hg_modified {
+    hg id | grep -q '+'
+}
+
+# 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
+function hg_diff {
+    hg diff -X "$REPO/$REPO_HIDE"
+}
+
+## Commit changes in repo, with given message:
+#
+#   hg_commit   $msg
+#
+function hg_commit {
+    local msg=$1
+    local user_opt=$(hg_user)
+    
+    debug "$user_opt: $msg"
+    hg commit $user_opt -m "$msg"
+}
+
+