Specify custom gitattributes file location for git

2019-07-23 16:05发布

问题:

I'd like to expose git archive, but have a way for users to ask for some files or globs to be excluded, so the resulting archive file is not very big.

Normally you'd write the file/glob list to .gitattributes and then run git archive. But this means only one person can ask for an archive per on-disk repository, since they have to write their ignore-export list to .gitattributes, then get the archive, before yielding to someone else. Also this means that whatever settings you had in there previously get clobbered.

Is there a way I could write to /tmp/attributes-1 or another similar temp file, and tell git archive to read that .gitattributes file instead of $repo/.gitattributes? Reading the doc string, it doesn't seem like it.

https://git-scm.com/docs/git-archive

https://git-scm.com/docs/gitattributes

回答1:

Did you miss the --worktree-attributes option of git archive?

--worktree-attributes

Look for attributes in .gitattributes files in the working tree as well (see the section called “ATTRIBUTES”).

...

Note that attributes are by default taken from the .gitattributes files in the tree that is being archived. If you want to tweak the way the output is generated after the fact (e.g. you committed without adding an appropriate export-ignore in its .gitattributes), adjust the checked out .gitattributes file as necessary and use --worktree-attributes option. Alternatively you can keep necessary attributes that should apply while archiving any tree in your $GIT_DIR/info/attributes file.

So you must checkout a temporary work-tree, set the desired gitattributes there and use the --worktree-attributes option.