summaryrefslogtreecommitdiff
path: root/lib/git.source.sh
blob: c51707bdd9393278774499a29d68cc5f23430c03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Git can not run concurrently
git::_lock () {
    local -r lock="${CONTENT_BASE_DIR}/git.lock"

    while test -d $lock; do
        sleep 0.1
    done
    while ! mkdir $lock 2>/dev/null; do
        sleep 0.1
    done
}

git::_unlock () {
    local -r lock="${CONTENT_BASE_DIR}/git.lock"
    rmdir $lock
    # log DEBUG "Removed $lock"
}

git::cleanup () {
    local -r lock="${CONTENT_BASE_DIR}/git.lock"
    if [ -d "$lock" ]; then
        git::_unlock
    fi
}

# Add a static content file to git
git::add () {
    if [[ "$USE_GIT" != yes ]]; then
        return
    fi

    local -r content_dir="$CONTENT_BASE_DIR/$1"; shift
    local file="$1"; shift
    file=${file/$content_dir/.\/}

    git::_lock
    cd "$content_dir" &>/dev/null
    git add "$file"
    cd - &>/dev/null
    git::_unlock
}

# Remove a static content file from git
git::rm () {
    if [[ "$USE_GIT" != yes ]]; then
        return
    fi

    local -r content_dir="$CONTENT_BASE_DIR/$1"; shift
    local file="$1"; shift
    file=${file/$content_dir/.\/}

    git::_lock
    cd "$content_dir" &>/dev/null
    git rm "$file"
    cd - &>/dev/null
    git::_unlock
}

# Commit all changes
git::commit () {
    if [[ "$USE_GIT" != yes ]]; then
        return
    fi

    local -r content_dir="$CONTENT_BASE_DIR/$1"; shift
    local -r message="$1"; shift

    git::_lock
    cd "$content_dir" &>/dev/null
    set +e
    git commit -a -m "$message"
    if [[ "$GIT_PUSH" == yes ]]; then
        log INFO "Invoking git pull/push in $content_dir"
        git pull
        git push
    fi
    set -e
    cd - &>/dev/null
    git::_unlock
}