2

I'm wondering if and how I can achieve the following in Github. I have a Git repository called Application. I have a master branch with a folder called Deployments - this folder needs to be empty at all times, with no files in it.

Developers then create a branch from master and it's typical that developers add files to this Deployments folder and commit these files to their feature branch. Once developer is done with developing the feature, he will make a pull request to merge feature to master. For now, developer will manually clean up the content of the Deployments folder before making the PR. I would like to automate this solution to ensure that when the developer makes the pull request, some workflow will get triggered which will either remove the content of the Deployments folder or will notify the developer that there are files in that folder and merge cannot happen. Is this possible to achieve?

I read about pre-commit git hooks and I created one as per bellow shell script. It works fine but only partially - if I locally checkout master and will add files to Deployments folder, when I try to commit this, the commit will be rejected - that's great, however, if I'm on the feature branch I add the files and pushed those to feature branch (what is the expected developer workflow), I can then merge feature to master and files will end up in Deployments on master what is something I want to prevent.

Here is the pre-commit git hook:

#!/bin/bash
DIR="Deployments"
BRANCH=$(git branch | grep \* | cut -d ' ' -f2)

if [ "${BRANCH}" = "master" ]; then
    if [ "$(ls $DIR)" ]; then
        echo "The directory: $DIR - is not Empty and cannot be merged to Master"
        exit 1
    else
        echo "The directory: $DIR - is Empty"
    fi
else
    echo "on another branch: $BRANCH"
fi
arhak
  • 2,488
  • 1
  • 24
  • 38
Petr Hecko
  • 460
  • 1
  • 8
  • 17
  • Your use of this deployments folder is something I've never seen before. What made you or your team choose this workflow? – Tim Biegeleisen Sep 06 '18 at 05:51
  • 1
    You are looking for something like a `pre-merge` hook, but it does not exist in git directly. Have a look at [this](https://stackoverflow.com/questions/19102714/how-would-i-write-a-pre-merge-hook-in-git) answer it gives you a workaround. – DTeuchert Sep 06 '18 at 12:10

0 Answers0