/ scripts / githelper.sh
githelper.sh
 1  #
 2  # This is a bash shell fragment, intended to be sourced by scripts that
 3  # want to work with git in the Hostmot2 Firmware repo.
 4  #
 5  # Sets GIT_BRANCH to the passed in branch name; if none is passed in it
 6  # attempts to detect the current branch (this will fail if the repo is in a
 7  # detached HEAD state).
 8  #
 9  # Sets DEB_COMPONENT based on the branch.  Official release branches get
10  # their own component, all other branches go in "scratch".
11  #
12  # Sets GIT_TAG to the most recent signed tag (this will fall back to the
13  # most recent tag of any kind if no signed tag is found).
14  #
15  
16  
17  function githelper() {
18      if [ -z "$1" ]; then
19          GIT_BRANCH=$(git branch | egrep '^\*' | cut -d ' ' -f 2)
20          if [ "$GIT_BRANCH" = "(no" ]; then
21              echo "'git branch' says we're not on a branch, pass one in as an argument" > /dev/null 1>&2
22              return
23          fi
24      else
25          GIT_BRANCH="$1"
26      fi
27  
28      case $GIT_BRANCH in
29          master)
30              GIT_TAG_GLOB="v1.*"
31              DEB_COMPONENT="master"
32              ;;
33          v0)
34              GIT_TAG_GLOB="v0.*"
35              DEB_COMPONENT="v0"
36              ;;
37          *)
38              GIT_TAG_GLOB="*"
39              DEB_COMPONENT="scratch"
40              ;;
41      esac
42  
43  
44      NEWEST_SIGNED_TAG_UTIME=-1
45      NEWEST_UNSIGNED_TAG_UTIME=-1
46      for TAG in $(git tag -l "$GIT_TAG_GLOB"); do
47          if ! git cat-file tag $TAG > /dev/null 2> /dev/null; then
48              continue
49          fi
50  
51          TAG_UTIME=$(git cat-file tag $TAG | grep tagger | awk '{print $(NF-1)-$NF*36}')
52  
53          if git tag -v "$TAG" > /dev/null 2> /dev/null; then
54              # it's a valid signed tag
55              if [ $TAG_UTIME -gt $NEWEST_SIGNED_TAG_UTIME ]; then
56                  NEWEST_SIGNED_TAG=$TAG
57                  NEWEST_SIGNED_TAG_UTIME=$TAG_UTIME
58              fi
59          else
60              # unsigned tag
61              if [ $TAG_UTIME -gt $NEWEST_UNSIGNED_TAG_UTIME ]; then
62                  NEWEST_UNSIGNED_TAG=$TAG
63                  NEWEST_UNSIGNED_TAG_UTIME=$TAG_UTIME
64              fi
65          fi
66  
67      done
68  
69      if [ $NEWEST_SIGNED_TAG_UTIME -gt -1 ]; then
70          GIT_TAG="$NEWEST_SIGNED_TAG"
71          return
72      fi
73  
74      if [ $NEWEST_UNSIGNED_TAG_UTIME -gt -1 ]; then
75          echo "no signed tags found, falling back to unsigned tags" > /dev/null 1>&2
76          GIT_TAG="$NEWEST_UNSIGNED_TAG"
77          return
78      fi
79  
80      echo "no annotated tags found, not even unsigned" > /dev/null 1>&2
81  }
82