lib/pvl/commit/hg.sh
changeset 627 a81206440be2
parent 619 bed4765fc56f
child 629 7214fe5c6fac
equal deleted inserted replaced
626:5cd99761fe4d 627:a81206440be2
       
     1 # HG wrappers
       
     2 
       
     3 HG=/usr/bin/hg
       
     4 HG_ARGS=(--config trusted.users=root)
       
     5 
       
     6 function hg_probe {
       
     7     local repo=$1
       
     8 
       
     9     [ -d "$repo/.hg" ]
       
    10 }
       
    11 
       
    12 ## Run `hg ...` within $REPO.
       
    13 function hg {
       
    14     local repo=$1
       
    15     cmd $HG -R "$repo" "${HG_ARGS[@]:-}" "${@:2}"
       
    16 }
       
    17 
       
    18 ## Does the repo have local modifications?
       
    19 function hg_modified {
       
    20     hg $1 id -i | grep -q '+'
       
    21 }
       
    22 
       
    23 ## Get the date for the current commit as an unix timestamp
       
    24 function hg_time {
       
    25     local repo=$1
       
    26     local hg_unix=
       
    27     local hg_tz=
       
    28 
       
    29     local hg_date=$(hg $repo log -r . --template '{date|hgdate}')
       
    30     local hg_unix=${hg_date% *}
       
    31     local hg_tz=${hg_date#* }
       
    32 
       
    33     [ -n "$hg_unix" ] || fail "failed to read hg time"
       
    34 
       
    35     echo "$hg_unix"
       
    36 }
       
    37 
       
    38 ## Show changes in repo
       
    39 #   hg_diff     [path ...]
       
    40 function hg_diff {
       
    41     local repo=$1
       
    42     hg $repo diff "${@:2}"
       
    43 }
       
    44 
       
    45 ## Commit changes in repo, with given message:
       
    46 #
       
    47 #   hg_commit   .../etc $msg
       
    48 #
       
    49 # Automatically determines possible -u to use when running with sudo.
       
    50 function hg_commit {
       
    51     local repo="$1"
       
    52     local msg="$2"
       
    53     local opts=()
       
    54 
       
    55     if [ ${SUDO_USER:-} ]; then
       
    56         opts+=('-u' "$SUDO_USER")
       
    57 
       
    58     elif [ $HOME ] && [ -e $HOME/.hgrc ]; then
       
    59         debug "using .hgrc user"
       
    60 
       
    61     else
       
    62         opts+=('-u' "$USER")
       
    63     fi
       
    64     
       
    65     if [ "$msg" ]; then
       
    66         opts+=('-m' "$msg")
       
    67     fi
       
    68    
       
    69     hg $repo commit "${opts[@]:-}"
       
    70 }