しばらく前に、Javaに埋め込み可能な分散バージョン管理システムを探していましたが、それはgitの純粋なJava実装であるJGitで見つけたと思います。ただし、サンプルコードやチュートリアルの方法はあまりありません。
JGitを使用して特定のファイルのHEADバージョンを取得するにはどうすればよいですsvn cat
か(同じように、またはhg cat
そうするでしょう)?
これにはいくつかのrev-tree-walkingが含まれていると思い、コードサンプルを探しています。
しばらく前に、Javaに埋め込み可能な分散バージョン管理システムを探していましたが、それはgitの純粋なJava実装であるJGitで見つけたと思います。ただし、サンプルコードやチュートリアルの方法はあまりありません。
JGitを使用して特定のファイルのHEADバージョンを取得するにはどうすればよいですsvn cat
か(同じように、またはhg cat
そうするでしょう)?
これにはいくつかのrev-tree-walkingが含まれていると思い、コードサンプルを探しています。
残念ながら、Thilo の回答は最新の JGit API では機能しません。これが私が見つけた解決策です:
File repoDir = new File("test-git");
// open the repository
Repository repository = new Repository(repoDir);
// find the HEAD
ObjectId lastCommitId = repository.resolve(Constants.HEAD);
// 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(path));
if (!treeWalk.next()) {
return null;
}
ObjectId objectId = treeWalk.getObjectId(0);
ObjectLoader loader = repository.open(objectId);
// and then one can use either
InputStream in = loader.openStream()
// or
loader.copyTo(out)
もっとシンプルになればいいのに。
これは@morisilの答えのより単純なバージョンで、@directed Laughの概念のいくつかを使用し、JGit 2.2.0でテストされています:
private String fetchBlob(String revSpec, String path) throws MissingObjectException, IncorrectObjectTypeException,
IOException {
// Resolve the revision specification
final ObjectId id = this.repo.resolve(revSpec);
// Makes it simpler to release the allocated resources in one go
ObjectReader reader = this.repo.newObjectReader();
try {
// Get the commit object for that revision
RevWalk walk = new RevWalk(reader);
RevCommit commit = walk.parseCommit(id);
// Get the revision's file tree
RevTree tree = commit.getTree();
// .. and narrow it down to the single file's path
TreeWalk treewalk = TreeWalk.forPath(reader, path, tree);
if (treewalk != null) {
// use the blob id to read the file's data
byte[] data = reader.open(treewalk.getObjectId(0)).getBytes();
return new String(data, "utf-8");
} else {
return "";
}
} finally {
reader.close();
}
}
repo
他の回答で作成されたリポジトリオブジェクトです。
@Thilo と @morisil の回答に従って、JGit 1.2.0 と互換性のあるこれを取得しました。
File repoDir = new File("test-git/.git");
// open the repository
Repository repo = new Repository(repoDir);
// find the HEAD
Commit head = repo.mapCommit(Constants.HEAD);
// retrieve the tree in HEAD
Tree tree = head.getTree();
// 1.2.0 api version here
// find a file (as a TreeEntry, which contains the blob object id)
TreeWalk treewalk = TreeWalk.forPath(repo, "b/test.txt", tree);
// use the blob id to read the file's data
byte[] data = repo.open(treewalk.getObjectId(0)).getBytes();
Java バージョンはテストしていませんが、動作するはずです。から翻訳します
(.getBytes (.open repo (.getObjectId (TreeWalk/forPath repo "b/test.txt" tree) 0)))
clojure で (最初のセクションと同じセットアップに従います)、これは機能します。
私は、 JGit を使用してブロブ、コミット、およびツリーを操作するための多くのヘルパーを含むgitectiveというライブラリの作成を開始しました。これは MIT ライセンスであり、GitHub で入手できます。
HEAD コミットでファイルの内容を取得する
Repository repo = new FileRepository("/repos/project/.git");
String content = BlobUtils.getHeadContent(repo, "src/Buffer.java");
ブランチ上のファイルのコンテンツを取得する
Repository repo = new FileRepository("/repos/project/.git");
String content = BlobUtils.getContent(repo, "master", "src/Buffer.java");
2 つのファイルの差分
Repository repo = new FileRepository("/repos/project/.git");
ObjectId current = BlobUtils.getId(repo, "master", "Main.java");
ObjectId previous = BlobUtils.getId(repo, "master~1", "Main.java");
Collection<Edit> edit = BlobUtils.diff(repo, previous, current);
提供されているユーティリティのその他の例については、READMEで詳しく説明しています。
自分でそれを考え出しました。API は非常に低レベルですが、それほど悪くはありません。
File repoDir = new File("test-git/.git");
// open the repository
Repository repo = new Repository(repoDir);
// find the HEAD
Commit head = repo.mapCommit(Constants.HEAD);
// retrieve the tree in HEAD
Tree tree = head.getTree();
// find a file (as a TreeEntry, which contains the blob object id)
TreeEntry entry = tree.findBlobMember("b/test.txt");
// use the blob id to read the file's data
byte[] data = repo.openBlob(entry.getId()).getBytes();
JGit チュートリアルにはいくつかの情報があります(ただし、ドキュメントがまだ利用できない場所でEclipseに切り替えたため、これも実際には役に立たず、完全でもなく、おそらく時代遅れです)。