Skip to content

Commit d64530e

Browse files
committed
Fix checkouts on Travis
The way Travis performs git checkouts puts the working directory in detached HEAD state. This breaks the logic for deriving version numbers from branch names in the build script. This commit updates the Travis build to replace the detached head checkout with a branch checkout if necessary.
1 parent 6801087 commit d64530e

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ language: java
44
jdk:
55
- oraclejdk8
66

7+
install:
8+
- ./travis/switch-to-branch.sh
9+
710
after_success:
811
- ./travis/publish.sh
912

travis/switch-to-branch.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env bash
2+
3+
#
4+
# When building branches Travis does not check out the branch but a specific
5+
# commit. This results in the working copy being in "detached HEAD" state. The
6+
# breaks the logic for deriving version numbers from branch names in the build
7+
# script.
8+
#
9+
# This script checks if the current build corresponds to the tip of a branch.
10+
# If it is then the branch is checked out.
11+
#
12+
13+
function main {
14+
require_not_building_pull_request
15+
require_not_triggered_by_tag
16+
require_on_tip_of_branch
17+
checkout_branch
18+
}
19+
20+
function require_not_building_pull_request {
21+
if [ -v TRAVIS_PULL_REQUEST -a "$TRAVIS_PULL_REQUEST" != "false" ]; then
22+
echo "Building pull request. Will not replace detached head with branch"
23+
exit 0
24+
fi
25+
}
26+
27+
#
28+
# In builds triggered by a tag Travis sets the variable TRAVIS_BRANCH to the
29+
# tag name instead of the branch name. It is not possible (and not necessary)
30+
# to check out the branch in this case.
31+
#
32+
function require_not_triggered_by_tag {
33+
if [ -v TRAVIS_TAG -a "$TRAVIS_TAG" != "" ] ; then
34+
echo "Build was triggered by a tag. Will not replace detached head with branch"
35+
exit 0
36+
fi
37+
}
38+
39+
function require_on_tip_of_branch {
40+
if [ $TRAVIS_COMMIT != $( git rev-parse --verify $TRAVIS_BRANCH ) ] ; then
41+
echo "Detached head does not match tip of current branch. Staying on detached head."
42+
exit 0
43+
fi
44+
}
45+
46+
function checkout_branch {
47+
echo "Detached head matches tip of current branch. Replacing detached head with branch"
48+
git checkout $TRAVIS_BRANCH
49+
}
50+
51+
main
52+

0 commit comments

Comments
 (0)