使用 Jgit 从远程 git 分支仅下载一个文件
Download only a single file from a remote git branch using Jgit
我正在尝试从远程 git 分支下载单个文件 (myFile.xml) 而无需克隆所有存储库,我找到了这段代码,但它确实有效,错误在 getCO 中;ponent仅在 RevCommit 中运行 commit = revWalk.parseCommit(lastCommitId);
public void getComponent() throws IOException
{
File repoDir = new File("https://**.git");
// open the repository
Repository repository = new FileRepository(repoDir);
// find the HEAD
ObjectId lastCommitId = repository.resolve("47100a898d1c76558e49d3f292b8e7a6d052fe51");
System.out.println("lastcommit to string "+lastCommitId.toString());
// now we have to get the commit
RevWalk revWalk = new RevWalk(repository);
RevCommit commit = revWalk.parseCommit(lastCommitId);
// and using commit's tree find the path
RevTree tree = commit.getTree();
TreeWalk treeWalk = new TreeWalk(repository);
treeWalk.addTree(tree);
treeWalk.setRecursive(true);
treeWalk.setFilter(PathFilter.create("abcd/myFile.xml"));
if (!treeWalk.next()) {
throw new IllegalStateException("Unable to download file.");
}
ObjectId objectId = treeWalk.getObjectId(0);
ObjectLoader loader = repository.open(objectId);
// and then one can use either
InputStream in = loader.openStream();
// or
loader.copyTo(System.out);
}
Exception in thread "main" org.eclipse.jgit.errors.MissingObjectException: Missing unknown 47100a898d1c76558e49d3f292b8e7a6d052fe51
at org.eclipse.jgit.internal.storage.file.WindowCursor.open(WindowCursor.java:168)
at org.eclipse.jgit.lib.ObjectReader.open(ObjectReader.java:236)
at org.eclipse.jgit.revwalk.RevWalk.parseAny(RevWalk.java:890)
at org.eclipse.jgit.revwalk.RevWalk.parseCommit(RevWalk.java:800)
at testGit.Authenticate.getComponent(Authenticate.java:138)
at testGit.Authenticate.main(Authenticate.java:90)
Git 协议不允许直接执行此操作,对远程存储库的访问受设计限制。 JGit 的一种选择是仅通过 InMemoryRepository 将存储库克隆到内存中,但这可能只对较小的存储库有用,否则它可能会占用大量内存。
另请参阅 jgit-cookbook
中的完整代码段
我正在尝试从远程 git 分支下载单个文件 (myFile.xml) 而无需克隆所有存储库,我找到了这段代码,但它确实有效,错误在 getCO 中;ponent仅在 RevCommit 中运行 commit = revWalk.parseCommit(lastCommitId);
public void getComponent() throws IOException
{
File repoDir = new File("https://**.git");
// open the repository
Repository repository = new FileRepository(repoDir);
// find the HEAD
ObjectId lastCommitId = repository.resolve("47100a898d1c76558e49d3f292b8e7a6d052fe51");
System.out.println("lastcommit to string "+lastCommitId.toString());
// now we have to get the commit
RevWalk revWalk = new RevWalk(repository);
RevCommit commit = revWalk.parseCommit(lastCommitId);
// and using commit's tree find the path
RevTree tree = commit.getTree();
TreeWalk treeWalk = new TreeWalk(repository);
treeWalk.addTree(tree);
treeWalk.setRecursive(true);
treeWalk.setFilter(PathFilter.create("abcd/myFile.xml"));
if (!treeWalk.next()) {
throw new IllegalStateException("Unable to download file.");
}
ObjectId objectId = treeWalk.getObjectId(0);
ObjectLoader loader = repository.open(objectId);
// and then one can use either
InputStream in = loader.openStream();
// or
loader.copyTo(System.out);
}
Exception in thread "main" org.eclipse.jgit.errors.MissingObjectException: Missing unknown 47100a898d1c76558e49d3f292b8e7a6d052fe51
at org.eclipse.jgit.internal.storage.file.WindowCursor.open(WindowCursor.java:168)
at org.eclipse.jgit.lib.ObjectReader.open(ObjectReader.java:236)
at org.eclipse.jgit.revwalk.RevWalk.parseAny(RevWalk.java:890)
at org.eclipse.jgit.revwalk.RevWalk.parseCommit(RevWalk.java:800)
at testGit.Authenticate.getComponent(Authenticate.java:138)
at testGit.Authenticate.main(Authenticate.java:90)
Git 协议不允许直接执行此操作,对远程存储库的访问受设计限制。 JGit 的一种选择是仅通过 InMemoryRepository 将存储库克隆到内存中,但这可能只对较小的存储库有用,否则它可能会占用大量内存。
另请参阅 jgit-cookbook
中的完整代码段