Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit » Jgit checkout/commit example.
Jgit checkout/commit example. [message #577604] Tue, 16 February 2010 08:19 Go to next message
Jan is currently offline Jan
Messages: 6
Registered: February 2010
Junior Member
Is there an example of how to commit a file and checkout a revision of a file using jgit anywhere?
Re: Jgit checkout/commit example. [message #577636 is a reply to message #577604] Wed, 17 February 2010 08:56 Go to previous messageGo to next message
Jan is currently offline Jan
Messages: 6
Registered: February 2010
Junior Member
I managed to checkout a file. Now I have to figure out how to commit a file.

This is the code I used for checkout. It's just a test and probably not the best way of doing it...



repo = new Repository(new File(gitFolder()));

public void checkout(String file, String toDir) throws IOException {

RevWalk rw = new RevWalk(repo);
ObjectId objHead = repo.resolve("HEAD");
RevCommit commitHead = rw.parseCommit(objHead);

Tree tree = repo.mapTree(commitHead.getTree());
TreeEntry fileTreeEntry = getFile(file.split("/"), 0, tree);

ObjectLoader loader = repo.openBlob(fileTreeEntry.getId());

// Write content
byte[] bytes = loader.getBytes();
File outFile = new File(toDir + "/" + fileTreeEntry.getName());

FileChannel channel = new FileOutputStream(outFile).getChannel();
ByteBuffer buffer = ByteBuffer.wrap(bytes);
channel.write(buffer);
channel.close();
}

private TreeEntry getFile(String[] path, int pathPos, Tree tree) throws IOException {
if (path.length == 0 && pathPos > path.length) { return null; }

boolean isFile = path.length == pathPos + 1;

for(TreeEntry entry : tree.members()) {
if (path[pathPos].equals(entry.getName())) {
if (isFile) {
return entry;
} else {
return getFile(path, ++pathPos, repo.mapTree(entry.getId()));
}
}
}
return null;
}
Re: Jgit checkout/commit example. [message #577678 is a reply to message #577636] Thu, 18 February 2010 08:19 Go to previous messageGo to next message
Jan is currently offline Jan
Messages: 6
Registered: February 2010
Junior Member
I managed to commit a file using this code.
(With help from : https://kerneltrap.org/mailarchive/git/2009/2/4/4899784/thre ad )


public void commit(String file, PersonIdent person, String commitMessage) throws IOException {
// Example source: https://kerneltrap.org/mailarchive/git/2009/2/4/4899784/thre ad

Ref oldHEAD = repo.getAllRefs().get(Constants.HEAD);

GitIndex index = new GitIndex(repo);
index.add(new File(workingDirectory), new File(workingDirectory + "/" + file));
index.write();

ObjectId objectId = index.writeTree();

Tree tree = repo.mapTree(objectId);

final Commit commit = new Commit(repo);
commit.setAuthor(person);
commit.setCommitter(person);
commit.setMessage(commitMessage);
commit.setTree(tree);
commit.setParentIds(new ObjectId[] { oldHEAD.getObjectId() });
commit.commit();

ObjectWriter writer = new ObjectWriter(repo);

commit.setCommitId(writer.writeCommit(commit));

final RefUpdate ru = repo.updateRef(Constants.HEAD);
ru.setNewObjectId(commit.getCommitId());
ru.setRefLogMessage(commitMessage, false);

if (oldHEAD != null) {
// commit has parents
ru.setExpectedOldObjectId(oldHEAD.getObjectId());

} else {
// commit has no parents
}

ru.update();
}
Re: Jgit checkout/commit example. [message #580650 is a reply to message #577678] Fri, 28 May 2010 11:14 Go to previous messageGo to next message
Deltera  is currently offline Deltera
Messages: 2
Registered: May 2010
Junior Member
Thanks a lot for posting this. It's really hard to find any examples of JGit code!
Re: Jgit checkout/commit example. [message #581721 is a reply to message #577604] Sun, 20 June 2010 08:04 Go to previous message
Anthyon is currently offline 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
Previous Topic:EGit zip file for local archive
Next Topic:Remote development support
Goto Forum:
  


Current Time: Wed Jul 23 22:23:23 EDT 2014

Powered by FUDForum. Page generated in 0.02775 seconds