Class JGitRepository
- java.lang.Object
-
- com.github.koraktor.mavanagaiata.git.AbstractGitRepository
-
- com.github.koraktor.mavanagaiata.git.jgit.JGitRepository
-
- All Implemented Interfaces:
GitRepository,AutoCloseable
public class JGitRepository extends AbstractGitRepository
Wrapper around JGit'sRepositoryobject to represent a Git repository- Author:
- Sebastian Staudt
-
-
Field Summary
Fields Modifier and Type Field Description private booleanchecked(package private) static StringCOMMONDIR_FILE(package private) static StringGITDIR_FILE(package private) RevCommitheadCommit(package private) ObjectIdheadObjectprivate static StringINDEX_FILEprivate static intMAX_DESCRIBE_CANDIDATES(package private) static StringREF_LINK_PREFIX(package private) Repositoryrepository-
Fields inherited from class com.github.koraktor.mavanagaiata.git.AbstractGitRepository
headRef
-
-
Constructor Summary
Constructors Constructor Description JGitRepository()Creates a new empty instanceJGitRepository(File workTree, File gitDir, String headRef)Creates a new instance for the given worktree and or Git directory
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) voidbuildRepository(File workTree, File gitDir)voidcheck()Checks whether the Git repository is accessible.voidclose()Closes any resources that are needed to access this repositoryprivate voidcorrectDistance(RevWalk revWalk, JGitTagCandidate candidate, RevFlagSet allFlags)Correct the distance for all tag candidates.(package private) IndexDiffcreateIndexDiff()Creates a new JGitIndexDiffinstance for this repository and worktreeGitTagDescriptiondescribe()Describes the current Git commit likegit describedoesprivate Collection<JGitTagCandidate>findTagCandidates(RevWalk revWalk, Map<String,GitTag> tagCommits, RevFlagSet allFlags)Find up to 10 tag candidates in the current branch.StringgetAbbreviatedCommitId(GitCommit commit)Returns the abbreviated commit SHA ID of the given Git commitStringgetBranch()Returns the currently checked out branch of the Git repositoryJGitCommitgetHeadCommit()Returns the currentHEADcommit of the Git repository(package private) ObjectIdgetHeadObject()Returns the object for the Git ref currently set asHEADStringgetHeadRef()Returns the Git ref used as theHEADcommit of the repository(package private) RevCommitgetHeadRevCommit()Returns a commit object forHEAD(package private) FileRepositoryBuildergetRepositoryBuilder()Creates a new JGitFileRepositoryBuilderinstance(package private) RevWalkgetRevWalk()Map<String,GitTag>getTags()Returns a map of tags available in this repositoryFilegetWorkTree()Returns the worktree of the repositorybooleanisChecked()Returns whether this repository instance has been checkedbooleanisDirty(boolean ignoreUntracked)Returns whether the worktree of the repository is in a clean statebooleanisOnUnbornBranch()Returns whether this repository is currently on an “unborn” branch An “unborn” branch is a branch without any actual commits.voidloadTag(GitTag tag)Load tag meta data<T extends CommitWalkAction>
TwalkCommits(T action)Runs the given action for all commits reachable from the currentHEADcommit-
Methods inherited from class com.github.koraktor.mavanagaiata.git.AbstractGitRepository
getAbbreviatedCommitId, getMailMap, setHeadRef
-
-
-
-
Field Detail
-
MAX_DESCRIBE_CANDIDATES
private static final int MAX_DESCRIBE_CANDIDATES
- See Also:
- Constant Field Values
-
COMMONDIR_FILE
static final String COMMONDIR_FILE
- See Also:
- Constant Field Values
-
GITDIR_FILE
static final String GITDIR_FILE
- See Also:
- Constant Field Values
-
INDEX_FILE
private static final String INDEX_FILE
- See Also:
- Constant Field Values
-
REF_LINK_PREFIX
static final String REF_LINK_PREFIX
- See Also:
- Constant Field Values
-
checked
private boolean checked
-
repository
Repository repository
-
headCommit
RevCommit headCommit
-
headObject
ObjectId headObject
-
-
Constructor Detail
-
JGitRepository
JGitRepository()
Creates a new empty instance
-
JGitRepository
public JGitRepository(File workTree, File gitDir, String headRef) throws GitRepositoryException
Creates a new instance for the given worktree and or Git directory- Parameters:
workTree- The worktree of the repository ornullgitDir- The GIT_DIR of the repository ornullheadRef- The ref to use asHEAD- Throws:
GitRepositoryException- if the parameters do not match a Git repository
-
-
Method Detail
-
buildRepository
final void buildRepository(File workTree, File gitDir) throws GitRepositoryException
- Throws:
GitRepositoryException
-
check
public void check() throws GitRepositoryExceptionDescription copied from interface:GitRepositoryChecks whether the Git repository is accessible.- Throws:
GitRepositoryException- if the repository is not accessible.
-
close
public void close()
Closes any resources that are needed to access this repositoryCloses JGit's repository instance.
- See Also:
Repository.close()
-
describe
public GitTagDescription describe() throws GitRepositoryException
Description copied from interface:GitRepositoryDescribes the current Git commit likegit describedoes- Returns:
- The description of the current
HEADcommit - Throws:
GitRepositoryException- if the description cannot be created
-
getHeadRef
public String getHeadRef()
Description copied from interface:GitRepositoryReturns the Git ref used as theHEADcommit of the repository- Returns:
- The ref used as
HEAD
-
findTagCandidates
private Collection<JGitTagCandidate> findTagCandidates(RevWalk revWalk, Map<String,GitTag> tagCommits, RevFlagSet allFlags) throws RevWalkException
Find up to 10 tag candidates in the current branch. One of these should be the latest tag.- Parameters:
revWalk- Repository informationtagCommits- Map of commits that are associated with a tagallFlags- All flags that have been set so far- Returns:
- A collection of tag candidates
- Throws:
RevWalkException- if there’s an error during the rev walk
-
correctDistance
private void correctDistance(RevWalk revWalk, JGitTagCandidate candidate, RevFlagSet allFlags) throws RevWalkException
Correct the distance for all tag candidates. We have to check all branches to get the correct distance at the end.- Parameters:
revWalk- Repository informationcandidate- Collection of tag candidatesallFlags- All flags that have been set so far- Throws:
RevWalkException- if there’s an error during the rev walk
-
getAbbreviatedCommitId
public String getAbbreviatedCommitId(GitCommit commit) throws GitRepositoryException
Description copied from interface:GitRepositoryReturns the abbreviated commit SHA ID of the given Git commit- Parameters:
commit- The Git commit to get the abbreviated ID for- Returns:
- The abbreviated commit ID of the given commit
- Throws:
GitRepositoryException- if the abbreviated commit ID cannot be determined
-
getBranch
public String getBranch() throws GitRepositoryException
Description copied from interface:GitRepositoryReturns the currently checked out branch of the Git repository- Returns:
- The current branch of the Git repository
- Throws:
GitRepositoryException- if the current branch cannot be determined
-
getHeadCommit
public JGitCommit getHeadCommit() throws GitRepositoryException
Description copied from interface:GitRepositoryReturns the currentHEADcommit of the Git repository- Returns:
- The current commit of the Git Repository
- Throws:
GitRepositoryException- if the current commit cannot be determined
-
createIndexDiff
IndexDiff createIndexDiff() throws GitRepositoryException, IOException
Creates a new JGitIndexDiffinstance for this repository and worktree- Returns:
- A new index diff
- Throws:
GitRepositoryException- if theHEADobject cannot be resolvedIOException- if the index diff cannot be created
-
getRepositoryBuilder
FileRepositoryBuilder getRepositoryBuilder()
Creates a new JGitFileRepositoryBuilderinstance- Returns:
- A new repository builder
-
getTags
public Map<String,GitTag> getTags() throws GitRepositoryException
Description copied from interface:GitRepositoryReturns a map of tags available in this repositoryThe keys of the map are the SHA IDs of the objects referenced by the tags. The map's values are the tags themselves.
Note: Only annotated tags referencing commit objects will be returned.
- Returns:
- A map of tags in this repository
- Throws:
GitRepositoryException- if an error occurs while determining the tags in this repository
-
getWorkTree
public File getWorkTree()
Description copied from interface:GitRepositoryReturns the worktree of the repository- Returns:
- The worktree of the repository
-
isChecked
public boolean isChecked()
Description copied from interface:GitRepositoryReturns whether this repository instance has been checked- Returns:
trueif this repository has already been checked- See Also:
GitRepository.check()
-
isDirty
public boolean isDirty(boolean ignoreUntracked) throws GitRepositoryExceptionDescription copied from interface:GitRepositoryReturns whether the worktree of the repository is in a clean state- Parameters:
ignoreUntracked- Iftrue, untracked files in the repository will be ignored- Returns:
trueif there are modified files in the repository's worktree- Throws:
GitRepositoryException- if an error occurs while checking the worktree state
-
isOnUnbornBranch
public boolean isOnUnbornBranch() throws GitRepositoryExceptionDescription copied from interface:GitRepositoryReturns whether this repository is currently on an “unborn” branch An “unborn” branch is a branch without any actual commits. This only applies when the configured head ref is actuallyHEAD. Otherwise a configuration error is assumed.- Returns:
trueif the current branch is- Throws:
GitRepositoryException- if an error occurs while retrieving the current HEAD commit
-
loadTag
public void loadTag(GitTag tag) throws GitRepositoryException
Description copied from interface:GitRepositoryLoad tag meta data- Parameters:
tag- The tag to load information for- Throws:
GitRepositoryException- if tag metadata cannot be loaded
-
walkCommits
public <T extends CommitWalkAction> T walkCommits(T action) throws GitRepositoryException
Description copied from interface:GitRepositoryRuns the given action for all commits reachable from the currentHEADcommit- Type Parameters:
T- The action’s type- Parameters:
action- The action to execute for each commit found- Returns:
- The action itself
- Throws:
GitRepositoryException- if an error occurs during walking through the commits
-
getHeadRevCommit
RevCommit getHeadRevCommit() throws GitRepositoryException
Returns a commit object forHEAD- Returns:
- The commit object for
HEAD - Throws:
GitRepositoryException- if the commit object cannot be retrieved- See Also:
RevCommit
-
getHeadObject
ObjectId getHeadObject() throws GitRepositoryException
Returns the object for the Git ref currently set asHEAD- Returns:
- The currently selected
HEADobject - Throws:
GitRepositoryException- if the ref cannot be resolved
-
getRevWalk
RevWalk getRevWalk()
- Returns:
- A new JGit
RevWalkinstance for this repository
-
-