Git 检查代码是否存在的挂钩
Git hook to check presence of code
我对 git 的了解有限,无法理解 git-hooks
上的文档。我想知道是否有办法添加 git-hook
或其他一些构造,以便它可以检查某个文件中是否有某些代码 present/commented 。我的工作流程是我有一个配置文件 globalConfig.js
,其中包含两组配置 development
和 production
。当我在开发时,我取消注释 development
配置并注释掉 production
配置但是当我将任何代码提交到我的 repo 时,我想确保 production
配置未被注释并且development
配置已评论。
摘自 globalConfig.js 文件
发展
// 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 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"
//};
是否可以通过 git-hook
或其他一些 git 构造来实现它?
以下预提交挂钩应该可以帮助您入门。如果文件中包含 development
一词,那么它必须是注释,否则不允许提交。
步骤:
- 使用以下内容创建
.git/hooks/pre-commit
:
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 $?
请注意 egrep 表达式非常简单 atm,但您应该能够根据您的要求修改它(即 globalConfig.js
的确切内容)或者,您可以使用来自 globalConfig.js
:)
- 更改权限
chmod 755 .git/hooks/pre-commit
- 然后尝试提交无效的
globalConfig.js
文件。这是我的测试结果:
☻ 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
我对 git 的了解有限,无法理解 git-hooks
上的文档。我想知道是否有办法添加 git-hook
或其他一些构造,以便它可以检查某个文件中是否有某些代码 present/commented 。我的工作流程是我有一个配置文件 globalConfig.js
,其中包含两组配置 development
和 production
。当我在开发时,我取消注释 development
配置并注释掉 production
配置但是当我将任何代码提交到我的 repo 时,我想确保 production
配置未被注释并且development
配置已评论。
摘自 globalConfig.js 文件
发展
// 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 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"
//};
是否可以通过 git-hook
或其他一些 git 构造来实现它?
以下预提交挂钩应该可以帮助您入门。如果文件中包含 development
一词,那么它必须是注释,否则不允许提交。
步骤:
- 使用以下内容创建
.git/hooks/pre-commit
:
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 $?
请注意 egrep 表达式非常简单 atm,但您应该能够根据您的要求修改它(即 globalConfig.js
的确切内容)或者,您可以使用来自 globalConfig.js
:)
- 更改权限
chmod 755 .git/hooks/pre-commit
- 然后尝试提交无效的
globalConfig.js
文件。这是我的测试结果:
☻ 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