0

I have limited knowledge in git to understand the documentation on git-hooks. I wanted to know if there is a way to add a git-hook or some other construct such that it can check whether some code is present/commented out in one of the files. My workflow is that I have a configuration file globalConfig.js which contains two set of configurations development and production. When I am developing, I uncomment out the development configuration and comment out production configuration but when I commit any code to my repo, I want to make sure that the production configuration is uncommented and development configuration is commented.

Excerpt from globalConfig.js file

Development

  // Production config - Always uncomment it before commiting
  // const firebaseConfig = {
  //   apiKey: "prod",
  //   authDomain: "prod",
  //   databaseURL: "prod",
  //   storageBucket: "prod",
  //   messagingSenderId: "prod"
  // };

  // Dev config - Always comment out before commiting
  const firebaseConfig = {
    apiKey: "dev",
    authDomain: "dev",
    databaseURL: "dev",
    storageBucket: "dev",
    messagingSenderId: "dev"
  };

Production

  // Production config - Always uncomment it before commiting
  const firebaseConfig = {
   apiKey: "prod",
   authDomain: "prod",
   databaseURL: "prod",
   storageBucket: "prod",
   messagingSenderId: "prod"
  };

  // Dev config - Always comment out before commiting
  // const firebaseConfig = {
  //  apiKey: "dev",
  //  authDomain: "dev",
  //  databaseURL: "dev",
  //  storageBucket: "dev",
  //  messagingSenderId: "dev"
  //};

Is it possible to achieve it via git-hookor some other git construct?

Varun Gupta
  • 2,870
  • 6
  • 33
  • 73
  • 1
    This should be possible, but for a more accurate answer it would be good to see snippets of the `globalConfig.js` with the production and development configuration since the pre-commit hook is going to do a bunch of `grep`ing to validate the file. – Ashutosh Jindal Oct 14 '16 at 16:21

1 Answers1

1

The following pre-commit hook should get you started. If a file has the word development in it then it must be a comment otherwise the commit is not allowed.

Steps:

  1. Create .git/hooks/pre-commit with the following content:

    git diff --cached --name-status | while read file; 
    do
                if egrep "development" $file ; then
                    echo "ERROR: Disallowed development configuration in file: ${file}"
                    exit 1
                fi
    done || exit $?

Note that the egrep expression is very simple atm, but you should be able to modify it according to your requirements (i.e the exact content of globalConfig.js) OR, you can update your questions with snippets from globalConfig.js :)

  1. Change permissions

chmod 755 .git/hooks/pre-commit

  1. And then try committing an invalid globalConfig.js file. Here is what my test showed:

☻  cat globalConfig.js                                 SomeOtherFeature/abc 0d174e2 ✗
//Configuration
var Production = new String("Production Configuration");
var development = new String("development Configuration");

$ git add globalConfig.js

$ git commit -m "Commit should not be allowed"
Checking if globalConfig.js is valid
var development = new String("development Configuration");
ERROR: Disallowed development configuration in file: globalConfig.js

Ashutosh Jindal
  • 18,501
  • 4
  • 62
  • 91
  • Thanks for the detailed response and sorry for my delay in responding. I have updated the questions with the excerpts of `globalConfig.js` file that I need to check before committing. I have a couple of questions:1. Is the `pre-commit` script running against all the files that are being committed or can I run it against some specific files only? 2. I am not sure which scripting language is used to write the actual script but is that only language we can write the pre-commit hook or can I also use some other language such as Python. – Varun Gupta Oct 17 '16 at 03:21
  • 1
    1. If you change `git diff --cached --name-status` to `git diff --cached --name-status -- globalConfig.js` that is what the hook will run against. 2. You can write the hook in python. See [this](http://stackoverflow.com/questions/3311869/writing-git-hooks-in-python-bash-scripts). – Ashutosh Jindal Oct 17 '16 at 18:51
  • Thanks Ashutosh. I will give the python script a try. – Varun Gupta Oct 18 '16 at 02:38