title: How to setup Gitlab hook to validate a git push to remote
body: <p>We have a DevOps process where people checkout or update from the remote master branch, create a new local working branch from the master and begin work in the local branch.</p>
  
  <p>1) I need to put in restrictions that will keep people from pushing straight to the remote master branch directly. Instead, people need to push changes to their local branches to the same branch on the remote, after which an admin or code reviewer will merge it into the remote master.</p>
  
  <p>2) I need a hook that will ensure that there is a valid Gitlab ticket or issue number in the form, say for example #PROJECTNAME123 before allowing their push to proceed to their remote branches (prior to code review and merging into remote master). Additionally, they must not be able to push if the ticket does not exist or is not yet open.</p>
  
  <p>I have already created a Bash pre-receive hook using information from both the following websites but they get called but still allow the git push to get to the server even when I do not pass a Gitlab ticket/issue number.</p>
  
  <p><a href="https://github.com/Praqma/git-hooks/commit/2aa087fada0b0da51724f37a902362ddd78e168f" rel="nofollow noreferrer">https://github.com/Praqma/git-hooks/commit/2aa087fada0b0da51724f37a902362ddd78e168f</a></p>
  
  <p><a href="http://blog.hgomez.net/2015/03/02/Gitlab-custom-hooks-Bash-Way.html" rel="nofollow noreferrer">http://blog.hgomez.net/2015/03/02/Gitlab-custom-hooks-Bash-Way.html</a></p>
  
  <p>The following is the pre-receive script and a bash function script that it calls.</p>
  
  <p>pre-receive (No extension)</p>
  
  <pre><code>#!/usr/bin/env bash
  #
  
  source /var/opt/gitlab/git-data/repositories/Product-common/ProductCommonParent.git/custom_hooks/pre-receive-functions.sh
  
  # enforced custom commit message format
  while read old_revision new_revision refname ; do
          process_revision
  done
  
  exit 0
  </code></pre>
  
  <p>pre-receive-functions.sh</p>
  
  <pre><code>#!/usr/bin/env bash
  #
  
  
  regexp="#[0-9]\+"
  
  grep_msg()
  {
          grepped=$( echo $message | grep -i $regexp )
  }
  
  process_revision ()
  {
    #revisions=$(git rev-list $old_revision..$new_revision)
  echo "In pre-receive hook. Just before retrieving the revisions"
  if [ "$old_revision" -eq 0 ]; then
      # list everything reachable from new_revision but not any heads
     revisions=$(git rev-list $(git for-each-ref --format='%(refname)' refs/heads/* | sed 's/^/\^/') $new_revision)
  else
     revisions=$(git rev-list $old_revision..$new_revision)
  fi
  
  echo "In pre-receive hook. Just before IFS"
    IFS='\n' read -ra array &lt;&lt;&lt; "$revisions"
    for rid in "${!array[@]}"; do
          revision=${array[rid]}
      message=$(git cat-file commit $revision | sed '1,/^$/d')
          grepped=$(echo $message | grep -i "#[0-9]\+")
      grep_msg()
      if [ -z "$grepped" ] ; then
                  grepped_none=$(echo $message | grep -i "#none")
                  if [ -n "$grepped_none" ] ; then
                          echo "Warning, you are committing without a ticket reference" &gt;&amp;1
                  else
                          echo "You have not included a ticket reference" &gt;&amp;2
                          exit 1
                  fi
      fi
    done
  
  
  }
  </code></pre>
  
  <p>The following is the output when I try to push (I am pushing from a Git Bash shell on Windows 8.1 to a Fedora Core 24 having Gitlab installed):</p>
  
  <pre><code>xxx@xxxxx-HP MINGW64 ~/Documents/DevOps Re-Engineering/ProductCommonParent (ProductCommonParent002)
  $ git push
  Counting objects: 3, done.
  Delta compression using up to 8 threads.
  Compressing objects: 100% (3/3), done.
  Writing objects: 100% (3/3), 369 bytes | 0 bytes/s, done.
  Total 3 (delta 2), reused 0 (delta 0)
  remote: In pre-receive hook. Just before retrieving the revisions
  remote: In pre-receive hook. Just before IFS
  remote:
  remote: To create a merge request for ProductCommonParent002, visit:
  remote:   http://localhost/Product-common/ProductCommonParent/merge_requests/new?merge_request%5Bsource_branch%5D=ProductCommonParent002
  remote:
  To http://192.168.56.101/Product-common/ProductCommonParent.git
   * [new branch]      ProductCommonParent002 -&gt; ProductCommonParent002
  </code></pre>
  
  <p>Note: Gitlab and its dependencies, including git, are installed on the same Fedora Core 24 Linux System.</p>
  
  <p>I will appreciate speedy help in getting past this. Thank you very much in advance for your help.</p>