如何在git-archive中实现类似GNU tar的--strip-components效果?

3

总的来说,我想要的是一个直接从tar到tar的转换,其中结果的根目录仅包含原始目录子树中的特定目录。

举个例子,假设我只想要git存储库中的gitweb目录。 运行

$ git archive --prefix=git-gitweb/ master gitweb | tar tf -

会得到以下结果:

git-gitweb/
git-gitweb/gitweb/
git-gitweb/gitweb/INSTALL
git-gitweb/gitweb/Makefile
git-gitweb/gitweb/README
git-gitweb/gitweb/gitweb.perl
git-gitweb/gitweb/static/
git-gitweb/gitweb/static/git-favicon.png
git-gitweb/gitweb/static/git-logo.png
git-gitweb/gitweb/static/gitweb.css
git-gitweb/gitweb/static/gitweb.js

但我想要

git-gitweb/
git-gitweb/INSTALL
git-gitweb/Makefile
git-gitweb/...

手册提供了额外的后端特定选项,但尝试传递--strip-components会产生错误:

$ git archive --prefix=git-gitweb/ --strip-components=1 master gitweb | \
  tar tf -
error: 未知选项 `strip-components=1'
用法:git archive [options]  [...] ...

tar 后端无论如何都不是 GNU tar。

freenode 的 #gnu 频道中的某人建议 Tardy,但它不理解 git-archive 的输出:

$ git archive master > old.tar
$ tardy old.tar new.tar
tardy: old.tar: 文件类型“g”未知

是的,我可以使用 --strip-components 提取 git-archive 的输出并创建结果的新存档,但我正在尝试避免将文件系统用作临时变量。

1个回答

8
将该目录归档为当前工作目录,这样你就不会有额外的路径组件:
(cd gitweb; git archive --prefix=git-gitweb/ master .) | tar tf -

或者按照 git-archive 手册中建议的语法使用:

git archive --prefix=git-gitweb/ master:gitweb | tar tf -

太棒了,而且它还可以与git-archive的“--remote”选项一起使用! - Greg Bacon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接