File: getversion

package info (click to toggle)
munin 2.0.49-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 7,160 kB
  • sloc: perl: 11,456; java: 1,924; sh: 1,806; makefile: 841; python: 267
file content (83 lines) | stat: -rwxr-xr-x 2,803 bytes parent folder | download | duplicates (4)
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
82
83
#!/bin/sh

# Generate a version string for use when building.
#
# * If RELEASE exists, and is non-empty, use the contents of that file.
#   This is in case we're building from a tarball.
#
# * If we're inside a git tree:
#   - For a current tagged commit, return this tag.
#   - For the "master/devel/stable-*" branches, use "git describe".
#   - For other branches or detached HEADs, return a
#     "$VERSION-$NBCOMMITS-$BRANCH-$DATE-g$COMMIT" version string
#     (with "BRANCH=detached" for a detached HEAD)
#
# * Try to make it up from the directory name (munin-2.0.1 -> 2.0.1)
#
# * If we're still looking for a version, just fallback to "unknown".
#
# NOTE: please keep this file as portable as possible (more strict than for plugins).
# Probably unexpected portability issues include:
# * '$(...)' is not supported by /bin/sh on Solaris
#
# Override shellcheck warnings due to portability constraints
# shellcheck disable=SC2006


get_current_git_branch_name() {
    # hide stderr (for detached HEADs)
    GB=`LANG='' git branch --points-at HEAD 2>/dev/null | awk '$1 == "*" {print $2}'`
    if echo "$GB" | grep -q "^("; then
        # we are not part of a specific branch (the reason is given in braces)
        echo
    else
        echo "$GB"
    fi
}


generate_version_string() {
    # try the obvious: is our current commit tagged?
    head_tag=`git tag --points-at HEAD`
    if [ -n "$head_tag" ]; then
        echo "$head_tag"
    else
        branch_name=`get_current_git_branch_name`
        case "$branch_name" in
            master|devel|stable-*)
                # public branches or detached HEADs
                git describe
                ;;
            *)
                if [ -n "$branch_name" ]; then
                    clean_branch_name=`echo "$branch_name" | sed -e 's/[^0-9A-Za-z\.\-\_]/_/g'`
                else
                    clean_branch_name="detached"
                fi
                # "foo | read VAR" does *not* work (variable is changed only in subshell)
                # workaround stolen from http://www.etalabs.net/sh_tricks.html
                read -r VERSION COMMITS <<EOF
`git describe --long | perl -lne 'print "$1 $2" if m/(.*)-(\d+)-g\w+/'`
EOF
                # As git describe, we also use the "-g" magic string to denote a git hash
                git log -n 1 --pretty="${VERSION}-${clean_branch_name}-%ad-c${COMMITS}-g%h" --date=short
                ;;
        esac
    fi
}


generate_version_string_from_dir() {
    basename "`pwd`" | grep -e '^munin-' | cut -c 7-
}


if [ -s "RELEASE" ]; then
    cat RELEASE
elif [ "`git rev-parse --is-inside-work-tree 2>/dev/null`" = "true" ]; then
    generate_version_string
elif [ -n "`generate_version_string_from_dir`" ]; then
    generate_version_string_from_dir
else
    echo "unknown"
fi