summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test
diff options
context:
space:
mode:
authorMatthias Sohn <matthias.sohn@sap.com>2022-03-08 17:23:57 +0100
committerMatthias Sohn <matthias.sohn@sap.com>2022-03-09 21:45:35 +0100
commit338f2adf160fdd72ee137e416174526d5c35eb18 (patch)
tree2d55def305066a654c46e335ed840bda6fef1bea /org.eclipse.jgit.test
parent600e96dfdc6769b7da6e2b2daea74cf69791da7a (diff)
parent8f7ef245f0c156730ef74ff3fe838c330728fc7f (diff)
downloadjgit-338f2adf160fdd72ee137e416174526d5c35eb18.tar.gz
jgit-338f2adf160fdd72ee137e416174526d5c35eb18.zip
Merge branch 'stable-6.1'
* stable-6.1: Prepare 6.1.1-SNAPSHOT builds JGit v6.1.0.202203080745-r [checkout] Use .gitattributes from the commit to be checked out Don't use final for method parameters [push] support the "matching" RefSpecs ":" and "+:" [push] Call the pre-push hook later in the push process IndexDiff: use tree filter also for SubmoduleWalk Run license check with option -Ddash.projectId=technology.jgit Exclude transitive dependencies of sshd-sftp Update DEPENDENCIES for 6.1.0 release Add dependency to dash-licenses Fix typos of some keys in LfsText Sort LfsText entries alphabetically Support for "lfs.url" from ".lfsconfig" Change-Id: I1b9f0c0ed647837e00b9640d235dbfab2329c5a6
Diffstat (limited to 'org.eclipse.jgit.test')
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PushCommandTest.java57
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StatusCommandTest.java29
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PushProcessTest.java71
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/RefSpecTest.java14
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FilterCommandsTest.java144
5 files changed, 297 insertions, 18 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PushCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PushCommandTest.java
index 1a1f5b487e..6f7aa63edc 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PushCommandTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PushCommandTest.java
@@ -10,6 +10,7 @@
package org.eclipse.jgit.api;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
@@ -392,28 +393,64 @@ public class PushCommandTest extends RepositoryTestCase {
git.add().addFilepattern("f").call();
RevCommit commit = git.commit().setMessage("adding f").call();
- git.checkout().setName("also-pushed").setCreateBranch(true).call();
+ git.checkout().setName("not-pushed").setCreateBranch(true).call();
git.checkout().setName("branchtopush").setCreateBranch(true).call();
assertEquals(null,
git2.getRepository().resolve("refs/heads/branchtopush"));
assertEquals(null,
- git2.getRepository().resolve("refs/heads/also-pushed"));
+ git2.getRepository().resolve("refs/heads/not-pushed"));
assertEquals(null,
git2.getRepository().resolve("refs/heads/master"));
- git.push().setRemote("test").setPushDefault(PushDefault.MATCHING)
+ // push master and branchtopush
+ git.push().setRemote("test").setRefSpecs(
+ new RefSpec("refs/heads/master:refs/heads/master"),
+ new RefSpec(
+ "refs/heads/branchtopush:refs/heads/branchtopush"))
.call();
assertEquals(commit.getId(),
- git2.getRepository().resolve("refs/heads/branchtopush"));
- assertEquals(commit.getId(),
- git2.getRepository().resolve("refs/heads/also-pushed"));
+ git2.getRepository().resolve("refs/heads/master"));
assertEquals(commit.getId(),
+ git2.getRepository().resolve("refs/heads/branchtopush"));
+ assertEquals(null,
+ git2.getRepository().resolve("refs/heads/not-pushed"));
+ // Create two different commits on these two branches
+ writeTrashFile("b", "on branchtopush");
+ git.add().addFilepattern("b").call();
+ RevCommit bCommit = git.commit().setMessage("on branchtopush")
+ .call();
+ git.checkout().setName("master").call();
+ writeTrashFile("m", "on master");
+ git.add().addFilepattern("m").call();
+ RevCommit mCommit = git.commit().setMessage("on master").call();
+ // Now push with mode "matching": should push both branches.
+ Iterable<PushResult> result = git.push().setRemote("test")
+ .setPushDefault(PushDefault.MATCHING)
+ .call();
+ int n = 0;
+ for (PushResult r : result) {
+ n++;
+ assertEquals(1, n);
+ assertEquals(2, r.getRemoteUpdates().size());
+ for (RemoteRefUpdate update : r.getRemoteUpdates()) {
+ assertFalse(update.isMatching());
+ assertTrue(update.getSrcRef()
+ .equals("refs/heads/branchtopush")
+ || update.getSrcRef().equals("refs/heads/master"));
+ assertEquals(RemoteRefUpdate.Status.OK, update.getStatus());
+ }
+ }
+ assertEquals(bCommit.getId(),
+ git2.getRepository().resolve("refs/heads/branchtopush"));
+ assertEquals(null,
+ git2.getRepository().resolve("refs/heads/not-pushed"));
+ assertEquals(mCommit.getId(),
git2.getRepository().resolve("refs/heads/master"));
- assertEquals(commit.getId(), git.getRepository()
+ assertEquals(bCommit.getId(), git.getRepository()
.resolve("refs/remotes/origin/branchtopush"));
- assertEquals(commit.getId(), git.getRepository()
- .resolve("refs/remotes/origin/also-pushed"));
- assertEquals(commit.getId(),
+ assertEquals(null, git.getRepository()
+ .resolve("refs/remotes/origin/not-pushed"));
+ assertEquals(mCommit.getId(),
git.getRepository().resolve("refs/remotes/origin/master"));
}
}
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StatusCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StatusCommandTest.java
index 5311edb0eb..19281f6c99 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StatusCommandTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StatusCommandTest.java
@@ -21,8 +21,10 @@ import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.NoFilepatternException;
import org.eclipse.jgit.errors.NoWorkTreeException;
import org.eclipse.jgit.junit.RepositoryTestCase;
+import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.Sets;
import org.eclipse.jgit.storage.file.FileBasedConfig;
+import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.util.FS;
import org.junit.Test;
@@ -181,4 +183,31 @@ public class StatusCommandTest extends RepositoryTestCase {
}
}
+ @Test
+ public void testNestedCommittedGitRepoAndPathFilter() throws Exception {
+ commitFile("file.txt", "file", "master");
+ try (Repository inner = new FileRepositoryBuilder()
+ .setWorkTree(new File(db.getWorkTree(), "subgit")).build()) {
+ inner.create();
+ writeTrashFile("subgit/sub.txt", "sub");
+ try (Git outerGit = new Git(db); Git innerGit = new Git(inner)) {
+ innerGit.add().addFilepattern("sub.txt").call();
+ innerGit.commit().setMessage("Inner commit").call();
+ outerGit.add().addFilepattern("subgit").call();
+ outerGit.commit().setMessage("Outer commit").call();
+ assertTrue(innerGit.status().call().isClean());
+ assertTrue(outerGit.status().call().isClean());
+ writeTrashFile("subgit/sub.txt", "sub2");
+ assertFalse(innerGit.status().call().isClean());
+ assertFalse(outerGit.status().call().isClean());
+ assertTrue(
+ outerGit.status().addPath("file.txt").call().isClean());
+ assertTrue(outerGit.status().addPath("doesntexist").call()
+ .isClean());
+ assertFalse(
+ outerGit.status().addPath("subgit").call().isClean());
+ }
+ }
+ }
+
}
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PushProcessTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PushProcessTest.java
index 6928859622..2e8b30f151 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PushProcessTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PushProcessTest.java
@@ -14,14 +14,19 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
+import java.io.PrintStream;
+import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
+import org.eclipse.jgit.api.errors.AbortedByHookException;
import org.eclipse.jgit.errors.NotSupportedException;
import org.eclipse.jgit.errors.TransportException;
+import org.eclipse.jgit.hooks.PrePushHook;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectIdRef;
import org.eclipse.jgit.lib.ProgressMonitor;
@@ -31,6 +36,7 @@ import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.TextProgressMonitor;
import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
import org.eclipse.jgit.transport.RemoteRefUpdate.Status;
+import org.eclipse.jgit.util.io.NullOutputStream;
import org.junit.Before;
import org.junit.Test;
@@ -220,7 +226,17 @@ public class PushProcessTest extends SampleDataRepositoryTestCase {
.fromString("0000000000000000000000000000000000000001"));
final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
ObjectId.fromString("ac7e7e44c1885efb472ad54a78327d66bfc4ecef"));
- testOneUpdateStatus(rru, ref, Status.REJECTED_REMOTE_CHANGED, null);
+ try (ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+ PrintStream out = new PrintStream(bytes, true,
+ StandardCharsets.UTF_8);
+ PrintStream err = new PrintStream(NullOutputStream.INSTANCE)) {
+ MockPrePushHook hook = new MockPrePushHook(db, out, err);
+ testOneUpdateStatus(rru, ref, Status.REJECTED_REMOTE_CHANGED, null,
+ hook);
+ out.flush();
+ String result = new String(bytes.toString(StandardCharsets.UTF_8));
+ assertEquals("", result);
+ }
}
/**
@@ -256,10 +272,22 @@ public class PushProcessTest extends SampleDataRepositoryTestCase {
refUpdates.add(rruOk);
refUpdates.add(rruReject);
advertisedRefs.add(refToChange);
- executePush();
- assertEquals(Status.OK, rruOk.getStatus());
- assertTrue(rruOk.isFastForward());
- assertEquals(Status.NON_EXISTING, rruReject.getStatus());
+ try (ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+ PrintStream out = new PrintStream(bytes, true,
+ StandardCharsets.UTF_8);
+ PrintStream err = new PrintStream(NullOutputStream.INSTANCE)) {
+ MockPrePushHook hook = new MockPrePushHook(db, out, err);
+ executePush(hook);
+ assertEquals(Status.OK, rruOk.getStatus());
+ assertTrue(rruOk.isFastForward());
+ assertEquals(Status.NON_EXISTING, rruReject.getStatus());
+ out.flush();
+ String result = new String(bytes.toString(StandardCharsets.UTF_8));
+ assertEquals(
+ "null 0000000000000000000000000000000000000000 "
+ + "refs/heads/master 2c349335b7f797072cf729c4f3bb0914ecb6dec9\n",
+ result);
+ }
}
/**
@@ -346,10 +374,18 @@ public class PushProcessTest extends SampleDataRepositoryTestCase {
final Ref advertisedRef, final Status expectedStatus,
Boolean fastForward) throws NotSupportedException,
TransportException {
+ return testOneUpdateStatus(rru, advertisedRef, expectedStatus,
+ fastForward, null);
+ }
+
+ private PushResult testOneUpdateStatus(final RemoteRefUpdate rru,
+ final Ref advertisedRef, final Status expectedStatus,
+ Boolean fastForward, PrePushHook hook)
+ throws NotSupportedException, TransportException {
refUpdates.add(rru);
if (advertisedRef != null)
advertisedRefs.add(advertisedRef);
- final PushResult result = executePush();
+ final PushResult result = executePush(hook);
assertEquals(expectedStatus, rru.getStatus());
if (fastForward != null)
assertEquals(fastForward, Boolean.valueOf(rru.isFastForward()));
@@ -358,7 +394,12 @@ public class PushProcessTest extends SampleDataRepositoryTestCase {
private PushResult executePush() throws NotSupportedException,
TransportException {
- process = new PushProcess(transport, refUpdates);
+ return executePush(null);
+ }
+
+ private PushResult executePush(PrePushHook hook)
+ throws NotSupportedException, TransportException {
+ process = new PushProcess(transport, refUpdates, hook);
return process.execute(new TextProgressMonitor());
}
@@ -416,4 +457,20 @@ public class PushProcessTest extends SampleDataRepositoryTestCase {
}
}
}
+
+ private static class MockPrePushHook extends PrePushHook {
+
+ private final PrintStream output;
+
+ public MockPrePushHook(Repository repo, PrintStream out,
+ PrintStream err) {
+ super(repo, out, err);
+ output = out;
+ }
+
+ @Override
+ protected void doRun() throws AbortedByHookException, IOException {
+ output.print(getStdinArgs());
+ }
+ }
}
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/RefSpecTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/RefSpecTest.java
index 5569bca23c..b56308cb72 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/RefSpecTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/RefSpecTest.java
@@ -466,4 +466,18 @@ public class RefSpecTest {
assertTrue(a.matchSource("refs/heads/master"));
assertNull(a.getDestination());
}
+
+ @Test
+ public void matching() {
+ RefSpec a = new RefSpec(":");
+ assertTrue(a.isMatching());
+ assertFalse(a.isForceUpdate());
+ }
+
+ @Test
+ public void matchingForced() {
+ RefSpec a = new RefSpec("+:");
+ assertTrue(a.isMatching());
+ assertTrue(a.isForceUpdate());
+ }
}
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FilterCommandsTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FilterCommandsTest.java
index 36f94fbd20..89d31c3e8f 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FilterCommandsTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FilterCommandsTest.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2016, Christian Halstrick <christian.halstrick@sap.com> and others
+ * Copyright (C) 2016, 2022 Christian Halstrick <christian.halstrick@sap.com> and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
@@ -10,12 +10,17 @@
package org.eclipse.jgit.util;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.util.HashSet;
+import java.util.Set;
import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.api.MergeResult;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.attributes.FilterCommand;
import org.eclipse.jgit.attributes.FilterCommandFactory;
@@ -86,6 +91,14 @@ public class FilterCommandsTest extends RepositoryTestCase {
secondCommit = git.commit().setMessage("Second commit").call();
}
+ @Override
+ public void tearDown() throws Exception {
+ Set<String> existingFilters = new HashSet<>(
+ FilterCommandRegistry.getRegisteredFilterCommands());
+ existingFilters.forEach(FilterCommandRegistry::unregister);
+ super.tearDown();
+ }
+
@Test
public void testBuiltinCleanFilter()
throws IOException, GitAPIException {
@@ -217,4 +230,133 @@ public class FilterCommandsTest extends RepositoryTestCase {
config.save();
}
+ @Test
+ public void testBranchSwitch() throws Exception {
+ String builtinCommandPrefix = "jgit://builtin/test/";
+ FilterCommandRegistry.register(builtinCommandPrefix + "smudge",
+ new TestCommandFactory('s'));
+ FilterCommandRegistry.register(builtinCommandPrefix + "clean",
+ new TestCommandFactory('c'));
+ StoredConfig config = git.getRepository().getConfig();
+ config.setString("filter", "test", "smudge",
+ builtinCommandPrefix + "smudge");
+ config.setString("filter", "test", "clean",
+ builtinCommandPrefix + "clean");
+ config.save();
+ // We're on the test branch
+ File aFile = writeTrashFile("a.txt", "a");
+ writeTrashFile(".gitattributes", "a.txt filter=test");
+ File cFile = writeTrashFile("cc/c.txt", "C");
+ writeTrashFile("cc/.gitattributes", "c.txt filter=test");
+ git.add().addFilepattern(".").call();
+ git.commit().setMessage("On test").call();
+ git.checkout().setName("master").call();
+ git.branchCreate().setName("other").call();
+ git.checkout().setName("other").call();
+ writeTrashFile("b.txt", "b");
+ writeTrashFile(".gitattributes", "b.txt filter=test");
+ git.add().addFilepattern(".").call();
+ git.commit().setMessage("On other").call();
+ git.checkout().setName("test").call();
+ checkFile(aFile, "scsa");
+ checkFile(cFile, "scsC");
+ }
+
+ @Test
+ public void testCheckoutSingleFile() throws Exception {
+ String builtinCommandPrefix = "jgit://builtin/test/";
+ FilterCommandRegistry.register(builtinCommandPrefix + "smudge",
+ new TestCommandFactory('s'));
+ FilterCommandRegistry.register(builtinCommandPrefix + "clean",
+ new TestCommandFactory('c'));
+ StoredConfig config = git.getRepository().getConfig();
+ config.setString("filter", "test", "smudge",
+ builtinCommandPrefix + "smudge");
+ config.setString("filter", "test", "clean",
+ builtinCommandPrefix + "clean");
+ config.save();
+ // We're on the test branch
+ File aFile = writeTrashFile("a.txt", "a");
+ File attributes = writeTrashFile(".gitattributes", "a.txt filter=test");
+ git.add().addFilepattern(".").call();
+ git.commit().setMessage("On test").call();
+ git.checkout().setName("master").call();
+ git.branchCreate().setName("other").call();
+ git.checkout().setName("other").call();
+ writeTrashFile("b.txt", "b");
+ writeTrashFile(".gitattributes", "b.txt filter=test");
+ git.add().addFilepattern(".").call();
+ git.commit().setMessage("On other").call();
+ git.checkout().setName("master").call();
+ assertFalse(aFile.exists());
+ assertFalse(attributes.exists());
+ git.checkout().setStartPoint("test").addPath("a.txt").call();
+ checkFile(aFile, "scsa");
+ }
+
+ @Test
+ public void testCheckoutSingleFile2() throws Exception {
+ String builtinCommandPrefix = "jgit://builtin/test/";
+ FilterCommandRegistry.register(builtinCommandPrefix + "smudge",
+ new TestCommandFactory('s'));
+ FilterCommandRegistry.register(builtinCommandPrefix + "clean",
+ new TestCommandFactory('c'));
+ StoredConfig config = git.getRepository().getConfig();
+ config.setString("filter", "test", "smudge",
+ builtinCommandPrefix + "smudge");
+ config.setString("filter", "test", "clean",
+ builtinCommandPrefix + "clean");
+ config.save();
+ // We're on the test branch
+ File aFile = writeTrashFile("a.txt", "a");
+ File attributes = writeTrashFile(".gitattributes", "a.txt filter=test");
+ git.add().addFilepattern(".").call();
+ git.commit().setMessage("On test").call();
+ git.checkout().setName("master").call();
+ git.branchCreate().setName("other").call();
+ git.checkout().setName("other").call();
+ writeTrashFile("b.txt", "b");
+ writeTrashFile(".gitattributes", "b.txt filter=test");
+ git.add().addFilepattern(".").call();
+ git.commit().setMessage("On other").call();
+ git.checkout().setName("master").call();
+ assertFalse(aFile.exists());
+ assertFalse(attributes.exists());
+ writeTrashFile(".gitattributes", "");
+ git.checkout().setStartPoint("test").addPath("a.txt").call();
+ checkFile(aFile, "scsa");
+ }
+
+ @Test
+ public void testMerge() throws Exception {
+ String builtinCommandPrefix = "jgit://builtin/test/";
+ FilterCommandRegistry.register(builtinCommandPrefix + "smudge",
+ new TestCommandFactory('s'));
+ FilterCommandRegistry.register(builtinCommandPrefix + "clean",
+ new TestCommandFactory('c'));
+ StoredConfig config = git.getRepository().getConfig();
+ config.setString("filter", "test", "smudge",
+ builtinCommandPrefix + "smudge");
+ config.setString("filter", "test", "clean",
+ builtinCommandPrefix + "clean");
+ config.save();
+ // We're on the test branch. Set up two branches that are expected to
+ // merge cleanly.
+ File aFile = writeTrashFile("a.txt", "a");
+ writeTrashFile(".gitattributes", "a.txt filter=test");
+ git.add().addFilepattern(".").call();
+ RevCommit aCommit = git.commit().setMessage("On test").call();
+ git.checkout().setName("master").call();
+ assertFalse(aFile.exists());
+ git.branchCreate().setName("other").call();
+ git.checkout().setName("other").call();
+ writeTrashFile("b/b.txt", "b");
+ writeTrashFile("b/.gitattributes", "b.txt filter=test");
+ git.add().addFilepattern(".").call();
+ git.commit().setMessage("On other").call();
+ MergeResult result = git.merge().include(aCommit).call();
+ assertEquals(MergeResult.MergeStatus.MERGED, result.getMergeStatus());
+ checkFile(aFile, "scsa");
+ }
+
}