lib/update.utils
changeset 575 b68b8615c512
child 578 a2d87cfd77e4
equal deleted inserted replaced
574:c486df8ea68a 575:b68b8615c512
       
     1 # vim: set ft=sh :
       
     2 #
       
     3 # Utility functions
       
     4 
       
     5 
       
     6 ### Command execution
       
     7 ## Execute command, possibly logging its execution.
       
     8 #
       
     9 #   cmd     $cmd...
       
    10 #
       
    11 # Fails if the command returns an error exit code.
       
    12 function cmd {
       
    13     log_cmd "$@"
       
    14 
       
    15     "$@" || die "Failed"
       
    16 }
       
    17 
       
    18 function indent () {
       
    19     local indent=$1; shift
       
    20 
       
    21     log_cmd "$@"
       
    22 
       
    23     "$@" | sed "s/^/$indent/"
       
    24 
       
    25     return ${PIPESTATUS[0]}
       
    26 }
       
    27 
       
    28 
       
    29 ### FS utils
       
    30 # Create dir in $ROOT if not exists.
       
    31 function ensure_dir {
       
    32     local dir=$1
       
    33 
       
    34     if [ ! -d $ROOT/$dir ]; then
       
    35         log_warn "Creating output dir: $dir"
       
    36         cmd mkdir $ROOT/$dir
       
    37     fi
       
    38 }
       
    39 
       
    40 ## Output absolute path from $ROOT:
       
    41 #
       
    42 #   abspath $path
       
    43 #
       
    44 function abspath () {
       
    45     local path=$1
       
    46 
       
    47     echo "$ROOT/$path"
       
    48 }
       
    49 
       
    50 ### HG wrappers
       
    51 # Run `hg ...` within $REPO.
       
    52 function hg {
       
    53     local repo=$REPO
       
    54 
       
    55     cmd $HG -R $ROOT/$repo "$@"
       
    56 }
       
    57 
       
    58 # Does the repo have local modifications?
       
    59 function hg_modified {
       
    60     hg id | grep -q '+'
       
    61 }
       
    62 
       
    63 # Output possible -u flag for commit.
       
    64 function hg_user {
       
    65     if [ ${SUDO_USER:-} ]; then
       
    66         echo '-u' "$SUDO_USER"
       
    67 
       
    68     elif [ $HOME ] && [ -e $HOME/.hgrc ]; then
       
    69         debug "using .hgrc user"
       
    70         echo ''
       
    71 
       
    72     else
       
    73         echo '-u' "$USER"
       
    74     fi
       
    75 }
       
    76 
       
    77 # Show changes in repo
       
    78 function hg_diff {
       
    79     hg diff -X "$REPO/$REPO_HIDE"
       
    80 }
       
    81 
       
    82 ## Commit changes in repo, with given message:
       
    83 #
       
    84 #   hg_commit   $msg
       
    85 #
       
    86 function hg_commit {
       
    87     local msg=$1
       
    88     local user_opt=$(hg_user)
       
    89     
       
    90     debug "$user_opt: $msg"
       
    91     hg commit $user_opt -m "$msg"
       
    92 }
       
    93 
       
    94