Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit / JGit » How to get a file from specific commit (JGit)
How to get a file from specific commit (JGit) [message #1053126] Thu, 02 May 2013 15:09 Go to next message
Tommaso De Sica is currently offline Tommaso De SicaFriend
Messages: 131
Registered: March 2012
Location: Italy
Senior Member

Goodevening everyone,

I thought that this was a common question but I could not find it on the forum:

How can I get a specific file ("MyFile.myext") from a previous commit (I have commit ObjectId) using JGit?

Thanks in advance.

[Updated on: Thu, 02 May 2013 15:30]

Report message to a moderator

Re: How to get a file from specific commit (JGit) [message #1053809 is a reply to message #1053126] Tue, 07 May 2013 16:37 Go to previous messageGo to next message
Stefan Lay is currently offline Stefan LayFriend
Messages: 342
Registered: July 2009
Senior Member
Hi,

something like this should work:

RevWalk revWalk = new RevWalk(repository);
RevCommit commit = revWalk.parseCommit(commitId);
// 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)
Re: How to get a file from specific commit (JGit) [message #1057833 is a reply to message #1053126] Thu, 09 May 2013 07:51 Go to previous messageGo to next message
Tommaso De Sica is currently offline Tommaso De SicaFriend
Messages: 131
Registered: March 2012
Location: Italy
Senior Member

Which path I have to use in filter?

[Updated on: Thu, 09 May 2013 07:52]

Report message to a moderator

Re: How to get a file from specific commit (JGit) [message #1060204 is a reply to message #1057833] Thu, 23 May 2013 13:42 Go to previous message
Stefan Lay is currently offline Stefan LayFriend
Messages: 342
Registered: July 2009
Senior Member
The path relative to the root of the git repository.
Previous Topic:how to solve conflicts
Next Topic:Connot install Egit on Eclipse
Goto Forum:
  


Current Time: Tue Mar 19 10:06:34 GMT 2024

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

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

Back to the top