/ 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 emc2 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="v2.9.*"
31              DEB_COMPONENT="master"
32              ;;
33          # release branches have names matching "number.number", which is awkward to express as a glob
34          [0-9].[0-9] | [0-9].[0-9][0-9] | [0-9].[0-9][0-9][0-9] | [0-9][0-9].[0-9] | [0-9][0-9].[0-9][0-9] | [0-9][0-9].[0-9][0-9][0-9])
35              GIT_TAG_GLOB="v${GIT_BRANCH}.*"
36              DEB_COMPONENT=$GIT_BRANCH
37              ;;
38          v2.5_branch)
39              GIT_TAG_GLOB="v2.5*"
40              DEB_COMPONENT="v2.5_branch"
41              ;;
42          v2.4_branch)
43              GIT_TAG_GLOB="v2.4*"
44              DEB_COMPONENT="v2.4_branch"
45              ;;
46          *)
47              GIT_TAG_GLOB="*"
48              DEB_COMPONENT="scratch"
49              ;;
50      esac
51  
52  
53      # use the gnupg keyring from our git repo to verify signatures on the release tags
54      export GNUPGHOME=$(git rev-parse --show-toplevel)/gnupg
55  
56      NEWEST_SIGNED_TAG_UTIME=-1
57      NEWEST_UNSIGNED_TAG_UTIME=-1
58      for TAG in $(git tag -l "$GIT_TAG_GLOB"); do
59          if ! git cat-file tag $TAG > /dev/null 2> /dev/null; then
60              continue
61          fi
62  
63          TAG_UTIME=$(git cat-file tag $TAG | grep tagger | awk '{print $(NF-1)-$NF*36}')
64  
65          if git tag -v "$TAG" > /dev/null 2> /dev/null; then
66              # it's a valid signed tag
67              if [ $TAG_UTIME -gt $NEWEST_SIGNED_TAG_UTIME ]; then
68                  NEWEST_SIGNED_TAG=$TAG
69                  NEWEST_SIGNED_TAG_UTIME=$TAG_UTIME
70              fi
71          else
72              # unsigned tag
73              if [ $TAG_UTIME -gt $NEWEST_UNSIGNED_TAG_UTIME ]; then
74                  NEWEST_UNSIGNED_TAG=$TAG
75                  NEWEST_UNSIGNED_TAG_UTIME=$TAG_UTIME
76              fi
77          fi
78  
79      done
80  
81      if [ $NEWEST_SIGNED_TAG_UTIME -gt -1 ]; then
82          GIT_TAG="$NEWEST_SIGNED_TAG"
83          return
84      fi
85  
86      if [ $NEWEST_UNSIGNED_TAG_UTIME -gt -1 ]; then
87          echo "no signed tags found, falling back to unsigned tags" > /dev/null 1>&2
88          GIT_TAG="$NEWEST_UNSIGNED_TAG"
89          return
90      fi
91  
92      echo "no annotated tags found, not even unsigned" > /dev/null 1>&2
93  }
94