From d79cadb3cf4bb26c01bc5d4795b05858bb25ef42 Mon Sep 17 00:00:00 2001 From: Dave Borowitz Date: Wed, 11 Mar 2015 15:21:48 -0700 Subject: TestRepository: Add a reset method to move HEAD around This flushed out a number of bugs in the way DfsRefUpdate, or at least the InMemoryRepository implementation, processes symrefs. These have been fixed, to an extent, in InMemoryRepository, but other implementations may still suffer from these bugs. Change-Id: Ifd12115a0060b9ff45a88d305b72f91ca0472f9a --- .../src/org/eclipse/jgit/junit/TestRepository.java | 55 ++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'org.eclipse.jgit.junit/src/org') diff --git a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java index 8549118ab2..479ac7e5cf 100644 --- a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java +++ b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java @@ -478,6 +478,61 @@ public class TestRepository { } } + /** + * Soft-reset HEAD to a detached state. + *

+ * @param id + * ID of detached head. + * @throws Exception + * @see #reset(String) + */ + public void reset(AnyObjectId id) throws Exception { + RefUpdate ru = db.updateRef(Constants.HEAD, true); + ru.setNewObjectId(id); + RefUpdate.Result result = ru.forceUpdate(); + switch (result) { + case FAST_FORWARD: + case FORCED: + case NEW: + case NO_CHANGE: + break; + default: + throw new IOException(String.format( + "Checkout \"%s\" failed: %s", id.name(), result)); + } + } + + /** + * Soft-reset HEAD to a different commit. + *

+ * This is equivalent to {@code git reset --soft} in that it modifies HEAD but + * not the index or the working tree of a non-bare repository. + * + * @param name + * revision string; either an existing ref name, or something that + * can be parsed to an object ID. + * @throws Exception + */ + public void reset(String name) throws Exception { + RefUpdate.Result result; + ObjectId id = db.resolve(name); + if (id == null) + throw new IOException("Not a revision: " + name); + RefUpdate ru = db.updateRef(Constants.HEAD, false); + ru.setNewObjectId(id); + result = ru.forceUpdate(); + switch (result) { + case FAST_FORWARD: + case FORCED: + case NEW: + case NO_CHANGE: + break; + default: + throw new IOException(String.format( + "Checkout \"%s\" failed: %s", name, result)); + } + } + /** * Update the dumb client server info files. * -- cgit v1.2.3