sfsnapshotgit 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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)
  9. # Handle command errors (-e) and coder sleep deprivation issues (-u)
  10. set -eu
  11. trap 'echo "An error occurred in sfsnapshotgit at line $LINENO"; exit 1' EXIT
  12. # Send all command output to STDERR while allowing us to write to STDOUT
  13. # using fd 3
  14. exec 3>&1 1>&2
  15. # Git repository, origin and destination directory can be overridden by
  16. # setting SFSNAP_REPO, SFSNAP_ORIGIN and SFSNAP_DEST respectively from the
  17. # caller The defaults are:
  18. SFSNAP_REPO=${SFSNAP_REPO-~/staging/nagiosplugins}
  19. SFSNAP_ORIGIN=${SFSNAP_ORIGIN-origin}
  20. SFSNAP_DEST=${SFSNAP_DEST-~/staging/snapshot}
  21. # If one argument is given, this is the branch to create the snapshot from
  22. if [ $# -eq 0 ]
  23. then
  24. HEAD='master'
  25. elif [ $# -eq 1 ]
  26. then
  27. if [ -z "$1" ]
  28. then
  29. echo "If specified, the refspec must not be empty"
  30. exit
  31. fi
  32. HEAD="$1"
  33. else
  34. echo "Too many arguments"
  35. exit
  36. fi
  37. # Clean up and pull
  38. cd "$SFSNAP_REPO"
  39. # Sometimes "make dist" can modify versioned files so we must reset first
  40. git reset --hard
  41. git clean -qfdx
  42. # Any branch used to create snapshots must already exist and be properly configured
  43. git checkout "$HEAD"
  44. # Get the remote tracking branch from config
  45. origin=$(git config branch.$HEAD.remote)
  46. ref=$(git config branch.$HEAD.merge |sed -e 's|^refs/heads/||')
  47. git fetch "$origin"
  48. git reset --hard "$origin/$ref"
  49. # Tags are important for git-describe, but take only the ones from the hard-coded origin
  50. git fetch --tags "$SFSNAP_ORIGIN"
  51. # Write our snapshot version string (similar to NP-VERSION-GEN) to "release"
  52. VS=$(git describe --abbrev=4 HEAD)
  53. VS=${VS#release-}
  54. # Configure and dist only if needed
  55. if [ ! -e "$SFSNAP_DEST/nagios-plugins-$VS.tar.gz" ]
  56. then
  57. tools/setup
  58. ./configure
  59. make dist VERSION=$VS RELEASE=snapshot
  60. cp nagios-plugins-$VS.tar.gz "$SFSNAP_DEST/"
  61. fi
  62. # fd 3 goes to STDOUT; print the generated filename
  63. echo "nagios-plugins-$VS.tar.gz" 1>&3
  64. trap - EXIT