diff options
author | Dave Borowitz <dborowitz@google.com> | 2015-03-11 15:21:48 -0700 |
---|---|---|
committer | Dave Borowitz <dborowitz@google.com> | 2015-03-12 12:45:46 -0700 |
commit | d79cadb3cf4bb26c01bc5d4795b05858bb25ef42 (patch) | |
tree | b2f1dc2c3997824401b867933adc9e2aa4a9fa86 /org.eclipse.jgit.junit/src/org | |
parent | c1d40caa320ff5b22beab97702acd6e39bb02c83 (diff) | |
download | jgit-d79cadb3cf4bb26c01bc5d4795b05858bb25ef42.tar.gz jgit-d79cadb3cf4bb26c01bc5d4795b05858bb25ef42.zip |
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
Diffstat (limited to 'org.eclipse.jgit.junit/src/org')
-rw-r--r-- | org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java | 55 |
1 files changed, 55 insertions, 0 deletions
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 @@ -479,6 +479,61 @@ public class TestRepository<R extends Repository> { } /** + * Soft-reset HEAD to a detached state. + * <p> + * @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. + * <p> + * 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. * * @throws Exception |