2010-04-29 39 views
7

爲了確保我的科學分析具有可重現性,我希望以編程方式檢查是否存在未檢入的代碼庫的任何修改,如果沒有,則打印出正在使用什麼提交。以編程方式打印git修訂並檢查未提交的更改

例如,如果存在未提交的變化,它應該輸出

Warning: uncommitted changes made. This output may not be reproducible. 

否則,產生

Current commit: d27ec73cf2f1df89cbccd41494f579e066bad6fe 

理想情況下,它應該使用 「管道」,而不是 「瓷」。

回答

1

這使用瓷器,但git diff --exit-code如果與現有文件存在差異,則退出1;如果與現有文件沒有差異,則退出0。不幸的是,它不檢查未跟蹤的文件。

This answer如何檢索Git中當前提交的散列?主張git rev-parse --verify HEAD打印當前提交。

8

Git探測你需要的比特 diff-indexrev-parse --verifyls-files --others也許rev-parse --show-cdup

以下shell程序使用這些Git管道命令,具有可調整的未跟蹤/忽略處理,並且對所有可能的錯誤情況都非常小心。

#!/bin/sh 

# warn-unclean: print a warning if the working tree and index are not clean 

# For utmost strictness, set check_untracked=yes and check_ignored=yes. 
# When both are 'yes', verify that working tree and index are identical to HEAD. 
# When only check_untracked is yes, extra ignored files are allowed. 
# When neither is yes, extra untracked files and ignored files are allowed. 

check_untracked=yes 
check_ignored=yes 

warn() { 
    echo 'Warning: '"$*" \ 
     'This output may not be reproducible.' 
} 

# Compare HEAD to index and/or working tree versions of tracked files 
git diff-index --quiet HEAD 
case $? in 
    0) 
     if test "$check_untracked" != yes; then 
      clean=yes 
     else 
      # Still need to check for untracked files 

      or_ignored='' 
      exclude=--exclude-standard 
      if test "$check_ignored" = yes; then 
       or_ignored=' or ignored' 
       exclude='' 
      fi 

      (
       # Move to top level of working tree 
       if up="$(git rev-parse --show-cdup)"; then 
        test -n "$up" && cd "$up" 
       else 
        echo 'error running "git rev-parse --show-cdup"' 
        exit 129 
       fi 

       # Check for untracked files 
       git ls-files --others $exclude --error-unmatch . >/dev/null 2>&1 
       case $? in 
        0) # some untracked/ignored file is present 
         warn 'some untracked'"$or_ignored"' file is present.' 
         exit 1 
        ;; 
        1) # no untracked files 
         exit 0 
        ;; 
        *) 
         echo 'error running "git diff-index"!' 
         exit 129 
        ;; 
       esac 

      ) 
      case $? in 
       0) clean=yes ;; 
       1) clean=no ;; 
       *) exit $? ;; 
      esac 
     fi 

     test "$clean" = yes && 
     if c="$(git rev-parse --verify HEAD)"; then 
      echo 'Current commit: '"$c" 
     else 
      echo 'error running "git rev-parse --verify"!' 
     fi 
    ;; 
    1) 
     warn 'some tracked file has an uncommitted change.' 
    ;; 
    *) 
     echo 'error running "git diff-index"!' 
     exit 129 
    ;; 
esac 

你可能會在一些exit■如果你想它的退出代碼在所有情況下有意義的撒。

如果你不喜歡所有的錯誤處理或未經跟蹤/忽略處理,然後一些短期可能就夠:

if git diff-index --quiet; then 
    printf 'Current commit: %s\n' "$(git rev-parse --verify HEAD) 
else 
    echo 'Warning: …' 
fi 

您還可以檢查未跟蹤文件(可進行調整的忽略等)以簡潔的方式,無錯誤處理:

git ls-files --others --exclude-standard --error-unmatch \ 
    "./$(git rev-parse --show-cdup)" >/dev/null 2>&1 
相關問題