2011-05-24 70 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