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  		submoduleStandalone.close();
94  		submodule_trash = submodule_db.getWorkTree();
95  		addRepoToClose(submodule_db);
96  		writeTrashFile("fileInRoot", "root");
97  		Git rootGit = Git.wrap(db);
98  		rootGit.add().addFilepattern("fileInRoot").call();
99  		rootGit.commit().setMessage("add submodule and root file").call();
100 	}
101 
102 	@Theory
103 	public void testInitiallyClean(IgnoreSubmoduleMode mode)
104 			throws IOException {
105 		IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
106 				new FileTreeIterator(db));
107 		indexDiff.setIgnoreSubmoduleMode(mode);
108 		assertFalse(indexDiff.diff());
109 	}
110 
111 	@Theory
112 	public void testDirtyRootWorktree(IgnoreSubmoduleMode mode)
113 			throws IOException {
114 		writeTrashFile("fileInRoot", "2");
115 
116 		IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
117 				new FileTreeIterator(db));
118 		indexDiff.setIgnoreSubmoduleMode(mode);
119 		assertTrue(indexDiff.diff());
120 	}
121 
122 	@Theory
123 	public void testDirtySubmoduleWorktree(IgnoreSubmoduleMode mode)
124 			throws IOException {
125 		JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "2");
126 		IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
127 				new FileTreeIterator(db));
128 		indexDiff.setIgnoreSubmoduleMode(mode);
129 		if (mode.equals(IgnoreSubmoduleMode.ALL)
130 				|| mode.equals(IgnoreSubmoduleMode.DIRTY))
131 			assertFalse("diff should be false with mode=" + mode,
132 					indexDiff.diff());
133 		else
134 			assertTrue("diff should be true with mode=" + mode,
135 					indexDiff.diff());
136 	}
137 
138 	@Theory
139 	public void testDirtySubmoduleHEAD(IgnoreSubmoduleMode mode)
140 			throws IOException, GitAPIException {
141 		JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "2");
142 		Git submoduleGit = Git.wrap(submodule_db);
143 		submoduleGit.add().addFilepattern("fileInSubmodule").call();
144 		submoduleGit.commit().setMessage("Modified fileInSubmodule").call();
145 
146 		IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
147 				new FileTreeIterator(db));
148 		indexDiff.setIgnoreSubmoduleMode(mode);
149 		if (mode.equals(IgnoreSubmoduleMode.ALL))
150 			assertFalse("diff should be false with mode=" + mode,
151 					indexDiff.diff());
152 		else
153 			assertTrue("diff should be true with mode=" + mode,
154 					indexDiff.diff());
155 	}
156 
157 	@Theory
158 	public void testDirtySubmoduleIndex(IgnoreSubmoduleMode mode)
159 			throws IOException, GitAPIException {
160 		JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "2");
161 		Git submoduleGit = Git.wrap(submodule_db);
162 		submoduleGit.add().addFilepattern("fileInSubmodule").call();
163 
164 		IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
165 				new FileTreeIterator(db));
166 		indexDiff.setIgnoreSubmoduleMode(mode);
167 		if (mode.equals(IgnoreSubmoduleMode.ALL)
168 				|| mode.equals(IgnoreSubmoduleMode.DIRTY))
169 			assertFalse("diff should be false with mode=" + mode,
170 					indexDiff.diff());
171 		else
172 			assertTrue("diff should be true with mode=" + mode,
173 					indexDiff.diff());
174 	}
175 
176 	@Theory
177 	public void testDirtySubmoduleIndexAndWorktree(IgnoreSubmoduleMode mode)
178 			throws IOException, GitAPIException, NoWorkTreeException {
179 		JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "2");
180 		Git submoduleGit = Git.wrap(submodule_db);
181 		submoduleGit.add().addFilepattern("fileInSubmodule").call();
182 		JGitTestUtil.writeTrashFile(submodule_db, "fileInSubmodule", "3");
183 
184 		IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
185 				new FileTreeIterator(db));
186 		indexDiff.setIgnoreSubmoduleMode(mode);
187 		if (mode.equals(IgnoreSubmoduleMode.ALL)
188 				|| mode.equals(IgnoreSubmoduleMode.DIRTY))
189 			assertFalse("diff should be false with mode=" + mode,
190 					indexDiff.diff());
191 		else
192 			assertTrue("diff should be true with mode=" + mode,
193 					indexDiff.diff());
194 	}
195 
196 	@Theory
197 	public void testDirtySubmoduleWorktreeUntracked(IgnoreSubmoduleMode mode)
198 			throws IOException {
199 		JGitTestUtil.writeTrashFile(submodule_db, "additionalFileInSubmodule",
200 				"2");
201 		IndexDiff indexDiff = new IndexDiff(db, Constants.HEAD,
202 				new FileTreeIterator(db));
203 		indexDiff.setIgnoreSubmoduleMode(mode);
204 		if (mode.equals(IgnoreSubmoduleMode.ALL)
205 				|| mode.equals(IgnoreSubmoduleMode.DIRTY)
206 				|| mode.equals(IgnoreSubmoduleMode.UNTRACKED))
207 			assertFalse("diff should be false with mode=" + mode,
208 					indexDiff.diff());
209 		else
210 			assertTrue("diff should be true with mode=" + mode,
211 					indexDiff.diff());
212 	}
213 }