~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/NGit/NGit.Merge/Merger.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
This code is derived from jgit (http://eclipse.org/jgit).
 
3
Copyright owners are documented in jgit's IP log.
 
4
 
 
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
 
9
 
 
10
All rights reserved.
 
11
 
 
12
Redistribution and use in source and binary forms, with or
 
13
without modification, are permitted provided that the following
 
14
conditions are met:
 
15
 
 
16
- Redistributions of source code must retain the above copyright
 
17
  notice, this list of conditions and the following disclaimer.
 
18
 
 
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.
 
23
 
 
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
 
27
  written permission.
 
28
 
 
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.
 
42
*/
 
43
 
 
44
using System.IO;
 
45
using NGit;
 
46
using NGit.Errors;
 
47
using NGit.Revwalk;
 
48
using NGit.Revwalk.Filter;
 
49
using NGit.Treewalk;
 
50
using Sharpen;
 
51
 
 
52
namespace NGit.Merge
 
53
{
 
54
        /// <summary>
 
55
        /// Instance of a specific
 
56
        /// <see cref="MergeStrategy">MergeStrategy</see>
 
57
        /// for a single
 
58
        /// <see cref="NGit.Repository">NGit.Repository</see>
 
59
        /// .
 
60
        /// </summary>
 
61
        public abstract class Merger
 
62
        {
 
63
                /// <summary>The repository this merger operates on.</summary>
 
64
                /// <remarks>The repository this merger operates on.</remarks>
 
65
                protected internal readonly Repository db;
 
66
 
 
67
                /// <summary>
 
68
                /// Reader to support
 
69
                /// <see cref="walk">walk</see>
 
70
                /// and other object loading.
 
71
                /// </summary>
 
72
                protected internal readonly ObjectReader reader;
 
73
 
 
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;
 
77
 
 
78
                private ObjectInserter inserter;
 
79
 
 
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;
 
83
 
 
84
                /// <summary>
 
85
                /// If
 
86
                /// <see cref="sourceObjects">sourceObjects</see>
 
87
                /// [i] is a commit, this is the commit.
 
88
                /// </summary>
 
89
                protected internal RevCommit[] sourceCommits;
 
90
 
 
91
                /// <summary>
 
92
                /// The trees matching every entry in
 
93
                /// <see cref="sourceObjects">sourceObjects</see>
 
94
                /// .
 
95
                /// </summary>
 
96
                protected internal RevTree[] sourceTrees;
 
97
 
 
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)
 
102
                {
 
103
                        db = local;
 
104
                        reader = db.NewObjectReader();
 
105
                        walk = new RevWalk(reader);
 
106
                }
 
107
 
 
108
                /// <returns>the repository this merger operates on.</returns>
 
109
                public virtual Repository GetRepository()
 
110
                {
 
111
                        return db;
 
112
                }
 
113
 
 
114
                /// <returns>
 
115
                /// an object writer to create objects in
 
116
                /// <see cref="GetRepository()">GetRepository()</see>
 
117
                /// .
 
118
                /// </returns>
 
119
                public virtual ObjectInserter GetObjectInserter()
 
120
                {
 
121
                        if (inserter == null)
 
122
                        {
 
123
                                inserter = GetRepository().NewObjectInserter();
 
124
                        }
 
125
                        return inserter;
 
126
                }
 
127
 
 
128
                /// <summary>Merge together two or more tree-ish objects.</summary>
 
129
                /// <remarks>
 
130
                /// Merge together two or more tree-ish objects.
 
131
                /// <p>
 
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.
 
134
                /// </remarks>
 
135
                /// <param name="tips">
 
136
                /// source trees to be combined together. The merge base is not
 
137
                /// included in this set.
 
138
                /// </param>
 
139
                /// <returns>
 
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.
 
143
                /// </returns>
 
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.
 
147
                /// </exception>
 
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.
 
151
                /// </exception>
 
152
                public virtual bool Merge(AnyObjectId[] tips)
 
153
                {
 
154
                        sourceObjects = new RevObject[tips.Length];
 
155
                        for (int i = 0; i < tips.Length; i++)
 
156
                        {
 
157
                                sourceObjects[i] = walk.ParseAny(tips[i]);
 
158
                        }
 
159
                        sourceCommits = new RevCommit[sourceObjects.Length];
 
160
                        for (int i_1 = 0; i_1 < sourceObjects.Length; i_1++)
 
161
                        {
 
162
                                try
 
163
                                {
 
164
                                        sourceCommits[i_1] = walk.ParseCommit(sourceObjects[i_1]);
 
165
                                }
 
166
                                catch (IncorrectObjectTypeException)
 
167
                                {
 
168
                                        sourceCommits[i_1] = null;
 
169
                                }
 
170
                        }
 
171
                        sourceTrees = new RevTree[sourceObjects.Length];
 
172
                        for (int i_2 = 0; i_2 < sourceObjects.Length; i_2++)
 
173
                        {
 
174
                                sourceTrees[i_2] = walk.ParseTree(sourceObjects[i_2]);
 
175
                        }
 
176
                        try
 
177
                        {
 
178
                                return MergeImpl();
 
179
                        }
 
180
                        finally
 
181
                        {
 
182
                                if (inserter != null)
 
183
                                {
 
184
                                        inserter.Release();
 
185
                                }
 
186
                                reader.Release();
 
187
                        }
 
188
                }
 
189
 
 
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>
 
195
                /// .
 
196
                /// </param>
 
197
                /// <param name="bIdx">
 
198
                /// index of the second commit in
 
199
                /// <see cref="sourceObjects">sourceObjects</see>
 
200
                /// .
 
201
                /// </param>
 
202
                /// <returns>the new iterator</returns>
 
203
                /// <exception cref="NGit.Errors.IncorrectObjectTypeException">one of the input objects is not a commit.
 
204
                ///     </exception>
 
205
                /// <exception cref="System.IO.IOException">objects are missing or multiple merge bases were found.
 
206
                ///     </exception>
 
207
                protected internal virtual AbstractTreeIterator MergeBase(int aIdx, int bIdx)
 
208
                {
 
209
                        RevCommit @base = GetBaseCommit(aIdx, bIdx);
 
210
                        return (@base == null) ? new EmptyTreeIterator() : OpenTree(@base.Tree);
 
211
                }
 
212
 
 
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>
 
218
                /// .
 
219
                /// </param>
 
220
                /// <param name="bIdx">
 
221
                /// index of the second commit in
 
222
                /// <see cref="sourceObjects">sourceObjects</see>
 
223
                /// .
 
224
                /// </param>
 
225
                /// <returns>the merge base of two commits</returns>
 
226
                /// <exception cref="NGit.Errors.IncorrectObjectTypeException">one of the input objects is not a commit.
 
227
                ///     </exception>
 
228
                /// <exception cref="System.IO.IOException">objects are missing or multiple merge bases were found.
 
229
                ///     </exception>
 
230
                public virtual RevCommit GetBaseCommit(int aIdx, int bIdx)
 
231
                {
 
232
                        if (sourceCommits[aIdx] == null)
 
233
                        {
 
234
                                throw new IncorrectObjectTypeException(sourceObjects[aIdx], Constants.TYPE_COMMIT
 
235
                                        );
 
236
                        }
 
237
                        if (sourceCommits[bIdx] == null)
 
238
                        {
 
239
                                throw new IncorrectObjectTypeException(sourceObjects[bIdx], Constants.TYPE_COMMIT
 
240
                                        );
 
241
                        }
 
242
                        walk.Reset();
 
243
                        walk.SetRevFilter(RevFilter.MERGE_BASE);
 
244
                        walk.MarkStart(sourceCommits[aIdx]);
 
245
                        walk.MarkStart(sourceCommits[bIdx]);
 
246
                        RevCommit @base = walk.Next();
 
247
                        if (@base == null)
 
248
                        {
 
249
                                return null;
 
250
                        }
 
251
                        RevCommit base2 = walk.Next();
 
252
                        if (base2 != null)
 
253
                        {
 
254
                                throw new IOException(MessageFormat.Format(JGitText.Get().multipleMergeBasesFor, 
 
255
                                        sourceCommits[aIdx].Name, sourceCommits[bIdx].Name, @base.Name, base2.Name));
 
256
                        }
 
257
                        return @base;
 
258
                }
 
259
 
 
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.
 
265
                ///     </exception>
 
266
                /// <exception cref="System.IO.IOException">the tree object is not found or cannot be read.
 
267
                ///     </exception>
 
268
                protected internal virtual AbstractTreeIterator OpenTree(AnyObjectId treeId)
 
269
                {
 
270
                        return new CanonicalTreeParser(null, reader, treeId);
 
271
                }
 
272
 
 
273
                /// <summary>Execute the merge.</summary>
 
274
                /// <remarks>
 
275
                /// Execute the merge.
 
276
                /// <p>
 
277
                /// This method is called from
 
278
                /// <see cref="Merge(NGit.AnyObjectId[])">Merge(NGit.AnyObjectId[])</see>
 
279
                /// after the
 
280
                /// <see cref="sourceObjects">sourceObjects</see>
 
281
                /// ,
 
282
                /// <see cref="sourceCommits">sourceCommits</see>
 
283
                /// and
 
284
                /// <see cref="sourceTrees">sourceTrees</see>
 
285
                /// have been populated.
 
286
                /// </remarks>
 
287
                /// <returns>
 
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.
 
291
                /// </returns>
 
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.
 
295
                /// </exception>
 
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.
 
299
                /// </exception>
 
300
                protected internal abstract bool MergeImpl();
 
301
 
 
302
                /// <returns>
 
303
                /// resulting tree, if
 
304
                /// <see cref="Merge(NGit.AnyObjectId[])">Merge(NGit.AnyObjectId[])</see>
 
305
                /// returned true.
 
306
                /// </returns>
 
307
                public abstract ObjectId GetResultTreeId();
 
308
        }
 
309
}