Re: [JGIT] help needed to create a siimple commit

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Yann Simon <yann.simon.fr@xxxxxxxxx> wrote:
> 		final Commit commit = new Commit(db);
> 		commit.setAuthor(new PersonIdent(jauthor, 1154236443000L, -4 * 60));
> 		commit.setCommitter(new PersonIdent(jcommitter, 1154236443000L, -4 * 60));
> 		commit.setMessage(commitMessage);
> 		commit.setTree(tree);

FWIW, its faster to use setTreeId().  Then you don't need to use
mapTree to read the tree object into memory.  This is an older area
of the Commit class API, before we understood how important it was
to avoid processing unnecessary data.  :-)

> 		assertEquals(tree.getTreeId(), commit.getTreeId());
> 		commit.commit();
> 		
> 		ObjectWriter writer = new ObjectWriter(db);
> 		commit.setCommitId(writer.writeCommit(commit));

Uh, commit.commit() is doing the same work as the above two lines,
so these above two lines are a no-op as the object already exists
in the object database.
 
> 		Ref oldHEAD = db.getAllRefs().get(Constants.HEAD);
> 		final RefUpdate ru = db.updateRef(Constants.HEAD);
> 		ru.setNewObjectId(commit.getCommitId());
> 		ru.setRefLogMessage(commitMessage, false);
> 
> 		if (oldHEAD != null) {
> 			// commit has parents
> 			ru.setExpectedOldObjectId(oldHEAD.getObjectId());
> 			assertSame(RefUpdate.Result.FAST_FORWARD, ru.update());

This fails because the commit has no parents, but you asked
for a fast-forward update.  Since HEAD already has a commit,
and the new commit isn't a super-set of the existing HEAD, and
RefUpdate.isForceUpdate() is false, the update is being rejected.

If you really mean to replace the commit, e.g. commit --amend,
you need to ru.setForceUpdate(true).

If you really mean to fast-forward the commit, e.g. just make
a new commit on top of the existing commits, you need to do
the read to get oldHEAD *before* you call commit.commit()
above, and use:

  commit.setParents(new Object[]{ oldHEAD.getObjectId() })

to set the current commit as the first parent of the new commit,
so the new commit is really a superset of the old one.

Again, back to the basic DAG in Git... JGit is just a library with
the building blocks necessary to manipulate the DAG.  Its up to
the appliction to create the new nodes correctly for its needs,
and to make the right requests to RefUpdate.  The RefUpdate API is
designed to help the application enforce CAS semantics, to avoid
race conditions and other ugly surprises.

-- 
Shawn.
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux