|
|
|
|
Re: Jgit checkout/commit example. [message #541353 is a reply to message #514720] |
Sun, 20 June 2010 12:04 |
Anthyon Messages: 2 Registered: June 2010 |
Junior Member |
|
|
Thanks a lot for the code... I needed some version control system based on java for a cms project. I have modified this source a little bit, but I'm not sure, if I got it the right way. I needed the whole path to be created when checking out so the part:
File outFile = new File(toDir + "/" + fileTreeEntry.getName());
became:
File outFile = new File(toDir + "/" + file);
outFile.getParentFile().mkdirs();
Also I needed to the ability to check out a specific revision (not only the last one), so I made up the following:
public void checkout(final String file, final String toDir, final Integer commitTime) throws IOException {
Repository repo = new Repository(this.frepo);
RevWalk rw = new RevWalk(repo);
ObjectId objHead = repo.resolve("HEAD");
rw.markStart(rw.parseCommit(objHead));
rw.setTreeFilter(AndTreeFilter.create(PathFilter.create(file), TreeFilter.ANY_DIFF));
for (RevCommit c : rw) {
if (c.getCommitTime() == commitTime) {
Tree tree = repo.mapTree(c.getTree());
TreeEntry fileTreeEntry = this.getFile(file.split("/"), 0, tree, repo);
ObjectLoader loader = repo.openBlob(fileTreeEntry.getId());
// Write content
byte[] bytes = loader.getBytes();
File outFile = new File(toDir + "/" + file);
outFile.getParentFile().mkdirs();
FileChannel channel = new FileOutputStream(outFile).getChannel();
ByteBuffer buffer = ByteBuffer.wrap(bytes);
channel.write(buffer);
channel.close();
}
}
}
Is there a better way to do it? And another question, eclipse marks GitIndex as deprecated. What else should be used instead?
Thanks in advance!
Anthyon
|
|
|
|
Powered by
FUDForum. Page generated in 0.05019 seconds