summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit
diff options
context:
space:
mode:
authorDavid Turner <dturner@twosigma.com>2017-10-12 18:43:15 -0400
committerMatthias Sohn <matthias.sohn@sap.com>2018-02-04 15:33:24 +0100
commitbe2a8ed63b74f977f0b78dc5909f94317c7b571e (patch)
tree0bc66b0e58ce3a51d4a66fc2436eec2836102b2a /org.eclipse.jgit
parent595f968b6d4e50f09afab791007be73f14b0e5a9 (diff)
downloadjgit-be2a8ed63b74f977f0b78dc5909f94317c7b571e.tar.gz
jgit-be2a8ed63b74f977f0b78dc5909f94317c7b571e.zip
Basic submodule merge handling
This doesn't handle the really hard thing, which is merging spurious conflicts inside .gitmodules files. That's OK: git.git doesn't either. Users can resolve the conflict themselves and then commit the merge. Previously, jgit would crash when attempting to merge conflicting submodule changes. Even if there was no conflict, after a merge which adds submodules, the repository would have been missing empty directories for newly-added submodules. This patch fixes the crash, and adds the empty directories where necessary. It ensures that the index is in a conflicted state when submodule changes conflict. Reported-by: Alexey Korobkov Bug: 494551 Change-Id: I79db6798c2bdcc1159b5b2589b02da198dc906a1 Signed-off-by: David Turner <dturner@twosigma.com> Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
Diffstat (limited to 'org.eclipse.jgit')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java12
-rwxr-xr-xorg.eclipse.jgit/src/org/eclipse/jgit/merge/ResolveMerger.java43
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleConflict.java77
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/treewalk/NameConflictTreeWalk.java9
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/treewalk/WorkingTreeIterator.java14
5 files changed, 145 insertions, 10 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java b/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java
index ba8bfe3e44..31500f5171 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java
@@ -570,7 +570,9 @@ public class DirCacheCheckout {
String path = e.getKey();
CheckoutMetadata meta = e.getValue();
DirCacheEntry entry = dc.getEntry(path);
- if (!FileMode.GITLINK.equals(entry.getRawMode())) {
+ if (FileMode.GITLINK.equals(entry.getRawMode())) {
+ checkoutGitlink(path, entry);
+ } else {
checkoutEntry(repo, entry, objectReader, false, meta);
}
e = null;
@@ -603,6 +605,14 @@ public class DirCacheCheckout {
return toBeDeleted.size() == 0;
}
+ private void checkoutGitlink(String path, DirCacheEntry entry)
+ throws IOException {
+ File gitlinkDir = new File(repo.getWorkTree(), path);
+ FileUtils.mkdirs(gitlinkDir, true);
+ FS fs = repo.getFS();
+ entry.setLastModified(fs.lastModified(gitlinkDir));
+ }
+
private static ArrayList<String> filterOut(ArrayList<String> strings,
IntList indicesToRemove) {
int n = indicesToRemove.size();
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/merge/ResolveMerger.java b/org.eclipse.jgit/src/org/eclipse/jgit/merge/ResolveMerger.java
index c3b3295c79..a9c139aad5 100755
--- a/org.eclipse.jgit/src/org/eclipse/jgit/merge/ResolveMerger.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/merge/ResolveMerger.java
@@ -96,6 +96,7 @@ import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.storage.pack.PackConfig;
+import org.eclipse.jgit.submodule.SubmoduleConflict;
import org.eclipse.jgit.treewalk.AbstractTreeIterator;
import org.eclipse.jgit.treewalk.CanonicalTreeParser;
import org.eclipse.jgit.treewalk.NameConflictTreeWalk;
@@ -393,8 +394,13 @@ public class ResolveMerger extends ThreeWayMerger {
}
for (Map.Entry<String, DirCacheEntry> entry : toBeCheckedOut
.entrySet()) {
- DirCacheCheckout.checkoutEntry(db, entry.getValue(), reader);
- modifiedFiles.add(entry.getKey());
+ DirCacheEntry cacheEntry = entry.getValue();
+ if (cacheEntry.getFileMode() == FileMode.GITLINK) {
+ new File(nonNullRepo().getWorkTree(), entry.getKey()).mkdirs();
+ } else {
+ DirCacheCheckout.checkoutEntry(db, cacheEntry, reader);
+ modifiedFiles.add(entry.getKey());
+ }
}
}
@@ -725,19 +731,44 @@ public class ResolveMerger extends ThreeWayMerger {
if (nonTree(modeO) && nonTree(modeT)) {
// Check worktree before modifying files
- if (isWorktreeDirty(work, ourDce))
+ boolean worktreeDirty = isWorktreeDirty(work, ourDce);
+ if (!attributes.canBeContentMerged() && worktreeDirty) {
return false;
+ }
+ boolean gitlinkConflict = isGitLink(modeO) || isGitLink(modeT);
// Don't attempt to resolve submodule link conflicts
- if (isGitLink(modeO) || isGitLink(modeT)
- || !attributes.canBeContentMerged()) {
+ if (gitlinkConflict || !attributes.canBeContentMerged()) {
add(tw.getRawPath(), base, DirCacheEntry.STAGE_1, 0, 0);
add(tw.getRawPath(), ours, DirCacheEntry.STAGE_2, 0, 0);
add(tw.getRawPath(), theirs, DirCacheEntry.STAGE_3, 0, 0);
- unmergedPaths.add(tw.getPathString());
+
+ if (gitlinkConflict) {
+ MergeResult<SubmoduleConflict> result = new MergeResult<>(
+ Arrays.asList(
+ new SubmoduleConflict(base == null ? null
+ : base.getEntryObjectId()),
+ new SubmoduleConflict(ours == null ? null
+ : ours.getEntryObjectId()),
+ new SubmoduleConflict(theirs == null ? null
+ : theirs.getEntryObjectId())));
+ result.setContainsConflicts(true);
+ mergeResults.put(tw.getPathString(), result);
+ if (!ignoreConflicts) {
+ unmergedPaths.add(tw.getPathString());
+ }
+ } else {
+ // attribute merge issues are conflicts but not failures
+ unmergedPaths.add(tw.getPathString());
+ }
return true;
}
+ // Check worktree before modifying files
+ if (worktreeDirty) {
+ return false;
+ }
+
MergeResult<RawText> result = contentMerge(base, ours, theirs);
if (ignoreConflicts) {
result.setContainsConflicts(false);
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleConflict.java b/org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleConflict.java
new file mode 100644
index 0000000000..856eb725dd
--- /dev/null
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleConflict.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2017, Two Sigma Open Source
+ * and other copyright owners as documented in the project's IP log.
+ *
+ * This program and the accompanying materials are made available
+ * under the terms of the Eclipse Distribution License v1.0 which
+ * accompanies this distribution, is reproduced below, and is
+ * available at http://www.eclipse.org/org/documents/edl-v10.php
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+*
+ * - Neither the name of the Eclipse Foundation, Inc. nor the
+ * names of its contributors may be used to endorse or promote
+ * products derived from this software without specific prior
+ * written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package org.eclipse.jgit.submodule;
+
+import org.eclipse.jgit.diff.Sequence;
+import org.eclipse.jgit.lib.ObjectId;
+
+/**
+ * Merges expect that conflicts will consist of Sequences, but that doesn't
+ * really make sense for submodules. So this represents a submodule conflict.
+ *
+ * @since 4.11
+ */
+public class SubmoduleConflict extends Sequence {
+ private final ObjectId objectId;
+
+ /**
+ * Create a SubmoduleConflict for the given submodule object id
+ * @param objectId
+ */
+ public SubmoduleConflict(ObjectId objectId) {
+ super();
+ this.objectId = objectId;
+ }
+
+ @Override
+ public int size() {
+ return 1;
+ }
+
+ /**
+ * @return the object id for the conflicting submodule
+ */
+ public ObjectId getObjectId() {
+ return objectId;
+ }
+}
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/NameConflictTreeWalk.java b/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/NameConflictTreeWalk.java
index 3e06f046bf..5560f7750c 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/NameConflictTreeWalk.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/NameConflictTreeWalk.java
@@ -187,7 +187,7 @@ public class NameConflictTreeWalk extends TreeWalk {
//
t.matches = minRef;
} else if (fastMinHasMatch && isTree(t) && !isTree(minRef)
- && nameEqual(t, minRef)) {
+ && !isGitlink(minRef) && nameEqual(t, minRef)) {
// The minimum is a file (non-tree) but the next entry
// of this iterator is a tree whose name matches our file.
// This is a classic D/F conflict and commonly occurs like
@@ -218,6 +218,10 @@ public class NameConflictTreeWalk extends TreeWalk {
return a.pathCompare(b, TREE_MODE) == 0;
}
+ private boolean isGitlink(AbstractTreeIterator p) {
+ return FileMode.GITLINK.equals(p.mode);
+ }
+
private static boolean isTree(final AbstractTreeIterator p) {
return FileMode.TREE.equals(p.mode);
}
@@ -306,8 +310,9 @@ public class NameConflictTreeWalk extends TreeWalk {
if (t.matches == minRef)
t.matches = treeMatch;
- if (dfConflict == null)
+ if (dfConflict == null && !isGitlink(minRef)) {
dfConflict = treeMatch;
+ }
return treeMatch;
}
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/WorkingTreeIterator.java b/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/WorkingTreeIterator.java
index 8d02f90ed1..0b1547535f 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/WorkingTreeIterator.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/WorkingTreeIterator.java
@@ -952,7 +952,19 @@ public abstract class WorkingTreeIterator extends AbstractTreeIterator {
}
return false;
case DIFFER_BY_METADATA:
- if (mode == FileMode.SYMLINK.getBits())
+ if (mode == FileMode.TREE.getBits()
+ && entry.getFileMode().equals(FileMode.GITLINK)) {
+ byte[] idBuffer = idBuffer();
+ int idOffset = idOffset();
+ if (entry.getObjectId().compareTo(idBuffer, idOffset) == 0) {
+ return true;
+ } else if (ObjectId.zeroId().compareTo(idBuffer,
+ idOffset) == 0) {
+ return new File(repository.getWorkTree(),
+ entry.getPathString()).list().length > 0;
+ }
+ return false;
+ } else if (mode == FileMode.SYMLINK.getBits())
return contentCheck(entry, reader);
return true;
default: