DefaultNoteMerger.java

  1. /*
  2.  * Copyright (C) 2010, Sasa Zivkov <sasa.zivkov@sap.com> and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */

  10. package org.eclipse.jgit.notes;

  11. import java.io.IOException;

  12. import org.eclipse.jgit.lib.Constants;
  13. import org.eclipse.jgit.lib.ObjectId;
  14. import org.eclipse.jgit.lib.ObjectInserter;
  15. import org.eclipse.jgit.lib.ObjectLoader;
  16. import org.eclipse.jgit.lib.ObjectReader;
  17. import org.eclipse.jgit.util.io.UnionInputStream;

  18. /**
  19.  * Default implementation of the {@link org.eclipse.jgit.notes.NoteMerger}.
  20.  * <p>
  21.  * If ours and theirs are both non-null, which means they are either both edits
  22.  * or both adds, then this merger will simply join the content of ours and
  23.  * theirs (in that order) and return that as the merge result.
  24.  * <p>
  25.  * If one or ours/theirs is non-null and the other one is null then the non-null
  26.  * value is returned as the merge result. This means that an edit/delete
  27.  * conflict is resolved by keeping the edit version.
  28.  * <p>
  29.  * If both ours and theirs are null then the result of the merge is also null.
  30.  */
  31. public class DefaultNoteMerger implements NoteMerger {

  32.     /** {@inheritDoc} */
  33.     @Override
  34.     public Note merge(Note base, Note ours, Note theirs, ObjectReader reader,
  35.             ObjectInserter inserter) throws IOException {
  36.         if (ours == null)
  37.             return theirs;

  38.         if (theirs == null)
  39.             return ours;

  40.         if (ours.getData().equals(theirs.getData()))
  41.             return ours;

  42.         ObjectLoader lo = reader.open(ours.getData());
  43.         ObjectLoader lt = reader.open(theirs.getData());
  44.         try (UnionInputStream union = new UnionInputStream(lo.openStream(),
  45.                 lt.openStream())) {
  46.             ObjectId noteData = inserter.insert(Constants.OBJ_BLOB,
  47.                     lo.getSize() + lt.getSize(), union);
  48.             return new Note(ours, noteData);
  49.         }
  50.     }
  51. }