Class TestRepository<R extends org.eclipse.jgit.lib.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:
AutoCloseable

public class TestRepository<R extends org.eclipse.jgit.lib.Repository> extends Object implements AutoCloseable
Wrapper to make creating test data easier.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    class 
    Helper to build a branch with one or more commits
    class 
    Helper to generate a commit.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    Constant AUTHOR="J.
    static final String
    Constant AUTHOR_EMAIL="jauthor@example.com"
    static final String
    Constant COMMITTER="J.
    static final String
    Constant COMMITTER_EMAIL="jcommitter@example.com"
  • Constructor Summary

    Constructors
    Constructor
    Description
    Wrap a repository with test building tools.
    TestRepository(R db, org.eclipse.jgit.revwalk.RevWalk rw)
    Wrap a repository with test building tools.
    TestRepository(R db, org.eclipse.jgit.revwalk.RevWalk rw, MockSystemReader reader)
    Wrap a repository with test building tools.
  • Method Summary

    Modifier and Type
    Method
    Description
    amend(org.eclipse.jgit.lib.AnyObjectId id)
    Amend an existing commit.
    Amend an existing ref.
    org.eclipse.jgit.revwalk.RevBlob
    blob(byte[] content)
    Create a new blob object in the repository.
    org.eclipse.jgit.revwalk.RevBlob
    blob(String content)
    Create a new blob object in the repository.
    Create a new branch builder for this repository.
    org.eclipse.jgit.revwalk.RevCommit
    cherryPick(org.eclipse.jgit.lib.AnyObjectId id)
    Cherry-pick a commit onto HEAD.
    void
    Closes the underlying Repository object and any other internal resources.
    Create commit builder
    org.eclipse.jgit.revwalk.RevCommit
    commit(int secDelta, org.eclipse.jgit.revwalk.RevCommit... parents)
    Create a new commit.
    org.eclipse.jgit.revwalk.RevCommit
    commit(int secDelta, org.eclipse.jgit.revwalk.RevTree tree, org.eclipse.jgit.revwalk.RevCommit... parents)
    Create a new commit.
    org.eclipse.jgit.revwalk.RevCommit
    commit(org.eclipse.jgit.revwalk.RevCommit... parents)
    Create a new commit.
    org.eclipse.jgit.revwalk.RevCommit
    commit(org.eclipse.jgit.revwalk.RevTree tree, org.eclipse.jgit.revwalk.RevCommit... parents)
    Create a new commit.
    void
    Delete a reference.
    org.eclipse.jgit.dircache.DirCacheEntry
    file(String path, org.eclipse.jgit.revwalk.RevBlob blob)
    Construct a regular file mode tree entry.
    void
    fsck(org.eclipse.jgit.revwalk.RevObject... tips)
    Run consistency checks against the object database.
    org.eclipse.jgit.revwalk.RevObject
    get(org.eclipse.jgit.revwalk.RevTree tree, String path)
    Lookup an entry stored in a tree, failing if not present.
    Deprecated.
    Use getInstant() instead.
    Get instant
    Get repository
    org.eclipse.jgit.revwalk.RevWalk
    Get RevWalk
    Deprecated.
    Use getTimeZoneId() instead.
    Get timezone
    org.eclipse.jgit.api.Git
    git()
    Return Git API wrapper
    org.eclipse.jgit.lib.ObjectId
    lightweightTag(String name, org.eclipse.jgit.lib.ObjectId obj)
    Tag an object using a lightweight tag.
    org.eclipse.jgit.dircache.DirCacheEntry
    link(String path, org.eclipse.jgit.revwalk.RevBlob blob)
    Construct a symlink mode tree entry.
    void
    Pack all reachable objects in the repository into a single pack file.
    <T extends org.eclipse.jgit.revwalk.RevObject>
    T
    parseBody(T object)
    Ensure the body of the given object has been parsed.
    void
    reset(String name)
    Soft-reset HEAD to a different commit.
    void
    reset(org.eclipse.jgit.lib.AnyObjectId id)
    Soft-reset HEAD to a detached state.
    void
    setAuthorAndCommitter(org.eclipse.jgit.lib.CommitBuilder c)
    Set the author and committer using getInstant().
    org.eclipse.jgit.revwalk.RevTag
    tag(String name, org.eclipse.jgit.revwalk.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.
    org.eclipse.jgit.revwalk.RevTree
    tree(org.eclipse.jgit.dircache.DirCacheEntry... entries)
    Construct a tree from a specific listing of file entries.
    org.eclipse.jgit.lib.ObjectId
    unparsedCommit(int secDelta, org.eclipse.jgit.revwalk.RevTree tree, org.eclipse.jgit.lib.ObjectId... parents)
    Create a new, unparsed commit.
    org.eclipse.jgit.lib.ObjectId
    unparsedCommit(org.eclipse.jgit.lib.ObjectId... parents)
    Create a new, unparsed commit.
    org.eclipse.jgit.revwalk.RevCommit
    Update a reference to point to an object.
    <T extends org.eclipse.jgit.lib.AnyObjectId>
    T
    update(String ref, T obj)
    Update a reference to point to an object.
    void
    Update the dumb client server info files.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • TestRepository

      public TestRepository(R db) throws IOException
      Wrap a repository with test building tools.
      Parameters:
      db - the test repository to write into.
      Throws:
      IOException - if an IO error occurred
    • TestRepository

      public TestRepository(R db, org.eclipse.jgit.revwalk.RevWalk rw) throws 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:
      IOException - if an IO error occurred
    • TestRepository

      public TestRepository(R db, org.eclipse.jgit.revwalk.RevWalk rw, MockSystemReader reader) throws 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:
      IOException - if an IO error occurred
      Since:
      4.2
  • Method Details

    • getRepository

      public R getRepository()
      Get repository
      Returns:
      the repository this helper class operates against.
    • getRevWalk

      public org.eclipse.jgit.revwalk.RevWalk getRevWalk()
      Get RevWalk
      Returns:
      get the RevWalk pool all objects are allocated through.
    • git

      public org.eclipse.jgit.api.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

      @Deprecated(since="7.2") public Date getDate()
      Deprecated.
      Use getInstant() instead.
      Get date
      Returns:
      current date.
      Since:
      4.2
    • getInstant

      public Instant getInstant()
      Get instant
      Returns:
      current instant.
      Since:
      6.8
    • getTimeZone

      @Deprecated(since="7.2") public TimeZone getTimeZone()
      Deprecated.
      Use getTimeZoneId() instead.
      Get timezone
      Returns:
      timezone used for default identities.
    • getTimeZoneId

      public ZoneId getTimeZoneId()
      Get timezone
      Returns:
      timezone used for default identities.
      Since:
      7.2
    • 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(org.eclipse.jgit.lib.CommitBuilder c)
      Set the author and committer using getInstant().
      Parameters:
      c - the commit builder to store.
    • blob

      public org.eclipse.jgit.revwalk.RevBlob blob(String content) throws Exception
      Create a new blob object in the repository.
      Parameters:
      content - file content, will be UTF-8 encoded.
      Returns:
      reference to the blob.
      Throws:
      Exception - if an error occurred
    • blob

      public org.eclipse.jgit.revwalk.RevBlob blob(byte[] content) throws Exception
      Create a new blob object in the repository.
      Parameters:
      content - binary file content.
      Returns:
      the new, fully parsed blob.
      Throws:
      Exception - if an error occurred
    • file

      public org.eclipse.jgit.dircache.DirCacheEntry file(String path, org.eclipse.jgit.revwalk.RevBlob blob) throws 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:
      Exception - if an error occurred
    • link

      public org.eclipse.jgit.dircache.DirCacheEntry link(String path, org.eclipse.jgit.revwalk.RevBlob blob) throws Exception
      Construct a symlink mode tree entry.
      Parameters:
      path - path of the symlink.
      blob - a blob, previously constructed in the repository.
      Returns:
      the entry.
      Throws:
      Exception - if an error occurred
      Since:
      6.3
    • tree

      public org.eclipse.jgit.revwalk.RevTree tree(org.eclipse.jgit.dircache.DirCacheEntry... entries) throws 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:
      Exception - if an error occurred
    • get

      public org.eclipse.jgit.revwalk.RevObject get(org.eclipse.jgit.revwalk.RevTree tree, String path) throws 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:
      Exception - if an error occurred
    • unparsedCommit

      public org.eclipse.jgit.lib.ObjectId unparsedCommit(org.eclipse.jgit.lib.ObjectId... parents) throws 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:
      Exception - if an error occurred
      Since:
      5.5
    • commit

      public org.eclipse.jgit.revwalk.RevCommit commit(org.eclipse.jgit.revwalk.RevCommit... parents) throws 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:
      Exception - if an error occurred
    • commit

      public org.eclipse.jgit.revwalk.RevCommit commit(org.eclipse.jgit.revwalk.RevTree tree, org.eclipse.jgit.revwalk.RevCommit... parents) throws Exception
      Create a new commit.

      See commit(int, RevTree, RevCommit...).

      Parameters:
      tree - the root tree for the commit.
      parents - zero or more parents of the commit.
      Returns:
      the new commit.
      Throws:
      Exception - if an error occurred
    • commit

      public org.eclipse.jgit.revwalk.RevCommit commit(int secDelta, org.eclipse.jgit.revwalk.RevCommit... parents) throws 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 advance tick(int) by.
      parents - zero or more parents of the commit.
      Returns:
      the new commit.
      Throws:
      Exception - if an error occurred
    • commit

      public org.eclipse.jgit.revwalk.RevCommit commit(int secDelta, org.eclipse.jgit.revwalk.RevTree tree, org.eclipse.jgit.revwalk.RevCommit... parents) throws 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 advance tick(int) by.
      tree - the root tree for the commit.
      parents - zero or more parents of the commit.
      Returns:
      the new, fully parsed commit.
      Throws:
      Exception - if an error occurred
    • unparsedCommit

      public org.eclipse.jgit.lib.ObjectId unparsedCommit(int secDelta, org.eclipse.jgit.revwalk.RevTree tree, org.eclipse.jgit.lib.ObjectId... parents) throws 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 advance tick(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:
      Exception - if an error occurred
      Since:
      5.5
    • commit

      public TestRepository<R>.CommitBuilder commit()
      Create commit builder
      Returns:
      a new commit builder.
    • tag

      public org.eclipse.jgit.revwalk.RevTag tag(String name, org.eclipse.jgit.revwalk.RevObject dst) throws 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 with refs/tags/.
      dst - object the tag should be pointed at.
      Returns:
      the new, fully parsed annotated tag object.
      Throws:
      Exception - if an error occurred
    • update

      public org.eclipse.jgit.revwalk.RevCommit update(String ref, TestRepository<R>.CommitBuilder to) throws Exception
      Update a reference to point to an object.
      Parameters:
      ref - the name of the reference to update to. If ref does not start with refs/ and is not the magic names HEAD FETCH_HEAD or MERGE_HEAD, then refs/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:
      Exception - if an error occurred
    • amendRef

      public TestRepository<R>.CommitBuilder amendRef(String ref) throws Exception
      Amend an existing ref.
      Parameters:
      ref - the name of the reference to amend, which must already exist. If ref does not start with refs/ and is not the magic names HEAD FETCH_HEAD or MERGE_HEAD, then refs/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:
      Exception - if an error occurred
    • amend

      public TestRepository<R>.CommitBuilder amend(org.eclipse.jgit.lib.AnyObjectId id) throws Exception
      Amend an existing commit.
      Parameters:
      id - the id of the commit to amend.
      Returns:
      commit builder.
      Throws:
      Exception - if an error occurred
    • update

      public <T extends org.eclipse.jgit.lib.AnyObjectId> T update(String ref, T obj) throws 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. If ref does not start with refs/ and is not the magic names HEAD FETCH_HEAD or MERGE_HEAD, then refs/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:
      Exception - if an error occurred
    • delete

      public void delete(String ref) throws Exception
      Delete a reference.
      Parameters:
      ref - the name of the reference to delete. This is normalized in the same way as update(String, AnyObjectId).
      Throws:
      Exception - if an error occurred
      Since:
      4.4
    • reset

      public void reset(org.eclipse.jgit.lib.AnyObjectId id) throws Exception
      Soft-reset HEAD to a detached state.
      Parameters:
      id - ID of detached head.
      Throws:
      Exception - if an error occurred
      See Also:
    • reset

      public void reset(String name) throws 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:
      Exception - if an error occurred
    • cherryPick

      public org.eclipse.jgit.revwalk.RevCommit cherryPick(org.eclipse.jgit.lib.AnyObjectId id) throws 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:
      Exception - if an error occurred
    • updateServerInfo

      public void updateServerInfo() throws Exception
      Update the dumb client server info files.
      Throws:
      Exception - if an error occurred
    • parseBody

      public <T extends org.eclipse.jgit.revwalk.RevObject> T parseBody(T object) throws Exception
      Ensure the body of the given object has been parsed.
      Type Parameters:
      T - type of object, e.g. RevTag or RevCommit.
      Parameters:
      object - reference to the (possibly unparsed) object to force body parsing of.
      Returns:
      object
      Throws:
      Exception - if an error occurred
    • branch

      public TestRepository<R>.BranchBuilder branch(String ref)
      Create a new branch builder for this repository.
      Parameters:
      ref - name of the branch to be constructed. If ref does not start with refs/ the prefix refs/heads/ will be added.
      Returns:
      builder for the named branch.
    • lightweightTag

      public org.eclipse.jgit.lib.ObjectId lightweightTag(String name, org.eclipse.jgit.lib.ObjectId obj) throws 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 it
      obj - the object to tag
      Returns:
      the tagged object
      Throws:
      Exception - if an error occurred
    • fsck

      public void fsck(org.eclipse.jgit.revwalk.RevObject... tips) throws org.eclipse.jgit.errors.MissingObjectException, org.eclipse.jgit.errors.IncorrectObjectTypeException, 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:
      org.eclipse.jgit.errors.MissingObjectException - if object is missing
      org.eclipse.jgit.errors.IncorrectObjectTypeException - if object has unexpected type
      IOException - if an IO error occurred
    • packAndPrune

      public void packAndPrune() throws 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:
      Exception - if an error occurred
    • close

      public void close()
      Closes the underlying Repository object and any other internal resources.

      AutoCloseable resources that may escape this object, such as those returned by the git and getRevWalk() methods are not closed.

      Specified by:
      close in interface AutoCloseable