12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136 |
- /*
- * Copyright (C) 2010, Stefan Lay <stefan.lay@sap.com>
- * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
- * 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.eclipse.jgit.util.FileUtils.RECURSIVE;
- import static org.junit.Assert.assertEquals;
- import static org.junit.Assert.assertNotNull;
- import static org.junit.Assert.assertTrue;
- import static org.junit.Assert.fail;
-
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.Set;
-
- import org.eclipse.jgit.api.errors.FilterFailedException;
- import org.eclipse.jgit.api.errors.GitAPIException;
- import org.eclipse.jgit.api.errors.NoFilepatternException;
- import org.eclipse.jgit.dircache.DirCache;
- import org.eclipse.jgit.dircache.DirCacheBuilder;
- import org.eclipse.jgit.dircache.DirCacheEntry;
- import org.eclipse.jgit.junit.JGitTestUtil;
- import org.eclipse.jgit.junit.RepositoryTestCase;
- import org.eclipse.jgit.lib.*;
- import org.eclipse.jgit.revwalk.RevCommit;
- import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
- import org.eclipse.jgit.treewalk.TreeWalk;
- import org.eclipse.jgit.treewalk.WorkingTreeOptions;
- import org.eclipse.jgit.util.FS;
- import org.eclipse.jgit.util.FileUtils;
- import org.junit.Test;
-
- public class AddCommandTest extends RepositoryTestCase {
-
- @Test
- public void testAddNothing() throws GitAPIException {
- try (Git git = new Git(db)) {
- git.add().call();
- fail("Expected IllegalArgumentException");
- } catch (NoFilepatternException e) {
- // expected
- }
-
- }
-
- @Test
- public void testAddNonExistingSingleFile() throws GitAPIException {
- try (Git git = new Git(db)) {
- DirCache dc = git.add().addFilepattern("a.txt").call();
- assertEquals(0, dc.getEntryCount());
- }
- }
-
- @Test
- public void testAddExistingSingleFile() throws IOException, GitAPIException {
- File file = new File(db.getWorkTree(), "a.txt");
- FileUtils.createNewFile(file);
- PrintWriter writer = new PrintWriter(file);
- writer.print("content");
- writer.close();
-
- try (Git git = new Git(db)) {
- git.add().addFilepattern("a.txt").call();
-
- assertEquals(
- "[a.txt, mode:100644, content:content]",
- indexState(CONTENT));
- }
- }
-
- @Test
- public void testCleanFilter() throws IOException,
- GitAPIException {
- writeTrashFile(".gitattributes", "*.txt filter=tstFilter");
- writeTrashFile("src/a.tmp", "foo");
- // Caution: we need a trailing '\n' since sed on mac always appends
- // linefeeds if missing
- writeTrashFile("src/a.txt", "foo\n");
- File script = writeTempFile("sed s/o/e/g");
-
- try (Git git = new Git(db)) {
- StoredConfig config = git.getRepository().getConfig();
- config.setString("filter", "tstFilter", "clean",
- "sh " + slashify(script.getPath()));
- config.save();
-
- git.add().addFilepattern("src/a.txt").addFilepattern("src/a.tmp")
- .call();
-
- assertEquals(
- "[src/a.tmp, mode:100644, content:foo][src/a.txt, mode:100644, content:fee\n]",
- indexState(CONTENT));
- }
- }
-
- @Test
- public void testCleanFilterEnvironment()
- throws IOException, GitAPIException {
- writeTrashFile(".gitattributes", "*.txt filter=tstFilter");
- writeTrashFile("src/a.txt", "foo");
- File script = writeTempFile("echo $GIT_DIR; echo 1 >xyz");
-
- try (Git git = new Git(db)) {
- StoredConfig config = git.getRepository().getConfig();
- config.setString("filter", "tstFilter", "clean",
- "sh " + slashify(script.getPath()));
- config.save();
- git.add().addFilepattern("src/a.txt").call();
-
- String gitDir = db.getDirectory().getAbsolutePath();
- assertEquals("[src/a.txt, mode:100644, content:" + gitDir
- + "\n]", indexState(CONTENT));
- assertTrue(new File(db.getWorkTree(), "xyz").exists());
- }
- }
-
- @Test
- public void testMultipleCleanFilter() throws IOException, GitAPIException {
- writeTrashFile(".gitattributes",
- "*.txt filter=tstFilter\n*.tmp filter=tstFilter2");
- // Caution: we need a trailing '\n' since sed on mac always appends
- // linefeeds if missing
- writeTrashFile("src/a.tmp", "foo\n");
- writeTrashFile("src/a.txt", "foo\n");
- File script = writeTempFile("sed s/o/e/g");
- File script2 = writeTempFile("sed s/f/x/g");
-
- try (Git git = new Git(db)) {
- StoredConfig config = git.getRepository().getConfig();
- config.setString("filter", "tstFilter", "clean",
- "sh " + slashify(script.getPath()));
- config.setString("filter", "tstFilter2", "clean",
- "sh " + slashify(script2.getPath()));
- config.save();
-
- git.add().addFilepattern("src/a.txt").addFilepattern("src/a.tmp")
- .call();
-
- assertEquals(
- "[src/a.tmp, mode:100644, content:xoo\n][src/a.txt, mode:100644, content:fee\n]",
- indexState(CONTENT));
-
- // TODO: multiple clean filters for one file???
- }
- }
-
- /**
- * The path of an added file name contains ';' and afterwards malicious
- * commands. Make sure when calling filter commands to properly escape the
- * filenames
- *
- * @throws IOException
- * @throws GitAPIException
- */
- @Test
- public void testCommandInjection() throws IOException, GitAPIException {
- // Caution: we need a trailing '\n' since sed on mac always appends
- // linefeeds if missing
- writeTrashFile("; echo virus", "foo\n");
- File script = writeTempFile("sed s/o/e/g");
-
- try (Git git = new Git(db)) {
- StoredConfig config = git.getRepository().getConfig();
- config.setString("filter", "tstFilter", "clean",
- "sh " + slashify(script.getPath()) + " %f");
- writeTrashFile(".gitattributes", "* filter=tstFilter");
-
- git.add().addFilepattern("; echo virus").call();
- // Without proper escaping the content would be "feovirus". The sed
- // command and the "echo virus" would contribute to the content
- assertEquals("[; echo virus, mode:100644, content:fee\n]",
- indexState(CONTENT));
- }
- }
-
- @Test
- public void testBadCleanFilter() throws IOException, GitAPIException {
- writeTrashFile("a.txt", "foo");
- File script = writeTempFile("sedfoo s/o/e/g");
-
- try (Git git = new Git(db)) {
- StoredConfig config = git.getRepository().getConfig();
- config.setString("filter", "tstFilter", "clean",
- "sh " + script.getPath());
- config.save();
- writeTrashFile(".gitattributes", "*.txt filter=tstFilter");
-
- try {
- git.add().addFilepattern("a.txt").call();
- fail("Didn't received the expected exception");
- } catch (FilterFailedException e) {
- assertEquals(127, e.getReturnCode());
- }
- }
- }
-
- @Test
- public void testBadCleanFilter2() throws IOException, GitAPIException {
- writeTrashFile("a.txt", "foo");
- File script = writeTempFile("sed s/o/e/g");
-
- try (Git git = new Git(db)) {
- StoredConfig config = git.getRepository().getConfig();
- config.setString("filter", "tstFilter", "clean",
- "shfoo " + script.getPath());
- config.save();
- writeTrashFile(".gitattributes", "*.txt filter=tstFilter");
-
- try {
- git.add().addFilepattern("a.txt").call();
- fail("Didn't received the expected exception");
- } catch (FilterFailedException e) {
- assertEquals(127, e.getReturnCode());
- }
- }
- }
-
- @Test
- public void testCleanFilterReturning12() throws IOException,
- GitAPIException {
- writeTrashFile("a.txt", "foo");
- File script = writeTempFile("exit 12");
-
- try (Git git = new Git(db)) {
- StoredConfig config = git.getRepository().getConfig();
- config.setString("filter", "tstFilter", "clean",
- "sh " + slashify(script.getPath()));
- config.save();
- writeTrashFile(".gitattributes", "*.txt filter=tstFilter");
-
- try {
- git.add().addFilepattern("a.txt").call();
- fail("Didn't received the expected exception");
- } catch (FilterFailedException e) {
- assertEquals(12, e.getReturnCode());
- }
- }
- }
-
- @Test
- public void testNotApplicableFilter() throws IOException, GitAPIException {
- writeTrashFile("a.txt", "foo");
- File script = writeTempFile("sed s/o/e/g");
-
- try (Git git = new Git(db)) {
- StoredConfig config = git.getRepository().getConfig();
- config.setString("filter", "tstFilter", "something",
- "sh " + script.getPath());
- config.save();
- writeTrashFile(".gitattributes", "*.txt filter=tstFilter");
-
- git.add().addFilepattern("a.txt").call();
-
- assertEquals("[a.txt, mode:100644, content:foo]",
- indexState(CONTENT));
- }
- }
-
- private File writeTempFile(String body) throws IOException {
- File f = File.createTempFile("AddCommandTest_", "");
- JGitTestUtil.write(f, body);
- return f;
- }
-
- @Test
- public void testAddExistingSingleSmallFileWithNewLine() throws IOException,
- GitAPIException {
- File file = new File(db.getWorkTree(), "a.txt");
- FileUtils.createNewFile(file);
- PrintWriter writer = new PrintWriter(file);
- writer.print("row1\r\nrow2");
- writer.close();
-
- try (Git git = new Git(db)) {
- db.getConfig().setString("core", null, "autocrlf", "false");
- git.add().addFilepattern("a.txt").call();
- assertEquals("[a.txt, mode:100644, content:row1\r\nrow2]",
- indexState(CONTENT));
- db.getConfig().setString("core", null, "autocrlf", "true");
- git.add().addFilepattern("a.txt").call();
- assertEquals("[a.txt, mode:100644, content:row1\nrow2]",
- indexState(CONTENT));
- db.getConfig().setString("core", null, "autocrlf", "input");
- git.add().addFilepattern("a.txt").call();
- assertEquals("[a.txt, mode:100644, content:row1\nrow2]",
- indexState(CONTENT));
- }
- }
-
- @Test
- public void testAddExistingSingleMediumSizeFileWithNewLine()
- throws IOException, GitAPIException {
- 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", "");
- try (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,
- GitAPIException {
- File file = new File(db.getWorkTree(), "a.txt");
- FileUtils.createNewFile(file);
- PrintWriter writer = new PrintWriter(file);
- writer.print("row1\r\nrow2\u0000");
- writer.close();
-
- try (Git git = new Git(db)) {
- db.getConfig().setString("core", null, "autocrlf", "false");
- git.add().addFilepattern("a.txt").call();
- assertEquals("[a.txt, mode:100644, content:row1\r\nrow2\u0000]",
- indexState(CONTENT));
- db.getConfig().setString("core", null, "autocrlf", "true");
- git.add().addFilepattern("a.txt").call();
- assertEquals("[a.txt, mode:100644, content:row1\r\nrow2\u0000]",
- indexState(CONTENT));
- db.getConfig().setString("core", null, "autocrlf", "input");
- git.add().addFilepattern("a.txt").call();
- assertEquals("[a.txt, mode:100644, content:row1\r\nrow2\u0000]",
- indexState(CONTENT));
- }
- }
-
- @Test
- public void testAddExistingSingleFileInSubDir() throws IOException,
- GitAPIException {
- FileUtils.mkdir(new File(db.getWorkTree(), "sub"));
- File file = new File(db.getWorkTree(), "sub/a.txt");
- FileUtils.createNewFile(file);
- PrintWriter writer = new PrintWriter(file);
- writer.print("content");
- writer.close();
-
- try (Git git = new Git(db)) {
- git.add().addFilepattern("sub/a.txt").call();
-
- assertEquals(
- "[sub/a.txt, mode:100644, content:content]",
- indexState(CONTENT));
- }
- }
-
- @Test
- public void testAddExistingSingleFileTwice() throws IOException,
- GitAPIException {
- File file = new File(db.getWorkTree(), "a.txt");
- FileUtils.createNewFile(file);
- PrintWriter writer = new PrintWriter(file);
- writer.print("content");
- writer.close();
-
- try (Git git = new Git(db)) {
- DirCache dc = git.add().addFilepattern("a.txt").call();
-
- dc.getEntry(0).getObjectId();
-
- writer = new PrintWriter(file);
- writer.print("other content");
- writer.close();
-
- dc = git.add().addFilepattern("a.txt").call();
-
- assertEquals(
- "[a.txt, mode:100644, content:other content]",
- indexState(CONTENT));
- }
- }
-
- @Test
- public void testAddExistingSingleFileTwiceWithCommit() throws Exception {
- File file = new File(db.getWorkTree(), "a.txt");
- FileUtils.createNewFile(file);
- PrintWriter writer = new PrintWriter(file);
- writer.print("content");
- writer.close();
-
- try (Git git = new Git(db)) {
- DirCache dc = git.add().addFilepattern("a.txt").call();
-
- dc.getEntry(0).getObjectId();
-
- git.commit().setMessage("commit a.txt").call();
-
- writer = new PrintWriter(file);
- writer.print("other content");
- writer.close();
-
- dc = git.add().addFilepattern("a.txt").call();
-
- assertEquals(
- "[a.txt, mode:100644, content:other content]",
- indexState(CONTENT));
- }
- }
-
- @Test
- public void testAddRemovedFile() throws Exception {
- File file = new File(db.getWorkTree(), "a.txt");
- FileUtils.createNewFile(file);
- PrintWriter writer = new PrintWriter(file);
- writer.print("content");
- writer.close();
-
- try (Git git = new Git(db)) {
- DirCache dc = git.add().addFilepattern("a.txt").call();
-
- dc.getEntry(0).getObjectId();
- FileUtils.delete(file);
-
- // is supposed to do nothing
- dc = git.add().addFilepattern("a.txt").call();
-
- assertEquals(
- "[a.txt, mode:100644, content:content]",
- indexState(CONTENT));
- }
- }
-
- @Test
- public void testAddRemovedCommittedFile() throws Exception {
- File file = new File(db.getWorkTree(), "a.txt");
- FileUtils.createNewFile(file);
- PrintWriter writer = new PrintWriter(file);
- writer.print("content");
- writer.close();
-
- try (Git git = new Git(db)) {
- DirCache dc = git.add().addFilepattern("a.txt").call();
-
- git.commit().setMessage("commit a.txt").call();
-
- dc.getEntry(0).getObjectId();
- FileUtils.delete(file);
-
- // is supposed to do nothing
- dc = git.add().addFilepattern("a.txt").call();
-
- assertEquals(
- "[a.txt, mode:100644, content:content]",
- indexState(CONTENT));
- }
- }
-
- @Test
- public void testAddWithConflicts() throws Exception {
- // prepare conflict
-
- File file = new File(db.getWorkTree(), "a.txt");
- FileUtils.createNewFile(file);
- PrintWriter writer = new PrintWriter(file);
- writer.print("content");
- writer.close();
-
- File file2 = new File(db.getWorkTree(), "b.txt");
- FileUtils.createNewFile(file2);
- writer = new PrintWriter(file2);
- writer.print("content b");
- writer.close();
-
- ObjectInserter newObjectInserter = db.newObjectInserter();
- DirCache dc = db.lockDirCache();
- DirCacheBuilder builder = dc.builder();
-
- addEntryToBuilder("b.txt", file2, newObjectInserter, builder, 0);
- addEntryToBuilder("a.txt", file, newObjectInserter, builder, 1);
-
- writer = new PrintWriter(file);
- writer.print("other content");
- writer.close();
- addEntryToBuilder("a.txt", file, newObjectInserter, builder, 3);
-
- writer = new PrintWriter(file);
- writer.print("our content");
- writer.close();
- addEntryToBuilder("a.txt", file, newObjectInserter, builder, 2)
- .getObjectId();
-
- builder.commit();
-
- assertEquals(
- "[a.txt, mode:100644, stage:1, content:content]" +
- "[a.txt, mode:100644, stage:2, content:our content]" +
- "[a.txt, mode:100644, stage:3, content:other content]" +
- "[b.txt, mode:100644, content:content b]",
- indexState(CONTENT));
-
- // now the test begins
-
- try (Git git = new Git(db)) {
- dc = git.add().addFilepattern("a.txt").call();
-
- assertEquals(
- "[a.txt, mode:100644, content:our content]" +
- "[b.txt, mode:100644, content:content b]",
- indexState(CONTENT));
- }
- }
-
- @Test
- public void testAddTwoFiles() throws Exception {
- File file = new File(db.getWorkTree(), "a.txt");
- FileUtils.createNewFile(file);
- PrintWriter writer = new PrintWriter(file);
- writer.print("content");
- writer.close();
-
- File file2 = new File(db.getWorkTree(), "b.txt");
- FileUtils.createNewFile(file2);
- writer = new PrintWriter(file2);
- writer.print("content b");
- writer.close();
-
- try (Git git = new Git(db)) {
- git.add().addFilepattern("a.txt").addFilepattern("b.txt").call();
- assertEquals(
- "[a.txt, mode:100644, content:content]" +
- "[b.txt, mode:100644, content:content b]",
- indexState(CONTENT));
- }
- }
-
- @Test
- public void testAddFolder() throws Exception {
- FileUtils.mkdir(new File(db.getWorkTree(), "sub"));
- File file = new File(db.getWorkTree(), "sub/a.txt");
- FileUtils.createNewFile(file);
- PrintWriter writer = new PrintWriter(file);
- writer.print("content");
- writer.close();
-
- File file2 = new File(db.getWorkTree(), "sub/b.txt");
- FileUtils.createNewFile(file2);
- writer = new PrintWriter(file2);
- writer.print("content b");
- writer.close();
-
- try (Git git = new Git(db)) {
- git.add().addFilepattern("sub").call();
- assertEquals(
- "[sub/a.txt, mode:100644, content:content]" +
- "[sub/b.txt, mode:100644, content:content b]",
- indexState(CONTENT));
- }
- }
-
- @Test
- public void testAddIgnoredFile() throws Exception {
- FileUtils.mkdir(new File(db.getWorkTree(), "sub"));
- File file = new File(db.getWorkTree(), "sub/a.txt");
- FileUtils.createNewFile(file);
- PrintWriter writer = new PrintWriter(file);
- writer.print("content");
- writer.close();
-
- File ignoreFile = new File(db.getWorkTree(), ".gitignore");
- FileUtils.createNewFile(ignoreFile);
- writer = new PrintWriter(ignoreFile);
- writer.print("sub/b.txt");
- writer.close();
-
- File file2 = new File(db.getWorkTree(), "sub/b.txt");
- FileUtils.createNewFile(file2);
- writer = new PrintWriter(file2);
- writer.print("content b");
- writer.close();
-
- try (Git git = new Git(db)) {
- git.add().addFilepattern("sub").call();
-
- assertEquals(
- "[sub/a.txt, mode:100644, content:content]",
- indexState(CONTENT));
- }
- }
-
- @Test
- public void testAddWholeRepo() throws Exception {
- FileUtils.mkdir(new File(db.getWorkTree(), "sub"));
- File file = new File(db.getWorkTree(), "sub/a.txt");
- FileUtils.createNewFile(file);
- PrintWriter writer = new PrintWriter(file);
- writer.print("content");
- writer.close();
-
- File file2 = new File(db.getWorkTree(), "sub/b.txt");
- FileUtils.createNewFile(file2);
- writer = new PrintWriter(file2);
- writer.print("content b");
- writer.close();
-
- try (Git git = new Git(db)) {
- git.add().addFilepattern(".").call();
- assertEquals(
- "[sub/a.txt, mode:100644, content:content]" +
- "[sub/b.txt, mode:100644, content:content b]",
- indexState(CONTENT));
- }
- }
-
- // the same three cases as in testAddWithParameterUpdate
- // file a exists in workdir and in index -> added
- // file b exists not in workdir but in index -> unchanged
- // file c exists in workdir but not in index -> added
- @Test
- public void testAddWithoutParameterUpdate() throws Exception {
- FileUtils.mkdir(new File(db.getWorkTree(), "sub"));
- File file = new File(db.getWorkTree(), "sub/a.txt");
- FileUtils.createNewFile(file);
- PrintWriter writer = new PrintWriter(file);
- writer.print("content");
- writer.close();
-
- File file2 = new File(db.getWorkTree(), "sub/b.txt");
- FileUtils.createNewFile(file2);
- writer = new PrintWriter(file2);
- writer.print("content b");
- writer.close();
-
- try (Git git = new Git(db)) {
- git.add().addFilepattern("sub").call();
-
- assertEquals(
- "[sub/a.txt, mode:100644, content:content]" +
- "[sub/b.txt, mode:100644, content:content b]",
- indexState(CONTENT));
-
- git.commit().setMessage("commit").call();
-
- // new unstaged file sub/c.txt
- File file3 = new File(db.getWorkTree(), "sub/c.txt");
- FileUtils.createNewFile(file3);
- writer = new PrintWriter(file3);
- writer.print("content c");
- writer.close();
-
- // file sub/a.txt is modified
- writer = new PrintWriter(file);
- writer.print("modified content");
- writer.close();
-
- // file sub/b.txt is deleted
- FileUtils.delete(file2);
-
- git.add().addFilepattern("sub").call();
- // change in sub/a.txt is staged
- // deletion of sub/b.txt is not staged
- // sub/c.txt is staged
- assertEquals(
- "[sub/a.txt, mode:100644, content:modified content]" +
- "[sub/b.txt, mode:100644, content:content b]" +
- "[sub/c.txt, mode:100644, content:content c]",
- indexState(CONTENT));
- }
- }
-
- // file a exists in workdir and in index -> added
- // file b exists not in workdir but in index -> deleted
- // file c exists in workdir but not in index -> unchanged
- @Test
- public void testAddWithParameterUpdate() throws Exception {
- FileUtils.mkdir(new File(db.getWorkTree(), "sub"));
- File file = new File(db.getWorkTree(), "sub/a.txt");
- FileUtils.createNewFile(file);
- PrintWriter writer = new PrintWriter(file);
- writer.print("content");
- writer.close();
-
- File file2 = new File(db.getWorkTree(), "sub/b.txt");
- FileUtils.createNewFile(file2);
- writer = new PrintWriter(file2);
- writer.print("content b");
- writer.close();
-
- try (Git git = new Git(db)) {
- git.add().addFilepattern("sub").call();
-
- assertEquals(
- "[sub/a.txt, mode:100644, content:content]" +
- "[sub/b.txt, mode:100644, content:content b]",
- indexState(CONTENT));
-
- git.commit().setMessage("commit").call();
-
- // new unstaged file sub/c.txt
- File file3 = new File(db.getWorkTree(), "sub/c.txt");
- FileUtils.createNewFile(file3);
- writer = new PrintWriter(file3);
- writer.print("content c");
- writer.close();
-
- // file sub/a.txt is modified
- writer = new PrintWriter(file);
- writer.print("modified content");
- writer.close();
-
- FileUtils.delete(file2);
-
- // change in sub/a.txt is staged
- // deletion of sub/b.txt is staged
- // sub/c.txt is not staged
- git.add().addFilepattern("sub").setUpdate(true).call();
- // change in sub/a.txt is staged
- assertEquals(
- "[sub/a.txt, mode:100644, content:modified content]",
- indexState(CONTENT));
- }
- }
-
- @Test
- public void testAssumeUnchanged() throws Exception {
- try (Git git = new Git(db)) {
- String path = "a.txt";
- writeTrashFile(path, "content");
- git.add().addFilepattern(path).call();
- String path2 = "b.txt";
- writeTrashFile(path2, "content");
- git.add().addFilepattern(path2).call();
- git.commit().setMessage("commit").call();
- assertEquals("[a.txt, mode:100644, content:"
- + "content, assume-unchanged:false]"
- + "[b.txt, mode:100644, content:content, "
- + "assume-unchanged:false]", indexState(CONTENT
- | ASSUME_UNCHANGED));
- assumeUnchanged(path2);
- assertEquals("[a.txt, mode:100644, content:content, "
- + "assume-unchanged:false][b.txt, mode:100644, "
- + "content:content, assume-unchanged:true]", indexState(CONTENT
- | ASSUME_UNCHANGED));
- writeTrashFile(path, "more content");
- writeTrashFile(path2, "more content");
-
- git.add().addFilepattern(".").call();
-
- assertEquals("[a.txt, mode:100644, content:more content,"
- + " assume-unchanged:false][b.txt, mode:100644,"
- + " content:content, assume-unchanged:true]",
- indexState(CONTENT
- | ASSUME_UNCHANGED));
- }
- }
-
- @Test
- public void testReplaceFileWithDirectory()
- throws IOException, NoFilepatternException, GitAPIException {
- try (Git git = new Git(db)) {
- writeTrashFile("df", "before replacement");
- git.add().addFilepattern("df").call();
- assertEquals("[df, mode:100644, content:before replacement]",
- indexState(CONTENT));
- FileUtils.delete(new File(db.getWorkTree(), "df"));
- writeTrashFile("df/f", "after replacement");
- git.add().addFilepattern("df").call();
- assertEquals("[df/f, mode:100644, content:after replacement]",
- indexState(CONTENT));
- }
- }
-
- @Test
- public void testReplaceDirectoryWithFile()
- throws IOException, NoFilepatternException, GitAPIException {
- try (Git git = new Git(db)) {
- writeTrashFile("df/f", "before replacement");
- git.add().addFilepattern("df").call();
- assertEquals("[df/f, mode:100644, content:before replacement]",
- indexState(CONTENT));
- FileUtils.delete(new File(db.getWorkTree(), "df"), RECURSIVE);
- writeTrashFile("df", "after replacement");
- git.add().addFilepattern("df").call();
- assertEquals("[df, mode:100644, content:after replacement]",
- indexState(CONTENT));
- }
- }
-
- @Test
- public void testReplaceFileByPartOfDirectory()
- throws IOException, NoFilepatternException, GitAPIException {
- try (Git git = new Git(db)) {
- writeTrashFile("src/main", "df", "before replacement");
- writeTrashFile("src/main", "z", "z");
- writeTrashFile("z", "z2");
- git.add().addFilepattern("src/main/df")
- .addFilepattern("src/main/z")
- .addFilepattern("z")
- .call();
- assertEquals(
- "[src/main/df, mode:100644, content:before replacement]" +
- "[src/main/z, mode:100644, content:z]" +
- "[z, mode:100644, content:z2]",
- indexState(CONTENT));
- FileUtils.delete(new File(db.getWorkTree(), "src/main/df"));
- writeTrashFile("src/main/df", "a", "after replacement");
- writeTrashFile("src/main/df", "b", "unrelated file");
- git.add().addFilepattern("src/main/df/a").call();
- assertEquals(
- "[src/main/df/a, mode:100644, content:after replacement]" +
- "[src/main/z, mode:100644, content:z]" +
- "[z, mode:100644, content:z2]",
- indexState(CONTENT));
- }
- }
-
- @Test
- public void testReplaceDirectoryConflictsWithFile()
- throws IOException, NoFilepatternException, GitAPIException {
- DirCache dc = db.lockDirCache();
- try (ObjectInserter oi = db.newObjectInserter()) {
- DirCacheBuilder builder = dc.builder();
- File f = writeTrashFile("a", "df", "content");
- addEntryToBuilder("a", f, oi, builder, 1);
-
- f = writeTrashFile("a", "df", "other content");
- addEntryToBuilder("a/df", f, oi, builder, 3);
-
- f = writeTrashFile("a", "df", "our content");
- addEntryToBuilder("a/df", f, oi, builder, 2);
-
- f = writeTrashFile("z", "z");
- addEntryToBuilder("z", f, oi, builder, 0);
- builder.commit();
- }
- assertEquals(
- "[a, mode:100644, stage:1, content:content]" +
- "[a/df, mode:100644, stage:2, content:our content]" +
- "[a/df, mode:100644, stage:3, content:other content]" +
- "[z, mode:100644, content:z]",
- indexState(CONTENT));
-
- try (Git git = new Git(db)) {
- FileUtils.delete(new File(db.getWorkTree(), "a"), RECURSIVE);
- writeTrashFile("a", "merged");
- git.add().addFilepattern("a").call();
- assertEquals("[a, mode:100644, content:merged]" +
- "[z, mode:100644, content:z]",
- indexState(CONTENT));
- }
- }
-
- @Test
- public void testExecutableRetention() throws Exception {
- StoredConfig config = db.getConfig();
- config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
- ConfigConstants.CONFIG_KEY_FILEMODE, true);
- config.save();
-
- FS executableFs = new FS() {
-
- public boolean supportsExecute() {
- return true;
- }
-
- public boolean setExecute(File f, boolean canExec) {
- return true;
- }
-
- public ProcessBuilder runInShell(String cmd, String[] args) {
- return null;
- }
-
- public boolean retryFailedLockFileCommit() {
- return false;
- }
-
- public FS newInstance() {
- return this;
- }
-
- protected File discoverGitExe() {
- return null;
- }
-
- public boolean canExecute(File f) {
- return true;
- }
-
- @Override
- public boolean isCaseSensitive() {
- return false;
- }
- };
-
- Git git = Git.open(db.getDirectory(), executableFs);
- String path = "a.txt";
- writeTrashFile(path, "content");
- git.add().addFilepattern(path).call();
- RevCommit commit1 = git.commit().setMessage("commit").call();
- TreeWalk walk = TreeWalk.forPath(db, path, commit1.getTree());
- assertNotNull(walk);
- assertEquals(FileMode.EXECUTABLE_FILE, walk.getFileMode(0));
-
- FS nonExecutableFs = new FS() {
-
- public boolean supportsExecute() {
- return false;
- }
-
- public boolean setExecute(File f, boolean canExec) {
- return false;
- }
-
- public ProcessBuilder runInShell(String cmd, String[] args) {
- return null;
- }
-
- public boolean retryFailedLockFileCommit() {
- return false;
- }
-
- public FS newInstance() {
- return this;
- }
-
- protected File discoverGitExe() {
- return null;
- }
-
- public boolean canExecute(File f) {
- return false;
- }
-
- @Override
- public boolean isCaseSensitive() {
- return false;
- }
- };
-
- config = db.getConfig();
- config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
- ConfigConstants.CONFIG_KEY_FILEMODE, false);
- config.save();
-
- Git git2 = Git.open(db.getDirectory(), nonExecutableFs);
- writeTrashFile(path, "content2");
- git2.add().addFilepattern(path).call();
- RevCommit commit2 = git2.commit().setMessage("commit2").call();
- walk = TreeWalk.forPath(db, path, commit2.getTree());
- assertNotNull(walk);
- assertEquals(FileMode.EXECUTABLE_FILE, walk.getFileMode(0));
- }
-
- @Test
- public void testAddGitlink() throws Exception {
- createNestedRepo("git-link-dir");
- try (Git git = new Git(db)) {
- git.add().addFilepattern("git-link-dir").call();
-
- assertEquals(
- "[git-link-dir, mode:160000]",
- indexState(0));
- Set<String> untrackedFiles = git.status().call().getUntracked();
- assert (untrackedFiles.isEmpty());
- }
-
- }
-
- @Test
- public void testAddSubrepoWithDirNoGitlinks() throws Exception {
- createNestedRepo("nested-repo");
-
- // Set DIR_NO_GITLINKS
- StoredConfig config = db.getConfig();
- config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
- ConfigConstants.CONFIG_KEY_DIRNOGITLINKS, true);
- config.save();
-
- assert (db.getConfig().get(WorkingTreeOptions.KEY).isDirNoGitLinks());
-
- try (Git git = new Git(db)) {
- git.add().addFilepattern("nested-repo").call();
-
- assertEquals(
- "[nested-repo/README1.md, mode:100644]" +
- "[nested-repo/README2.md, mode:100644]",
- indexState(0));
- }
-
- // Turn off DIR_NO_GITLINKS, ensure nested-repo is still treated as
- // a normal directory
- // Set DIR_NO_GITLINKS
- config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
- ConfigConstants.CONFIG_KEY_DIRNOGITLINKS, false);
- config.save();
-
- writeTrashFile("nested-repo", "README3.md", "content");
-
- try (Git git = new Git(db)) {
- git.add().addFilepattern("nested-repo").call();
-
- assertEquals(
- "[nested-repo/README1.md, mode:100644]" +
- "[nested-repo/README2.md, mode:100644]" +
- "[nested-repo/README3.md, mode:100644]",
- indexState(0));
- }
- }
-
- @Test
- public void testAddGitlinkDoesNotChange() throws Exception {
- createNestedRepo("nested-repo");
-
- try (Git git = new Git(db)) {
- git.add().addFilepattern("nested-repo").call();
-
- assertEquals(
- "[nested-repo, mode:160000]",
- indexState(0));
- }
-
- // Set DIR_NO_GITLINKS
- StoredConfig config = db.getConfig();
- config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
- ConfigConstants.CONFIG_KEY_DIRNOGITLINKS, true);
- config.save();
-
- assert (db.getConfig().get(WorkingTreeOptions.KEY).isDirNoGitLinks());
-
- try (Git git = new Git(db)) {
- git.add().addFilepattern("nested-repo").call();
-
- assertEquals(
- "[nested-repo, mode:160000]",
- indexState(0));
- }
- }
-
- private static DirCacheEntry addEntryToBuilder(String path, File file,
- ObjectInserter newObjectInserter, DirCacheBuilder builder, int stage)
- throws IOException {
- FileInputStream inputStream = new FileInputStream(file);
- ObjectId id = newObjectInserter.insert(
- Constants.OBJ_BLOB, file.length(), inputStream);
- inputStream.close();
- DirCacheEntry entry = new DirCacheEntry(path, stage);
- entry.setObjectId(id);
- entry.setFileMode(FileMode.REGULAR_FILE);
- entry.setLastModified(file.lastModified());
- entry.setLength((int) file.length());
-
- builder.add(entry);
- return entry;
- }
-
- private void assumeUnchanged(String path) throws IOException {
- final DirCache dirc = db.lockDirCache();
- final DirCacheEntry ent = dirc.getEntry(path);
- if (ent != null)
- ent.setAssumeValid(true);
- dirc.write();
- if (!dirc.commit())
- throw new IOException("could not commit");
- }
-
- private void createNestedRepo(String path) throws IOException {
- File gitLinkDir = new File(db.getWorkTree(), path);
- FileUtils.mkdir(gitLinkDir);
-
- FileRepositoryBuilder nestedBuilder = new FileRepositoryBuilder();
- nestedBuilder.setWorkTree(gitLinkDir);
-
- Repository nestedRepo = nestedBuilder.build();
- nestedRepo.create();
-
- writeTrashFile(path, "README1.md", "content");
- writeTrashFile(path, "README2.md", "content");
-
- // Commit these changes in the subrepo
- try (Git git = new Git(nestedRepo)) {
- git.add().addFilepattern(".").call();
- git.commit().setMessage("subrepo commit").call();
- } catch (GitAPIException e) {
- throw new RuntimeException(e);
- }
- }
- }
|