lib/update.hg
author Tero Marttila <terom@paivola.fi>
Thu, 19 Dec 2013 23:52:17 +0200
branchdns-new
changeset 96 bed4765fc56f
parent 95 a756f317d083
permissions -rw-r--r--
fix hg commit message argument passing
82
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     1
#!/bin/bash
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     2
#
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     3
# HG wrappers
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     4
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     5
## Run `hg ...` within $REPO.
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     6
function hg {
89
51270237a6ff cleanup update, reintroduce separate hg repo
Tero Marttila <terom@paivola.fi>
parents: 82
diff changeset
     7
    local repo=$1; shift
51270237a6ff cleanup update, reintroduce separate hg repo
Tero Marttila <terom@paivola.fi>
parents: 82
diff changeset
     8
    cmd $HG -R "$repo" "${HG_ARGS[@]}" "$@"
82
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     9
}
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    10
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    11
## Does the repo have local modifications?
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    12
function hg_modified {
95
a756f317d083 update: commit before update, and use the hg commit timestamp as the serial
Tero Marttila <terom@paivola.fi>
parents: 89
diff changeset
    13
    hg $1 id -i | grep -q '+'
a756f317d083 update: commit before update, and use the hg commit timestamp as the serial
Tero Marttila <terom@paivola.fi>
parents: 89
diff changeset
    14
}
a756f317d083 update: commit before update, and use the hg commit timestamp as the serial
Tero Marttila <terom@paivola.fi>
parents: 89
diff changeset
    15
a756f317d083 update: commit before update, and use the hg commit timestamp as the serial
Tero Marttila <terom@paivola.fi>
parents: 89
diff changeset
    16
## Get the date for the current commit as an unix timestamp
a756f317d083 update: commit before update, and use the hg commit timestamp as the serial
Tero Marttila <terom@paivola.fi>
parents: 89
diff changeset
    17
function hg_time {
a756f317d083 update: commit before update, and use the hg commit timestamp as the serial
Tero Marttila <terom@paivola.fi>
parents: 89
diff changeset
    18
    local repo=$1
a756f317d083 update: commit before update, and use the hg commit timestamp as the serial
Tero Marttila <terom@paivola.fi>
parents: 89
diff changeset
    19
    local hg_unix=
a756f317d083 update: commit before update, and use the hg commit timestamp as the serial
Tero Marttila <terom@paivola.fi>
parents: 89
diff changeset
    20
    local hg_tz=
a756f317d083 update: commit before update, and use the hg commit timestamp as the serial
Tero Marttila <terom@paivola.fi>
parents: 89
diff changeset
    21
a756f317d083 update: commit before update, and use the hg commit timestamp as the serial
Tero Marttila <terom@paivola.fi>
parents: 89
diff changeset
    22
    local hg_date=$(hg $repo log -r . --template '{date|hgdate}')
a756f317d083 update: commit before update, and use the hg commit timestamp as the serial
Tero Marttila <terom@paivola.fi>
parents: 89
diff changeset
    23
    local hg_unix=${hg_date% *}
a756f317d083 update: commit before update, and use the hg commit timestamp as the serial
Tero Marttila <terom@paivola.fi>
parents: 89
diff changeset
    24
    local hg_tz=${hg_date#* }
a756f317d083 update: commit before update, and use the hg commit timestamp as the serial
Tero Marttila <terom@paivola.fi>
parents: 89
diff changeset
    25
a756f317d083 update: commit before update, and use the hg commit timestamp as the serial
Tero Marttila <terom@paivola.fi>
parents: 89
diff changeset
    26
    [ -n "$hg_unix" ] || fail "failed to read hg time"
a756f317d083 update: commit before update, and use the hg commit timestamp as the serial
Tero Marttila <terom@paivola.fi>
parents: 89
diff changeset
    27
a756f317d083 update: commit before update, and use the hg commit timestamp as the serial
Tero Marttila <terom@paivola.fi>
parents: 89
diff changeset
    28
    echo "$hg_unix"
82
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    29
}
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    30
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    31
## Show changes in repo
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    32
#   hg_diff     [path ...]
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    33
function hg_diff {
89
51270237a6ff cleanup update, reintroduce separate hg repo
Tero Marttila <terom@paivola.fi>
parents: 82
diff changeset
    34
    local repo=$1; shift
51270237a6ff cleanup update, reintroduce separate hg repo
Tero Marttila <terom@paivola.fi>
parents: 82
diff changeset
    35
    hg $repo diff "$@"
82
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    36
}
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    37
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    38
## Commit changes in repo, with given message:
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    39
#
96
bed4765fc56f fix hg commit message argument passing
Tero Marttila <terom@paivola.fi>
parents: 95
diff changeset
    40
#   hg_commit   .../etc $msg
82
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    41
#
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    42
# Automatically determines possible -u to use when running with sudo.
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    43
function hg_commit {
96
bed4765fc56f fix hg commit message argument passing
Tero Marttila <terom@paivola.fi>
parents: 95
diff changeset
    44
    local repo="$1"
bed4765fc56f fix hg commit message argument passing
Tero Marttila <terom@paivola.fi>
parents: 95
diff changeset
    45
    local msg="$2"
bed4765fc56f fix hg commit message argument passing
Tero Marttila <terom@paivola.fi>
parents: 95
diff changeset
    46
    local user_opt=
82
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    47
    local msg_opt=
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    48
96
bed4765fc56f fix hg commit message argument passing
Tero Marttila <terom@paivola.fi>
parents: 95
diff changeset
    49
    if [ ${SUDO_USER:-} ]; then
bed4765fc56f fix hg commit message argument passing
Tero Marttila <terom@paivola.fi>
parents: 95
diff changeset
    50
        user_opt=('-u' "$SUDO_USER")
bed4765fc56f fix hg commit message argument passing
Tero Marttila <terom@paivola.fi>
parents: 95
diff changeset
    51
bed4765fc56f fix hg commit message argument passing
Tero Marttila <terom@paivola.fi>
parents: 95
diff changeset
    52
    elif [ $HOME ] && [ -e $HOME/.hgrc ]; then
bed4765fc56f fix hg commit message argument passing
Tero Marttila <terom@paivola.fi>
parents: 95
diff changeset
    53
        debug "using .hgrc user"
bed4765fc56f fix hg commit message argument passing
Tero Marttila <terom@paivola.fi>
parents: 95
diff changeset
    54
        user_opt=( )
bed4765fc56f fix hg commit message argument passing
Tero Marttila <terom@paivola.fi>
parents: 95
diff changeset
    55
bed4765fc56f fix hg commit message argument passing
Tero Marttila <terom@paivola.fi>
parents: 95
diff changeset
    56
    else
bed4765fc56f fix hg commit message argument passing
Tero Marttila <terom@paivola.fi>
parents: 95
diff changeset
    57
        user_opt=('-u' "$USER")
bed4765fc56f fix hg commit message argument passing
Tero Marttila <terom@paivola.fi>
parents: 95
diff changeset
    58
    fi
82
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    59
    
96
bed4765fc56f fix hg commit message argument passing
Tero Marttila <terom@paivola.fi>
parents: 95
diff changeset
    60
    if [ "$msg" ]; then
bed4765fc56f fix hg commit message argument passing
Tero Marttila <terom@paivola.fi>
parents: 95
diff changeset
    61
        msg_opt=('-m' "$msg")
bed4765fc56f fix hg commit message argument passing
Tero Marttila <terom@paivola.fi>
parents: 95
diff changeset
    62
    fi
bed4765fc56f fix hg commit message argument passing
Tero Marttila <terom@paivola.fi>
parents: 95
diff changeset
    63
   
bed4765fc56f fix hg commit message argument passing
Tero Marttila <terom@paivola.fi>
parents: 95
diff changeset
    64
    # XXX:  there's something about bash arrays that I don't like... empty arrays behave badly
bed4765fc56f fix hg commit message argument passing
Tero Marttila <terom@paivola.fi>
parents: 95
diff changeset
    65
    #       mercurial does not like it if you pass it '' as an argument
bed4765fc56f fix hg commit message argument passing
Tero Marttila <terom@paivola.fi>
parents: 95
diff changeset
    66
    if [ -n "${user_opt:-}" -a -n "${msg_opt:-}" ]; then
bed4765fc56f fix hg commit message argument passing
Tero Marttila <terom@paivola.fi>
parents: 95
diff changeset
    67
        hg $repo commit "${user_opt[@]}" "${msg_opt[@]}"
bed4765fc56f fix hg commit message argument passing
Tero Marttila <terom@paivola.fi>
parents: 95
diff changeset
    68
    elif [ -n "${user_opt:-}" ]; then
bed4765fc56f fix hg commit message argument passing
Tero Marttila <terom@paivola.fi>
parents: 95
diff changeset
    69
        hg $repo commit "${user_opt[@]}"
bed4765fc56f fix hg commit message argument passing
Tero Marttila <terom@paivola.fi>
parents: 95
diff changeset
    70
    elif [ -n "${msg_opt:-}" ]; then
bed4765fc56f fix hg commit message argument passing
Tero Marttila <terom@paivola.fi>
parents: 95
diff changeset
    71
        hg $repo commit "${msg_opt[@]}"
bed4765fc56f fix hg commit message argument passing
Tero Marttila <terom@paivola.fi>
parents: 95
diff changeset
    72
    else
bed4765fc56f fix hg commit message argument passing
Tero Marttila <terom@paivola.fi>
parents: 95
diff changeset
    73
        hg $repo commit
bed4765fc56f fix hg commit message argument passing
Tero Marttila <terom@paivola.fi>
parents: 95
diff changeset
    74
    fi
82
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    75
}