Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit / JGit » How to get content of files with JGIT
How to get content of files with JGIT [message #1744341] Mon, 26 September 2016 07:22 Go to next message
Yossi Balan is currently offline Yossi BalanFriend
Messages: 4
Registered: September 2016
Junior Member
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 13:02 Go to previous messageGo to next message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 241
Registered: July 2009
Senior Member
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));
                        }
                }
        }
}



Ciao
Chris
Re: How to get content of files with JGIT [message #1744406 is a reply to message #1744392] Mon, 26 September 2016 15:33 Go to previous messageGo to next message
Yossi Balan is currently offline Yossi BalanFriend
Messages: 4
Registered: September 2016
Junior Member
I found an example that they did
DirCache cache = db.readDirCache();
DirCacheEntry entry = cache.getEntry(pattern);
if (entry == null) {
return null;
}
ObjectId blobId = entry.getObjectId();

without the use of the RevWalk
Is it ok ?
what is the different ?
Re: How to get content of files with JGIT [message #1744415 is a reply to message #1744406] Mon, 26 September 2016 16:22 Go to previous messageGo to next message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 241
Registered: July 2009
Senior Member
With revwalk you can walk over directory structures and for every path you get the three states. That's convient for a lot of applications. If you are interested only in the index state for a single path then your code is more appropriate. But if you do that for the index, then another block for the commit and finally another block of code for the filesystem .... then I think the revwalk code has much less codelines. But again, if you just want to read a single path from the index then your code is ok

Ciao
Chris
Re: How to get content of files with JGIT [message #1744426 is a reply to message #1744415] Mon, 26 September 2016 19:51 Go to previous messageGo to next message
Yossi Balan is currently offline Yossi BalanFriend
Messages: 4
Registered: September 2016
Junior Member
With revwalk you can walk over directory structures and for every path you get the three state.

How I can get different state ? and how I can know in which state is the result ? did you mean working directory , staging and commit file ?
Does when you tw.next() you got all the state ?
Re: How to get content of files with JGIT [message #1744446 is a reply to message #1744426] Tue, 27 September 2016 07:14 Go to previous messageGo to next message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 241
Registered: July 2009
Senior Member
look at the example code I provided. You create one treewalk and add three AbstractTreeIterators to this walk. The first iterator for the commit, the second for the index and the third for the workingtree. When you say walk.next() you move to the next available path, e.g. "src/a.txt". Treewalk takes care to advance every iterator so that every iterator looks now at "src/a.txt". With walk.getObjectID(0) you get the objectid for "src/a.txt" for the first Iterator (that one looking at the commit). And walk.getObjectID(1) returns the objectid for "src/a.txt" for the second Iterator (that one looking at the index). The example should clarify that.

Ciao
Chris
Re: How to get content of files with JGIT [message #1744455 is a reply to message #1744446] Tue, 27 September 2016 08:04 Go to previous message
Yossi Balan is currently offline Yossi BalanFriend
Messages: 4
Registered: September 2016
Junior Member
Thanks for the help
Previous Topic:How does Eclipse/eGit connect workspace to git repo and its project metadata/source files?
Next Topic:Unauthorized 401 when trying to clone
Goto Forum:
  


Current Time: Mon Sep 25 02:38:56 GMT 2017

Powered by FUDForum. Page generated in 0.03618 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software