lib/update.hg
author Tero Marttila <terom@paivola.fi>
Thu, 19 Dec 2013 23:22:12 +0200
branchdns-new
changeset 95 a756f317d083
parent 89 51270237a6ff
child 96 bed4765fc56f
permissions -rw-r--r--
update: commit before update, and use the hg commit timestamp as the serial
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
## Output possible -u flag for commit.
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    32
function hg_user {
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    33
    if [ ${SUDO_USER:-} ]; then
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    34
        echo '-u' "$SUDO_USER"
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    35
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    36
    elif [ $HOME ] && [ -e $HOME/.hgrc ]; then
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    37
        debug "using .hgrc user"
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    38
        echo ''
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    39
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    40
    else
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    41
        echo '-u' "$USER"
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    42
    fi
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    43
}
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    44
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    45
## Show changes in repo
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    46
#   hg_diff     [path ...]
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    47
function hg_diff {
89
51270237a6ff cleanup update, reintroduce separate hg repo
Tero Marttila <terom@paivola.fi>
parents: 82
diff changeset
    48
    local repo=$1; shift
51270237a6ff cleanup update, reintroduce separate hg repo
Tero Marttila <terom@paivola.fi>
parents: 82
diff changeset
    49
    hg $repo diff "$@"
82
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    50
}
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    51
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    52
## Commit changes in repo, with given message:
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    53
#
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    54
#   hg_commit   $msg
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    55
#
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    56
# Automatically determines possible -u to use when running with sudo.
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    57
function hg_commit {
89
51270237a6ff cleanup update, reintroduce separate hg repo
Tero Marttila <terom@paivola.fi>
parents: 82
diff changeset
    58
    local repo=$1
51270237a6ff cleanup update, reintroduce separate hg repo
Tero Marttila <terom@paivola.fi>
parents: 82
diff changeset
    59
    local msg=$2
82
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    60
    local user_opt=$(hg_user)
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    61
    local msg_opt=
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    62
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    63
    [ $msg ] && msg_opt=('-m' "$msg")
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    64
    
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    65
    debug "$user_opt: $msg"
89
51270237a6ff cleanup update, reintroduce separate hg repo
Tero Marttila <terom@paivola.fi>
parents: 82
diff changeset
    66
    hg $repo commit ${user_opt[@]} ${msg_opt[@]}
82
26a307558602 update update
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    67
}