aboutsummaryrefslogtreecommitdiff
path: root/tools/sfsnapshotgit
blob: 8bc19fce803c1f19af4688b84def5c73951ae664 (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
#!/bin/bash
# sfsnapshotgit - Snapshot script for Git repository
# Original author: Thomas Guyot-Sionnest <tguyot@gmail.com>
#
# Given an optional branch name (master by default), this script creates
# a snapshot from the tip of the branch and move it to ~/staging/.
# The repository, origin and destination directory can be overridden
# with environment variable (see below)

# Handle command errors (-e) and coder sleep deprivation issues (-u)
set -eu
trap 'echo "An error occurred in sfsnapshotgit at line $LINENO"; exit 1' EXIT

# Send all command output to STDERR while allowing us to write to STDOUT
# using fd 3
exec 3>&1 1>&2

# Git repository, origin and destination directory can be overridden by
# setting SFSNAP_REPO, SFSNAP_ORIGIN and SFSNAP_DEST respectively from the
# caller The defaults are:
SFSNAP_REPO=${SFSNAP_REPO-~/staging/nagiosplugins}
SFSNAP_ORIGIN=${SFSNAP_ORIGIN-origin}
SFSNAP_DEST=${SFSNAP_DEST-~/staging/snapshot}

# If one argument is given, this is the branch to create the snapshot from
if [ $# -eq 0 ]
then
	HEAD='master'
elif [ $# -eq 1 ]
then
	if [ -z "$1" ]
	then
		echo "If specified, the refspec must not be empty"
		exit
	fi
	HEAD="$1"
else
	echo "Too many arguments"
	exit
fi

# Clean up and pull
cd "$SFSNAP_REPO"
# Sometimes "make dist" can modify versioned files so we must reset first
git reset --hard
git clean -qfdx

# Any branch used to create snapshots must already exist and be properly configured
git checkout "$HEAD"

# Get the remote tracking branch from config
origin=$(git config branch.$HEAD.remote)
ref=$(git config branch.$HEAD.merge |sed -e 's|^refs/heads/||')
git fetch "$origin"
git reset --hard "$origin/$ref"

# Tags are important for git-describe, but take only the ones from the hard-coded origin
git fetch --tags "$SFSNAP_ORIGIN"

# Write our snapshot version string (similar to NP-VERSION-GEN) to "release"
VS=$(git describe --abbrev=4 HEAD)
VS=${VS#release-}

# Configure and dist only if needed
if [ ! -e "$SFSNAP_DEST/nagios-plugins-$VS.tar.gz" ]
then
	tools/setup
	./configure
	make dist VERSION=$VS RELEASE=snapshot
	cp nagios-plugins-$VS.tar.gz "$SFSNAP_DEST/"
fi

# fd 3 goes to STDOUT; print the generated filename
echo "nagios-plugins-$VS.tar.gz" 1>&3

trap - EXIT