diff options
Diffstat (limited to 'org.eclipse.jgit.test/tst/org')
13 files changed, 1272 insertions, 20 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java index 632288ec64..2fb228e01d 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java @@ -111,7 +111,7 @@ public class AddCommandTest extends RepositoryTestCase { } @Test - public void testAddExistingSingleFileWithNewLine() throws IOException, + public void testAddExistingSingleSmallFileWithNewLine() throws IOException, NoFilepatternException { File file = new File(db.getWorkTree(), "a.txt"); FileUtils.createNewFile(file); @@ -135,6 +135,35 @@ public class AddCommandTest extends RepositoryTestCase { } @Test + public void testAddExistingSingleMediumSizeFileWithNewLine() + throws IOException, NoFilepatternException { + File file = new File(db.getWorkTree(), "a.txt"); + FileUtils.createNewFile(file); + StringBuilder data = new StringBuilder(); + for (int i = 0; i < 1000; ++i) { + data.append("row1\r\nrow2"); + } + String crData = data.toString(); + PrintWriter writer = new PrintWriter(file); + writer.print(crData); + writer.close(); + String lfData = data.toString().replaceAll("\r", ""); + Git git = new Git(db); + db.getConfig().setString("core", null, "autocrlf", "false"); + git.add().addFilepattern("a.txt").call(); + assertEquals("[a.txt, mode:100644, content:" + data + "]", + indexState(CONTENT)); + db.getConfig().setString("core", null, "autocrlf", "true"); + git.add().addFilepattern("a.txt").call(); + assertEquals("[a.txt, mode:100644, content:" + lfData + "]", + indexState(CONTENT)); + db.getConfig().setString("core", null, "autocrlf", "input"); + git.add().addFilepattern("a.txt").call(); + assertEquals("[a.txt, mode:100644, content:" + lfData + "]", + indexState(CONTENT)); + } + + @Test public void testAddExistingSingleBinaryFile() throws IOException, NoFilepatternException { File file = new File(db.getWorkTree(), "a.txt"); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java new file mode 100644 index 0000000000..16c0a9b447 --- /dev/null +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java @@ -0,0 +1,191 @@ +/* + * Copyright (C) 2011, 2012, IBM Corporation and others. + * 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.api; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.fail; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; + +import org.eclipse.jgit.api.errors.PatchApplyException; +import org.eclipse.jgit.api.errors.PatchFormatException; +import org.eclipse.jgit.diff.DiffFormatterReflowTest; +import org.eclipse.jgit.diff.RawText; +import org.eclipse.jgit.lib.RepositoryTestCase; +import org.junit.Test; + +public class ApplyCommandTest extends RepositoryTestCase { + + private RawText a; + + private RawText b; + + private ApplyResult init(final String name) throws Exception { + return init(name, true, true); + } + + private ApplyResult init(final String name, final boolean preExists, + final boolean postExists) throws Exception { + Git git = new Git(db); + + if (preExists) { + a = new RawText(readFile(name + "_PreImage")); + write(new File(db.getDirectory().getParent(), name), + a.getString(0, a.size(), false)); + + git.add().addFilepattern(name).call(); + git.commit().setMessage("PreImage").call(); + } + + if (postExists) + b = new RawText(readFile(name + "_PostImage")); + + return git + .apply() + .setPatch( + DiffFormatterReflowTest.class.getResourceAsStream(name + + ".patch")).call(); + } + + @Test + public void testAddA1() throws Exception { + ApplyResult result = init("A1", false, true); + assertEquals(1, result.getUpdatedFiles().size()); + assertEquals(new File(db.getWorkTree(), "A1"), result.getUpdatedFiles() + .get(0)); + checkFile(new File(db.getWorkTree(), "A1"), + b.getString(0, b.size(), false)); + } + + @Test + public void testAddA2() throws Exception { + ApplyResult result = init("A2", false, true); + assertEquals(1, result.getUpdatedFiles().size()); + assertEquals(new File(db.getWorkTree(), "A2"), result.getUpdatedFiles() + .get(0)); + checkFile(new File(db.getWorkTree(), "A2"), + b.getString(0, b.size(), false)); + } + + @Test + public void testDeleteD() throws Exception { + ApplyResult result = init("D", true, false); + assertEquals(1, result.getUpdatedFiles().size()); + assertEquals(new File(db.getWorkTree(), "D"), result.getUpdatedFiles() + .get(0)); + assertFalse(new File(db.getWorkTree(), "D").exists()); + } + + @Test(expected = PatchFormatException.class) + public void testFailureF1() throws Exception { + init("F1", true, false); + } + + @Test(expected = PatchApplyException.class) + public void testFailureF2() throws Exception { + init("F2", true, false); + } + + @Test + public void testModifyE() throws Exception { + ApplyResult result = init("E"); + assertEquals(1, result.getUpdatedFiles().size()); + assertEquals(new File(db.getWorkTree(), "E"), result.getUpdatedFiles() + .get(0)); + checkFile(new File(db.getWorkTree(), "E"), + b.getString(0, b.size(), false)); + } + + @Test + public void testModifyX() throws Exception { + ApplyResult result = init("X"); + assertEquals(1, result.getUpdatedFiles().size()); + assertEquals(new File(db.getWorkTree(), "X"), result.getUpdatedFiles() + .get(0)); + checkFile(new File(db.getWorkTree(), "X"), + b.getString(0, b.size(), false)); + } + + @Test + public void testModifyY() throws Exception { + ApplyResult result = init("Y"); + assertEquals(1, result.getUpdatedFiles().size()); + assertEquals(new File(db.getWorkTree(), "Y"), result.getUpdatedFiles() + .get(0)); + checkFile(new File(db.getWorkTree(), "Y"), + b.getString(0, b.size(), false)); + } + + @Test + public void testModifyZ() throws Exception { + ApplyResult result = init("Z"); + assertEquals(1, result.getUpdatedFiles().size()); + assertEquals(new File(db.getWorkTree(), "Z"), result.getUpdatedFiles() + .get(0)); + checkFile(new File(db.getWorkTree(), "Z"), + b.getString(0, b.size(), false)); + } + + private byte[] readFile(final String patchFile) throws IOException { + final InputStream in = DiffFormatterReflowTest.class + .getResourceAsStream(patchFile); + if (in == null) { + fail("No " + patchFile + " test vector"); + return null; // Never happens + } + try { + final byte[] buf = new byte[1024]; + final ByteArrayOutputStream temp = new ByteArrayOutputStream(); + int n; + while ((n = in.read(buf)) > 0) + temp.write(buf, 0, n); + return temp.toByteArray(); + } finally { + in.close(); + } + } +} diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java index 7d75a6cda9..b1cac3a54d 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java @@ -56,6 +56,7 @@ import java.io.FileInputStream; import java.io.IOException; import org.eclipse.jgit.api.CheckoutResult.Status; +import org.eclipse.jgit.api.errors.CheckoutConflictException; import org.eclipse.jgit.api.errors.InvalidRefNameException; import org.eclipse.jgit.api.errors.JGitInternalException; import org.eclipse.jgit.api.errors.RefAlreadyExistsException; @@ -127,7 +128,8 @@ public class CheckoutCommandTest extends RepositoryTestCase { @Test public void testCheckoutToNonExistingBranch() throws JGitInternalException, - RefAlreadyExistsException, InvalidRefNameException { + RefAlreadyExistsException, InvalidRefNameException, + CheckoutConflictException { try { git.checkout().setName("badbranch").call(); fail("Should have failed"); @@ -222,7 +224,7 @@ public class CheckoutCommandTest extends RepositoryTestCase { @Test public void testDetachedHeadOnCheckout() throws JGitInternalException, RefAlreadyExistsException, RefNotFoundException, - InvalidRefNameException, IOException { + InvalidRefNameException, IOException, CheckoutConflictException { CheckoutCommand co = git.checkout(); co.setName("master").call(); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java index ea59b14479..b63a4158f9 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java @@ -43,6 +43,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.assertTrue; import static org.junit.Assert.fail; @@ -65,8 +66,10 @@ import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.RepositoryTestCase; import org.eclipse.jgit.revwalk.RevBlob; import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.storage.file.FileBasedConfig; import org.eclipse.jgit.submodule.SubmoduleStatus; import org.eclipse.jgit.submodule.SubmoduleStatusType; +import org.eclipse.jgit.util.SystemReader; import org.junit.Test; public class CloneCommandTest extends RepositoryTestCase { @@ -299,4 +302,54 @@ public class CloneCommandTest extends RepositoryTestCase { assertEquals(commit, pathStatus.getHeadId()); assertEquals(commit, pathStatus.getIndexId()); } + + @Test + public void testCloneWithAutoSetupRebase() throws Exception { + File directory = createTempDirectory("testCloneRepository1"); + CloneCommand command = Git.cloneRepository(); + command.setDirectory(directory); + command.setURI("file://" + git.getRepository().getWorkTree().getPath()); + Git git2 = command.call(); + addRepoToClose(git2.getRepository()); + assertFalse(git2 + .getRepository() + .getConfig() + .getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, "test", + ConfigConstants.CONFIG_KEY_REBASE, false)); + + FileBasedConfig userConfig = SystemReader.getInstance().openUserConfig( + null, git.getRepository().getFS()); + userConfig.setString(ConfigConstants.CONFIG_BRANCH_SECTION, null, + ConfigConstants.CONFIG_KEY_AUTOSETUPREBASE, + ConfigConstants.CONFIG_KEY_ALWAYS); + userConfig.save(); + directory = createTempDirectory("testCloneRepository2"); + command = Git.cloneRepository(); + command.setDirectory(directory); + command.setURI("file://" + git.getRepository().getWorkTree().getPath()); + git2 = command.call(); + addRepoToClose(git2.getRepository()); + assertTrue(git2 + .getRepository() + .getConfig() + .getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, "test", + ConfigConstants.CONFIG_KEY_REBASE, false)); + + userConfig.setString(ConfigConstants.CONFIG_BRANCH_SECTION, null, + ConfigConstants.CONFIG_KEY_AUTOSETUPREBASE, + ConfigConstants.CONFIG_KEY_REMOTE); + userConfig.save(); + directory = createTempDirectory("testCloneRepository2"); + command = Git.cloneRepository(); + command.setDirectory(directory); + command.setURI("file://" + git.getRepository().getWorkTree().getPath()); + git2 = command.call(); + addRepoToClose(git2.getRepository()); + assertTrue(git2 + .getRepository() + .getConfig() + .getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, "test", + ConfigConstants.CONFIG_KEY_REBASE, false)); + + } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PathCheckoutCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PathCheckoutCommandTest.java index 20a1acbd99..243d791cd2 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PathCheckoutCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PathCheckoutCommandTest.java @@ -233,4 +233,14 @@ public class PathCheckoutCommandTest extends RepositoryTestCase { assertEquals(0, status.getRemoved().size()); assertEquals(0, status.getUntracked().size()); } + + @Test + public void testCheckoutRepository() throws Exception { + CheckoutCommand co = git.checkout(); + File test = writeTrashFile(FILE1, ""); + File test2 = writeTrashFile(FILE2, ""); + co.setStartPoint("HEAD~2").setAllPaths(true).call(); + assertEquals("1", read(test)); + assertEquals("a", read(test2)); + } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java index 672f4d86aa..648ef6f01b 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java @@ -503,6 +503,55 @@ public class RebaseCommandTest extends RepositoryTestCase { } @Test + public void testStopOnConflictAndContinueWithNoDeltaToMaster() + throws Exception { + // create file1 on master + RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1", + "2", "3"); + // change in master + writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3"); + + checkFile(FILE1, "1master", "2", "3"); + // create a topic branch based on the first commit + createBranch(firstInMaster, "refs/heads/topic"); + checkoutBranch("refs/heads/topic"); + // we have the old content again + checkFile(FILE1, "1", "2", "3"); + + // change first line (conflicting) + writeFileAndCommit(FILE1, + "change file1 in topic\n\nThis is conflicting", "1topic", "2", + "3", "4topic"); + + RebaseResult res = git.rebase().setUpstream("refs/heads/master").call(); + assertEquals(Status.STOPPED, res.getStatus()); + + // continue should throw a meaningful exception + try { + res = git.rebase().setOperation(Operation.CONTINUE).call(); + fail("Expected Exception not thrown"); + } catch (UnmergedPathsException e) { + // expected + } + + // merge the file; the second topic commit should go through + writeFileAndAdd(FILE1, "1master", "2", "3"); + + res = git.rebase().setOperation(Operation.CONTINUE).call(); + assertNotNull(res); + assertEquals(Status.NOTHING_TO_COMMIT, res.getStatus()); + assertEquals(RepositoryState.REBASING_INTERACTIVE, + db.getRepositoryState()); + + git.rebase().setOperation(Operation.SKIP).call(); + + ObjectId headId = db.resolve(Constants.HEAD); + RevWalk rw = new RevWalk(db); + RevCommit rc = rw.parseCommit(headId); + assertEquals("change file1 in master", rc.getFullMessage()); + } + + @Test public void testStopOnConflictAndFailContinueIfFileIsDirty() throws Exception { // create file1 on master @@ -775,8 +824,15 @@ public class RebaseCommandTest extends RepositoryTestCase { res = git.rebase().setOperation(Operation.CONTINUE).call(); assertNotNull(res); - assertEquals(Status.OK, res.getStatus()); - assertEquals(RepositoryState.SAFE, db.getRepositoryState()); + + // nothing to commit. this leaves the repo state in rebase, so that the + // user can decide what to do. if he accidentally committed, reset soft, + // and continue, if he really has nothing to commit, skip. + assertEquals(Status.NOTHING_TO_COMMIT, res.getStatus()); + assertEquals(RepositoryState.REBASING_INTERACTIVE, + db.getRepositoryState()); + + git.rebase().setOperation(Operation.SKIP).call(); ObjectId headId = db.resolve(Constants.HEAD); RevWalk rw = new RevWalk(db); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StashApplyCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StashApplyCommandTest.java new file mode 100644 index 0000000000..97d0efe109 --- /dev/null +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StashApplyCommandTest.java @@ -0,0 +1,346 @@ +/* + * Copyright (C) 2012, GitHub Inc. + * 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.api; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; + +import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.RepositoryTestCase; +import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.util.FileUtils; +import org.junit.Before; +import org.junit.Test; + +/** + * Unit tests of {@link StashApplyCommand} + */ +public class StashApplyCommandTest extends RepositoryTestCase { + + private static final String PATH = "file.txt"; + + private RevCommit head; + + private Git git; + + private File committedFile; + + @Before + public void setUp() throws Exception { + super.setUp(); + git = Git.wrap(db); + committedFile = writeTrashFile(PATH, "content"); + git.add().addFilepattern(PATH).call(); + head = git.commit().setMessage("add file").call(); + assertNotNull(head); + } + + @Test + public void workingDirectoryDelete() throws Exception { + deleteTrashFile(PATH); + assertFalse(committedFile.exists()); + RevCommit stashed = git.stashCreate().call(); + assertNotNull(stashed); + assertEquals("content", read(committedFile)); + + ObjectId unstashed = git.stashApply().call(); + assertEquals(stashed, unstashed); + assertFalse(committedFile.exists()); + + Status status = git.status().call(); + assertTrue(status.getAdded().isEmpty()); + assertTrue(status.getChanged().isEmpty()); + assertTrue(status.getConflicting().isEmpty()); + assertTrue(status.getModified().isEmpty()); + assertTrue(status.getUntracked().isEmpty()); + assertTrue(status.getRemoved().isEmpty()); + + assertEquals(1, status.getMissing().size()); + assertTrue(status.getMissing().contains(PATH)); + } + + @Test + public void indexAdd() throws Exception { + String addedPath = "file2.txt"; + File addedFile = writeTrashFile(addedPath, "content2"); + git.add().addFilepattern(addedPath).call(); + + RevCommit stashed = git.stashCreate().call(); + assertNotNull(stashed); + assertFalse(addedFile.exists()); + + ObjectId unstashed = git.stashApply().call(); + assertEquals(stashed, unstashed); + assertTrue(addedFile.exists()); + assertEquals("content2", read(addedFile)); + + Status status = git.status().call(); + assertTrue(status.getChanged().isEmpty()); + assertTrue(status.getConflicting().isEmpty()); + assertTrue(status.getMissing().isEmpty()); + assertTrue(status.getModified().isEmpty()); + assertTrue(status.getRemoved().isEmpty()); + assertTrue(status.getUntracked().isEmpty()); + + assertEquals(1, status.getAdded().size()); + assertTrue(status.getAdded().contains(addedPath)); + } + + @Test + public void indexDelete() throws Exception { + git.rm().addFilepattern("file.txt").call(); + + RevCommit stashed = git.stashCreate().call(); + assertNotNull(stashed); + assertEquals("content", read(committedFile)); + + ObjectId unstashed = git.stashApply().call(); + assertEquals(stashed, unstashed); + assertFalse(committedFile.exists()); + + Status status = git.status().call(); + assertTrue(status.getAdded().isEmpty()); + assertTrue(status.getChanged().isEmpty()); + assertTrue(status.getConflicting().isEmpty()); + assertTrue(status.getModified().isEmpty()); + assertTrue(status.getMissing().isEmpty()); + assertTrue(status.getUntracked().isEmpty()); + + assertEquals(1, status.getRemoved().size()); + assertTrue(status.getRemoved().contains(PATH)); + } + + @Test + public void workingDirectoryModify() throws Exception { + writeTrashFile("file.txt", "content2"); + + RevCommit stashed = git.stashCreate().call(); + assertNotNull(stashed); + assertEquals("content", read(committedFile)); + + ObjectId unstashed = git.stashApply().call(); + assertEquals(stashed, unstashed); + assertEquals("content2", read(committedFile)); + + Status status = git.status().call(); + assertTrue(status.getAdded().isEmpty()); + assertTrue(status.getChanged().isEmpty()); + assertTrue(status.getConflicting().isEmpty()); + assertTrue(status.getMissing().isEmpty()); + assertTrue(status.getRemoved().isEmpty()); + assertTrue(status.getUntracked().isEmpty()); + + assertEquals(1, status.getModified().size()); + assertTrue(status.getModified().contains(PATH)); + } + + @Test + public void workingDirectoryModifyInSubfolder() throws Exception { + String path = "d1/d2/f.txt"; + File subfolderFile = writeTrashFile(path, "content"); + git.add().addFilepattern(path).call(); + head = git.commit().setMessage("add file").call(); + + writeTrashFile(path, "content2"); + + RevCommit stashed = git.stashCreate().call(); + assertNotNull(stashed); + assertEquals("content", read(subfolderFile)); + + ObjectId unstashed = git.stashApply().call(); + assertEquals(stashed, unstashed); + assertEquals("content2", read(subfolderFile)); + + Status status = git.status().call(); + assertTrue(status.getAdded().isEmpty()); + assertTrue(status.getChanged().isEmpty()); + assertTrue(status.getConflicting().isEmpty()); + assertTrue(status.getMissing().isEmpty()); + assertTrue(status.getRemoved().isEmpty()); + assertTrue(status.getUntracked().isEmpty()); + + assertEquals(1, status.getModified().size()); + assertTrue(status.getModified().contains(path)); + } + + @Test + public void workingDirectoryModifyIndexChanged() throws Exception { + writeTrashFile("file.txt", "content2"); + git.add().addFilepattern("file.txt").call(); + writeTrashFile("file.txt", "content3"); + + RevCommit stashed = git.stashCreate().call(); + assertNotNull(stashed); + assertEquals("content", read(committedFile)); + + ObjectId unstashed = git.stashApply().call(); + assertEquals(stashed, unstashed); + assertEquals("content3", read(committedFile)); + + Status status = git.status().call(); + assertTrue(status.getAdded().isEmpty()); + assertTrue(status.getConflicting().isEmpty()); + assertTrue(status.getMissing().isEmpty()); + assertTrue(status.getRemoved().isEmpty()); + assertTrue(status.getUntracked().isEmpty()); + + assertEquals(1, status.getChanged().size()); + assertTrue(status.getChanged().contains(PATH)); + assertEquals(1, status.getModified().size()); + assertTrue(status.getModified().contains(PATH)); + } + + @Test + public void workingDirectoryCleanIndexModify() throws Exception { + writeTrashFile("file.txt", "content2"); + git.add().addFilepattern("file.txt").call(); + writeTrashFile("file.txt", "content"); + + RevCommit stashed = git.stashCreate().call(); + assertNotNull(stashed); + assertEquals("content", read(committedFile)); + + ObjectId unstashed = git.stashApply().call(); + assertEquals(stashed, unstashed); + assertEquals("content2", read(committedFile)); + + Status status = git.status().call(); + assertTrue(status.getAdded().isEmpty()); + assertTrue(status.getConflicting().isEmpty()); + assertTrue(status.getMissing().isEmpty()); + assertTrue(status.getModified().isEmpty()); + assertTrue(status.getRemoved().isEmpty()); + assertTrue(status.getUntracked().isEmpty()); + + assertEquals(1, status.getChanged().size()); + assertTrue(status.getChanged().contains(PATH)); + } + + @Test + public void workingDirectoryDeleteIndexAdd() throws Exception { + String path = "file2.txt"; + File added = writeTrashFile(path, "content2"); + assertTrue(added.exists()); + git.add().addFilepattern(path).call(); + FileUtils.delete(added); + assertFalse(added.exists()); + + RevCommit stashed = git.stashCreate().call(); + assertNotNull(stashed); + assertFalse(added.exists()); + + ObjectId unstashed = git.stashApply().call(); + assertEquals(stashed, unstashed); + assertEquals("content2", read(added)); + + Status status = git.status().call(); + assertTrue(status.getChanged().isEmpty()); + assertTrue(status.getConflicting().isEmpty()); + assertTrue(status.getMissing().isEmpty()); + assertTrue(status.getModified().isEmpty()); + assertTrue(status.getRemoved().isEmpty()); + assertTrue(status.getUntracked().isEmpty()); + + assertEquals(1, status.getAdded().size()); + assertTrue(status.getAdded().contains(path)); + } + + @Test + public void workingDirectoryDeleteIndexEdit() throws Exception { + writeTrashFile(PATH, "content2"); + git.add().addFilepattern(PATH).call(); + FileUtils.delete(committedFile); + assertFalse(committedFile.exists()); + + RevCommit stashed = git.stashCreate().call(); + assertNotNull(stashed); + assertEquals("content", read(committedFile)); + + ObjectId unstashed = git.stashApply().call(); + assertEquals(stashed, unstashed); + assertFalse(committedFile.exists()); + + Status status = git.status().call(); + assertTrue(status.getAdded().isEmpty()); + assertTrue(status.getChanged().isEmpty()); + assertTrue(status.getConflicting().isEmpty()); + assertTrue(status.getMissing().isEmpty()); + assertTrue(status.getModified().isEmpty()); + assertTrue(status.getUntracked().isEmpty()); + + assertEquals(1, status.getRemoved().size()); + assertTrue(status.getRemoved().contains(PATH)); + } + + @Test + public void multipleEdits() throws Exception { + String addedPath = "file2.txt"; + git.rm().addFilepattern(PATH).call(); + File addedFile = writeTrashFile(addedPath, "content2"); + git.add().addFilepattern(addedPath).call(); + + RevCommit stashed = git.stashCreate().call(); + assertNotNull(stashed); + assertTrue(committedFile.exists()); + assertFalse(addedFile.exists()); + + ObjectId unstashed = git.stashApply().call(); + assertEquals(stashed, unstashed); + + Status status = git.status().call(); + assertTrue(status.getChanged().isEmpty()); + assertTrue(status.getConflicting().isEmpty()); + assertTrue(status.getMissing().isEmpty()); + assertTrue(status.getModified().isEmpty()); + assertTrue(status.getUntracked().isEmpty()); + + assertEquals(1, status.getRemoved().size()); + assertTrue(status.getRemoved().contains(PATH)); + assertEquals(1, status.getAdded().size()); + assertTrue(status.getAdded().contains(addedPath)); + } +} diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StashCreateCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StashCreateCommandTest.java new file mode 100644 index 0000000000..16fadef153 --- /dev/null +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StashCreateCommandTest.java @@ -0,0 +1,420 @@ +/* + * Copyright (C) 2012, GitHub Inc. + * 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.api; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +import org.eclipse.jgit.diff.DiffEntry; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.PersonIdent; +import org.eclipse.jgit.lib.Ref; +import org.eclipse.jgit.lib.RepositoryTestCase; +import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.revwalk.RevWalk; +import org.eclipse.jgit.storage.file.ReflogEntry; +import org.eclipse.jgit.storage.file.ReflogReader; +import org.eclipse.jgit.treewalk.TreeWalk; +import org.eclipse.jgit.treewalk.filter.TreeFilter; +import org.eclipse.jgit.util.FileUtils; +import org.junit.Before; +import org.junit.Test; + +/** + * Unit tests of {@link StashCreateCommand} + */ +public class StashCreateCommandTest extends RepositoryTestCase { + + private RevCommit head; + + private Git git; + + private File committedFile; + + @Before + public void setUp() throws Exception { + super.setUp(); + git = Git.wrap(db); + committedFile = writeTrashFile("file.txt", "content"); + git.add().addFilepattern("file.txt").call(); + head = git.commit().setMessage("add file").call(); + assertNotNull(head); + } + + /** + * Core validation to be performed on all stashed commits + * + * @param commit + * @throws IOException + */ + private void validateStashedCommit(final RevCommit commit) + throws IOException { + assertNotNull(commit); + Ref stashRef = db.getRef(Constants.R_STASH); + assertNotNull(stashRef); + assertEquals(commit, stashRef.getObjectId()); + assertNotNull(commit.getAuthorIdent()); + assertEquals(commit.getAuthorIdent(), commit.getCommitterIdent()); + assertEquals(2, commit.getParentCount()); + + // Load parents + RevWalk walk = new RevWalk(db); + try { + for (RevCommit parent : commit.getParents()) + walk.parseBody(parent); + } finally { + walk.release(); + } + + assertEquals(1, commit.getParent(1).getParentCount()); + assertEquals(head, commit.getParent(1).getParent(0)); + assertFalse("Head tree matches stashed commit tree", commit.getTree() + .equals(head.getTree())); + assertEquals(head, commit.getParent(0)); + assertFalse(commit.getFullMessage().equals( + commit.getParent(1).getFullMessage())); + } + + private TreeWalk createTreeWalk() { + TreeWalk walk = new TreeWalk(db); + walk.setRecursive(true); + walk.setFilter(TreeFilter.ANY_DIFF); + return walk; + } + + private List<DiffEntry> diffWorkingAgainstHead(final RevCommit commit) + throws IOException { + TreeWalk walk = createTreeWalk(); + try { + walk.addTree(commit.getParent(0).getTree()); + walk.addTree(commit.getTree()); + return DiffEntry.scan(walk); + } finally { + walk.release(); + } + } + + private List<DiffEntry> diffIndexAgainstHead(final RevCommit commit) + throws IOException { + TreeWalk walk = createTreeWalk(); + try { + walk.addTree(commit.getParent(0).getTree()); + walk.addTree(commit.getParent(1).getTree()); + return DiffEntry.scan(walk); + } finally { + walk.release(); + } + } + + @Test + public void noLocalChanges() throws Exception { + assertNull(git.stashCreate().call()); + } + + @Test + public void workingDirectoryDelete() throws Exception { + deleteTrashFile("file.txt"); + RevCommit stashed = git.stashCreate().call(); + assertNotNull(stashed); + assertEquals("content", read(committedFile)); + validateStashedCommit(stashed); + + assertEquals(head.getTree(), stashed.getParent(1).getTree()); + + List<DiffEntry> diffs = diffWorkingAgainstHead(stashed); + assertEquals(1, diffs.size()); + assertEquals(DiffEntry.ChangeType.DELETE, diffs.get(0).getChangeType()); + assertEquals("file.txt", diffs.get(0).getOldPath()); + } + + @Test + public void indexAdd() throws Exception { + File addedFile = writeTrashFile("file2.txt", "content2"); + git.add().addFilepattern("file2.txt").call(); + + RevCommit stashed = Git.wrap(db).stashCreate().call(); + assertNotNull(stashed); + assertFalse(addedFile.exists()); + validateStashedCommit(stashed); + + assertEquals(stashed.getTree(), stashed.getParent(1).getTree()); + + List<DiffEntry> diffs = diffWorkingAgainstHead(stashed); + assertEquals(1, diffs.size()); + assertEquals(DiffEntry.ChangeType.ADD, diffs.get(0).getChangeType()); + assertEquals("file2.txt", diffs.get(0).getNewPath()); + } + + @Test + public void indexDelete() throws Exception { + git.rm().addFilepattern("file.txt").call(); + + RevCommit stashed = Git.wrap(db).stashCreate().call(); + assertNotNull(stashed); + assertEquals("content", read(committedFile)); + validateStashedCommit(stashed); + + assertEquals(stashed.getTree(), stashed.getParent(1).getTree()); + + List<DiffEntry> diffs = diffWorkingAgainstHead(stashed); + assertEquals(1, diffs.size()); + assertEquals(DiffEntry.ChangeType.DELETE, diffs.get(0).getChangeType()); + assertEquals("file.txt", diffs.get(0).getOldPath()); + } + + @Test + public void workingDirectoryModify() throws Exception { + writeTrashFile("file.txt", "content2"); + + RevCommit stashed = Git.wrap(db).stashCreate().call(); + assertNotNull(stashed); + assertEquals("content", read(committedFile)); + validateStashedCommit(stashed); + + assertEquals(head.getTree(), stashed.getParent(1).getTree()); + + List<DiffEntry> diffs = diffWorkingAgainstHead(stashed); + assertEquals(1, diffs.size()); + assertEquals(DiffEntry.ChangeType.MODIFY, diffs.get(0).getChangeType()); + assertEquals("file.txt", diffs.get(0).getNewPath()); + } + + @Test + public void workingDirectoryModifyInSubfolder() throws Exception { + String path = "d1/d2/f.txt"; + File subfolderFile = writeTrashFile(path, "content"); + git.add().addFilepattern(path).call(); + head = git.commit().setMessage("add file").call(); + + writeTrashFile(path, "content2"); + + RevCommit stashed = Git.wrap(db).stashCreate().call(); + assertNotNull(stashed); + assertEquals("content", read(subfolderFile)); + validateStashedCommit(stashed); + + assertEquals(head.getTree(), stashed.getParent(1).getTree()); + + List<DiffEntry> diffs = diffWorkingAgainstHead(stashed); + assertEquals(1, diffs.size()); + assertEquals(DiffEntry.ChangeType.MODIFY, diffs.get(0).getChangeType()); + assertEquals(path, diffs.get(0).getNewPath()); + } + + @Test + public void workingDirectoryModifyIndexChanged() throws Exception { + writeTrashFile("file.txt", "content2"); + git.add().addFilepattern("file.txt").call(); + writeTrashFile("file.txt", "content3"); + + RevCommit stashed = Git.wrap(db).stashCreate().call(); + assertNotNull(stashed); + assertEquals("content", read(committedFile)); + validateStashedCommit(stashed); + + assertFalse(stashed.getTree().equals(stashed.getParent(1).getTree())); + + List<DiffEntry> workingDiffs = diffWorkingAgainstHead(stashed); + assertEquals(1, workingDiffs.size()); + assertEquals(DiffEntry.ChangeType.MODIFY, workingDiffs.get(0) + .getChangeType()); + assertEquals("file.txt", workingDiffs.get(0).getNewPath()); + + List<DiffEntry> indexDiffs = diffIndexAgainstHead(stashed); + assertEquals(1, indexDiffs.size()); + assertEquals(DiffEntry.ChangeType.MODIFY, indexDiffs.get(0) + .getChangeType()); + assertEquals("file.txt", indexDiffs.get(0).getNewPath()); + + assertEquals(workingDiffs.get(0).getOldId(), indexDiffs.get(0) + .getOldId()); + assertFalse(workingDiffs.get(0).getNewId() + .equals(indexDiffs.get(0).getNewId())); + } + + @Test + public void workingDirectoryCleanIndexModify() throws Exception { + writeTrashFile("file.txt", "content2"); + git.add().addFilepattern("file.txt").call(); + writeTrashFile("file.txt", "content"); + + RevCommit stashed = Git.wrap(db).stashCreate().call(); + assertNotNull(stashed); + assertEquals("content", read(committedFile)); + validateStashedCommit(stashed); + + assertTrue(stashed.getTree().equals(stashed.getParent(1).getTree())); + + List<DiffEntry> workingDiffs = diffWorkingAgainstHead(stashed); + assertEquals(1, workingDiffs.size()); + assertEquals(DiffEntry.ChangeType.MODIFY, workingDiffs.get(0) + .getChangeType()); + assertEquals("file.txt", workingDiffs.get(0).getNewPath()); + + List<DiffEntry> indexDiffs = diffIndexAgainstHead(stashed); + assertEquals(1, indexDiffs.size()); + assertEquals(DiffEntry.ChangeType.MODIFY, indexDiffs.get(0) + .getChangeType()); + assertEquals("file.txt", indexDiffs.get(0).getNewPath()); + + assertEquals(workingDiffs.get(0).getOldId(), indexDiffs.get(0) + .getOldId()); + assertTrue(workingDiffs.get(0).getNewId() + .equals(indexDiffs.get(0).getNewId())); + } + + @Test + public void workingDirectoryDeleteIndexAdd() throws Exception { + String path = "file2.txt"; + File added = writeTrashFile(path, "content2"); + assertTrue(added.exists()); + git.add().addFilepattern(path).call(); + FileUtils.delete(added); + assertFalse(added.exists()); + + RevCommit stashed = Git.wrap(db).stashCreate().call(); + assertNotNull(stashed); + assertFalse(added.exists()); + + validateStashedCommit(stashed); + + assertTrue(stashed.getTree().equals(stashed.getParent(1).getTree())); + + List<DiffEntry> workingDiffs = diffWorkingAgainstHead(stashed); + assertEquals(1, workingDiffs.size()); + assertEquals(DiffEntry.ChangeType.ADD, workingDiffs.get(0) + .getChangeType()); + assertEquals(path, workingDiffs.get(0).getNewPath()); + + List<DiffEntry> indexDiffs = diffIndexAgainstHead(stashed); + assertEquals(1, indexDiffs.size()); + assertEquals(DiffEntry.ChangeType.ADD, indexDiffs.get(0) + .getChangeType()); + assertEquals(path, indexDiffs.get(0).getNewPath()); + + assertEquals(workingDiffs.get(0).getOldId(), indexDiffs.get(0) + .getOldId()); + assertTrue(workingDiffs.get(0).getNewId() + .equals(indexDiffs.get(0).getNewId())); + } + + @Test + public void workingDirectoryDeleteIndexEdit() throws Exception { + File edited = writeTrashFile("file.txt", "content2"); + git.add().addFilepattern("file.txt").call(); + FileUtils.delete(edited); + assertFalse(edited.exists()); + + RevCommit stashed = Git.wrap(db).stashCreate().call(); + assertNotNull(stashed); + assertEquals("content", read(committedFile)); + validateStashedCommit(stashed); + + assertFalse(stashed.getTree().equals(stashed.getParent(1).getTree())); + + List<DiffEntry> workingDiffs = diffWorkingAgainstHead(stashed); + assertEquals(1, workingDiffs.size()); + assertEquals(DiffEntry.ChangeType.DELETE, workingDiffs.get(0) + .getChangeType()); + assertEquals("file.txt", workingDiffs.get(0).getOldPath()); + + List<DiffEntry> indexDiffs = diffIndexAgainstHead(stashed); + assertEquals(1, indexDiffs.size()); + assertEquals(DiffEntry.ChangeType.MODIFY, indexDiffs.get(0) + .getChangeType()); + assertEquals("file.txt", indexDiffs.get(0).getNewPath()); + + assertEquals(workingDiffs.get(0).getOldId(), indexDiffs.get(0) + .getOldId()); + assertFalse(workingDiffs.get(0).getNewId() + .equals(indexDiffs.get(0).getNewId())); + } + + @Test + public void multipleEdits() throws Exception { + git.rm().addFilepattern("file.txt").call(); + File addedFile = writeTrashFile("file2.txt", "content2"); + git.add().addFilepattern("file2.txt").call(); + + RevCommit stashed = Git.wrap(db).stashCreate().call(); + assertNotNull(stashed); + assertFalse(addedFile.exists()); + validateStashedCommit(stashed); + + assertEquals(stashed.getTree(), stashed.getParent(1).getTree()); + + List<DiffEntry> diffs = diffWorkingAgainstHead(stashed); + assertEquals(2, diffs.size()); + assertEquals(DiffEntry.ChangeType.DELETE, diffs.get(0).getChangeType()); + assertEquals("file.txt", diffs.get(0).getOldPath()); + assertEquals(DiffEntry.ChangeType.ADD, diffs.get(1).getChangeType()); + assertEquals("file2.txt", diffs.get(1).getNewPath()); + } + + @Test + public void refLogIncludesCommitMessage() throws Exception { + PersonIdent who = new PersonIdent("user", "user@email.com"); + deleteTrashFile("file.txt"); + RevCommit stashed = git.stashCreate().setPerson(who).call(); + assertNotNull(stashed); + assertEquals("content", read(committedFile)); + validateStashedCommit(stashed); + + ReflogReader reader = new ReflogReader(git.getRepository(), + Constants.R_STASH); + ReflogEntry entry = reader.getLastEntry(); + assertNotNull(entry); + assertEquals(ObjectId.zeroId(), entry.getOldId()); + assertEquals(stashed, entry.getNewId()); + assertEquals(who, entry.getWho()); + assertEquals(stashed.getFullMessage(), entry.getComment()); + } +} diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextTest.java index 8d504e55d4..2a1e2c95b6 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextTest.java @@ -46,6 +46,7 @@ package org.eclipse.jgit.diff; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import java.io.ByteArrayOutputStream; @@ -189,6 +190,46 @@ public class RawTextTest { assertEquals(new Edit(0, 1, 0, 2), e); } + @Test + public void testLineDelimiter() throws Exception { + RawText rt = new RawText(Constants.encodeASCII("foo\n")); + assertEquals("\n", rt.getLineDelimiter()); + assertFalse(rt.isMissingNewlineAtEnd()); + rt = new RawText(Constants.encodeASCII("foo\r\n")); + assertEquals("\r\n", rt.getLineDelimiter()); + assertFalse(rt.isMissingNewlineAtEnd()); + + rt = new RawText(Constants.encodeASCII("foo\nbar")); + assertEquals("\n", rt.getLineDelimiter()); + assertTrue(rt.isMissingNewlineAtEnd()); + rt = new RawText(Constants.encodeASCII("foo\r\nbar")); + assertEquals("\r\n", rt.getLineDelimiter()); + assertTrue(rt.isMissingNewlineAtEnd()); + + rt = new RawText(Constants.encodeASCII("foo\nbar\r\n")); + assertEquals("\n", rt.getLineDelimiter()); + assertFalse(rt.isMissingNewlineAtEnd()); + rt = new RawText(Constants.encodeASCII("foo\r\nbar\n")); + assertEquals("\r\n", rt.getLineDelimiter()); + assertFalse(rt.isMissingNewlineAtEnd()); + + rt = new RawText(Constants.encodeASCII("foo")); + assertNull(rt.getLineDelimiter()); + assertTrue(rt.isMissingNewlineAtEnd()); + + rt = new RawText(Constants.encodeASCII("")); + assertNull(rt.getLineDelimiter()); + assertTrue(rt.isMissingNewlineAtEnd()); + + rt = new RawText(Constants.encodeASCII("\n")); + assertEquals("\n", rt.getLineDelimiter()); + assertFalse(rt.isMissingNewlineAtEnd()); + + rt = new RawText(Constants.encodeASCII("\r\n")); + assertEquals("\r\n", rt.getLineDelimiter()); + assertFalse(rt.isMissingNewlineAtEnd()); + } + private static RawText t(String text) { StringBuilder r = new StringBuilder(); for (int i = 0; i < text.length(); i++) { diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutTest.java index efb686da59..6c6c911f23 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutTest.java @@ -62,7 +62,6 @@ import org.eclipse.jgit.api.MergeResult.MergeStatus; import org.eclipse.jgit.api.ResetCommand.ResetType; import org.eclipse.jgit.api.Status; import org.eclipse.jgit.api.errors.GitAPIException; -import org.eclipse.jgit.api.errors.JGitInternalException; import org.eclipse.jgit.api.errors.NoFilepatternException; import org.eclipse.jgit.dircache.DirCache; import org.eclipse.jgit.dircache.DirCacheCheckout; @@ -1015,7 +1014,7 @@ public class DirCacheCheckoutTest extends RepositoryTestCase { try { checkout.call(); fail("Checkout exception not thrown"); - } catch (JGitInternalException e) { + } catch (org.eclipse.jgit.api.errors.CheckoutConflictException e) { CheckoutResult result = checkout.getResult(); assertNotNull(result); assertNotNull(result.getConflictList()); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java index dee2accf24..4c5a2c1381 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleAddTest.java @@ -66,6 +66,7 @@ import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.RepositoryTestCase; import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.storage.file.FileBasedConfig; import org.junit.Test; /** @@ -212,4 +213,44 @@ public class SubmoduleAddTest extends RepositoryTestCase { assertTrue(status.getAdded().contains(Constants.DOT_GIT_MODULES)); assertTrue(status.getAdded().contains(path)); } + + @Test + public void addSubmoduleWithExistingSubmoduleDefined() throws Exception { + String path1 = "sub1"; + String url1 = "git://server/repo1.git"; + String path2 = "sub2"; + + FileBasedConfig modulesConfig = new FileBasedConfig(new File( + db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS()); + modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, + path1, ConfigConstants.CONFIG_KEY_PATH, path1); + modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, + path1, ConfigConstants.CONFIG_KEY_URL, url1); + modulesConfig.save(); + + Git git = new Git(db); + writeTrashFile("file.txt", "content"); + git.add().addFilepattern("file.txt").call(); + assertNotNull(git.commit().setMessage("create file").call()); + + SubmoduleAddCommand command = new SubmoduleAddCommand(db); + command.setPath(path2); + String url2 = db.getDirectory().toURI().toString(); + command.setURI(url2); + assertNotNull(command.call()); + + modulesConfig.load(); + assertEquals(path1, modulesConfig.getString( + ConfigConstants.CONFIG_SUBMODULE_SECTION, path1, + ConfigConstants.CONFIG_KEY_PATH)); + assertEquals(url1, modulesConfig.getString( + ConfigConstants.CONFIG_SUBMODULE_SECTION, path1, + ConfigConstants.CONFIG_KEY_URL)); + assertEquals(path2, modulesConfig.getString( + ConfigConstants.CONFIG_SUBMODULE_SECTION, path2, + ConfigConstants.CONFIG_KEY_PATH)); + assertEquals(url2, modulesConfig.getString( + ConfigConstants.CONFIG_SUBMODULE_SECTION, path2, + ConfigConstants.CONFIG_KEY_URL)); + } }
\ No newline at end of file diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/ReceivePackRefFilterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/ReceivePackAdvertiseRefsHookTest.java index c58252a230..5f17394e2e 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/ReceivePackRefFilterTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/ReceivePackAdvertiseRefsHookTest.java @@ -74,6 +74,7 @@ import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevBlob; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevTree; +import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.storage.file.ObjectDirectory; import org.eclipse.jgit.storage.pack.BinaryDelta; import org.eclipse.jgit.util.NB; @@ -82,7 +83,7 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; -public class ReceivePackRefFilterTest extends LocalDiskRepositoryTestCase { +public class ReceivePackAdvertiseRefsHookTest extends LocalDiskRepositoryTestCase { private static final NullProgressMonitor PM = NullProgressMonitor.INSTANCE; private static final String R_MASTER = Constants.R_HEADS + Constants.MASTER; @@ -150,7 +151,7 @@ public class ReceivePackRefFilterTest extends LocalDiskRepositoryTestCase { dst.incrementOpen(); final ReceivePack rp = super.createReceivePack(dst); - rp.setRefFilter(new HidePrivateFilter()); + rp.setAdvertiseRefsHook(new HidePrivateHook()); return rp; } }; @@ -215,7 +216,7 @@ public class ReceivePackRefFilterTest extends LocalDiskRepositoryTestCase { final ReceivePack rp = super.createReceivePack(dst); rp.setCheckReceivedObjects(true); rp.setCheckReferencedObjectsAreReachable(true); - rp.setRefFilter(new HidePrivateFilter()); + rp.setAdvertiseRefsHook(new HidePrivateHook()); return rp; } }; @@ -259,7 +260,7 @@ public class ReceivePackRefFilterTest extends LocalDiskRepositoryTestCase { final ReceivePack rp = new ReceivePack(dst); rp.setCheckReceivedObjects(true); rp.setCheckReferencedObjectsAreReachable(true); - rp.setRefFilter(new HidePrivateFilter()); + rp.setAdvertiseRefsHook(new HidePrivateHook()); try { receive(rp, inBuf, outBuf); fail("Expected UnpackException"); @@ -317,7 +318,7 @@ public class ReceivePackRefFilterTest extends LocalDiskRepositoryTestCase { final ReceivePack rp = new ReceivePack(dst); rp.setCheckReceivedObjects(true); rp.setCheckReferencedObjectsAreReachable(true); - rp.setRefFilter(new HidePrivateFilter()); + rp.setAdvertiseRefsHook(new HidePrivateHook()); try { receive(rp, inBuf, outBuf); fail("Expected UnpackException"); @@ -367,7 +368,7 @@ public class ReceivePackRefFilterTest extends LocalDiskRepositoryTestCase { final ReceivePack rp = new ReceivePack(dst); rp.setCheckReceivedObjects(true); rp.setCheckReferencedObjectsAreReachable(true); - rp.setRefFilter(new HidePrivateFilter()); + rp.setAdvertiseRefsHook(new HidePrivateHook()); try { receive(rp, inBuf, outBuf); fail("Expected UnpackException"); @@ -418,7 +419,7 @@ public class ReceivePackRefFilterTest extends LocalDiskRepositoryTestCase { final ReceivePack rp = new ReceivePack(dst); rp.setCheckReceivedObjects(true); rp.setCheckReferencedObjectsAreReachable(true); - rp.setRefFilter(new HidePrivateFilter()); + rp.setAdvertiseRefsHook(new HidePrivateHook()); try { receive(rp, inBuf, outBuf); fail("Expected UnpackException"); @@ -466,7 +467,7 @@ public class ReceivePackRefFilterTest extends LocalDiskRepositoryTestCase { final ReceivePack rp = new ReceivePack(dst); rp.setCheckReceivedObjects(true); rp.setCheckReferencedObjectsAreReachable(true); - rp.setRefFilter(new HidePrivateFilter()); + rp.setAdvertiseRefsHook(new HidePrivateHook()); try { receive(rp, inBuf, outBuf); fail("Expected UnpackException"); @@ -560,11 +561,11 @@ public class ReceivePackRefFilterTest extends LocalDiskRepositoryTestCase { return new PacketLineIn(new ByteArrayInputStream(buf.toByteArray())); } - private static final class HidePrivateFilter implements RefFilter { - public Map<String, Ref> filter(Map<String, Ref> refs) { - Map<String, Ref> r = new HashMap<String, Ref>(refs); - assertNotNull(r.remove(R_PRIVATE)); - return r; + private static final class HidePrivateHook extends AbstractAdvertiseRefsHook { + public Map<String, Ref> getAdvertisedRefs(Repository r, RevWalk revWalk) { + Map<String, Ref> refs = new HashMap<String, Ref>(r.getAllRefs()); + assertNotNull(refs.remove(R_PRIVATE)); + return refs; } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/RemoteConfigTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/RemoteConfigTest.java index 03a6c019ed..0cada5c7ee 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/RemoteConfigTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/RemoteConfigTest.java @@ -50,6 +50,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; +import java.util.Arrays; import java.util.List; import org.eclipse.jgit.errors.ConfigInvalidException; @@ -455,4 +456,66 @@ public class RemoteConfigTest { + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n" + "\ttimeout = 60\n"); } + + @Test + public void noInsteadOf() throws Exception { + config.setString("remote", "origin", "url", "short:project.git"); + config.setString("url", "https://server/repos/", "name", "short:"); + RemoteConfig rc = new RemoteConfig(config, "origin"); + assertFalse(rc.getURIs().isEmpty()); + assertEquals("short:project.git", rc.getURIs().get(0).toASCIIString()); + } + + @Test + public void singleInsteadOf() throws Exception { + config.setString("remote", "origin", "url", "short:project.git"); + config.setString("url", "https://server/repos/", "insteadOf", "short:"); + RemoteConfig rc = new RemoteConfig(config, "origin"); + assertFalse(rc.getURIs().isEmpty()); + assertEquals("https://server/repos/project.git", rc.getURIs().get(0) + .toASCIIString()); + } + + @Test + public void multipleInsteadOf() throws Exception { + config.setString("remote", "origin", "url", "prefixproject.git"); + config.setStringList("url", "https://server/repos/", "insteadOf", + Arrays.asList("pre", "prefix", "pref", "perf")); + RemoteConfig rc = new RemoteConfig(config, "origin"); + assertFalse(rc.getURIs().isEmpty()); + assertEquals("https://server/repos/project.git", rc.getURIs().get(0) + .toASCIIString()); + } + + @Test + public void noPushInsteadOf() throws Exception { + config.setString("remote", "origin", "pushurl", "short:project.git"); + config.setString("url", "https://server/repos/", "name", "short:"); + RemoteConfig rc = new RemoteConfig(config, "origin"); + assertFalse(rc.getPushURIs().isEmpty()); + assertEquals("short:project.git", rc.getPushURIs().get(0) + .toASCIIString()); + } + + @Test + public void singlePushInsteadOf() throws Exception { + config.setString("remote", "origin", "pushurl", "short:project.git"); + config.setString("url", "https://server/repos/", "pushInsteadOf", + "short:"); + RemoteConfig rc = new RemoteConfig(config, "origin"); + assertFalse(rc.getPushURIs().isEmpty()); + assertEquals("https://server/repos/project.git", rc.getPushURIs() + .get(0).toASCIIString()); + } + + @Test + public void multiplePushInsteadOf() throws Exception { + config.setString("remote", "origin", "pushurl", "prefixproject.git"); + config.setStringList("url", "https://server/repos/", "pushInsteadOf", + Arrays.asList("pre", "prefix", "pref", "perf")); + RemoteConfig rc = new RemoteConfig(config, "origin"); + assertFalse(rc.getPushURIs().isEmpty()); + assertEquals("https://server/repos/project.git", rc.getPushURIs() + .get(0).toASCIIString()); + } } |