Code

Fix fetching of remote branches
[nagiosplug.git] / tools / sfsnapshotgit
1 #!/bin/bash
2 # sfsnapshotgit - Snapshot script for Git repository
3 # Original author: Thomas Guyot-Sionnest <tguyot@gmail.com>
4 #
5 # Given an optional branch name (master by default), this script creates
6 # a snapshot from the tip of the branch and move it to ~/staging/.
7 # The repository, origin and destination directory can be overridden
8 # with environment variable (see below)
10 # Handle command errors (-e) and coder sleep deprivation issues (-u)
11 set -eu
12 trap 'echo "An error occurred in sfsnapshotgit at line $LINENO"; exit 1' EXIT
14 # Send all command output to STDERR while allowing us to write to STDOUT
15 # using fd 3
16 exec 3>&1 1>&2
18 # Git repository, origin and destination directory can be overridden by
19 # setting SFSNAP_REPO, SFSNAP_ORIGIN and SFSNAP_DEST respectively from the
20 # caller The defaults are:
21 SFSNAP_REPO=${SFSNAP_REPO-~/staging/nagiosplugins}
22 SFSNAP_ORIGIN=${SFSNAP_ORIGIN-origin}
23 SFSNAP_DEST=${SFSNAP_DEST-~/staging/snapshot}
25 # If one argument is given, this is the branch to create the snapshot from
26 if [ $# -eq 0 ]
27 then
28         HEAD='master'
29 elif [ $# -eq 1 ]
30 then
31         if [ -z "$1" ]
32         then
33                 echo "If specified, the refspec must not be empty"
34                 exit
35         fi
36         HEAD="$1"
37 else
38         echo "Too many arguments"
39         exit
40 fi
42 # Clean up and pull
43 cd "$SFSNAP_REPO"
44 # Sometimes "make dist" can modify versioned files so we must reset first
45 git reset --hard
46 git clean -qfdx
47 # Any branch used to create snapshots must already exist
48 git checkout "$HEAD"
49 git fetch "$SFSNAP_ORIGIN"
50 git reset --hard "$SFSNAP_ORIGIN"/"$HEAD"
51 # Tags are important for git-describe
52 git fetch --tags "$SFSNAP_ORIGIN"
54 # Write our snapshot version string (similar to NP-VERSION-GEN) to "release"
55 VS=$(git describe --abbrev=4 HEAD)
56 VS=${VS#release-}
58 # Configure and dist only if needed
59 if [ ! -e "$SFSNAP_DEST/nagios-plugins-$VS.tar.gz" ]
60 then
61         tools/setup
62         ./configure
63         make dist VERSION=$VS RELEASE=snapshot
64         cp nagios-plugins-$VS.tar.gz "$SFSNAP_DEST/"
65 fi
67 # fd 3 goes to STDOUT; print the generated filename
68 echo "nagios-plugins-$VS.tar.gz" 1>&3
70 trap - EXIT