View Javadoc
1   /*
2    * Copyright (C) 2008-2009, Google Inc.
3    * and other copyright owners as documented in the project'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  package org.eclipse.jgit.merge;
45  
46  import java.io.IOException;
47  
48  import org.eclipse.jgit.dircache.DirCache;
49  import org.eclipse.jgit.dircache.DirCacheBuilder;
50  import org.eclipse.jgit.dircache.DirCacheEntry;
51  import org.eclipse.jgit.errors.UnmergedPathException;
52  import org.eclipse.jgit.lib.FileMode;
53  import org.eclipse.jgit.lib.ObjectId;
54  import org.eclipse.jgit.lib.ObjectInserter;
55  import org.eclipse.jgit.lib.Repository;
56  import org.eclipse.jgit.treewalk.AbstractTreeIterator;
57  import org.eclipse.jgit.treewalk.NameConflictTreeWalk;
58  
59  /**
60   * Merges two commits together in-memory, ignoring any working directory.
61   * <p>
62   * The strategy chooses a path from one of the two input trees if the path is
63   * unchanged in the other relative to their common merge base tree. This is a
64   * trivial 3-way merge (at the file path level only).
65   * <p>
66   * Modifications of the same file path (content and/or file mode) by both input
67   * trees will cause a merge conflict, as this strategy does not attempt to merge
68   * file contents.
69   */
70  public class StrategySimpleTwoWayInCore extends ThreeWayMergeStrategy {
71  	/** Create a new instance of the strategy. */
72  	protected StrategySimpleTwoWayInCore() {
73  		//
74  	}
75  
76  	@Override
77  	public String getName() {
78  		return "simple-two-way-in-core"; //$NON-NLS-1$
79  	}
80  
81  	@Override
82  	public ThreeWayMerger newMerger(final Repository db) {
83  		return new InCoreMerger(db);
84  	}
85  
86  	@Override
87  	public ThreeWayMerger newMerger(Repository db, boolean inCore) {
88  		// This class is always inCore, so ignore the parameter
89  		return newMerger(db);
90  	}
91  
92  	private static class InCoreMerger extends ThreeWayMerger {
93  		private static final int T_BASE = 0;
94  
95  		private static final int T_OURS = 1;
96  
97  		private static final int T_THEIRS = 2;
98  
99  		private final NameConflictTreeWalk tw;
100 
101 		private final DirCache cache;
102 
103 		private DirCacheBuilder builder;
104 
105 		private ObjectId resultTree;
106 
107 		InCoreMerger(final Repository local) {
108 			super(local);
109 			tw = new NameConflictTreeWalk(local, reader);
110 			cache = DirCache.newInCore();
111 		}
112 
113 		@Override
114 		protected boolean mergeImpl() throws IOException {
115 			tw.addTree(mergeBase());
116 			tw.addTree(sourceTrees[0]);
117 			tw.addTree(sourceTrees[1]);
118 
119 			boolean hasConflict = false;
120 			builder = cache.builder();
121 			while (tw.next()) {
122 				final int modeO = tw.getRawMode(T_OURS);
123 				final int modeT = tw.getRawMode(T_THEIRS);
124 				if (modeO == modeT && tw.idEqual(T_OURS, T_THEIRS)) {
125 					add(T_OURS, DirCacheEntry.STAGE_0);
126 					continue;
127 				}
128 
129 				final int modeB = tw.getRawMode(T_BASE);
130 				if (modeB == modeO && tw.idEqual(T_BASE, T_OURS))
131 					add(T_THEIRS, DirCacheEntry.STAGE_0);
132 				else if (modeB == modeT && tw.idEqual(T_BASE, T_THEIRS))
133 					add(T_OURS, DirCacheEntry.STAGE_0);
134 				else {
135 					if (nonTree(modeB)) {
136 						add(T_BASE, DirCacheEntry.STAGE_1);
137 						hasConflict = true;
138 					}
139 					if (nonTree(modeO)) {
140 						add(T_OURS, DirCacheEntry.STAGE_2);
141 						hasConflict = true;
142 					}
143 					if (nonTree(modeT)) {
144 						add(T_THEIRS, DirCacheEntry.STAGE_3);
145 						hasConflict = true;
146 					}
147 					if (tw.isSubtree())
148 						tw.enterSubtree();
149 				}
150 			}
151 			builder.finish();
152 			builder = null;
153 
154 			if (hasConflict)
155 				return false;
156 			try {
157 				ObjectInserter odi = getObjectInserter();
158 				resultTree = cache.writeTree(odi);
159 				odi.flush();
160 				return true;
161 			} catch (UnmergedPathException upe) {
162 				resultTree = null;
163 				return false;
164 			}
165 		}
166 
167 		private static boolean nonTree(final int mode) {
168 			return mode != 0 && !FileMode.TREE.equals(mode);
169 		}
170 
171 		private void add(final int tree, final int stage) throws IOException {
172 			final AbstractTreeIterator i = getTree(tree);
173 			if (i != null) {
174 				if (FileMode.TREE.equals(tw.getRawMode(tree))) {
175 					builder.addTree(tw.getRawPath(), stage, reader, tw
176 							.getObjectId(tree));
177 				} else {
178 					final DirCacheEntry e;
179 
180 					e = new DirCacheEntry(tw.getRawPath(), stage);
181 					e.setObjectIdFromRaw(i.idBuffer(), i.idOffset());
182 					e.setFileMode(tw.getFileMode(tree));
183 					builder.add(e);
184 				}
185 			}
186 		}
187 
188 		private AbstractTreeIterator getTree(final int tree) {
189 			return tw.getTree(tree, AbstractTreeIterator.class);
190 		}
191 
192 		@Override
193 		public ObjectId getResultTreeId() {
194 			return resultTree;
195 		}
196 	}
197 
198 }