-
Notifications
You must be signed in to change notification settings - Fork 0
/
MIKE_VERSION_GEN
executable file
·90 lines (73 loc) · 2.63 KB
/
MIKE_VERSION_GEN
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
84
85
86
87
88
89
90
#!/usr/bin/env bash
# path
MIKE_VERSION_GEN_REALPATH=$(realpath $0)
MIKE_PATH=$(dirname $MIKE_VERSION_GEN_REALPATH)
# include mike.sh
source "$MIKE_PATH/lib/mike.sh"
# Mike info
MIKE_GIT_URL="[email protected]:~/priv/git/mike.git"
MIKE_WHO=$(whoami)@$(hostname)
# default values
MIKE_VERSION_FILE="$MIKE_PATH/MIKE_VERSION_FILE"
MIKE_HEAD_DIFF_FILE="$MIKE_PATH/MIKE_HEAD_DIFF_FILE"
MIKE_TAG_DIFF_FILE="$MIKE_PATH/MIKE_TAG_DIFF_FILE"
# MIKE_VERSION_FILE
if [ ! -z "$1" ]; then
MIKE_VERSION_FILE=$1;
fi
# MIKE_COMMIT_DIFF_FILE
if [ ! -z "$2" ]; then
MIKE_HEAD_DIFF_FILE=$2
fi
# MIKE_TAG_DIFF_FILE
if [ ! -z "$3" ]; then
MIKE_TAG_DIFF_FILE=$3
fi
# if git repository
if [ -d "$MIKE_PATH/.git" ]; then
# exporting git dit
GIT_DIR="$MIKE_PATH/.git"
GIT_WORK_TREE="$MIKE_PATH"
export GIT_DIR GIT_WORK_TREE
MIKE_COMMIT=$(git rev-parse HEAD)
MIKE_COMMIT_DATE=$(git log --pretty=format:"%ci" -1)
MIKE_BRANCH=$(git symbolic-ref -q HEAD | sed -e 's#refs/heads/##')
if [ -z "$MIKE_BRANCH" ]; then
for branch in $(git branch --contains HEAD | grep -v '(no branch)' | cut -b '3-'); do
MIKE_BRANCH=$(echo -n "$MIKE_BRANCH $branch" | sed -e 's#^ ##g')
done
fi
# generating MIKE_VERSION
if [ ! -z "$(git describe --match "v[0-9]*.[0-9]*.[0-9]*" --dirty 2>/dev/null)" ]; then
MIKE_VERSION=$(git describe --match "v[0-9]*.[0-9]*.[0-9]*" --dirty --abbrev 2>/dev/null)
MIKE_LAST_TAG=$(git describe --match "v[0-9]*.[0-9]*.[0-9]*" --no-abbrev 2>/dev/null)
if [ ! -z "$(git diff --name-only $MIKE_LAST_TAG HEAD --)" ]; then
git diff --patience --no-color $MIKE_LAST_TAG HEAD -- > $MIKE_TAG_DIFF_FILE
else
rm -f $MIKE_TAG_DIFF_FILE
fi
fi
# generating MIKE_HEAD_DIFF_FILE
if [ ! -z "$(git diff-index --name-only HEAD --)" ]; then
MIKE_COMMIT="$MIKE_COMMIT-dirty";
git diff --patience --no-color HEAD -- > $MIKE_HEAD_DIFF_FILE
else
rm -f $MIKE_HEAD_DIFF_FILE
fi
# removing for safety
rm -f $MIKE_VERSION_FILE
else
if [ ! -f "$MIKE_VERSION_FILE" ]; then
die "not in a git repository and $MIKE_VERSION_FILE not here.\n" \
"You messed up something"
fi
exit 0
fi
# writing MIKE_VERSION_FILE
echo "MIKE_GIT_URL=$MIKE_GIT_URL" >> $MIKE_VERSION_FILE
echo "MIKE_VERSION=$MIKE_VERSION" >> $MIKE_VERSION_FILE
echo "MIKE_COMMIT=$MIKE_COMMIT" >> $MIKE_VERSION_FILE
echo "MIKE_COMMIT_DATE=$MIKE_COMMIT_DATE" >> $MIKE_VERSION_FILE
echo "MIKE_BRANCH=$MIKE_BRANCH" >> $MIKE_VERSION_FILE
echo 'MIKE_WHO_INSTALLED=$(shell whoami)@$(shell hostname)' >> $MIKE_VERSION_FILE
exit 0