Varun Gupta Varun Gupta - 1 month ago 8
Git Question

Git hook to check presence of code

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-hook
or some other git construct?

Answer

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