如何忽略 Subversion 中的文件? [英] How do I ignore files in Subversion?

查看:39
本文介绍了如何忽略 Subversion 中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Subversion 中忽略文件?

另外,如何找到不受版本控制的文件?

解决方案

(此答案已更新以匹配 SVN 1.8 和 1.9 的行为)

您有两个问题:

将文件标记为忽略:

忽略的文件"是指即使未版本化"该文件也不会出现在列表中:您的 SVN 客户端会假装该文件在文件系统中根本不存在.

忽略的文件由文件模式"指定.文件模式的语法和格式在 SVN 的在线文档中有解释:http://svnbook.red-bean.com/nightly/en/svn.advanced.props.special.ignore.htmlSubversion 中的文件模式".

从 1.8 版(2013 年 6 月)及更高版本开始,Subversion 支持 3 种不同的文件模式指定方式.以下是示例摘要:

1 - 运行时配置区 - global-ignores 选项:

  • 这是一个仅客户端设置,因此您的global-ignores 列表不会被其他用户共享,它适用于您结帐到的所有存储库你的电脑.
  • 此设置在您的运行时配置区域文件中定义:
    • Windows(基于文件) - C:Users{you}AppDataRoamingSubversionconfig
    • Windows(基于注册表) - HKLMHKCU 中的 SoftwareTigris.orgSubversionConfigMiscellanyglobal-ignores>.
    • Linux/Unix - ~/.subversion/config

2 - svn:ignore 属性,在目录(不是文件)上设置:

  • 这存储在 repo 中,因此其他用户将拥有相同的忽略文件.类似于 .gitignore 的工作方式.
  • svn:ignore 应用于目录并且是非递归或继承的.将排除与文件模式匹配的任何文件或父目录的直接子目录.
  • 虽然 SVN 1.8 添加了继承属性"的概念,但 svn:ignore 属性本身在非直接后代目录中被忽略:

    cd ~/myRepoRoot # 打开一个现有的仓库.回声富">"ignoreThis.txt" # 创建一个名为 "ignoreThis.txt" 的文件.svn status # 检查文件是否被忽略.>?./ignoreThis.txt>1 个未版本控制的文件 # ...当前未被忽略.svn propset svn:ignore "ignoreThis.txt" .# 将 svn:ignore 属性应用到myRepoRoot"目录.svn 状态>0 unversioned files # ...但现在文件被忽略了!cd subdirectory # 现在打开一个子目录.回声富">"ignoreThis.txt" # 创建另一个名为 "ignoreThis.txt" 的文件.svn 状态>?./subdirectory/ignoreThis.txt # ...并且不会被忽略!>1 个未版本化的文件

    (因此文件 ./subdirectory/ignoreThis 不会被忽略,即使ignoreThis.txt"应用于 . 存储库根).

  • 因此,要递归地应用忽略列表,您必须使用 svn propset svn:ignore .--递归.

    • 这将在每个子目录中创建该属性的副本.
    • 如果 值在子目录中不同,则子目录的值会完全覆盖父目录,因此没有相加"效果.
    • 因此,如果您更改根 . 上的 ,则必须使用 --recursive 更改它以覆盖它在子目录和后代目录上.
  • 我注意到命令行语法是违反直觉的.

    • 我开始假设您会通过键入类似 svn ignore pathToFileToIgnore.txt 的内容来忽略 SVN 中的文件,但这不是 SVN 的忽略功能的工作方式.

3- svn:global-ignores 属性.需要 SVN 1.8(2013 年 6 月):

  • 这类似于 svn:ignore,不同之处在于它利用了 SVN 1.8 的继承属性"功能.
  • svn:ignore 相比,文件模式会自动应用于每个后代目录(不仅仅是直接子目录).
    • 这意味着不必使用 --recursive 标志设置 svn:global-ignores,因为继承的忽略文件模式会在继承时自动应用.
  • 运行与上一个示例相同的命令集,但使用 svn:global-ignores 代替:

    cd ~/myRepoRoot # 打开一个现有的仓库回声富">"ignoreThis.txt" # 创建一个名为 "ignoreThis.txt" 的文件svn status # 检查文件是否被忽略>?./ignoreThis.txt>1 个未版本控制的文件 # ...当前未被忽略svn propset svn:global-ignores "ignoreThis.txt" .svn 状态>0 unversioned files # ...但现在文件被忽略了!cd subdirectory # 现在打开一个子目录回声富">"ignoreThis.txt" # 创建另一个名为 "ignoreThis.txt" 的文件svn 状态>0 unversioned files # 这里也忽略文件!

对于 TortoiseSVN 用户:

整个安排让我感到困惑,因为 TortoiseSVN 的术语(在他们的 Windows 资源管理器菜单系统中使用)最初误导了我 - 我不确定忽略菜单的递归添加"、添加 *"和添加"选项.我希望这篇文章解释了忽略功能如何与 SVN 属性功能相关联.也就是说,我建议使用命令行来设置忽略的文件,以便您了解它的工作原理,而不是使用 GUI,并且只有在您熟悉命令行后才使用 GUI 来操作属性.

列出被忽略的文件:

命令 svn status 将隐藏被忽略的文件(即匹配 RGA global-ignores 模式的文件,或匹配直接父目录的 svn:忽略 模式或匹配任何祖先目录的svn:global-ignores 模式.

使用 --no-ignore 选项查看列出的文件.忽略的文件的状态为 I,然后将输出通过管道传送到 grep 以仅显示以I"开头的行.

命令是:

svn status --no-ignore |grep "^I"

例如:

svn 状态>?foo # 一个未版本化的文件>M modifiedFile.txt # 修改过的版本化文件svn 状态 --no-ignore>?foo # 一个未版本化的文件>I ignoreThis.txt # 匹配 svn:ignore 模式的文件>M modifiedFile.txt # 修改过的版本文件svn 状态 --no-ignore |grep "^I">I ignoreThis.txt # 匹配 svn:ignore 模式的文件

达达!

How do I ignore files in Subversion?

Also, how do I find files which are not under version control?

解决方案

(This answer has been updated to match SVN 1.8 and 1.9's behaviour)

You have 2 questions:

Marking files as ignored:

By "ignored file" I mean the file won't appear in lists even as "unversioned": your SVN client will pretend the file doesn't exist at all in the filesystem.

Ignored files are specified by a "file pattern". The syntax and format of file patterns is explained in SVN's online documentation: http://svnbook.red-bean.com/nightly/en/svn.advanced.props.special.ignore.html "File Patterns in Subversion".

Subversion, as of version 1.8 (June 2013) and later, supports 3 different ways of specifying file patterns. Here's a summary with examples:

1 - Runtime Configuration Area - global-ignores option:

  • This is a client-side only setting, so your global-ignores list won't be shared by other users, and it applies to all repos you checkout onto your computer.
  • This setting is defined in your Runtime Configuration Area file:
    • Windows (file-based) - C:Users{you}AppDataRoamingSubversionconfig
    • Windows (registry-based) - SoftwareTigris.orgSubversionConfigMiscellanyglobal-ignores in both HKLM and HKCU.
    • Linux/Unix - ~/.subversion/config

2 - The svn:ignore property, which is set on directories (not files):

  • This is stored within the repo, so other users will have the same ignore files. Similar to how .gitignore works.
  • svn:ignore is applied to directories and is non-recursive or inherited. Any file or immediate subdirectory of the parent directory that matches the File Pattern will be excluded.
  • While SVN 1.8 adds the concept of "inherited properties", the svn:ignore property itself is ignored in non-immediate descendant directories:

    cd ~/myRepoRoot                             # Open an existing repo.
    echo "foo" > "ignoreThis.txt"                # Create a file called "ignoreThis.txt".
    
    svn status                                  # Check to see if the file is ignored or not.
    > ?    ./ignoreThis.txt
    > 1 unversioned file                        # ...it is NOT currently ignored.
    
    svn propset svn:ignore "ignoreThis.txt" .   # Apply the svn:ignore property to the "myRepoRoot" directory.
    svn status
    > 0 unversioned files                       # ...but now the file is ignored!
    
    cd subdirectory                             # now open a subdirectory.
    echo "foo" > "ignoreThis.txt"                # create another file named "ignoreThis.txt".
    
    svn status
    > ?    ./subdirectory/ignoreThis.txt        # ...and is is NOT ignored!
    > 1 unversioned file
    

    (So the file ./subdirectory/ignoreThis is not ignored, even though "ignoreThis.txt" is applied on the . repo root).

  • Therefore, to apply an ignore list recursively you must use svn propset svn:ignore <filePattern> . --recursive.

    • This will create a copy of the property on every subdirectory.
    • If the <filePattern> value is different in a child directory then the child's value completely overrides the parents, so there is no "additive" effect.
    • So if you change the <filePattern> on the root ., then you must change it with --recursive to overwrite it on the child and descendant directories.
  • I note that the command-line syntax is counter-intuitive.

    • I started-off assuming that you would ignore a file in SVN by typing something like svn ignore pathToFileToIgnore.txt however this is not how SVN's ignore feature works.

3- The svn:global-ignores property. Requires SVN 1.8 (June 2013):

  • This is similar to svn:ignore, except it makes use of SVN 1.8's "inherited properties" feature.
  • Compare to svn:ignore, the file pattern is automatically applied in every descendant directory (not just immediate children).
    • This means that is unnecessary to set svn:global-ignores with the --recursive flag, as inherited ignore file patterns are automatically applied as they're inherited.
  • Running the same set of commands as in the previous example, but using svn:global-ignores instead:

    cd ~/myRepoRoot                                    # Open an existing repo
    echo "foo" > "ignoreThis.txt"                       # Create a file called "ignoreThis.txt"
    svn status                                         # Check to see if the file is ignored or not
    > ?    ./ignoreThis.txt
    > 1 unversioned file                               # ...it is NOT currently ignored
    
    svn propset svn:global-ignores "ignoreThis.txt" .
    svn status
    > 0 unversioned files                              # ...but now the file is ignored!
    
    cd subdirectory                                    # now open a subdirectory
    echo "foo" > "ignoreThis.txt"                       # create another file named "ignoreThis.txt"
    svn status
    > 0 unversioned files                              # the file is ignored here too!
    

For TortoiseSVN users:

This whole arrangement was confusing for me, because TortoiseSVN's terminology (as used in their Windows Explorer menu system) was initially misleading to me - I was unsure what the significance of the Ignore menu's "Add recursively", "Add *" and "Add " options. I hope this post explains how the Ignore feature ties-in to the SVN Properties feature. That said, I suggest using the command-line to set ignored files so you get a feel for how it works instead of using the GUI, and only using the GUI to manipulate properties after you're comfortable with the command-line.

Listing files that are ignored:

The command svn status will hide ignored files (that is, files that match an RGA global-ignores pattern, or match an immediate parent directory's svn:ignore pattern or match any ancesor directory's svn:global-ignores pattern.

Use the --no-ignore option to see those files listed. Ignored files have a status of I, then pipe the output to grep to only show lines starting with "I".

The command is:

svn status --no-ignore | grep "^I"

For example:

svn status
> ? foo                             # An unversioned file
> M modifiedFile.txt                # A versioned file that has been modified

svn status --no-ignore
> ? foo                             # An unversioned file
> I ignoreThis.txt                  # A file matching an svn:ignore pattern
> M modifiedFile.txt                # A versioned file that has been modified

svn status --no-ignore | grep "^I"
> I ignoreThis.txt                  # A file matching an svn:ignore pattern

ta-da!

这篇关于如何忽略 Subversion 中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆