2011-05-24 92 views
1

由於編譯或打開項目文件而導致多次創建臨時文件。我希望SVN忽略(不是版本)這些文件,而不必全局忽略它們,因爲不同的程序/進程可能在別處創建它們,並且可能需要版本控制(相同的擴展名不同的文件類型)。使用Bash和SVN做這件事的最好方法是什麼?我創建的以下bash腳本是否足夠?遞歸地忽略SVN中的文件或目錄模式而不會全局忽略的最佳方式

# Recursively find, remove, and set svn:ignore on files or folders. 
# For safety, you must perform an svn status and an svn commit yourself. 
# 
# $1 is the pattern to ignore 
# $2 is the starting directory (optional) 
# $3 is a flag to remove the ignored files locally (optional) 
# 
# Example: recursive_svn_ignore "*.bak" . --remove 
# 
recursive_svn_ignore() 
{ 
    cd "$2"; 
    svn info > /dev/null 2>&1; 
    if [ "$?" -ne "0" ]; then 
     echo "Skipping `pwd` because it is not under version control."; 
     return 1; 
    fi; 
    # Check if pattern matches 
    ls -l $1 > /dev/null 2>&1; 
    if [ "$?" = "0" ]; then 
     for f in "$1"; do 
      if [ "$3" = "--remove" ]; then 
       # Remove file from working directory and repository 
       svn rm --force -q $f; 
      else 
       # Remove file from repository only 
       svn rm --force --keep-local $f; 
      fi; 
     done 
     echo "Adding "$1" in `pwd` to svn:ignore list"; 
     svn propget svn:ignore . > svnignore.tempfile; 
     echo "$1" >> svnignore.tempfile; 
     svn propset -q svn:ignore -F svnignore.tempfile .; 
     rm svnignore.tempfile; 
    fi; 
    # Recurse 
    for d in * 
    do 
     if [ -d "$d" ]; then 
      (recursive_svn_ignore "$1" "$d") 
     fi; 
    done 
} 

至於建議由@大衛麪包車,邊緣,使用find通過消除遞歸簡化了很多東西。這是我想出的。

#!/bin/bash 
# Recursively find and set svn:ignore on files or folders. 
# If the file was already version it will be removed from the repository 
# and keep the local copy (can be overridden). 
# This does not perform the final commit so you can review the changes 
# using svn status. 
# 
# $1 pattern to ignore 
# $2 remove the ignored files locally as well (optional) 
# 
# Example: find_svn_ignore "*.bak" . --remove 
# 
for a in `find . -name $1` 
do 
     svn info ${a%/*} > /dev/null 2>&1; 
     if [ "$?" -ne "0" ]; then 
       echo "Skipping ${a%/*} because it is not under version control."; 
       continue; 
     fi; 
     echo "Ignoring ${a##*/} in ${a%/*}"; 
     svn propget svn:ignore ${a%/*} > svnignore.tempfile; 
     echo "$1" >> svnignore.tempfile; 
     svn propset -q svn:ignore -F svnignore.tempfile ${a%/*}; 
     rm svnignore.tempfile; 
     if [ "$2" = "--remove" ]; then 
       # Remove file from working directory and repository 
       svn rm --force -q ${a##*/}; 
     else 
       # Remove file from repository only 
       svn rm --force --keep-local ${a##*/}; 
     fi; 
done 

回答

2

使用「查找」可能會更好。例如:

for a in `find . -name '*.bak'` 
do 
    echo something $a 
    echo something else to $a 
done 

或者將模式作爲參數傳遞給函數。 用你的工作邏輯取代我的「回聲一些東西」。它只是避免了實際的遞歸,以這種方式使用「查找」。


當然,真的您應調整流動到別的地方產生的文件,或至少在短短的一個「輸出」文件夾,整潔,只要有可能,所以你不需要忽略的不斷增長的文件列表。

但是,有些工具沒有我們想要的那麼整齊,唉。

1

也許還有別的事情可以做。

爲什麼不簡單地將禁止帶有某些後綴的文件放入您的Subversion存儲庫?

我有一個hook script這樣做。您給它一個glob模式或正則表達式,並且如果一個文件被添加到存儲庫中,並且名稱與glob模式或正則表達式匹配,則不會發生提交。開發人員會收到一條錯誤消息,並且必須在提交完成之前刪除添加的文件。

這確實幾件事情:

  • 開發商將添加忽略模式本身。
  • 他們將開始移動內置對象到其他目錄
  • 他們確保他們的清潔功能,在其構建腳本真的清理編譯產生的所有垃圾。

否則,您將不得不一直運行您的腳本一遍又一遍。在某個地方,某些開發人員會檢查一些他們本不應該出現的東西,並且這將是您的錯因爲您採取了自己防止發生這種情況。

+0

這是David W.的一個好點,我希望能夠自動提醒人們這樣的政策。我會試試這個。我仍然需要一種輕鬆設置svn:ignore的方法,David V. B.幫助簡化了我的生活。感謝你們倆。 – Lucas 2011-08-18 17:25:48