2
This code is derived from jgit (http://eclipse.org/jgit).
3
Copyright owners are documented in jgit's IP log.
5
This program and the accompanying materials are made available
6
under the terms of the Eclipse Distribution License v1.0 which
7
accompanies this distribution, is reproduced below, and is
8
available at http://www.eclipse.org/org/documents/edl-v10.php
12
Redistribution and use in source and binary forms, with or
13
without modification, are permitted provided that the following
16
- Redistributions of source code must retain the above copyright
17
notice, this list of conditions and the following disclaimer.
19
- Redistributions in binary form must reproduce the above
20
copyright notice, this list of conditions and the following
21
disclaimer in the documentation and/or other materials provided
22
with the distribution.
24
- Neither the name of the Eclipse Foundation, Inc. nor the
25
names of its contributors may be used to endorse or promote
26
products derived from this software without specific prior
29
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48
using NGit.Revwalk.Filter;
55
/// Instance of a specific
56
/// <see cref="MergeStrategy">MergeStrategy</see>
58
/// <see cref="NGit.Repository">NGit.Repository</see>
61
public abstract class Merger
63
/// <summary>The repository this merger operates on.</summary>
64
/// <remarks>The repository this merger operates on.</remarks>
65
protected internal readonly Repository db;
69
/// <see cref="walk">walk</see>
70
/// and other object loading.
72
protected internal readonly ObjectReader reader;
74
/// <summary>A RevWalk for computing merge bases, or listing incoming commits.</summary>
75
/// <remarks>A RevWalk for computing merge bases, or listing incoming commits.</remarks>
76
protected internal readonly RevWalk walk;
78
private ObjectInserter inserter;
80
/// <summary>The original objects supplied in the merge; this can be any tree-ish.</summary>
81
/// <remarks>The original objects supplied in the merge; this can be any tree-ish.</remarks>
82
protected internal RevObject[] sourceObjects;
86
/// <see cref="sourceObjects">sourceObjects</see>
87
/// [i] is a commit, this is the commit.
89
protected internal RevCommit[] sourceCommits;
92
/// The trees matching every entry in
93
/// <see cref="sourceObjects">sourceObjects</see>
96
protected internal RevTree[] sourceTrees;
98
/// <summary>Create a new merge instance for a repository.</summary>
99
/// <remarks>Create a new merge instance for a repository.</remarks>
100
/// <param name="local">the repository this merger will read and write data on.</param>
101
protected internal Merger(Repository local)
104
reader = db.NewObjectReader();
105
walk = new RevWalk(reader);
108
/// <returns>the repository this merger operates on.</returns>
109
public virtual Repository GetRepository()
115
/// an object writer to create objects in
116
/// <see cref="GetRepository()">GetRepository()</see>
119
public virtual ObjectInserter GetObjectInserter()
121
if (inserter == null)
123
inserter = GetRepository().NewObjectInserter();
128
/// <summary>Merge together two or more tree-ish objects.</summary>
130
/// Merge together two or more tree-ish objects.
132
/// Any tree-ish may be supplied as inputs. Commits and/or tags pointing at
133
/// trees or commits may be passed as input objects.
135
/// <param name="tips">
136
/// source trees to be combined together. The merge base is not
137
/// included in this set.
140
/// true if the merge was completed without conflicts; false if the
141
/// merge strategy cannot handle this merge or there were conflicts
142
/// preventing it from automatically resolving all paths.
144
/// <exception cref="NGit.Errors.IncorrectObjectTypeException">
145
/// one of the input objects is not a commit, but the strategy
146
/// requires it to be a commit.
148
/// <exception cref="System.IO.IOException">
149
/// one or more sources could not be read, or outputs could not
150
/// be written to the Repository.
152
public virtual bool Merge(AnyObjectId[] tips)
154
sourceObjects = new RevObject[tips.Length];
155
for (int i = 0; i < tips.Length; i++)
157
sourceObjects[i] = walk.ParseAny(tips[i]);
159
sourceCommits = new RevCommit[sourceObjects.Length];
160
for (int i_1 = 0; i_1 < sourceObjects.Length; i_1++)
164
sourceCommits[i_1] = walk.ParseCommit(sourceObjects[i_1]);
166
catch (IncorrectObjectTypeException)
168
sourceCommits[i_1] = null;
171
sourceTrees = new RevTree[sourceObjects.Length];
172
for (int i_2 = 0; i_2 < sourceObjects.Length; i_2++)
174
sourceTrees[i_2] = walk.ParseTree(sourceObjects[i_2]);
182
if (inserter != null)
190
/// <summary>Create an iterator to walk the merge base of two commits.</summary>
191
/// <remarks>Create an iterator to walk the merge base of two commits.</remarks>
192
/// <param name="aIdx">
193
/// index of the first commit in
194
/// <see cref="sourceObjects">sourceObjects</see>
197
/// <param name="bIdx">
198
/// index of the second commit in
199
/// <see cref="sourceObjects">sourceObjects</see>
202
/// <returns>the new iterator</returns>
203
/// <exception cref="NGit.Errors.IncorrectObjectTypeException">one of the input objects is not a commit.
205
/// <exception cref="System.IO.IOException">objects are missing or multiple merge bases were found.
207
protected internal virtual AbstractTreeIterator MergeBase(int aIdx, int bIdx)
209
RevCommit @base = GetBaseCommit(aIdx, bIdx);
210
return (@base == null) ? new EmptyTreeIterator() : OpenTree(@base.Tree);
213
/// <summary>Return the merge base of two commits.</summary>
214
/// <remarks>Return the merge base of two commits.</remarks>
215
/// <param name="aIdx">
216
/// index of the first commit in
217
/// <see cref="sourceObjects">sourceObjects</see>
220
/// <param name="bIdx">
221
/// index of the second commit in
222
/// <see cref="sourceObjects">sourceObjects</see>
225
/// <returns>the merge base of two commits</returns>
226
/// <exception cref="NGit.Errors.IncorrectObjectTypeException">one of the input objects is not a commit.
228
/// <exception cref="System.IO.IOException">objects are missing or multiple merge bases were found.
230
public virtual RevCommit GetBaseCommit(int aIdx, int bIdx)
232
if (sourceCommits[aIdx] == null)
234
throw new IncorrectObjectTypeException(sourceObjects[aIdx], Constants.TYPE_COMMIT
237
if (sourceCommits[bIdx] == null)
239
throw new IncorrectObjectTypeException(sourceObjects[bIdx], Constants.TYPE_COMMIT
243
walk.SetRevFilter(RevFilter.MERGE_BASE);
244
walk.MarkStart(sourceCommits[aIdx]);
245
walk.MarkStart(sourceCommits[bIdx]);
246
RevCommit @base = walk.Next();
251
RevCommit base2 = walk.Next();
254
throw new IOException(MessageFormat.Format(JGitText.Get().multipleMergeBasesFor,
255
sourceCommits[aIdx].Name, sourceCommits[bIdx].Name, @base.Name, base2.Name));
260
/// <summary>Open an iterator over a tree.</summary>
261
/// <remarks>Open an iterator over a tree.</remarks>
262
/// <param name="treeId">the tree to scan; must be a tree (not a treeish).</param>
263
/// <returns>an iterator for the tree.</returns>
264
/// <exception cref="NGit.Errors.IncorrectObjectTypeException">the input object is not a tree.
266
/// <exception cref="System.IO.IOException">the tree object is not found or cannot be read.
268
protected internal virtual AbstractTreeIterator OpenTree(AnyObjectId treeId)
270
return new CanonicalTreeParser(null, reader, treeId);
273
/// <summary>Execute the merge.</summary>
275
/// Execute the merge.
277
/// This method is called from
278
/// <see cref="Merge(NGit.AnyObjectId[])">Merge(NGit.AnyObjectId[])</see>
280
/// <see cref="sourceObjects">sourceObjects</see>
282
/// <see cref="sourceCommits">sourceCommits</see>
284
/// <see cref="sourceTrees">sourceTrees</see>
285
/// have been populated.
288
/// true if the merge was completed without conflicts; false if the
289
/// merge strategy cannot handle this merge or there were conflicts
290
/// preventing it from automatically resolving all paths.
292
/// <exception cref="NGit.Errors.IncorrectObjectTypeException">
293
/// one of the input objects is not a commit, but the strategy
294
/// requires it to be a commit.
296
/// <exception cref="System.IO.IOException">
297
/// one or more sources could not be read, or outputs could not
298
/// be written to the Repository.
300
protected internal abstract bool MergeImpl();
303
/// resulting tree, if
304
/// <see cref="Merge(NGit.AnyObjectId[])">Merge(NGit.AnyObjectId[])</see>
307
public abstract ObjectId GetResultTreeId();