blob: 0e402535d555f510959c86280c5c55fdf243c642 (
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
|
#! /bin/bash
#
# Retrieves git repository info for directory ${1} using command ${2}
# Args
REPO_DIR='.'
if [[ ${1} ]]; then
REPO_DIR=${1}
fi
GIT_EXE='git'
if [[ ${2} ]]; then
GIT_EXE=${2}
fi
# Switch to working directory
if ! cd ${REPO_DIR} 2>/dev/null; then
echo "Invalid directory ${REPO_DIR}." 1>&2
exit 1
fi
# Check whether we have git
if ! hash ${GIT_EXE} 2>/dev/null; then
echo "Command '${GIT_EXE}' not found." 1>&2
exit 1
fi
# Check if there is a valid git repo here
HASH=$(${GIT_EXE} rev-parse HEAD)
ERR=$?
if [[ ${ERR} -ne 0 ]]; then
echo "Not a valid repository." 1>&2
exit ${ERR}
elif [[ -z ${HASH} ]]; then
echo "Not a valid repository." 1>&2
exit 1
fi
# Retrieve info
URL=$(${GIT_EXE} config remote.origin.url)
TAG=$(${GIT_EXE} describe --tags --abbrev=0)
if [[ ${TAG} ]]; then
REV=$(${GIT_EXE} rev-list ${TAG}.. --count)
else
TAG=$(${GIT_EXE} describe --tags $(${GIT_EXE} rev-list --tags --max-count=1))
if [[ ${TAG} ]]; then
REV=$(${GIT_EXE} rev-list $(${GIT_EXE} merge-base ${TAG} HEAD).. --count)
else
REV=$(${GIT_EXE} rev-list HEAD --count)
fi
fi
BRANCH=$(${GIT_EXE} symbolic-ref -q --short HEAD)
REMOTE="${URL}"
UPSTREAM=$(${GIT_EXE} config branch.${BRANCH}.remote)
if [[ ${UPSTREAM} ]]; then
REMOTE="${UPSTREAM}"
fi
DATE=$(${GIT_EXE} log -1 --format="format:%ai")
# Output
# Only write tag and rev if they exist.
echo "URL=${URL}"
echo "HASH=${HASH}"
if [[ ${TAG} ]]; then echo "TAG=${TAG}"; fi
if [[ ${REV} ]]; then echo "REV=${REV}"; fi
echo "BRANCH=${BRANCH}"
echo "REMOTE=${REMOTE}"
echo "DATE=${DATE}"
|