View Javadoc
1   /*
2    * Copyright (C) 2011, Christian Halstrick <christian.halstrick@sap.com> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.api;
11  
12  import static org.junit.Assert.assertEquals;
13  import static org.junit.Assert.assertFalse;
14  import static org.junit.Assert.assertTrue;
15  import static org.junit.Assume.assumeTrue;
16  
17  import java.io.File;
18  import java.io.IOException;
19  
20  import org.eclipse.jgit.api.errors.GitAPIException;
21  import org.eclipse.jgit.api.errors.NoFilepatternException;
22  import org.eclipse.jgit.errors.NoWorkTreeException;
23  import org.eclipse.jgit.junit.RepositoryTestCase;
24  import org.eclipse.jgit.lib.Sets;
25  import org.eclipse.jgit.storage.file.FileBasedConfig;
26  import org.eclipse.jgit.util.FS;
27  import org.junit.Test;
28  
29  public class StatusCommandTest extends RepositoryTestCase {
30  
31  	@Test
32  	public void testEmptyStatus() throws NoWorkTreeException,
33  			GitAPIException {
34  		try (Git git = new Git(db)) {
35  			Status stat = git.status().call();
36  			assertEquals(0, stat.getAdded().size());
37  			assertEquals(0, stat.getChanged().size());
38  			assertEquals(0, stat.getMissing().size());
39  			assertEquals(0, stat.getModified().size());
40  			assertEquals(0, stat.getRemoved().size());
41  			assertEquals(0, stat.getUntracked().size());
42  		}
43  	}
44  
45  	@Test
46  	public void testDifferentStates() throws IOException,
47  			NoFilepatternException, GitAPIException {
48  		try (Git git = new Git(db)) {
49  			writeTrashFile("a", "content of a");
50  			writeTrashFile("b", "content of b");
51  			writeTrashFile("c", "content of c");
52  			git.add().addFilepattern("a").addFilepattern("b").call();
53  			Status stat = git.status().call();
54  			assertEquals(Sets.of("a", "b"), stat.getAdded());
55  			assertEquals(0, stat.getChanged().size());
56  			assertEquals(0, stat.getMissing().size());
57  			assertEquals(0, stat.getModified().size());
58  			assertEquals(0, stat.getRemoved().size());
59  			assertEquals(Sets.of("c"), stat.getUntracked());
60  			git.commit().setMessage("initial").call();
61  
62  			writeTrashFile("a", "modified content of a");
63  			writeTrashFile("b", "modified content of b");
64  			writeTrashFile("d", "content of d");
65  			git.add().addFilepattern("a").addFilepattern("d").call();
66  			writeTrashFile("a", "again modified content of a");
67  			stat = git.status().call();
68  			assertEquals(Sets.of("d"), stat.getAdded());
69  			assertEquals(Sets.of("a"), stat.getChanged());
70  			assertEquals(0, stat.getMissing().size());
71  			assertEquals(Sets.of("b", "a"), stat.getModified());
72  			assertEquals(0, stat.getRemoved().size());
73  			assertEquals(Sets.of("c"), stat.getUntracked());
74  			git.add().addFilepattern(".").call();
75  			git.commit().setMessage("second").call();
76  
77  			stat = git.status().call();
78  			assertEquals(0, stat.getAdded().size());
79  			assertEquals(0, stat.getChanged().size());
80  			assertEquals(0, stat.getMissing().size());
81  			assertEquals(0, stat.getModified().size());
82  			assertEquals(0, stat.getRemoved().size());
83  			assertEquals(0, stat.getUntracked().size());
84  
85  			deleteTrashFile("a");
86  			assertFalse(new File(git.getRepository().getWorkTree(), "a").exists());
87  			git.add().addFilepattern("a").setUpdate(true).call();
88  			writeTrashFile("a", "recreated content of a");
89  			stat = git.status().call();
90  			assertEquals(0, stat.getAdded().size());
91  			assertEquals(0, stat.getChanged().size());
92  			assertEquals(0, stat.getMissing().size());
93  			assertEquals(0, stat.getModified().size());
94  			assertEquals(Sets.of("a"), stat.getRemoved());
95  			assertEquals(Sets.of("a"), stat.getUntracked());
96  			git.commit().setMessage("t").call();
97  
98  			writeTrashFile("sub/a", "sub-file");
99  			stat = git.status().call();
100 			assertEquals(1, stat.getUntrackedFolders().size());
101 			assertTrue(stat.getUntrackedFolders().contains("sub"));
102 		}
103 	}
104 
105 	@Test
106 	public void testDifferentStatesWithPaths() throws IOException,
107 			NoFilepatternException, GitAPIException {
108 		try (Git git = new Git(db)) {
109 			writeTrashFile("a", "content of a");
110 			writeTrashFile("D/b", "content of b");
111 			writeTrashFile("D/c", "content of c");
112 			writeTrashFile("D/D/d", "content of d");
113 			git.add().addFilepattern(".").call();
114 
115 			writeTrashFile("a", "new content of a");
116 			writeTrashFile("D/b", "new content of b");
117 			writeTrashFile("D/D/d", "new content of d");
118 
119 
120 			// filter on an not existing path
121 			Status stat = git.status().addPath("x").call();
122 			assertEquals(0, stat.getModified().size());
123 
124 			// filter on an existing file
125 			stat = git.status().addPath("a").call();
126 			assertEquals(Sets.of("a"), stat.getModified());
127 
128 			// filter on an existing folder
129 			stat = git.status().addPath("D").call();
130 			assertEquals(Sets.of("D/b", "D/D/d"), stat.getModified());
131 
132 			// filter on an existing folder and file
133 			stat = git.status().addPath("D/D").addPath("a").call();
134 			assertEquals(Sets.of("a", "D/D/d"), stat.getModified());
135 
136 			// do not filter at all
137 			stat = git.status().call();
138 			assertEquals(Sets.of("a", "D/b", "D/D/d"), stat.getModified());
139 		}
140 	}
141 
142 	@Test
143 	public void testExecutableWithNonNormalizedIndex() throws Exception {
144 		assumeTrue(FS.DETECTED.supportsExecute());
145 		try (Git git = new Git(db)) {
146 			// Commit a file with CR/LF into the index
147 			FileBasedConfig config = db.getConfig();
148 			config.setString("core", null, "autocrlf", "false");
149 			config.save();
150 			File testFile = writeTrashFile("file.txt", "line 1\r\nline 2\r\n");
151 			FS.DETECTED.setExecute(testFile, true);
152 			git.add().addFilepattern("file.txt").call();
153 			git.commit().setMessage("Initial").call();
154 			assertEquals(
155 					"[file.txt, mode:100755, content:line 1\r\nline 2\r\n]",
156 					indexState(CONTENT));
157 			config.setString("core", null, "autocrlf", "true");
158 			config.save();
159 			Status status = git.status().call();
160 			assertTrue("Expected no differences", status.isClean());
161 		}
162 	}
163 
164 	@Test
165 	public void testFolderPrefix() throws Exception {
166 		// "audio" is a prefix of "audio-new" and "audio.new".
167 		try (Git git = new Git(db)) {
168 			// Order here is the git order, but that doesn't really matter.
169 			// They are processed by StatusCommand in this order even if written
170 			// in a different order. Bug 566799 would, when having processed
171 			// audio/foo, remove previously recorded untracked folders that have
172 			// "audio" as a prefix: audio-new and audio.new.
173 			writeTrashFile("audi", "foo", "foo");
174 			writeTrashFile("audio-new", "foo", "foo");
175 			writeTrashFile("audio.new", "foo", "foo");
176 			writeTrashFile("audio", "foo", "foo");
177 			writeTrashFile("audio_new", "foo", "foo");
178 			Status stat = git.status().call();
179 			assertEquals(Sets.of("audi", "audio-new", "audio.new", "audio",
180 					"audio_new"), stat.getUntrackedFolders());
181 		}
182 	}
183 
184 }