lib/update.utils
author Tero Marttila <terom@paivola.fi>
Tue, 20 Mar 2012 14:12:11 +0200
changeset 52 b68b8615c512
child 55 a2d87cfd77e4
permissions -rw-r--r--
update: split out code into lib/update.foo
# 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"
}