Class TestRepository<R extends Repository>
- java.lang.Object
-
- org.eclipse.jgit.junit.TestRepository<R>
-
- Type Parameters:
R
- type of Repository the test data is stored on.
- All Implemented Interfaces:
java.lang.AutoCloseable
public class TestRepository<R extends Repository> extends java.lang.Object implements java.lang.AutoCloseable
Wrapper to make creating test data easier.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
TestRepository.BranchBuilder
Helper to build a branch with one or more commitsclass
TestRepository.CommitBuilder
Helper to generate a commit.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
AUTHOR
ConstantAUTHOR="J.
static java.lang.String
AUTHOR_EMAIL
ConstantAUTHOR_EMAIL="jauthor@example.com"
static java.lang.String
COMMITTER
ConstantCOMMITTER="J.
static java.lang.String
COMMITTER_EMAIL
ConstantCOMMITTER_EMAIL="jcommitter@example.com"
private R
db
private PersonIdent
defaultAuthor
private PersonIdent
defaultCommitter
private Git
git
private ObjectInserter
inserter
private MockSystemReader
mockSystemReader
private RevWalk
pool
-
Constructor Summary
Constructors Constructor Description TestRepository(R db)
Wrap a repository with test building tools.TestRepository(R db, RevWalk rw)
Wrap a repository with test building tools.TestRepository(R db, RevWalk rw, MockSystemReader reader)
Wrap a repository with test building tools.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description TestRepository.CommitBuilder
amend(AnyObjectId id)
Amend an existing commit.private TestRepository.CommitBuilder
amend(RevCommit old, TestRepository.CommitBuilder b)
TestRepository.CommitBuilder
amendRef(java.lang.String ref)
Amend an existing ref.private static void
assertHash(RevObject id, byte[] bin)
RevBlob
blob(byte[] content)
Create a new blob object in the repository.RevBlob
blob(java.lang.String content)
Create a new blob object in the repository.TestRepository.BranchBuilder
branch(java.lang.String ref)
Create a new branch builder for this repository.RevCommit
cherryPick(AnyObjectId id)
Cherry-pick a commit onto HEAD.void
close()
Closes the underlyingRepository
object and any other internal resources.TestRepository.CommitBuilder
commit()
Create commit builderRevCommit
commit(int secDelta, RevCommit... parents)
Create a new commit.RevCommit
commit(int secDelta, RevTree tree, RevCommit... parents)
Create a new commit.RevCommit
commit(RevCommit... parents)
Create a new commit.RevCommit
commit(RevTree tree, RevCommit... parents)
Create a new commit.void
delete(java.lang.String ref)
Delete a reference.DirCacheEntry
file(java.lang.String path, RevBlob blob)
Construct a regular file mode tree entry.void
fsck(RevObject... tips)
Run consistency checks against the object database.RevObject
get(RevTree tree, java.lang.String path)
Lookup an entry stored in a tree, failing if not present.java.util.Date
getDate()
Get dateR
getRepository()
Get repositoryRevWalk
getRevWalk()
Get RevWalkjava.util.TimeZone
getTimeZone()
Get timezoneGit
git()
Return Git API wrapperObjectId
lightweightTag(java.lang.String name, ObjectId obj)
Tag an object using a lightweight tag.private static java.io.File
nameFor(ObjectDirectory odb, ObjectId name, java.lang.String t)
private static java.lang.String
normalizeRef(java.lang.String ref)
void
packAndPrune()
Pack all reachable objects in the repository into a single pack file.<T extends RevObject>
TparseBody(T object)
Ensure the body of the given object has been parsed.private static void
prunePacked(ObjectDirectory odb)
void
reset(java.lang.String name)
Soft-reset HEAD to a different commit.void
reset(AnyObjectId id)
Soft-reset HEAD to a detached state.void
setAuthorAndCommitter(CommitBuilder c)
Set the author and committer usinggetDate()
.RevTag
tag(java.lang.String name, RevObject dst)
Construct an annotated tag object pointing at another object.void
tick(int secDelta)
Adjust the current time that will used by the next commit.RevTree
tree(DirCacheEntry... entries)
Construct a tree from a specific listing of file entries.ObjectId
unparsedCommit(int secDelta, RevTree tree, ObjectId... parents)
Create a new, unparsed commit.ObjectId
unparsedCommit(ObjectId... parents)
Create a new, unparsed commit.RevCommit
update(java.lang.String ref, TestRepository.CommitBuilder to)
Update a reference to point to an object.<T extends AnyObjectId>
Tupdate(java.lang.String ref, T obj)
Update a reference to point to an object.void
updateServerInfo()
Update the dumb client server info files.private void
writeFile(java.io.File p, byte[] bin)
-
-
-
Field Detail
-
AUTHOR
public static final java.lang.String AUTHOR
ConstantAUTHOR="J. Author"
- See Also:
- Constant Field Values
-
AUTHOR_EMAIL
public static final java.lang.String AUTHOR_EMAIL
ConstantAUTHOR_EMAIL="jauthor@example.com"
- See Also:
- Constant Field Values
-
COMMITTER
public static final java.lang.String COMMITTER
ConstantCOMMITTER="J. Committer"
- See Also:
- Constant Field Values
-
COMMITTER_EMAIL
public static final java.lang.String COMMITTER_EMAIL
ConstantCOMMITTER_EMAIL="jcommitter@example.com"
- See Also:
- Constant Field Values
-
defaultAuthor
private final PersonIdent defaultAuthor
-
defaultCommitter
private final PersonIdent defaultCommitter
-
db
private final R extends Repository db
-
git
private final Git git
-
pool
private final RevWalk pool
-
inserter
private final ObjectInserter inserter
-
mockSystemReader
private final MockSystemReader mockSystemReader
-
-
Constructor Detail
-
TestRepository
public TestRepository(R db) throws java.io.IOException
Wrap a repository with test building tools.- Parameters:
db
- the test repository to write into.- Throws:
java.io.IOException
-
TestRepository
public TestRepository(R db, RevWalk rw) throws java.io.IOException
Wrap a repository with test building tools.- Parameters:
db
- the test repository to write into.rw
- the RevObject pool to use for object lookup.- Throws:
java.io.IOException
-
TestRepository
public TestRepository(R db, RevWalk rw, MockSystemReader reader) throws java.io.IOException
Wrap a repository with test building tools.- Parameters:
db
- the test repository to write into.rw
- the RevObject pool to use for object lookup.reader
- the MockSystemReader to use for clock and other system operations.- Throws:
java.io.IOException
- Since:
- 4.2
-
-
Method Detail
-
getRepository
public R getRepository()
Get repository- Returns:
- the repository this helper class operates against.
-
getRevWalk
public RevWalk getRevWalk()
Get RevWalk- Returns:
- get the RevWalk pool all objects are allocated through.
-
git
public Git git()
Return Git API wrapper- Returns:
- an API wrapper for the underlying repository. This wrapper does not allocate any new resources and need not be closed (but closing it is harmless).
-
getDate
public java.util.Date getDate()
Get date- Returns:
- current date.
- Since:
- 4.2
-
getTimeZone
public java.util.TimeZone getTimeZone()
Get timezone- Returns:
- timezone used for default identities.
-
tick
public void tick(int secDelta)
Adjust the current time that will used by the next commit.- Parameters:
secDelta
- number of seconds to add to the current time.
-
setAuthorAndCommitter
public void setAuthorAndCommitter(CommitBuilder c)
Set the author and committer usinggetDate()
.- Parameters:
c
- the commit builder to store.
-
blob
public RevBlob blob(java.lang.String content) throws java.lang.Exception
Create a new blob object in the repository.- Parameters:
content
- file content, will be UTF-8 encoded.- Returns:
- reference to the blob.
- Throws:
java.lang.Exception
-
blob
public RevBlob blob(byte[] content) throws java.lang.Exception
Create a new blob object in the repository.- Parameters:
content
- binary file content.- Returns:
- the new, fully parsed blob.
- Throws:
java.lang.Exception
-
file
public DirCacheEntry file(java.lang.String path, RevBlob blob) throws java.lang.Exception
Construct a regular file mode tree entry.- Parameters:
path
- path of the file.blob
- a blob, previously constructed in the repository.- Returns:
- the entry.
- Throws:
java.lang.Exception
-
tree
public RevTree tree(DirCacheEntry... entries) throws java.lang.Exception
Construct a tree from a specific listing of file entries.- Parameters:
entries
- the files to include in the tree. The collection does not need to be sorted properly and may be empty.- Returns:
- the new, fully parsed tree specified by the entry list.
- Throws:
java.lang.Exception
-
get
public RevObject get(RevTree tree, java.lang.String path) throws java.lang.Exception
Lookup an entry stored in a tree, failing if not present.- Parameters:
tree
- the tree to search.path
- the path to find the entry of.- Returns:
- the parsed object entry at this path, never null.
- Throws:
java.lang.Exception
-
unparsedCommit
public ObjectId unparsedCommit(ObjectId... parents) throws java.lang.Exception
Create a new, unparsed commit.See
unparsedCommit(int, RevTree, ObjectId...)
. The tree is the empty tree (no files or subdirectories).- Parameters:
parents
- zero or more IDs of the commit's parents.- Returns:
- the ID of the new commit.
- Throws:
java.lang.Exception
-
commit
public RevCommit commit(RevCommit... parents) throws java.lang.Exception
Create a new commit.See
commit(int, RevTree, RevCommit...)
. The tree is the empty tree (no files or subdirectories).- Parameters:
parents
- zero or more parents of the commit.- Returns:
- the new commit.
- Throws:
java.lang.Exception
-
commit
public RevCommit commit(RevTree tree, RevCommit... parents) throws java.lang.Exception
Create a new commit.- Parameters:
tree
- the root tree for the commit.parents
- zero or more parents of the commit.- Returns:
- the new commit.
- Throws:
java.lang.Exception
-
commit
public RevCommit commit(int secDelta, RevCommit... parents) throws java.lang.Exception
Create a new commit.See
commit(int, RevTree, RevCommit...)
. The tree is the empty tree (no files or subdirectories).- Parameters:
secDelta
- number of seconds to advancetick(int)
by.parents
- zero or more parents of the commit.- Returns:
- the new commit.
- Throws:
java.lang.Exception
-
commit
public RevCommit commit(int secDelta, RevTree tree, RevCommit... parents) throws java.lang.Exception
Create a new commit.The author and committer identities are stored using the current timestamp, after being incremented by
secDelta
. The message body is empty.- Parameters:
secDelta
- number of seconds to advancetick(int)
by.tree
- the root tree for the commit.parents
- zero or more parents of the commit.- Returns:
- the new, fully parsed commit.
- Throws:
java.lang.Exception
-
unparsedCommit
public ObjectId unparsedCommit(int secDelta, RevTree tree, ObjectId... parents) throws java.lang.Exception
Create a new, unparsed commit.The author and committer identities are stored using the current timestamp, after being incremented by
secDelta
. The message body is empty.- Parameters:
secDelta
- number of seconds to advancetick(int)
by.tree
- the root tree for the commit.parents
- zero or more IDs of the commit's parents.- Returns:
- the ID of the new commit.
- Throws:
java.lang.Exception
-
commit
public TestRepository.CommitBuilder commit()
Create commit builder- Returns:
- a new commit builder.
-
tag
public RevTag tag(java.lang.String name, RevObject dst) throws java.lang.Exception
Construct an annotated tag object pointing at another object.The tagger is the committer identity, at the current time as specified by
tick(int)
. The time is not increased.The tag message is empty.
- Parameters:
name
- name of the tag. Traditionally a tag name should not start withrefs/tags/
.dst
- object the tag should be pointed at.- Returns:
- the new, fully parsed annotated tag object.
- Throws:
java.lang.Exception
-
update
public RevCommit update(java.lang.String ref, TestRepository.CommitBuilder to) throws java.lang.Exception
Update a reference to point to an object.- Parameters:
ref
- the name of the reference to update to. Ifref
does not start withrefs/
and is not the magic namesHEAD
FETCH_HEAD
orMERGE_HEAD
, thenrefs/heads/
will be prefixed in front of the given name, thereby assuming it is a branch.to
- the target object.- Returns:
- the target object.
- Throws:
java.lang.Exception
-
amendRef
public TestRepository.CommitBuilder amendRef(java.lang.String ref) throws java.lang.Exception
Amend an existing ref.- Parameters:
ref
- the name of the reference to amend, which must already exist. Ifref
does not start withrefs/
and is not the magic namesHEAD
FETCH_HEAD
orMERGE_HEAD
, thenrefs/heads/
will be prefixed in front of the given name, thereby assuming it is a branch.- Returns:
- commit builder that amends the branch on commit.
- Throws:
java.lang.Exception
-
amend
public TestRepository.CommitBuilder amend(AnyObjectId id) throws java.lang.Exception
Amend an existing commit.- Parameters:
id
- the id of the commit to amend.- Returns:
- commit builder.
- Throws:
java.lang.Exception
-
amend
private TestRepository.CommitBuilder amend(RevCommit old, TestRepository.CommitBuilder b) throws java.lang.Exception
- Throws:
java.lang.Exception
-
update
public <T extends AnyObjectId> T update(java.lang.String ref, T obj) throws java.lang.Exception
Update a reference to point to an object.- Type Parameters:
T
- type of the target object.- Parameters:
ref
- the name of the reference to update to. Ifref
does not start withrefs/
and is not the magic namesHEAD
FETCH_HEAD
orMERGE_HEAD
, thenrefs/heads/
will be prefixed in front of the given name, thereby assuming it is a branch.obj
- the target object.- Returns:
- the target object.
- Throws:
java.lang.Exception
-
delete
public void delete(java.lang.String ref) throws java.lang.Exception
Delete a reference.- Parameters:
ref
- the name of the reference to delete. This is normalized in the same way asupdate(String, AnyObjectId)
.- Throws:
java.lang.Exception
- Since:
- 4.4
-
normalizeRef
private static java.lang.String normalizeRef(java.lang.String ref)
-
reset
public void reset(AnyObjectId id) throws java.lang.Exception
Soft-reset HEAD to a detached state.- Parameters:
id
- ID of detached head.- Throws:
java.lang.Exception
- See Also:
reset(String)
-
reset
public void reset(java.lang.String name) throws java.lang.Exception
Soft-reset HEAD to a different commit.This is equivalent to
git reset --soft
in that it modifies HEAD but not the index or the working tree of a non-bare repository.- Parameters:
name
- revision string; either an existing ref name, or something that can be parsed to an object ID.- Throws:
java.lang.Exception
-
cherryPick
public RevCommit cherryPick(AnyObjectId id) throws java.lang.Exception
Cherry-pick a commit onto HEAD.This differs from
git cherry-pick
in that it works in a bare repository. As a result, any merge failure results in an exception, as there is no way to recover.- Parameters:
id
- commit-ish to cherry-pick.- Returns:
- the new, fully parsed commit, or null if no work was done due to the resulting tree being identical.
- Throws:
java.lang.Exception
-
updateServerInfo
public void updateServerInfo() throws java.lang.Exception
Update the dumb client server info files.- Throws:
java.lang.Exception
-
parseBody
public <T extends RevObject> T parseBody(T object) throws java.lang.Exception
Ensure the body of the given object has been parsed.
-
branch
public TestRepository.BranchBuilder branch(java.lang.String ref)
Create a new branch builder for this repository.- Parameters:
ref
- name of the branch to be constructed. Ifref
does not start withrefs/
the prefixrefs/heads/
will be added.- Returns:
- builder for the named branch.
-
lightweightTag
public ObjectId lightweightTag(java.lang.String name, ObjectId obj) throws java.lang.Exception
Tag an object using a lightweight tag.- Parameters:
name
- the tag name. The /refs/tags/ prefix will be added if the name doesn't start with itobj
- the object to tag- Returns:
- the tagged object
- Throws:
java.lang.Exception
-
fsck
public void fsck(RevObject... tips) throws MissingObjectException, IncorrectObjectTypeException, java.io.IOException
Run consistency checks against the object database.This method completes silently if the checks pass. A temporary revision pool is constructed during the checking.
- Parameters:
tips
- the tips to start checking from; if not supplied the refs of the repository are used instead.- Throws:
MissingObjectException
IncorrectObjectTypeException
java.io.IOException
-
assertHash
private static void assertHash(RevObject id, byte[] bin)
-
packAndPrune
public void packAndPrune() throws java.lang.Exception
Pack all reachable objects in the repository into a single pack file.All loose objects are automatically pruned. Existing packs however are not removed.
- Throws:
java.lang.Exception
-
close
public void close()
Closes the underlyingRepository
object and any other internal resources.AutoCloseable
resources that may escape this object, such as those returned by thegit
andgetRevWalk()
methods are not closed.- Specified by:
close
in interfacejava.lang.AutoCloseable
-
prunePacked
private static void prunePacked(ObjectDirectory odb) throws java.io.IOException
- Throws:
java.io.IOException
-
nameFor
private static java.io.File nameFor(ObjectDirectory odb, ObjectId name, java.lang.String t)
-
writeFile
private void writeFile(java.io.File p, byte[] bin) throws java.io.IOException, ObjectWritingException
- Throws:
java.io.IOException
ObjectWritingException
-
-