Is it safe to use the same ignores file for Git, M

2019-06-16 03:20发布

Git, Mercurial, and Bazaar all seem to have similar formats for their ignore file (.gitignore, .hgignore, .bzrignore [see also bzr patterns]).

In order to improve synchronization of the global ignore files, would it be safe to use one as an actual file and simply symlink the other two to that file? In other words, is there any danger in making my $HOME/.gitignore the canonical ignores file and doing

ln -s $HOME/.gitignore $HOME/.hgignore
ln -s $HOME/.gitignore $HOME/.bazaar/ignore

or is there some subtle difference among them that would bite me at some point?

2条回答
叼着烟拽天下
2楼-- · 2019-06-16 04:12

The syntax used in the ignore files is different from system to system:

  • Mercurial: list of regular expressions — can be changed with a syntax: glob line.

  • Bazaar: list of shell glob patterns — prefixing with RE: to match as regular expression.

  • Git: list of shell glob patterns.

Furthermore, the exact rules for how the shell patterns and regular expressions are matched differ from tool to tool. All in all, this means that you can only hope to use this trick if your global ignore file is pretty simple. Otherwise the differences can come and bite you, as you say.

I tested it with this file:

syntax: glob
.bzr
.git
.hg
*.o
*~

and it seems to work as intended across all three tools. To test it, I created this directory tree:

$ tree
.
|-- foo.c
|-- foo.c.~1~
|-- foo.h
|-- foo.o
`-- src
    |-- bar.c
    |-- bar.c.~1~
    `-- bar.o

and ran the status command for each tool:

$ for tool in hg git bzr; do echo "== $tool status =="; $tool status; done
== hg status ==
? foo.c
? foo.h
? src/bar.c
== git status ==
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       foo.c
#       foo.h
#       src/
nothing added to commit but untracked files present (use "git add" to track)
== bzr status ==
unknown:
  foo.c
  foo.h
  src/

As you can see, this simple file works fine.

Technically, Git and Bazaar will now ignore a file called syntax: glob, but unless you plan to create a file with that weird name, this doesn't matter. Finally, note that Mercurial doesn't read a $HOME/.hgignore file by default. But you can make it read it by adding

[ui]
ignore.my-default = ~/.hgignore

to your $HOME/.hgrc file.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-06-16 04:16

Syntax of at least Git and Mercurial differ (glob and regex patterns in hg), don't forget also filesets in hg, ! conditions in bzr

查看更多
登录 后发表回答