2017-09-22 25 views
0

我有一些調試代碼,我想確保我不提交給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上執行此操作,因爲可能有其他實例,但我很樂意添加另一個標記。

+0

備選建議:在分段更改時使用'git add -up',以便您可以手動批准每個更改。 – cdhowie

+4

而不是NDEBUG使用其他一些PAULB_DEBUG_CODE定義你可以在makefile或項目設置中定義/ undefine並將此文件添加到你的個人gitignore中。所以即使代碼被提交 - 它也不會影響其他程序員,因爲他們沒有定義它。而所有這樣的代碼將很容易找到,因爲它將是你的專用名稱 –

+4

看起來像一個涉及'grep'的客戶端鉤子的工作,不是嗎? https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks –

回答

2

這裏是一個pre-commit鉤子我用:

它會掃描所有文件上演犯了一個特殊的詞: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 
    } 
} 

這不是萬無一失的,但它爲我工作。