在存在调试代码时,如何禁止GIT提交



我有一些调试代码,我想确保我不承诺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上这样做,因为可能还有其他实例,但是我很乐意添加另一个标签是可能的。

这是我使用的预订挂钩:

它扫描为特殊单词提交的所有文件: dontcommit;如果此词存在某个地方,则提交命令失败。

#!/bin/bash                                                                     
                                                                            
# check if 'dontcommit' tag is present in files staged for commit
function dontcommit_tag () {                                                    
    git grep --cached -n -i "dontcommit" -- $(git diff --cached --name-only)                            
}                                                                               
# 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
    }
}

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

最新更新