lib/update.utils
author Tero Marttila <terom@paivola.fi>
Tue, 20 Mar 2012 14:35:38 +0200
changeset 58 6292cb597954
parent 57 d83a5e9be193
child 60 b65ce9123039
permissions -rw-r--r--
update: hg trust root
# 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"
}

## Execute command, prefixing its output on stdout with given indent prefix.
#
#   indent  "    " $cmd...
#
# Output is kept on stdout, exit status is that of the given command.
# Also logs the executed command at log_cmd level..
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 "${HG_ARGS[@]}" "$@"
}

# 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 {
    # just stat hidden files, but show the rest
    hg diff --stat -I "$REPO/$REPO_HIDE"
    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"
}