View Javadoc
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  
11  package org.eclipse.jgit.notes;
12  
13  import java.io.IOException;
14  
15  import org.eclipse.jgit.lib.Constants;
16  import org.eclipse.jgit.lib.ObjectId;
17  import org.eclipse.jgit.lib.ObjectInserter;
18  import org.eclipse.jgit.lib.ObjectLoader;
19  import org.eclipse.jgit.lib.ObjectReader;
20  import org.eclipse.jgit.util.io.UnionInputStream;
21  
22  /**
23   * Default implementation of the {@link org.eclipse.jgit.notes.NoteMerger}.
24   * <p>
25   * If ours and theirs are both non-null, which means they are either both edits
26   * or both adds, then this merger will simply join the content of ours and
27   * theirs (in that order) and return that as the merge result.
28   * <p>
29   * If one or ours/theirs is non-null and the other one is null then the non-null
30   * value is returned as the merge result. This means that an edit/delete
31   * conflict is resolved by keeping the edit version.
32   * <p>
33   * If both ours and theirs are null then the result of the merge is also null.
34   */
35  public class DefaultNoteMerger implements NoteMerger {
36  
37  	/** {@inheritDoc} */
38  	@Override
39  	public Note merge(Note base, Note ours, Note theirs, ObjectReader reader,
40  			ObjectInserter inserter) throws IOException {
41  		if (ours == null)
42  			return theirs;
43  
44  		if (theirs == null)
45  			return ours;
46  
47  		if (ours.getData().equals(theirs.getData()))
48  			return ours;
49  
50  		ObjectLoader lo = reader.open(ours.getData());
51  		ObjectLoader lt = reader.open(theirs.getData());
52  		try (UnionInputStream union = new UnionInputStream(lo.openStream(),
53  				lt.openStream())) {
54  			ObjectId noteData = inserter.insert(Constants.OBJ_BLOB,
55  					lo.getSize() + lt.getSize(), union);
56  			return new Note(ours, noteData);
57  		}
58  	}
59  }