git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Yann Simon <yann.simon.fr@gmail.com>
To: "Shawn O. Pearce" <spearce@spearce.org>
Cc: Robin Rosenberg <robin.rosenberg@dewire.com>, git@vger.kernel.org
Subject: Re: [JGIT] help needed to create a siimple commit
Date: Wed, 4 Feb 2009 17:10:48 +0100	[thread overview]
Message-ID: <551f769b0902040810o6b45008fgcc1ef70108b3d90@mail.gmail.com> (raw)
In-Reply-To: <20090203155107.GU26880@spearce.org>

2009/2/3 Shawn O. Pearce <spearce@spearce.org>:
> You are missing the final step of updating the HEAD ref with the
> commit.  Calling commit() on the Commit object only writes it to
> the object database, this is similar to git-commit-tree.
>
> Try adding on the end:
>
>        RefUpdate ru = db.updateRef(Constants.HEAD);
>        ru.setRefLogMessage("commit");
>        ru.setNewObjectId(c1.getCommitId());
>        assertSame(RefUpdate.Result.NEW, ru.update());
>
> If your commit had parents, you might want to do instead:
>
>        ru.setExpectedOldObjectId(oldHEAD);
>        assertSame(RefUpdate.Result.FAST_FORWARD, ru.update());
>
> where oldHEAD is the value of HEAD you read and used as the first
> parent of the commit.  This ensures that the update method fails
> if someone else has updated HEAD since you last read it.
>
> The update method returns a number of different states, usually we
> check its result with a switch statement as a number of states are
> sometimes permissible in a context.  Sometimes though, you know it
> has to be exactly one state, and everything else is a failure.
>
I updated my simple test like this:

package org.spearce.jgit.lib;

import java.io.File;
import java.io.IOException;

public class CommitTest extends RepositoryTestCase {

	public void testASimpleCommit() throws IOException {
		
		recursiveDelete(trash_git, false, null, true);
		db = new Repository(trash_git);
		db.create();

		GitIndex index = new GitIndex(db);
		index.filemode = Boolean.TRUE;
		
		commitIndex(index, "commit 1");

		File file1 = writeTrashFile("file1", "file1");
		index.add(trash, file1);
		
		commitIndex(index, "commit 2");

	}
	
	private void commitIndex(GitIndex index, String commitMessage) throws
IOException {
		index.write();
		ObjectId objectId = index.writeTree();
		Tree tree = db.mapTree(objectId);
		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);
		assertEquals(tree.getTreeId(), commit.getTreeId());
		commit.commit();
		
		ObjectWriter writer = new ObjectWriter(db);
		commit.setCommitId(writer.writeCommit(commit));

		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());
		} else {
			// commit has no parents
			assertSame(RefUpdate.Result.NEW, ru.update());
		}
		

	}
}

The first commit "with an empty workspace" is ok. I can see the commit
in the log.
The second commit fails, with ru.update() = REJECTED.

I tried different combination, without success.

If someone could tell me, what I am doing wrong...

Yann

  reply	other threads:[~2009-02-04 16:12 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-02-03 11:07 [JGIT] help needed to create a siimple commit Yann Simon
2009-02-03 15:51 ` Shawn O. Pearce
2009-02-04 16:10   ` Yann Simon [this message]
2009-02-04 17:07     ` Shawn O. Pearce

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=551f769b0902040810o6b45008fgcc1ef70108b3d90@mail.gmail.com \
    --to=yann.simon.fr@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=robin.rosenberg@dewire.com \
    --cc=spearce@spearce.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).