代码之家  ›  专栏  ›  技术社区  ›  Nick Meyer

如何删除工作副本中所有未版本/忽略的文件/文件夹?

  •  127
  • Nick Meyer  · 技术社区  · 15 年前

    如果我有一个Subversion存储库的工作副本,有没有一种方法可以用一个命令或工具删除该工作副本中所有未版本化或忽略的文件?基本上,我在寻找SVN模拟 git clean .

    可以接受命令行或GUI解决方案(对于Tortoissesvn)。

    12 回复  |  直到 15 年前
        1
  •  96
  •   oddRaven Alok Singhal    8 年前

    使用Tortoissesvn:

        2
  •  128
  •   Richard Hansen Bi Rico    12 年前
    svn status --no-ignore | grep '^[I?]' | cut -c 9- | while IFS= read -r f; do rm -rf "$f"; done
    

    它具有以下功能:

    • 忽略和未跟踪的文件都将被删除
    • 即使文件名包含空格(换行符除外),它也可以工作,但是除了使用 --xml 选项并分析结果的XML输出)
    • 即使是在 svn status 在文件名之前打印其他状态字符(不应该这样做,因为文件没有被跟踪,只是为了以防万一…)
    • 它应该在任何符合POSIX的系统上工作

    我使用一个名为 svnclean 包含以下内容:

    #!/bin/sh
    
    # make sure this script exits with a non-zero return value if the
    # current directory is not in a svn working directory
    svn info >/dev/null || exit 1
    
    svn status --no-ignore | grep '^[I?]' | cut -c 9- |
    # setting IFS to the empty string ensures that any leading or
    # trailing whitespace is not trimmed from the filename
    while IFS= read -r f; do
        # tell the user which file is being deleted.  use printf
        # instead of echo because different implementations of echo do
        # different things if the arguments begin with hyphens or
        # contain backslashes; the behavior of printf is consistent
        printf '%s\n' "Deleting ${f}..."
        # if rm -rf can't delete the file, something is wrong so bail
        rm -rf "${f}" || exit 1
    done
    
        3
  •  90
  •   gregpaton08    8 年前

    我知道这是旧的,但如果其他人偶然发现它,SVN支持的新版本(1.9或更高版本) --remove-unversioned ,例如 svn cleanup . --remove-unversioned .

    https://subversion.apache.org/docs/release-notes/1.9.html#svn-cleanup-options

        4
  •  38
  •   jkramer    15 年前

    这个一行程序可能会帮助您:

    $ svn status | grep '^?' | awk '{print $2}' | xargs rm -rf
    

    小心使用!

        5
  •  14
  •   gwilk    12 年前

    使用PowerShell修改Yanal Yves Fargialla和Gimpf的答案(但不允许StackOverflow对原始帖子进行评论):

    powershell -Command "&{(svn status --no-ignore) -match '^[\?i]' -replace '^.\s+' | rm -recurse -force}
    

    这将添加卡拉(“^”)以指定行的开头,避免匹配包含字母“i”的所有文件。还可以将-recurse和-force的标志添加到rm中,使该命令不具有交互作用,因此可以在脚本中使用。

        6
  •  7
  •   Michael Sorens    13 年前

    SVN中的许多事情都可以以不同的方式完成,这里给出的各种命令行答案就是明证。随着版本1.7的出现,还有另一种Tortoissesvn技术,事实上,它提供了比Stefan提供的答案更好的纹理分辨率,允许您分别选择未版本化的文件和被忽略的文件。只需选择 Tortoissesvn>>clean up… 打开此对话框。

    这里给出了D命令行答案。随着版本1.7的出现,还有另一种Tortoissesvn技术,事实上,它提供了比Stefan提供的答案更好的纹理分辨率,允许您分别选择未版本化的文件和被忽略的文件。只要选择 TortoiseSvn >> Clean up... 打开此对话框。

    TortoiseSVN cleanup options

        7
  •  7
  •   Yanal-Yves Fargialla    12 年前

    使用PowerShell:

    (svn status --no-ignore) -match '[?]' -replace '^.\s+' | rm
    

    从命令行:

    powershell -Command "&{(svn status --no-ignore) -match '[?]' -replace '^.\s+' | rm}"
    
        8
  •  5
  •   Thomas Lötzer    15 年前

    使用Tortoissesvn:

    1. 右键单击工作副本的根并选择Tortoissesvn->“检查修改”
    2. 选择“显示忽略的文件”
    3. 按“文本状态”列排序
    4. 滚动到“非版本化”文件,现在所有文件都分组在一起;选择所有文件并右键单击->删除
    5. 滚动到“忽略”文件,现在所有文件都分组在一起;选择所有文件并右键单击->删除

    不是一个好的干净的解决方案,而是我所知道的最快的方法(在Windows上)。

    感谢pkh提供带有忽略文件的提示。

        9
  •  5
  •   Dennis Golomazov    12 年前

    这一行程序适用于我(基于Richard Hansen的回答,令人惊讶的是它不适用于包含空格的文件):

    svn status --no-ignore | grep '^[I?]' | cut -c 9- | xargs -d"\n" -I{} rm {}
    
        10
  •  4
  •   pkh    15 年前

    这与其他答案类似,但实际上会被忽略文件(请注意res中的“i”):

     rm -rf `svn status --no-ignore | grep '^[\?I]' | sed 's/^[\?I]//'`
    
        11
  •  4
  •   ChokesMcGee    10 年前

    有人说你不能从Windows命令行执行。

    公牛。

    for /f "tokens=2 delims= " %I IN ('svn st --no-ignore ^| findstr /R "^[I?]"') DO (DEL /S /F /Q /A:H "%I" & rmdir /S /Q "%I")
    

    它是否在一行中,并且不需要单个GNU工具。:)

        12
  •  1
  •   Juriy    15 年前

    如果您使用的是Linux系统,那么您不能只使用svn命令行删除它们(但不确定GUI工具),这可能有助于:

    http://www.guyrutenberg.com/2008/01/18/delete-unversioned-files-under-svn/

    另一种(残酷的)方法是提交更改,从文件夹中删除所有内容,然后再次签出。