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

时间:2017-09-22 16:25:18

标签: c++ 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上这样做,因为可能还有其他实例,但我很乐意添加另一个标签,这是可能的。

1 个答案:

答案 0 :(得分:2)

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

它扫描所有提交的文件以获取特殊字:dontcommit;如果这个词存在于某处,则commit命令失败。

#!/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
    }
}

它并非万无一失,但它对我有用。