Class ResolveMerger
- Direct Known Subclasses:
RecursiveMerger
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumIf the merge fails (means: not stopped because of unresolved conflicts) this enum is used to explain why it failed -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected DirCacheBuilderBuilder to update the cache during this merge.private Map<String,DirCacheCheckout.CheckoutMetadata> KeepsDirCacheCheckout.CheckoutMetadataforcheckout().private Map<String,DirCacheCheckout.CheckoutMetadata> KeepsDirCacheCheckout.CheckoutMetadataforcleanUp().protected String[]string versions of a list of commit SHA1sprivate ContentMergeStrategyTheContentMergeStrategyto use for "resolve" and "recursive" merges.protected DirCacheDirectory cacheprotected booleanUpdated as we merge entries of the tree walk.protected Map<String,ResolveMerger.MergeFailureReason> Paths for which the merge failed altogether.protected booleanSet to true if this merger should use the default dircache of the repository and should handle locking and unlocking of the dircache.protected booleanSet to true if this merge should work in-memory.private intThe size limit (bytes) which controls a file to be stored inHeaporLocalFileduring the merge.protected MergeAlgorithmour merge algorithmprotected Map<String,MergeResult<? extends Sequence>> Low-level textual merge results.Files modified during this merge operation.private static final Attributesprotected ObjectIdmerge result as treeprotected static final intIndex of the base tree within thetree walk.protected static final intIndex of the working directory tree within thetree walk.protected static final intIndex of the index tree within thetree walk.protected static final intIndex of our tree in withthetree walk.protected static final intIndex of their tree within thetree walk.protected Map<String,DirCacheEntry> If the merger has nothing to do for a file but check it out at the end of the operation, it can be added here.Paths in this list will be deleted from the local copy at the end of the operation.protected NameConflictTreeWalkThe tree walk which we'll iterate over to merge entries.Paths that could not be merged by this merger because of an unsolvable conflict.protected WorkingTreeIteratorThe iterator to access the working tree.protected WorkingTreeOptionsTheWorkingTreeOptionsare needed to determine line endings for merged files.Fields inherited from class org.eclipse.jgit.merge.Merger
db, monitor, reader, sourceCommits, sourceObjects, sourceTrees, walk -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedResolveMerger(ObjectInserter inserter, Config config) Constructor for ResolveMerger.protectedResolveMerger(Repository local) Constructor for ResolveMerger.protectedResolveMerger(Repository local, boolean inCore) Constructor for ResolveMerger. -
Method Summary
Modifier and TypeMethodDescriptionprivate DirCacheEntryadd(byte[] path, CanonicalTreeParser p, int stage, Instant lastMod, long len) adds a new path with the specified stage to the index builderprotected voidaddCheckoutMetadata(Map<String, DirCacheCheckout.CheckoutMetadata> map, String path, Attributes attributes) Remembers theDirCacheCheckout.CheckoutMetadatafor the given path; it may be needed incheckout()or incleanUp().protected voidaddDeletion(String path, boolean isFile, Attributes attributes) Remember a path for deletion, and remember itsDirCacheCheckout.CheckoutMetadatain case it has to be restored incleanUp().protected voidaddToCheckout(String path, DirCacheEntry entry, Attributes[] attributes) Adds aDirCacheEntryfor direct checkout and remembers itsDirCacheCheckout.CheckoutMetadata.private voidcheckout()protected voidcleanUp()Reverts the worktree after an unsuccessful merge.private MergeResult<RawText>contentMerge(CanonicalTreeParser base, CanonicalTreeParser ours, CanonicalTreeParser theirs, Attributes[] attributes, ContentMergeStrategy strategy) Does the content merge.private static MergeResult<SubmoduleConflict>createGitLinksMergeResult(CanonicalTreeParser base, CanonicalTreeParser ours, CanonicalTreeParser theirs) private static String[]private TemporaryBufferdoMerge(MergeResult<RawText> result) booleanfailed()Returns whether this merge failed (i.e.String[]Get the names of the commits as they would appear in conflict markers.Retrieves the content merge strategy for content conflicts.Get list of paths causing this merge to fail (not stopped because of a conflict).private static intgetInCoreLimit(Config config) private static MergeAlgorithmgetMergeAlgorithm(Config config) Map<String,MergeResult<? extends Sequence>> Get the mergeResultsGet the paths of files which have been modified by this merge.private RawTextgetRawText(ObjectId id, Attributes attributes) Get resulting tree.Get a map which maps the paths of files which have to be checked out because the merge created new fully-merged content for this file into the index.Get the paths with conflicts.private ObjectIdinsertMergeResult(TemporaryBuffer buf, Attributes attributes) private static booleanisGitLink(int mode) private booleanprivate booleanisWorktreeDirty(WorkingTreeIterator work, DirCacheEntry ourDce) private DirCacheEntryadds a entry to the index builder which is a copy of the specified DirCacheEntryprivate intmergeFileModes(int modeB, int modeO, int modeT) Try to merge filemodes.protected booleanExecute the merge.protected booleanmergeTrees(AbstractTreeIterator baseTree, RevTree headTree, RevTree mergeTree, boolean ignoreConflicts) The resolve conflict way of three way mergingprotected booleanmergeTreeWalk(TreeWalk treeWalk, boolean ignoreConflicts) Process the given TreeWalk's entries.private static booleannonTree(int mode) protected booleanprocessEntry(CanonicalTreeParser base, CanonicalTreeParser ours, CanonicalTreeParser theirs, DirCacheBuildIterator index, WorkingTreeIterator work, boolean ignoreConflicts, Attributes[] attributes) Processes one path and tries to merge taking git attributes in account.voidsetCommitNames(String[] commitNames) Set the names of the commits as they would appear in conflict markersvoidsetContentMergeStrategy(ContentMergeStrategy strategy) Sets the content merge strategy for content conflicts.voidsetDirCache(DirCache dc) Sets the DirCache which shall be used by this merger.voidsetWorkingTreeIterator(WorkingTreeIterator workingTreeIterator) Sets the WorkingTreeIterator to be used by this merger.private voidupdateIndex(CanonicalTreeParser base, CanonicalTreeParser ours, CanonicalTreeParser theirs, MergeResult<RawText> result, Attributes attributes) Updates the index after a content merge has happened.private FilewriteMergedFile(TemporaryBuffer rawMerged, Attributes attributes) Writes merged file content to the working tree.Methods inherited from class org.eclipse.jgit.merge.ThreeWayMerger
getBaseCommitId, merge, mergeBase, setBaseMethods inherited from class org.eclipse.jgit.merge.Merger
getBaseCommit, getObjectInserter, getRepository, merge, nonNullRepo, openTree, setObjectInserter, setProgressMonitor
-
Field Details
-
tw
The tree walk which we'll iterate over to merge entries.- Since:
- 3.4
-
commitNames
string versions of a list of commit SHA1s- Since:
- 3.0
-
T_BASE
protected static final int T_BASEIndex of the base tree within thetree walk.- Since:
- 3.4
- See Also:
-
T_OURS
protected static final int T_OURSIndex of our tree in withthetree walk.- Since:
- 3.4
- See Also:
-
T_THEIRS
protected static final int T_THEIRSIndex of their tree within thetree walk.- Since:
- 3.4
- See Also:
-
T_INDEX
protected static final int T_INDEXIndex of the index tree within thetree walk.- Since:
- 3.4
- See Also:
-
T_FILE
protected static final int T_FILEIndex of the working directory tree within thetree walk.- Since:
- 3.4
- See Also:
-
builder
Builder to update the cache during this merge.- Since:
- 3.4
-
resultTree
merge result as tree- Since:
- 3.0
-
unmergedPaths
Paths that could not be merged by this merger because of an unsolvable conflict.- Since:
- 3.4
-
modifiedFiles
Files modified during this merge operation.- Since:
- 3.4
-
toBeCheckedOut
If the merger has nothing to do for a file but check it out at the end of the operation, it can be added here.- Since:
- 3.4
-
toBeDeleted
Paths in this list will be deleted from the local copy at the end of the operation.- Since:
- 3.4
-
mergeResults
Low-level textual merge results. Will be passed on to the callers in case of conflicts.- Since:
- 3.4
-
failingPaths
Paths for which the merge failed altogether.- Since:
- 3.4
-
enterSubtree
protected boolean enterSubtreeUpdated as we merge entries of the tree walk. Tells us whether we should recurse into the entry if it is a subtree.- Since:
- 3.4
-
inCore
protected boolean inCoreSet to true if this merge should work in-memory. The repos dircache and workingtree are not touched by this method. Eventually needed files are created as temporary files and a new empty, in-memory dircache will be used instead the repo's one. Often used for bare repos where the repo doesn't even have a workingtree and dircache.- Since:
- 3.0
-
implicitDirCache
protected boolean implicitDirCacheSet to true if this merger should use the default dircache of the repository and should handle locking and unlocking of the dircache. If this merger should work in-core or if an explicit dircache was specified during construction then this field is set to false.- Since:
- 3.0
-
dircache
Directory cache- Since:
- 3.0
-
workingTreeIterator
The iterator to access the working tree. If set tonullthis merger will not touch the working tree.- Since:
- 3.0
-
mergeAlgorithm
our merge algorithm- Since:
- 3.0
-
workingTreeOptions
TheWorkingTreeOptionsare needed to determine line endings for merged files.- Since:
- 4.11
-
inCoreLimit
private int inCoreLimitThe size limit (bytes) which controls a file to be stored inHeaporLocalFileduring the merge. -
contentStrategy
TheContentMergeStrategyto use for "resolve" and "recursive" merges. -
checkoutMetadata
KeepsDirCacheCheckout.CheckoutMetadataforcheckout(). -
cleanupMetadata
KeepsDirCacheCheckout.CheckoutMetadataforcleanUp(). -
NO_ATTRIBUTES
-
-
Constructor Details
-
ResolveMerger
Constructor for ResolveMerger.- Parameters:
local- theRepository.inCore- a boolean.
-
ResolveMerger
Constructor for ResolveMerger.- Parameters:
local- theRepository.
-
ResolveMerger
Constructor for ResolveMerger.- Parameters:
inserter- anObjectInserterobject.config- the repository configuration- Since:
- 4.8
-
-
Method Details
-
getMergeAlgorithm
-
getInCoreLimit
-
defaultCommitNames
-
getContentMergeStrategy
Retrieves the content merge strategy for content conflicts.- Returns:
- the
ContentMergeStrategyin effect - Since:
- 5.12
-
setContentMergeStrategy
Sets the content merge strategy for content conflicts.- Parameters:
strategy-ContentMergeStrategyto use- Since:
- 5.12
-
mergeImpl
Execute the merge.This method is called from
Merger.merge(AnyObjectId[])after theMerger.sourceObjects,Merger.sourceCommitsandMerger.sourceTreeshave been populated.- Specified by:
mergeImplin classMerger- Returns:
- true if the merge was completed without conflicts; false if the merge strategy cannot handle this merge or there were conflicts preventing it from automatically resolving all paths.
- Throws:
IncorrectObjectTypeException- one of the input objects is not a commit, but the strategy requires it to be a commit.IOException- one or more sources could not be read, or outputs could not be written to the Repository.
-
checkout
- Throws:
NoWorkTreeExceptionIOException
-
cleanUp
Reverts the worktree after an unsuccessful merge. We know that for all modified files the old content was in the old index and the index contained only stage 0. In case if inCore operation just clear the history of modified files.- Throws:
IOExceptionCorruptObjectExceptionNoWorkTreeException- Since:
- 3.4
-
add
adds a new path with the specified stage to the index builder- Parameters:
path-p-stage-lastMod-len-- Returns:
- the entry which was added to the index
-
keep
adds a entry to the index builder which is a copy of the specified DirCacheEntry- Parameters:
e- the entry which should be copied- Returns:
- the entry which was added to the index
-
addCheckoutMetadata
protected void addCheckoutMetadata(Map<String, DirCacheCheckout.CheckoutMetadata> map, String path, Attributes attributes) throws IOExceptionRemembers theDirCacheCheckout.CheckoutMetadatafor the given path; it may be needed incheckout()or incleanUp().- Parameters:
map- to add the metadata topath- of the current nodeattributes- to use for determining the metadata- Throws:
IOException- if the smudge filter cannot be determined- Since:
- 6.1
-
addToCheckout
protected void addToCheckout(String path, DirCacheEntry entry, Attributes[] attributes) throws IOException Adds aDirCacheEntryfor direct checkout and remembers itsDirCacheCheckout.CheckoutMetadata.- Parameters:
path- of the entryentry- to addattributes- theAttributesof the trees- Throws:
IOException- if theDirCacheCheckout.CheckoutMetadatacannot be determined- Since:
- 6.1
-
addDeletion
Remember a path for deletion, and remember itsDirCacheCheckout.CheckoutMetadatain case it has to be restored incleanUp().- Parameters:
path- of the entryisFile- whether it is a fileattributes- to use for determining theDirCacheCheckout.CheckoutMetadata- Throws:
IOException- if theDirCacheCheckout.CheckoutMetadatacannot be determined- Since:
- 5.1
-
processEntry
protected boolean processEntry(CanonicalTreeParser base, CanonicalTreeParser ours, CanonicalTreeParser theirs, DirCacheBuildIterator index, WorkingTreeIterator work, boolean ignoreConflicts, Attributes[] attributes) throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException Processes one path and tries to merge taking git attributes in account. This method will do all trivial (not content) merges and will also detect if a merge will fail. The merge will fail when one of the following is true- the index entry does not match the entry in ours. When merging one branch into the current HEAD, ours will point to HEAD and theirs will point to the other branch. It is assumed that the index matches the HEAD because it will only not match HEAD if it was populated before the merge operation. But the merge commit should not accidentally contain modifications done before the merge. Check the git read-tree documentation for further explanations.
- A conflict was detected and the working-tree file is dirty. When a conflict is detected the content-merge algorithm will try to write a merged version into the working-tree. If the file is dirty we would override unsaved data.
- Parameters:
base- the common base for ours and theirsours- the ours side of the merge. When merging a branch into the HEAD ours will point to HEADtheirs- the theirs side of the merge. When merging a branch into the current HEAD theirs will point to the branch which is merged into HEAD.index- the index entrywork- the file in the working treeignoreConflicts- seemergeTrees(AbstractTreeIterator, RevTree, RevTree, boolean)attributes- theAttributesfor the three trees- Returns:
falseif the merge will fail because the index entry didn't match ours or the working-dir file was dirty and a conflict occurred- Throws:
MissingObjectExceptionIncorrectObjectTypeExceptionCorruptObjectExceptionIOException- Since:
- 6.1
-
createGitLinksMergeResult
private static MergeResult<SubmoduleConflict> createGitLinksMergeResult(CanonicalTreeParser base, CanonicalTreeParser ours, CanonicalTreeParser theirs) -
contentMerge
private MergeResult<RawText> contentMerge(CanonicalTreeParser base, CanonicalTreeParser ours, CanonicalTreeParser theirs, Attributes[] attributes, ContentMergeStrategy strategy) throws BinaryBlobException, IOException Does the content merge. The three texts base, ours and theirs are specified withCanonicalTreeParser. If any of the parsers is specified asnullthen an empty text will be used instead.- Parameters:
base-ours-theirs-attributes-strategy-- Returns:
- the result of the content merge
- Throws:
BinaryBlobException- if any of the blobs looks like a binary blobIOException
-
isIndexDirty
private boolean isIndexDirty() -
isWorktreeDirty
- Throws:
IOException
-
updateIndex
private void updateIndex(CanonicalTreeParser base, CanonicalTreeParser ours, CanonicalTreeParser theirs, MergeResult<RawText> result, Attributes attributes) throws FileNotFoundException, IOException Updates the index after a content merge has happened. If no conflict has occurred this includes persisting the merged content to the object database. In case of conflicts this method takes care to write the correct stages to the index.- Parameters:
base-ours-theirs-result-attributes-- Throws:
FileNotFoundExceptionIOException
-
writeMergedFile
private File writeMergedFile(TemporaryBuffer rawMerged, Attributes attributes) throws FileNotFoundException, IOException Writes merged file content to the working tree.- Parameters:
rawMerged- the raw merged contentattributes- the files .gitattributes entries- Returns:
- the working tree file to which the merged content was written.
- Throws:
FileNotFoundExceptionIOException
-
doMerge
- Throws:
IOException
-
insertMergeResult
- Throws:
IOException
-
mergeFileModes
private int mergeFileModes(int modeB, int modeO, int modeT) Try to merge filemodes. If only ours or theirs have changed the mode (compared to base) we choose that one. If ours and theirs have equal modes return that one. If also that is not the case the modes are not mergeable. ReturnFileMode.MISSINGint that case.- Parameters:
modeB- filemode found in BASEmodeO- filemode found in OURSmodeT- filemode found in THEIRS- Returns:
- the merged filemode or
FileMode.MISSINGin case of a conflict
-
getRawText
private RawText getRawText(ObjectId id, Attributes attributes) throws IOException, BinaryBlobException - Throws:
IOExceptionBinaryBlobException
-
nonTree
private static boolean nonTree(int mode) -
isGitLink
private static boolean isGitLink(int mode) -
getResultTreeId
Get resulting tree.- Specified by:
getResultTreeIdin classMerger- Returns:
- resulting tree, if
Merger.merge(AnyObjectId[])returned true.
-
setCommitNames
Set the names of the commits as they would appear in conflict markers- Parameters:
commitNames- the names of the commits as they would appear in conflict markers
-
getCommitNames
Get the names of the commits as they would appear in conflict markers.- Returns:
- the names of the commits as they would appear in conflict markers.
-
getUnmergedPaths
Get the paths with conflicts. This is a subset of the files listed bygetModifiedFiles()- Returns:
- the paths with conflicts. This is a subset of the files listed by
getModifiedFiles()
-
getModifiedFiles
Get the paths of files which have been modified by this merge.- Returns:
- the paths of files which have been modified by this merge. A file
will be modified if a content-merge works on this path or if the
merge algorithm decides to take the theirs-version. This is a
superset of the files listed by
getUnmergedPaths().
-
getToBeCheckedOut
Get a map which maps the paths of files which have to be checked out because the merge created new fully-merged content for this file into the index.- Returns:
- a map which maps the paths of files which have to be checked out because the merge created new fully-merged content for this file into the index. This means: the merge wrote a new stage 0 entry for this path.
-
getMergeResults
Get the mergeResults- Returns:
- the mergeResults
-
getFailingPaths
Get list of paths causing this merge to fail (not stopped because of a conflict).- Returns:
- lists paths causing this merge to fail (not stopped because of a
conflict).
nullis returned if this merge didn't fail.
-
failed
public boolean failed()Returns whether this merge failed (i.e. not stopped because of a conflict)- Returns:
trueif a failure occurred,falseotherwise
-
setDirCache
Sets the DirCache which shall be used by this merger. If the DirCache is not set explicitly and if this merger doesn't work in-core, this merger will implicitly get and lock a default DirCache. If the DirCache is explicitly set the caller is responsible to lock it in advance. Finally the merger will callDirCache.commit()which requires that the DirCache is locked. If themergeImpl()returns without throwing an exception the lock will be released. In case of exceptions the caller is responsible to release the lock.- Parameters:
dc- the DirCache to set
-
setWorkingTreeIterator
Sets the WorkingTreeIterator to be used by this merger. If no WorkingTreeIterator is set this merger will ignore the working tree and fail if a content merge is necessary.TODO: enhance WorkingTreeIterator to support write operations. Then this merger will be able to merge with a different working tree abstraction.
- Parameters:
workingTreeIterator- the workingTreeIt to set
-
mergeTrees
protected boolean mergeTrees(AbstractTreeIterator baseTree, RevTree headTree, RevTree mergeTree, boolean ignoreConflicts) throws IOException The resolve conflict way of three way merging- Parameters:
baseTree- aAbstractTreeIteratorobject.headTree- aRevTreeobject.mergeTree- aRevTreeobject.ignoreConflicts- Controls what to do in case a content-merge is done and a conflict is detected. The default setting for this should befalse. In this case the working tree file is filled with new content (containing conflict markers) and the index is filled with multiple stages containing BASE, OURS and THEIRS content. Having such non-0 stages is the sign to git tools that there are still conflicts for that path.If
trueis specified the behavior is different. In case a conflict is detected the working tree file is again filled with new content (containing conflict markers). But also stage 0 of the index is filled with that content. No other stages are filled. Means: there is no conflict on that path but the new content (including conflict markers) is stored as successful merge result. This is needed in the context ofRecursiveMergerwhere when determining merge bases we don't want to deal with content-merge conflicts.- Returns:
- whether the trees merged cleanly
- Throws:
IOException- Since:
- 3.5
-
mergeTreeWalk
Process the given TreeWalk's entries.- Parameters:
treeWalk- The walk to iterate over.ignoreConflicts- seemergeTrees(AbstractTreeIterator, RevTree, RevTree, boolean)- Returns:
- Whether the trees merged cleanly.
- Throws:
IOException- Since:
- 3.5
-