从远程 git 存储库获取单个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3642143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:33:18  来源:igfitidea点击:

Get a single file from a remote git repository

javagitdownloadjgit

提问by Oak

Is there a way to programmatically download a single file from a remote git repository, in Java?

有没有办法在 Java 中以编程方式从远程 git 存储库下载单个文件?

  1. I prefer a solution which uses as little bandwidth as possible, preferably only downloading that single file. I do not need to browse the repository, I already have the file's path.
  2. I prefer a solution which does not depend on other applications (e.g. an installation of another git client on the machine). A Java library which contains a git client implementation itself would be optimal.
  1. 我更喜欢使用尽可能少的带宽的解决方案,最好只下载该单个文件。我不需要浏览存储库,我已经有了文件的路径。
  2. 我更喜欢不依赖于其他应用程序的解决方案(例如在机器上安装另一个 git 客户端)。包含 git 客户端实现本身的 Java 库将是最佳选择。

I was able to do something similar with Subversion using SVNKitand I've seen there is a pure java implementation of git (eclipse's JGit) which might be able to do something similar, so I hope there is a positive answer; though from what I understand about how git works - allowing updates only from local repositories - this could prove to be problematic.

我能够使用SVNKit对 Subversion 做类似的事情,并且我看到有一个纯 Java 实现的 git(eclipse 的 JGit)可能能够做类似的事情,所以我希望有一个肯定的答案;尽管根据我对 git 工作原理的了解 - 仅允许从本地存储库更新 - 这可能会证明是有问题的。

回答by CB Bailey

git isn't really designed for single file access from a remote repository but you can abuse git archivefor this. The downside is that you have to download a "tree" rather than just the blob that you need.

git 并不是真正为从远程存储库访问单个文件而设计的,但您可以滥用git archive它。缺点是您必须下载“树”而不仅仅是您需要的 blob。

E.g.

例如

git archive --remote=url://to.git.repo branch path/to/dir | tar -x file

As an alternative, if you have gitweb set up on the remote repository you can use a simple curl or wget command to download any file in its "raw" format.

作为替代方案,如果您在远程存储库上设置了 gitweb,您可以使用简单的 curl 或 wget 命令以“原始”格式下载任何文件。

回答by Aristotle Pagaltzis

What sort of access to do you have to the remote repository? Is it via SSH, can you call commands? If so, you can just invoke git show HEAD:$path_to_file.

您对远程存储库有什么样的访问权限?是通过SSH,可以调用命令吗?如果是这样,您只需调用.git show HEAD:$path_to_file

If you cannot invoke commands on that machine, it is still entirely possible to do this, but you'll have to understand the Git repository format. (That's much less scary than it sounds, since it is very simple by design. Unlike eg. Subversion, the repository format is not intended as a black box.)

如果您无法在该机器上调用命令,仍然完全可以执行此操作,但您必须了解 Git 存储库格式。(这不像听起来那么可怕,因为它的设计非常简单。与例如 Subversion 不同,存储库格式并不是一个黑匣子。)

回答by msw

Quoth git-clone(1):

引用git-clone(1)

--depth depth

Create a shallow clone with a history truncated to the specified number of revisions. A shallow repository has a number of limitations (you cannot clone or fetch from it, nor push from nor into it), but is adequate if you are only interested in the recent history of a large project with a long history, and would want to send in fixes as patches.

--depth 深度

创建一个浅克隆,其历史记录被截断为指定数量的修订。浅层存储库有许多限制(你不能从它克隆或获取,也不能从它推入或推入),但如果你只对一个历史悠久的大型项目的近期历史感兴趣,并且想要将修复作为补丁发送。

So the granularity is a directory but you can limit the revision history.

所以粒度是一个目录,但你可以限制修订历史。