lib/pvl/commit/git.sh
author Tero Marttila <tero.marttila@aalto.fi>
Thu, 26 Feb 2015 22:53:26 +0200
changeset 629 7214fe5c6fac
parent 628 b10ad946d01d
permissions -rw-r--r--
lib/pvl: fixfix
628
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     1
# Git wrappers
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     2
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     3
GIT=/usr/bin/git
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     4
GIT_ARGS=()
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     5
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     6
function git_probe {
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     7
    local repo=$1
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     8
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     9
    [ -d "$repo/.git" ]
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    10
}
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    11
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    12
## Run `git ...` within $REPO.
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    13
function git {
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    14
    local repo=$1
629
7214fe5c6fac lib/pvl: fixfix
Tero Marttila <tero.marttila@aalto.fi>
parents: 628
diff changeset
    15
    cmd $GIT -C "$repo" ${GIT_ARGS[@]:-} "${@:2}"
7214fe5c6fac lib/pvl: fixfix
Tero Marttila <tero.marttila@aalto.fi>
parents: 628
diff changeset
    16
}
7214fe5c6fac lib/pvl: fixfix
Tero Marttila <tero.marttila@aalto.fi>
parents: 628
diff changeset
    17
7214fe5c6fac lib/pvl: fixfix
Tero Marttila <tero.marttila@aalto.fi>
parents: 628
diff changeset
    18
function git_test {
7214fe5c6fac lib/pvl: fixfix
Tero Marttila <tero.marttila@aalto.fi>
parents: 628
diff changeset
    19
    local repo=$1
7214fe5c6fac lib/pvl: fixfix
Tero Marttila <tero.marttila@aalto.fi>
parents: 628
diff changeset
    20
    cmd_test $GIT -C "$repo" ${GIT_ARGS[@]:-} "${@:2}"
628
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    21
}
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    22
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    23
## Does the repo have local modifications?
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    24
function git_modified {
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    25
    local repo=$1
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    26
629
7214fe5c6fac lib/pvl: fixfix
Tero Marttila <tero.marttila@aalto.fi>
parents: 628
diff changeset
    27
    if git_test $repo diff --quiet; then
7214fe5c6fac lib/pvl: fixfix
Tero Marttila <tero.marttila@aalto.fi>
parents: 628
diff changeset
    28
        # unmodified
7214fe5c6fac lib/pvl: fixfix
Tero Marttila <tero.marttila@aalto.fi>
parents: 628
diff changeset
    29
        return 1
7214fe5c6fac lib/pvl: fixfix
Tero Marttila <tero.marttila@aalto.fi>
parents: 628
diff changeset
    30
    else
7214fe5c6fac lib/pvl: fixfix
Tero Marttila <tero.marttila@aalto.fi>
parents: 628
diff changeset
    31
        return 0
7214fe5c6fac lib/pvl: fixfix
Tero Marttila <tero.marttila@aalto.fi>
parents: 628
diff changeset
    32
    fi
628
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    33
}
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    34
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    35
## Get the date for the current commit as an unix timestamp
629
7214fe5c6fac lib/pvl: fixfix
Tero Marttila <tero.marttila@aalto.fi>
parents: 628
diff changeset
    36
function git_time {
628
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    37
    local repo=$1
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    38
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    39
    git $repo log -1 --format=format:%ct
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    40
}
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    41
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    42
## Show changes in repo
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    43
#   git_diff     [path ...]
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    44
function git_diff {
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    45
    local repo=$1
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    46
    git $repo diff --cached "$@"
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    47
    git $repo diff "$@"
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    48
}
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    49
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    50
## Commit changes in repo, with given message:
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    51
#
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    52
#   git_commit   $repo $msg
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    53
#
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    54
# Automatically determines possible -u to use when running with sudo.
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    55
function git_commit {
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    56
    local repo="$1"
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    57
    local msg="$2"
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    58
    local opts=()
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    59
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    60
    if [ ${SUDO_USER:-} ]; then
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    61
        opts+=("--author=$SUDO_USER")
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    62
    fi
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    63
    
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    64
    if [ "$msg" ]; then
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    65
        opts+=('-m' "$msg")
629
7214fe5c6fac lib/pvl: fixfix
Tero Marttila <tero.marttila@aalto.fi>
parents: 628
diff changeset
    66
    else
7214fe5c6fac lib/pvl: fixfix
Tero Marttila <tero.marttila@aalto.fi>
parents: 628
diff changeset
    67
        opts+=('--allow-empty-message')
628
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    68
    fi
629
7214fe5c6fac lib/pvl: fixfix
Tero Marttila <tero.marttila@aalto.fi>
parents: 628
diff changeset
    69
    
7214fe5c6fac lib/pvl: fixfix
Tero Marttila <tero.marttila@aalto.fi>
parents: 628
diff changeset
    70
    if [ ${#opts[@]} = 0 ]; then
7214fe5c6fac lib/pvl: fixfix
Tero Marttila <tero.marttila@aalto.fi>
parents: 628
diff changeset
    71
        git $repo commit -a
7214fe5c6fac lib/pvl: fixfix
Tero Marttila <tero.marttila@aalto.fi>
parents: 628
diff changeset
    72
    else
7214fe5c6fac lib/pvl: fixfix
Tero Marttila <tero.marttila@aalto.fi>
parents: 628
diff changeset
    73
        git $repo commit -a "${opts[@]}"
7214fe5c6fac lib/pvl: fixfix
Tero Marttila <tero.marttila@aalto.fi>
parents: 628
diff changeset
    74
    fi
628
b10ad946d01d update: refactor into modularized lib/pvl/**.sh layout
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    75
}