StrategyOneSided.java

  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. package org.eclipse.jgit.merge;

  12. import java.io.IOException;

  13. import org.eclipse.jgit.lib.Config;
  14. import org.eclipse.jgit.lib.ObjectId;
  15. import org.eclipse.jgit.lib.ObjectInserter;
  16. import org.eclipse.jgit.lib.Repository;

  17. /**
  18.  * Trivial merge strategy to make the resulting tree exactly match an input.
  19.  * <p>
  20.  * This strategy can be used to cauterize an entire side branch of history, by
  21.  * setting the output tree to one of the inputs, and ignoring any of the paths
  22.  * of the other inputs.
  23.  */
  24. public class StrategyOneSided extends MergeStrategy {
  25.     private final String strategyName;

  26.     private final int treeIndex;

  27.     /**
  28.      * Create a new merge strategy to select a specific input tree.
  29.      *
  30.      * @param name
  31.      *            name of this strategy.
  32.      * @param index
  33.      *            the position of the input tree to accept as the result.
  34.      */
  35.     protected StrategyOneSided(String name, int index) {
  36.         strategyName = name;
  37.         treeIndex = index;
  38.     }

  39.     /** {@inheritDoc} */
  40.     @Override
  41.     public String getName() {
  42.         return strategyName;
  43.     }

  44.     /** {@inheritDoc} */
  45.     @Override
  46.     public Merger newMerger(Repository db) {
  47.         return new OneSide(db, treeIndex);
  48.     }

  49.     /** {@inheritDoc} */
  50.     @Override
  51.     public Merger newMerger(Repository db, boolean inCore) {
  52.         return new OneSide(db, treeIndex);
  53.     }

  54.     /** {@inheritDoc} */
  55.     @Override
  56.     public Merger newMerger(ObjectInserter inserter, Config config) {
  57.         return new OneSide(inserter, treeIndex);
  58.     }

  59.     static class OneSide extends Merger {
  60.         private final int treeIndex;

  61.         protected OneSide(Repository local, int index) {
  62.             super(local);
  63.             treeIndex = index;
  64.         }

  65.         protected OneSide(ObjectInserter inserter, int index) {
  66.             super(inserter);
  67.             treeIndex = index;
  68.         }

  69.         @Override
  70.         protected boolean mergeImpl() throws IOException {
  71.             return treeIndex < sourceTrees.length;
  72.         }

  73.         @Override
  74.         public ObjectId getResultTreeId() {
  75.             return sourceTrees[treeIndex];
  76.         }

  77.         @Override
  78.         public ObjectId getBaseCommitId() {
  79.             return null;
  80.         }
  81.     }
  82. }