You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CGitAttributesTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * Copyright (C) 2017 Thomas Wolf <thomas.wolf@paranor.ch>
  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. package org.eclipse.jgit.attributes;
  44. import static java.nio.charset.StandardCharsets.UTF_8;
  45. import static org.junit.Assert.assertArrayEquals;
  46. import static org.junit.Assert.assertEquals;
  47. import java.io.BufferedInputStream;
  48. import java.io.BufferedReader;
  49. import java.io.ByteArrayInputStream;
  50. import java.io.File;
  51. import java.io.IOException;
  52. import java.io.InputStreamReader;
  53. import java.util.Iterator;
  54. import java.util.LinkedHashMap;
  55. import java.util.Map;
  56. import java.util.Set;
  57. import org.eclipse.jgit.junit.RepositoryTestCase;
  58. import org.eclipse.jgit.lib.StoredConfig;
  59. import org.eclipse.jgit.treewalk.FileTreeIterator;
  60. import org.eclipse.jgit.treewalk.TreeWalk;
  61. import org.eclipse.jgit.treewalk.filter.NotIgnoredFilter;
  62. import org.eclipse.jgit.util.FS;
  63. import org.eclipse.jgit.util.FS.ExecutionResult;
  64. import org.eclipse.jgit.util.RawParseUtils;
  65. import org.eclipse.jgit.util.TemporaryBuffer;
  66. import org.junit.Before;
  67. import org.junit.Test;
  68. /**
  69. * Tests that verify that the attributes of files in a repository are the same
  70. * in JGit and in C-git.
  71. */
  72. public class CGitAttributesTest extends RepositoryTestCase {
  73. @Before
  74. public void initRepo() throws IOException {
  75. // Because we run C-git, we must ensure that global or user exclude
  76. // files cannot influence the tests. So we set core.excludesFile to an
  77. // empty file inside the repository.
  78. StoredConfig config = db.getConfig();
  79. File fakeUserGitignore = writeTrashFile(".fake_user_gitignore", "");
  80. config.setString("core", null, "excludesFile",
  81. fakeUserGitignore.getAbsolutePath());
  82. // Disable case-insensitivity -- JGit doesn't handle that yet.
  83. config.setBoolean("core", null, "ignoreCase", false);
  84. // And try to switch off the global attributes file, too.
  85. config.setString("core", null, "attributesFile",
  86. fakeUserGitignore.getAbsolutePath());
  87. config.save();
  88. }
  89. private void createFiles(String... paths) throws IOException {
  90. for (String path : paths) {
  91. writeTrashFile(path, "x");
  92. }
  93. }
  94. private String toString(TemporaryBuffer b) throws IOException {
  95. return RawParseUtils.decode(b.toByteArray());
  96. }
  97. private Attribute fromString(String key, String value) {
  98. if ("set".equals(value)) {
  99. return new Attribute(key, Attribute.State.SET);
  100. }
  101. if ("unset".equals(value)) {
  102. return new Attribute(key, Attribute.State.UNSET);
  103. }
  104. if ("unspecified".equals(value)) {
  105. return new Attribute(key, Attribute.State.UNSPECIFIED);
  106. }
  107. return new Attribute(key, value);
  108. }
  109. private LinkedHashMap<String, Attributes> cgitAttributes(
  110. Set<String> allFiles) throws Exception {
  111. FS fs = db.getFS();
  112. StringBuilder input = new StringBuilder();
  113. for (String filename : allFiles) {
  114. input.append(filename).append('\n');
  115. }
  116. ProcessBuilder builder = fs.runInShell("git",
  117. new String[] { "check-attr", "--stdin", "--all" });
  118. builder.directory(db.getWorkTree());
  119. builder.environment().put("HOME", fs.userHome().getAbsolutePath());
  120. ExecutionResult result = fs.execute(builder, new ByteArrayInputStream(
  121. input.toString().getBytes(UTF_8)));
  122. String errorOut = toString(result.getStderr());
  123. assertEquals("External git failed", "exit 0\n",
  124. "exit " + result.getRc() + '\n' + errorOut);
  125. LinkedHashMap<String, Attributes> map = new LinkedHashMap<>();
  126. try (BufferedReader r = new BufferedReader(new InputStreamReader(
  127. new BufferedInputStream(result.getStdout().openInputStream()),
  128. UTF_8))) {
  129. r.lines().forEach(line -> {
  130. // Parse the line and add to result map
  131. int start = 0;
  132. int i = line.indexOf(':');
  133. String path = line.substring(0, i).trim();
  134. start = i + 1;
  135. i = line.indexOf(':', start);
  136. String key = line.substring(start, i).trim();
  137. String value = line.substring(i + 1).trim();
  138. Attribute attr = fromString(key, value);
  139. Attributes attrs = map.get(path);
  140. if (attrs == null) {
  141. attrs = new Attributes(attr);
  142. map.put(path, attrs);
  143. } else {
  144. attrs.put(attr);
  145. }
  146. });
  147. }
  148. return map;
  149. }
  150. private LinkedHashMap<String, Attributes> jgitAttributes()
  151. throws IOException {
  152. // Do a tree walk and return a list of all files and directories with
  153. // their attributes
  154. LinkedHashMap<String, Attributes> result = new LinkedHashMap<>();
  155. try (TreeWalk walk = new TreeWalk(db)) {
  156. walk.addTree(new FileTreeIterator(db));
  157. walk.setFilter(new NotIgnoredFilter(0));
  158. while (walk.next()) {
  159. String path = walk.getPathString();
  160. if (walk.isSubtree() && !path.endsWith("/")) {
  161. // git check-attr expects directory paths to end with a
  162. // slash
  163. path += '/';
  164. }
  165. Attributes attrs = walk.getAttributes();
  166. if (attrs != null && !attrs.isEmpty()) {
  167. result.put(path, attrs);
  168. } else {
  169. result.put(path, null);
  170. }
  171. if (walk.isSubtree()) {
  172. walk.enterSubtree();
  173. }
  174. }
  175. }
  176. return result;
  177. }
  178. private void assertSameAsCGit() throws Exception {
  179. LinkedHashMap<String, Attributes> jgit = jgitAttributes();
  180. LinkedHashMap<String, Attributes> cgit = cgitAttributes(jgit.keySet());
  181. // remove all without attributes
  182. Iterator<Map.Entry<String, Attributes>> iterator = jgit.entrySet()
  183. .iterator();
  184. while (iterator.hasNext()) {
  185. Map.Entry<String, Attributes> entry = iterator.next();
  186. if (entry.getValue() == null) {
  187. iterator.remove();
  188. }
  189. }
  190. assertArrayEquals("JGit attributes differ from C git",
  191. cgit.entrySet().toArray(), jgit.entrySet().toArray());
  192. }
  193. @Test
  194. public void testBug508568() throws Exception {
  195. createFiles("foo.xml/bar.jar", "sub/foo.xml/bar.jar");
  196. writeTrashFile(".gitattributes", "*.xml xml\n" + "*.jar jar\n");
  197. assertSameAsCGit();
  198. }
  199. @Test
  200. public void testRelativePath() throws Exception {
  201. createFiles("sub/foo.txt");
  202. writeTrashFile("sub/.gitattributes", "sub/** sub\n" + "*.txt txt\n");
  203. assertSameAsCGit();
  204. }
  205. @Test
  206. public void testRelativePaths() throws Exception {
  207. createFiles("sub/foo.txt", "sub/sub/bar", "foo/sub/a.txt",
  208. "foo/sub/bar/a.tmp");
  209. writeTrashFile(".gitattributes", "sub/** sub\n" + "*.txt txt\n");
  210. assertSameAsCGit();
  211. }
  212. @Test
  213. public void testNestedMatchNot() throws Exception {
  214. createFiles("foo.xml/bar.jar", "foo.xml/bar.xml", "sub/b.jar",
  215. "sub/b.xml");
  216. writeTrashFile("sub/.gitattributes", "*.xml xml\n" + "*.jar jar\n");
  217. assertSameAsCGit();
  218. }
  219. @Test
  220. public void testNestedMatch() throws Exception {
  221. // This is an interesting test. At the time of this writing, the
  222. // gitignore documentation says: "In other words, foo/ will match a
  223. // directory foo AND PATHS UNDERNEATH IT, but will not match a regular
  224. // file or a symbolic link foo". (Emphasis added.) And gitattributes is
  225. // supposed to follow the same rules. But the documentation appears to
  226. // lie: C-git will *not* apply the attribute "xml" to *any* files in
  227. // any subfolder "foo" here. It will only apply the "jar" attribute
  228. // to the three *.jar files.
  229. //
  230. // The point is probably that ignores are handled top-down, and once a
  231. // directory "foo" is matched (here: on paths "foo" and "sub/foo" by
  232. // pattern "foo/"), the directory is excluded and the gitignore
  233. // documentation also says: "It is not possible to re-include a file if
  234. // a parent directory of that file is excluded." So once the pattern
  235. // "foo/" has matched, it appears as if everything beneath would also be
  236. // matched.
  237. //
  238. // But not so for gitattributes! The foo/ rule only matches the
  239. // directory itself, but not anything beneath.
  240. createFiles("foo/bar.jar", "foo/bar.xml", "sub/b.jar", "sub/b.xml",
  241. "sub/foo/b.jar");
  242. writeTrashFile(".gitattributes",
  243. "foo/ xml\n" + "sub/foo/ sub\n" + "*.jar jar\n");
  244. assertSameAsCGit();
  245. }
  246. @Test
  247. public void testNestedMatchWithWildcard() throws Exception {
  248. // See above.
  249. createFiles("foo/bar.jar", "foo/bar.xml", "sub/b.jar", "sub/b.xml",
  250. "sub/foo/b.jar");
  251. writeTrashFile(".gitattributes",
  252. "**/foo/ xml\n" + "*/foo/ sub\n" + "*.jar jar\n");
  253. assertSameAsCGit();
  254. }
  255. @Test
  256. public void testNestedMatchRecursive() throws Exception {
  257. createFiles("foo/bar.jar", "foo/bar.xml", "sub/b.jar", "sub/b.xml",
  258. "sub/foo/b.jar");
  259. writeTrashFile(".gitattributes", "foo/** xml\n" + "*.jar jar\n");
  260. assertSameAsCGit();
  261. }
  262. @Test
  263. public void testStarMatchOnSlashNot() throws Exception {
  264. createFiles("sub/a.txt", "foo/sext", "foo/s.txt");
  265. writeTrashFile(".gitattributes", "s*xt bar");
  266. assertSameAsCGit();
  267. }
  268. @Test
  269. public void testPrefixMatchNot() throws Exception {
  270. createFiles("src/new/foo.txt");
  271. writeTrashFile(".gitattributes", "src/new bar\n");
  272. assertSameAsCGit();
  273. }
  274. @Test
  275. public void testComplexPathMatchNot() throws Exception {
  276. createFiles("src/new/foo.txt", "src/ndw");
  277. writeTrashFile(".gitattributes", "s[p-s]c/n[de]w bar\n");
  278. assertSameAsCGit();
  279. }
  280. @Test
  281. public void testStarPathMatchNot() throws Exception {
  282. createFiles("src/new/foo.txt", "src/ndw");
  283. writeTrashFile(".gitattributes", "src/* bar\n");
  284. assertSameAsCGit();
  285. }
  286. @Test
  287. public void testDirectoryMatchSubSimple() throws Exception {
  288. createFiles("src/new/foo.txt", "foo/src/new/foo.txt", "sub/src/new");
  289. writeTrashFile(".gitattributes", "src/new/ bar\n");
  290. assertSameAsCGit();
  291. }
  292. @Test
  293. public void testDirectoryMatchSubRecursive() throws Exception {
  294. createFiles("src/new/foo.txt", "foo/src/new/foo.txt", "sub/src/new");
  295. writeTrashFile(".gitattributes", "**/src/new/ bar\n");
  296. assertSameAsCGit();
  297. }
  298. @Test
  299. public void testDirectoryMatchSubRecursiveBacktrack() throws Exception {
  300. createFiles("src/new/foo.txt", "src/src/new/foo.txt");
  301. writeTrashFile(".gitattributes", "**/src/new/ bar\n");
  302. assertSameAsCGit();
  303. }
  304. @Test
  305. public void testDirectoryMatchSubRecursiveBacktrack2() throws Exception {
  306. createFiles("src/new/foo.txt", "src/src/new/foo.txt");
  307. writeTrashFile(".gitattributes", "**/**/src/new/ bar\n");
  308. assertSameAsCGit();
  309. }
  310. @Test
  311. public void testDirectoryMatchSubRecursiveBacktrack3() throws Exception {
  312. createFiles("src/new/src/new/foo.txt",
  313. "foo/src/new/bar/src/new/foo.txt");
  314. writeTrashFile(".gitattributes", "**/src/new/ bar\n");
  315. assertSameAsCGit();
  316. }
  317. @Test
  318. public void testDirectoryMatchSubRecursiveBacktrack4() throws Exception {
  319. createFiles("src/src/src/new/foo.txt",
  320. "foo/src/src/bar/src/new/foo.txt");
  321. writeTrashFile(".gitattributes", "**/src/ bar\n");
  322. assertSameAsCGit();
  323. }
  324. @Test
  325. public void testDirectoryMatchSubRecursiveBacktrack5() throws Exception {
  326. createFiles("x/a/a/b/foo.txt", "x/y/z/b/a/b/foo.txt",
  327. "x/y/a/a/a/a/b/foo.txt", "x/y/a/a/a/a/b/a/b/foo.txt");
  328. writeTrashFile(".gitattributes", "**/*/a/b bar\n");
  329. assertSameAsCGit();
  330. }
  331. @Test
  332. public void testDirectoryMatchSubRecursiveBacktrack6() throws Exception {
  333. createFiles("x/a/a/b/foo.txt", "x/y/a/b/a/b/foo.txt");
  334. writeTrashFile(".gitattributes", "**/*/**/a/b bar\n");
  335. assertSameAsCGit();
  336. }
  337. @Test
  338. public void testDirectoryWildmatchDoesNotMatchFiles1() throws Exception {
  339. createFiles("a", "dir/b", "dir/sub/c");
  340. writeTrashFile(".gitattributes", "**/ bar\n");
  341. assertSameAsCGit();
  342. }
  343. @Test
  344. public void testDirectoryWildmatchDoesNotMatchFiles2() throws Exception {
  345. createFiles("a", "dir/b", "dir/sub/c");
  346. writeTrashFile(".gitattributes", "**/**/ bar\n");
  347. assertSameAsCGit();
  348. }
  349. @Test
  350. public void testDirectoryWildmatchDoesNotMatchFiles3() throws Exception {
  351. createFiles("a", "x/b", "sub/x/c", "sub/x/d/e");
  352. writeTrashFile(".gitattributes", "x/**/ bar\n");
  353. assertSameAsCGit();
  354. }
  355. @Test
  356. public void testDirectoryWildmatchDoesNotMatchFiles4() throws Exception {
  357. createFiles("a", "dir/x", "dir/sub1/x", "dir/sub2/x/y");
  358. writeTrashFile(".gitattributes", "x/**/ bar\n");
  359. assertSameAsCGit();
  360. }
  361. @Test
  362. public void testDirectoryMatchSubComplex() throws Exception {
  363. createFiles("src/new/foo.txt", "foo/src/new/foo.txt", "sub/src/new");
  364. writeTrashFile(".gitattributes", "s[rs]c/n*/ bar\n");
  365. assertSameAsCGit();
  366. }
  367. @Test
  368. public void testDirectoryMatch() throws Exception {
  369. createFiles("src/new/foo.txt", "foo/src/new/foo.txt", "sub/src/new");
  370. writeTrashFile(".gitattributes", "new/ bar\n");
  371. assertSameAsCGit();
  372. }
  373. @Test
  374. public void testBracketsInGroup() throws Exception {
  375. createFiles("[", "]", "[]", "][", "[[]", "[]]", "[[]]");
  376. writeTrashFile(".gitattributes", "[[]] bar1\n" + "[\\[]] bar2\n"
  377. + "[[\\]] bar3\n" + "[\\[\\]] bar4\n");
  378. assertSameAsCGit();
  379. }
  380. }