Class ResolveMerger
- java.lang.Object
-
- org.eclipse.jgit.merge.Merger
-
- org.eclipse.jgit.merge.ThreeWayMerger
-
- org.eclipse.jgit.merge.ResolveMerger
-
- Direct Known Subclasses:
RecursiveMerger
public class ResolveMerger extends ThreeWayMerger
A three-way merger performing a content-merge if necessary
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ResolveMerger.MergeFailureReason
If the merge fails (means: not stopped because of unresolved conflicts) this enum is used to explain why it failed
-
Field Summary
Fields Modifier and Type Field Description protected String[]
commitNames
string versions of a list of commit SHA1sprotected DirCache
dircache
Directory cacheprotected boolean
enterSubtree
Updated as we merge entries of the tree walk.protected Map<String,ResolveMerger.MergeFailureReason>
failingPaths
Paths for which the merge failed altogether.protected boolean
inCore
Set to true if this merge should work in-memory.protected MergeAlgorithm
mergeAlgorithm
our merge algorithmprotected Map<String,MergeResult<? extends Sequence>>
mergeResults
Low-level textual merge results.protected List<String>
modifiedFiles
Files modified during this operation.protected ObjectId
resultTree
merge result as treeprotected static int
T_BASE
Index of the base tree within thetree walk
.protected static int
T_FILE
Index of the working directory tree within thetree walk
.protected static int
T_INDEX
Index of the index tree within thetree walk
.protected static int
T_OURS
Index of our tree in withthetree walk
.protected static int
T_THEIRS
Index of their tree within thetree walk
.protected NameConflictTreeWalk
tw
The tree walk which we'll iterate over to merge entries.protected List<String>
unmergedPaths
Paths that could not be merged by this merger because of an unsolvable conflict.protected WorkingTreeIterator
workingTreeIterator
The iterator to access the working tree.protected org.eclipse.jgit.merge.WorkTreeUpdater
workTreeUpdater
Handler for repository I/O actions.-
Fields inherited from class org.eclipse.jgit.merge.Merger
db, monitor, reader, sourceCommits, sourceObjects, sourceTrees, walk
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
ResolveMerger(ObjectInserter inserter, Config config)
Constructor for ResolveMerger.protected
ResolveMerger(Repository local)
Constructor for ResolveMerger.protected
ResolveMerger(Repository local, boolean inCore)
Constructor for ResolveMerger.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
addDeletion(String path, boolean isFile, Attributes attributes)
Remember a path for deletion, and remember itsDirCacheCheckout.CheckoutMetadata
in case it has to be restored in the cleanUp.protected void
addToCheckout(String path, DirCacheEntry entry, Attributes[] attributes)
Adds aDirCacheEntry
for direct checkout and remembers itsDirCacheCheckout.CheckoutMetadata
.boolean
failed()
Returns whether this merge failed (i.e.String[]
getCommitNames()
Get the names of the commits as they would appear in conflict markers.ContentMergeStrategy
getContentMergeStrategy()
Retrieves the content merge strategy for content conflicts.Map<String,ResolveMerger.MergeFailureReason>
getFailingPaths()
Get list of paths causing this merge to fail (not stopped because of a conflict).Map<String,MergeResult<? extends Sequence>>
getMergeResults()
Get the mergeResultsList<String>
getModifiedFiles()
Get the paths of files which have been modified by this merge.ObjectId
getResultTreeId()
Get resulting tree.Map<String,DirCacheEntry>
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.List<String>
getUnmergedPaths()
Get the paths with conflicts.protected boolean
mergeImpl()
Execute the merge.protected boolean
mergeTrees(AbstractTreeIterator baseTree, RevTree headTree, RevTree mergeTree, boolean ignoreConflicts)
The resolve conflict way of three way mergingprotected boolean
mergeTreeWalk(TreeWalk treeWalk, boolean ignoreConflicts)
Process the given TreeWalk's entries.protected boolean
processEntry(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.void
setCommitNames(String[] commitNames)
Set the names of the commits as they would appear in conflict markersvoid
setContentMergeStrategy(ContentMergeStrategy strategy)
Sets the content merge strategy for content conflicts.void
setDirCache(DirCache dc)
Sets the DirCache which shall be used by this merger.void
setWorkingTreeIterator(WorkingTreeIterator workingTreeIterator)
Sets the WorkingTreeIterator to be used by this merger.-
Methods inherited from class org.eclipse.jgit.merge.ThreeWayMerger
getBaseCommitId, merge, mergeBase, setBase
-
Methods inherited from class org.eclipse.jgit.merge.Merger
getBaseCommit, getObjectInserter, getRepository, merge, nonNullRepo, openTree, setObjectInserter, setProgressMonitor
-
-
-
-
Field Detail
-
tw
protected NameConflictTreeWalk tw
The tree walk which we'll iterate over to merge entries.- Since:
- 3.4
-
commitNames
protected String[] commitNames
string versions of a list of commit SHA1s- Since:
- 3.0
-
T_BASE
protected static final int T_BASE
Index of the base tree within thetree walk
.- Since:
- 3.4
- See Also:
- Constant Field Values
-
T_OURS
protected static final int T_OURS
Index of our tree in withthetree walk
.- Since:
- 3.4
- See Also:
- Constant Field Values
-
T_THEIRS
protected static final int T_THEIRS
Index of their tree within thetree walk
.- Since:
- 3.4
- See Also:
- Constant Field Values
-
T_INDEX
protected static final int T_INDEX
Index of the index tree within thetree walk
.- Since:
- 3.4
- See Also:
- Constant Field Values
-
T_FILE
protected static final int T_FILE
Index of the working directory tree within thetree walk
.- Since:
- 3.4
- See Also:
- Constant Field Values
-
workTreeUpdater
protected org.eclipse.jgit.merge.WorkTreeUpdater workTreeUpdater
Handler for repository I/O actions.- Since:
- 6.3
-
resultTree
protected ObjectId resultTree
merge result as tree- Since:
- 3.0
-
modifiedFiles
protected List<String> modifiedFiles
Files modified during this operation. Note this list is only updated after a successful write.
-
unmergedPaths
protected List<String> unmergedPaths
Paths that could not be merged by this merger because of an unsolvable conflict.- Since:
- 3.4
-
mergeResults
protected Map<String,MergeResult<? extends Sequence>> mergeResults
Low-level textual merge results. Will be passed on to the callers in case of conflicts.- Since:
- 3.4
-
failingPaths
protected Map<String,ResolveMerger.MergeFailureReason> failingPaths
Paths for which the merge failed altogether.- Since:
- 3.4
-
enterSubtree
protected boolean enterSubtree
Updated 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 inCore
Set 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
-
dircache
protected DirCache dircache
Directory cache- Since:
- 3.0
-
workingTreeIterator
protected WorkingTreeIterator workingTreeIterator
The iterator to access the working tree. If set tonull
this merger will not touch the working tree.- Since:
- 3.0
-
mergeAlgorithm
protected MergeAlgorithm mergeAlgorithm
our merge algorithm- Since:
- 3.0
-
-
Constructor Detail
-
ResolveMerger
protected ResolveMerger(Repository local, boolean inCore)
Constructor for ResolveMerger.- Parameters:
local
- theRepository
.inCore
- a boolean.
-
ResolveMerger
protected ResolveMerger(Repository local)
Constructor for ResolveMerger.- Parameters:
local
- theRepository
.
-
ResolveMerger
protected ResolveMerger(ObjectInserter inserter, Config config)
Constructor for ResolveMerger.- Parameters:
inserter
- anObjectInserter
object.config
- the repository configuration- Since:
- 4.8
-
-
Method Detail
-
getContentMergeStrategy
@NonNull public ContentMergeStrategy getContentMergeStrategy()
Retrieves the content merge strategy for content conflicts.- Returns:
- the
ContentMergeStrategy
in effect - Since:
- 5.12
-
setContentMergeStrategy
public void setContentMergeStrategy(ContentMergeStrategy strategy)
Sets the content merge strategy for content conflicts.- Parameters:
strategy
-ContentMergeStrategy
to use- Since:
- 5.12
-
mergeImpl
protected boolean mergeImpl() throws IOException
Execute the merge.This method is called from
Merger.merge(AnyObjectId[])
after theMerger.sourceObjects
,Merger.sourceCommits
andMerger.sourceTrees
have been populated.- Specified by:
mergeImpl
in 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.
-
addToCheckout
protected void addToCheckout(String path, DirCacheEntry entry, Attributes[] attributes) throws IOException
Adds aDirCacheEntry
for direct checkout and remembers itsDirCacheCheckout.CheckoutMetadata
.- Parameters:
path
- of the entryentry
- to addattributes
- theAttributes
of the trees- Throws:
IOException
- if theDirCacheCheckout.CheckoutMetadata
cannot be determined- Since:
- 6.1
-
addDeletion
protected void addDeletion(String path, boolean isFile, Attributes attributes) throws IOException
Remember a path for deletion, and remember itsDirCacheCheckout.CheckoutMetadata
in case it has to be restored in the cleanUp.- Parameters:
path
- of the entryisFile
- whether it is a fileattributes
- to use for determining theDirCacheCheckout.CheckoutMetadata
- Throws:
IOException
- if theDirCacheCheckout.CheckoutMetadata
cannot be determined- Since:
- 5.1
-
processEntry
protected boolean processEntry(CanonicalTreeParser base, CanonicalTreeParser ours, CanonicalTreeParser theirs, DirCacheBuildIterator index, WorkingTreeIterator work, boolean ignoreConflicts, Attributes[] attributes) throws 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
- theAttributes
for the three trees- Returns:
false
if the merge will fail because the index entry didn't match ours or the working-dir file was dirty and a conflict occurred- Throws:
IOException
- Since:
- 6.1
-
getResultTreeId
public ObjectId getResultTreeId()
Get resulting tree.- Specified by:
getResultTreeId
in classMerger
- Returns:
- resulting tree, if
Merger.merge(AnyObjectId[])
returned true.
-
setCommitNames
public void setCommitNames(String[] commitNames)
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
public String[] 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
public List<String> 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
public List<String> 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
public Map<String,DirCacheEntry> 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
public Map<String,MergeResult<? extends Sequence>> getMergeResults()
Get the mergeResults- Returns:
- the mergeResults
-
getFailingPaths
public Map<String,ResolveMerger.MergeFailureReason> 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).
null
is 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:
true
if a failure occurred,false
otherwise
-
setDirCache
public void setDirCache(DirCache dc)
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
public void setWorkingTreeIterator(WorkingTreeIterator workingTreeIterator)
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
- aAbstractTreeIterator
object.headTree
- aRevTree
object.mergeTree
- aRevTree
object.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
true
is 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 ofRecursiveMerger
where 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
protected boolean mergeTreeWalk(TreeWalk treeWalk, boolean ignoreConflicts) throws IOException
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
-
-