terom@60: #!/bin/bash terom@52: # vim: set ft=sh : terom@52: # terom@52: # Utility functions terom@52: terom@52: terom@52: ### Command execution terom@52: ## Execute command, possibly logging its execution. terom@52: # terom@52: # cmd $cmd... terom@52: # terom@52: # Fails if the command returns an error exit code. terom@52: function cmd { terom@52: log_cmd "$@" terom@52: terom@52: "$@" || die "Failed" terom@52: } terom@52: terom@66: ### Command execution terom@66: ## Execute command as a test, logging its execution at log_cmd terom@66: # terom@66: # cmd_test $cmd... && ... || ... terom@66: # terom@66: # Fails if the command returns an error exit code. terom@66: function cmd_test { terom@66: log_cmd "$@" terom@66: terom@66: "$@" terom@66: } terom@55: ## Execute command, prefixing its output on stdout with given indent prefix. terom@55: # terom@55: # indent " " $cmd... terom@55: # terom@55: # Output is kept on stdout, exit status is that of the given command. terom@55: # Also logs the executed command at log_cmd level.. terom@52: function indent () { terom@52: local indent=$1; shift terom@52: terom@52: log_cmd "$@" terom@52: terom@52: "$@" | sed "s/^/$indent/" terom@52: terom@52: return ${PIPESTATUS[0]} terom@52: } terom@52: terom@52: terom@52: ### FS utils terom@52: # Create dir in $ROOT if not exists. terom@52: function ensure_dir { terom@52: local dir=$1 terom@52: terom@52: if [ ! -d $ROOT/$dir ]; then terom@52: log_warn "Creating output dir: $dir" terom@52: cmd mkdir $ROOT/$dir terom@52: fi terom@52: } terom@52: terom@52: ## Output absolute path from $ROOT: terom@52: # terom@52: # abspath $path terom@52: # terom@52: function abspath () { terom@52: local path=$1 terom@52: terom@52: echo "$ROOT/$path" terom@52: } terom@52: terom@52: ### HG wrappers terom@52: # Run `hg ...` within $REPO. terom@52: function hg { terom@52: local repo=$REPO terom@52: terom@58: cmd $HG -R $ROOT/$repo "${HG_ARGS[@]}" "$@" terom@52: } terom@52: terom@52: # Does the repo have local modifications? terom@52: function hg_modified { terom@52: hg id | grep -q '+' terom@52: } terom@52: terom@52: # Output possible -u flag for commit. terom@52: function hg_user { terom@52: if [ ${SUDO_USER:-} ]; then terom@52: echo '-u' "$SUDO_USER" terom@52: terom@52: elif [ $HOME ] && [ -e $HOME/.hgrc ]; then terom@52: debug "using .hgrc user" terom@52: echo '' terom@52: terom@52: else terom@52: echo '-u' "$USER" terom@52: fi terom@52: } terom@52: terom@52: # Show changes in repo terom@52: function hg_diff { terom@57: # just stat hidden files, but show the rest terom@57: hg diff --stat -I "$REPO/$REPO_HIDE" terom@52: hg diff -X "$REPO/$REPO_HIDE" terom@52: } terom@52: terom@52: ## Commit changes in repo, with given message: terom@52: # terom@52: # hg_commit $msg terom@52: # terom@52: function hg_commit { terom@52: local msg=$1 terom@52: local user_opt=$(hg_user) terom@52: terom@52: debug "$user_opt: $msg" terom@52: hg commit $user_opt -m "$msg" terom@52: } terom@52: terom@52: