View Javadoc
1   /*
2    * Copyright (C) 2008-2009, Google Inc.
3    * Copyright (C) 2009, Robin Rosenberg <robin.rosenberg@dewire.com> and others
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
10   */
11  
12  package org.eclipse.jgit.merge;
13  
14  import java.io.IOException;
15  
16  import org.eclipse.jgit.lib.Config;
17  import org.eclipse.jgit.lib.ObjectId;
18  import org.eclipse.jgit.lib.ObjectInserter;
19  import org.eclipse.jgit.lib.Repository;
20  
21  /**
22   * Trivial merge strategy to make the resulting tree exactly match an input.
23   * <p>
24   * This strategy can be used to cauterize an entire side branch of history, by
25   * setting the output tree to one of the inputs, and ignoring any of the paths
26   * of the other inputs.
27   */
28  public class StrategyOneSided extends MergeStrategy {
29  	private final String strategyName;
30  
31  	private final int treeIndex;
32  
33  	/**
34  	 * Create a new merge strategy to select a specific input tree.
35  	 *
36  	 * @param name
37  	 *            name of this strategy.
38  	 * @param index
39  	 *            the position of the input tree to accept as the result.
40  	 */
41  	protected StrategyOneSided(String name, int index) {
42  		strategyName = name;
43  		treeIndex = index;
44  	}
45  
46  	/** {@inheritDoc} */
47  	@Override
48  	public String getName() {
49  		return strategyName;
50  	}
51  
52  	/** {@inheritDoc} */
53  	@Override
54  	public Merger newMerger(Repository db) {
55  		return new OneSide(db, treeIndex);
56  	}
57  
58  	/** {@inheritDoc} */
59  	@Override
60  	public Merger newMerger(Repository db, boolean inCore) {
61  		return new OneSide(db, treeIndex);
62  	}
63  
64  	/** {@inheritDoc} */
65  	@Override
66  	public Merger newMerger(ObjectInserter inserter, Config config) {
67  		return new OneSide(inserter, treeIndex);
68  	}
69  
70  	static class OneSide extends Merger {
71  		private final int treeIndex;
72  
73  		protected OneSide(Repository local, int index) {
74  			super(local);
75  			treeIndex = index;
76  		}
77  
78  		protected OneSide(ObjectInserter inserter, int index) {
79  			super(inserter);
80  			treeIndex = index;
81  		}
82  
83  		@Override
84  		protected boolean mergeImpl() throws IOException {
85  			return treeIndex < sourceTrees.length;
86  		}
87  
88  		@Override
89  		public ObjectId getResultTreeId() {
90  			return sourceTrees[treeIndex];
91  		}
92  
93  		@Override
94  		public ObjectId getBaseCommitId() {
95  			return null;
96  		}
97  	}
98  }