View Javadoc
1   /*
2    * Copyright (C) 2014, Christian Halstrick <christian.halstrick@sap.com>
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.lib;
45  
46  import static org.junit.Assert.assertFalse;
47  import static org.junit.Assert.assertTrue;
48  
49  import java.io.File;
50  import java.io.IOException;
51  
52  import org.eclipse.jgit.api.Git;
53  import org.eclipse.jgit.api.errors.GitAPIException;
54  import org.eclipse.jgit.errors.NoWorkTreeException;
55  import org.eclipse.jgit.internal.storage.file.FileRepository;
56  import org.eclipse.jgit.junit.JGitTestUtil;
57  import org.eclipse.jgit.junit.RepositoryTestCase;
58  import org.eclipse.jgit.submodule.SubmoduleWalk.IgnoreSubmoduleMode;
59  import org.eclipse.jgit.treewalk.FileTreeIterator;
60  import org.junit.Before;
61  import org.junit.experimental.theories.DataPoints;
62  import org.junit.experimental.theories.Theories;
63  import org.junit.experimental.theories.Theory;
64  import org.junit.runner.RunWith;
65  
66  @RunWith(Theories.class)
67  public class IndexDiffSubmoduleTest extends RepositoryTestCase {
68  	/** a submodule repository inside a root repository */
69  	protected FileRepository submodule_db;
70  
71  	/** Working directory of the submodule repository */
72  	protected File submodule_trash;
73  
74  	@DataPoints
75  	public static IgnoreSubmoduleMode allModes[] = IgnoreSubmoduleMode.values();
76  
77  	@Override
78  	@Before
79  	public void setUp() throws Exception {
80  		super.setUp();
81  		FileRepository submoduleStandalone = createWorkRepository();
82  		JGitTestUtil.writeTrashFile(submoduleStandalone, "fileInSubmodule",
83  				"submodule");
84  		Git submoduleStandaloneGit = Git.wrap(submoduleStandalone);
85  		submoduleStandaloneGit.add().addFilepattern("fileInSubmodule").call();
86  		submoduleStandaloneGit.commit().setMessage("add file to submodule")
87  				.call();
88  
89  		submodule_db = (FileRepository) Git.wrap(db).submoduleAdd()
90  				.setPath("modules/submodule")
91  				.setURI(submoduleStandalone.getDirectory().toURI().toString())
92  				.call();
93  		submodule_trash = submodule_db.getWorkTree();
94  		addRepoToClose(submodule_db);
95  		writeTrashFile("fileInRoot", "root");
96  		Git rootGit = Git.wrap(db);
97  		rootGit.add().addFilepattern("fileInRoot").call();
98  		rootGit.commit().setMessage("add submodule and root file").call();
99  	}
100 
101 	@Theory
102 	public void testInitiallyClean(IgnoreSubmoduleMode mode)
103 			throws IOException {
104 		IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
105 				new FileTreeIterator(db));
106 		indexDiff.setIgnoreSubmoduleMode(mode);
107 		assertFalse(indexDiff.diff());
108 	}
109 
110 	@Theory
111 	public void testDirtyRootWorktree(IgnoreSubmoduleMode mode)
112 			throws IOException {
113 		writeTrashFile("fileInRoot", "2");
114 
115 		IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
116 				new FileTreeIterator(db));
117 		indexDiff.setIgnoreSubmoduleMode(mode);
118 		assertTrue(indexDiff.diff());
119 	}
120 
121 	@Theory
122 	public void testDirtySubmoduleWorktree(IgnoreSubmoduleMode mode)
123 			throws IOException {
124 		JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "2");
125 		IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
126 				new FileTreeIterator(db));
127 		indexDiff.setIgnoreSubmoduleMode(mode);
128 		if (mode.equals(IgnoreSubmoduleMode.ALL)
129 				|| mode.equals(IgnoreSubmoduleMode.DIRTY))
130 			assertFalse("diff should be false with mode=" + mode,
131 					indexDiff.diff());
132 		else
133 			assertTrue("diff should be true with mode=" + mode,
134 					indexDiff.diff());
135 	}
136 
137 	@Theory
138 	public void testDirtySubmoduleHEAD(IgnoreSubmoduleMode mode)
139 			throws IOException, GitAPIException {
140 		JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "2");
141 		Git submoduleGit = Git.wrap(submodule_db);
142 		submoduleGit.add().addFilepattern("fileInSubmodule").call();
143 		submoduleGit.commit().setMessage("Modified fileInSubmodule").call();
144 
145 		IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
146 				new FileTreeIterator(db));
147 		indexDiff.setIgnoreSubmoduleMode(mode);
148 		if (mode.equals(IgnoreSubmoduleMode.ALL))
149 			assertFalse("diff should be false with mode=" + mode,
150 					indexDiff.diff());
151 		else
152 			assertTrue("diff should be true with mode=" + mode,
153 					indexDiff.diff());
154 	}
155 
156 	@Theory
157 	public void testDirtySubmoduleIndex(IgnoreSubmoduleMode mode)
158 			throws IOException, GitAPIException {
159 		JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "2");
160 		Git submoduleGit = Git.wrap(submodule_db);
161 		submoduleGit.add().addFilepattern("fileInSubmodule").call();
162 
163 		IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
164 				new FileTreeIterator(db));
165 		indexDiff.setIgnoreSubmoduleMode(mode);
166 		if (mode.equals(IgnoreSubmoduleMode.ALL)
167 				|| mode.equals(IgnoreSubmoduleMode.DIRTY))
168 			assertFalse("diff should be false with mode=" + mode,
169 					indexDiff.diff());
170 		else
171 			assertTrue("diff should be true with mode=" + mode,
172 					indexDiff.diff());
173 	}
174 
175 	@Theory
176 	public void testDirtySubmoduleIndexAndWorktree(IgnoreSubmoduleMode mode)
177 			throws IOException, GitAPIException, NoWorkTreeException {
178 		JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "2");
179 		Git submoduleGit = Git.wrap(submodule_db);
180 		submoduleGit.add().addFilepattern("fileInSubmodule").call();
181 		JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "3");
182 
183 		IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
184 				new FileTreeIterator(db));
185 		indexDiff.setIgnoreSubmoduleMode(mode);
186 		if (mode.equals(IgnoreSubmoduleMode.ALL)
187 				|| mode.equals(IgnoreSubmoduleMode.DIRTY))
188 			assertFalse("diff should be false with mode=" + mode,
189 					indexDiff.diff());
190 		else
191 			assertTrue("diff should be true with mode=" + mode,
192 					indexDiff.diff());
193 	}
194 
195 	@Theory
196 	public void testDirtySubmoduleWorktreeUntracked(IgnoreSubmoduleMode mode)
197 			throws IOException {
198 		JGitTestUtil.writeTrashFile(submodule_db, "additionalFileInSubmodule",
199 				"2");
200 		IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
201 				new FileTreeIterator(db));
202 		indexDiff.setIgnoreSubmoduleMode(mode);
203 		if (mode.equals(IgnoreSubmoduleMode.ALL)
204 				|| mode.equals(IgnoreSubmoduleMode.DIRTY)
205 				|| mode.equals(IgnoreSubmoduleMode.UNTRACKED))
206 			assertFalse("diff should be false with mode=" + mode,
207 					indexDiff.diff());
208 		else
209 			assertTrue("diff should be true with mode=" + mode,
210 					indexDiff.diff());
211 	}
212 }