gpt4 book ai didi

c++ - 存在调试代码时如何禁止 git commit

转载 作者:行者123 更新时间:2023-11-30 02:23:18 25 4
gpt4 key购买 nike

我有一些调试代码,我想确保我不提交给 Git。

类似于:

void myImportantFunction () {
while (true) {
//MyCode
#ifndef NDEBUG
//TODO remove before commit
std::this_thread::sleep_for (std::chrono::seconds(1));
#endif
}
}

ifndef NDEBUG 会保护我免受这种意外进入生产环境的影响,但我仍然会因为让调试版本运行得非常慢而让我的同事不高兴。

有没有一种方法可以设置 GIT 在提交时不接受此代码。我不想在 TODO 上这样做,因为可能有它的其他实例,但我很乐意添加另一个标签,这是可能的。

最佳答案

这是我使用的预提交钩子(Hook):

它扫描所有准备提交的文件以查找特殊词:dontcommit;如果这个词出现在某处,提交命令将失败。

#!/bin/bash                                                                     

# check if 'dontcommit' tag is present in files staged for commit
function dontcommit_tag () {
git grep --cached -n -i "dontcommit"
}

# if 'dontcommit' tag is present, exit with error code
if dontcommit_tag
then
echo "*** Found 'DONTCOMMIT' flag in staged files, commit refused"
exit 1
fi

每当我添加一个调试代码块时,我打算在提交之前删除它,我输入一个额外的 //dontcommit 注释:

void myImportantFunction () {
while (true) {
//MyCode
#ifndef NDEBUG
// dontcommit
std::this_thread::sleep_for (std::chrono::seconds(1));
#endif
}
}

这不是万无一失的,但对我有用。

关于c++ - 存在调试代码时如何禁止 git commit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46369244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com