How to get content of files with JGIT [message #1744341] |
Mon, 26 September 2016 03:22  |
Eclipse User |
|
|
|
Hi
I want to get the content of file in different mode :
1. working directory
2. stage
3. commit
I saw this example in the documentation but it give me the content only in case of commit. How I can get the content of file wiht jgit in working directory and in stage area.
try (RevWalk revWalk = new RevWalk(getRepository())) {
RevCommit revCommit = revWalk.parseCommit(revision);
RevTree tree = revCommit.getTree();
try (TreeWalk treeWalk = new TreeWalk(getRepository())) {
treeWalk.addTree(tree);
treeWalk.setRecursive(true);
treeWalk.setFilter(PathFilter.create(request.getFile()));
if (!treeWalk.next()) {
//error
}
ObjectId objectId = treeWalk.getObjectId(0);
ObjectLoader loader = repository.open(objectId);
content = new String(loader.getBytes());
}
}
|
|
|
Re: How to get content of files with JGIT [message #1744392 is a reply to message #1744341] |
Mon, 26 September 2016 09:02   |
Eclipse User |
|
|
|
Try adding index and workingtree iterators to the walk. To get real content you can ask objectdb with the oid for commit and index. But for workingtree content you need to use java File io to read content from disk
public class ListWTIndexHEAD {
public static void main(String args[]) throws IOException, GitAPIException, JGitInternalException {
Git r = Git.open(new File(args[0]));
RevWalk rw = new RevWalk(r.getRepository());
try (TreeWalk tw = new TreeWalk(r.getRepository())) {
RevCommit commitToCheck = rw.parseCommit(r.getRepository().resolve("HEAD"));
tw.addTree(commitToCheck.getTree());
tw.addTree(new DirCacheIterator(r.getRepository().readDirCache()));
tw.addTree(new FileTreeIterator(r.getRepository()));
tw.setRecursive(true);
while (tw.next()) {
System.out.printf(
"path: %s, Commit(mode/oid): %s/%s, Index(mode/oid): %s/%s, Workingtree(mode/oid): %s/%s\n",
tw.getPathString(), tw.getFileMode(0), tw.getObjectId(0), tw.getFileMode(1), tw.getObjectId(1),
tw.getFileMode(2), tw.getObjectId(2));
}
}
}
}
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03662 seconds