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.

EolRepositoryTest.java 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /*
  2. * Copyright (C) 2015, Ivan Motsch <ivan.motsch@bsiag.com>
  3. *
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Distribution License v1.0 which
  6. * accompanies this distribution, is reproduced below, and is
  7. * available at http://www.eclipse.org/org/documents/edl-v10.php
  8. *
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials provided
  21. * with the distribution.
  22. *
  23. * - Neither the name of the Eclipse Foundation, Inc. nor the
  24. * names of its contributors may be used to endorse or promote
  25. * products derived from this software without specific prior
  26. * written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  29. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  32. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  35. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  36. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  37. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. */
  39. package org.eclipse.jgit.api;
  40. import static org.junit.Assert.assertEquals;
  41. import static org.junit.Assert.assertFalse;
  42. import static org.junit.Assert.assertTrue;
  43. import java.io.File;
  44. import java.io.IOException;
  45. import org.eclipse.jgit.api.ResetCommand.ResetType;
  46. import org.eclipse.jgit.api.errors.CheckoutConflictException;
  47. import org.eclipse.jgit.api.errors.GitAPIException;
  48. import org.eclipse.jgit.api.errors.NoFilepatternException;
  49. import org.eclipse.jgit.attributes.Attribute;
  50. import org.eclipse.jgit.dircache.DirCache;
  51. import org.eclipse.jgit.dircache.DirCacheEntry;
  52. import org.eclipse.jgit.dircache.DirCacheIterator;
  53. import org.eclipse.jgit.errors.RevisionSyntaxException;
  54. import org.eclipse.jgit.junit.RepositoryTestCase;
  55. import org.eclipse.jgit.lib.ConfigConstants;
  56. import org.eclipse.jgit.lib.Constants;
  57. import org.eclipse.jgit.lib.CoreConfig.AutoCRLF;
  58. import org.eclipse.jgit.lib.CoreConfig.EOL;
  59. import org.eclipse.jgit.lib.FileMode;
  60. import org.eclipse.jgit.lib.ObjectLoader;
  61. import org.eclipse.jgit.storage.file.FileBasedConfig;
  62. import org.eclipse.jgit.treewalk.FileTreeIterator;
  63. import org.eclipse.jgit.treewalk.TreeWalk;
  64. import org.eclipse.jgit.util.IO;
  65. import org.junit.Assert;
  66. import org.junit.Test;
  67. import org.junit.experimental.theories.DataPoint;
  68. import org.junit.experimental.theories.Theories;
  69. import org.junit.runner.RunWith;
  70. /**
  71. * Unit tests for end-of-line conversion and settings using core.autocrlf, *
  72. * core.eol and the .gitattributes eol, text, binary (macro for -diff -merge
  73. * -text)
  74. */
  75. @RunWith(Theories.class)
  76. public class EolRepositoryTest extends RepositoryTestCase {
  77. private static final FileMode D = FileMode.TREE;
  78. private static final FileMode F = FileMode.REGULAR_FILE;
  79. @DataPoint
  80. public static String smallContents[] = {
  81. generateTestData(3, 1, true, false),
  82. generateTestData(3, 1, false, true),
  83. generateTestData(3, 1, true, true) };
  84. @DataPoint
  85. public static String hugeContents[] = {
  86. generateTestData(1000000, 17, true, false),
  87. generateTestData(1000000, 17, false, true),
  88. generateTestData(1000000, 17, true, true) };
  89. static String generateTestData(int size, int lineSize, boolean withCRLF,
  90. boolean withLF) {
  91. StringBuilder sb = new StringBuilder();
  92. for (int i = 0; i < size; i++) {
  93. if (i > 0 && i % lineSize == 0) {
  94. // newline
  95. if (withCRLF && withLF) {
  96. // mixed
  97. if (i % 2 == 0)
  98. sb.append("\r\n");
  99. else
  100. sb.append("\n");
  101. } else if (withCRLF) {
  102. sb.append("\r\n");
  103. } else if (withLF) {
  104. sb.append("\n");
  105. }
  106. }
  107. sb.append("A");
  108. }
  109. return sb.toString();
  110. }
  111. public EolRepositoryTest(String[] testContent) {
  112. CONTENT_CRLF = testContent[0];
  113. CONTENT_LF = testContent[1];
  114. CONTENT_MIXED = testContent[2];
  115. }
  116. protected String CONTENT_CRLF;
  117. protected String CONTENT_LF;
  118. protected String CONTENT_MIXED;
  119. private TreeWalk walk;
  120. /** work tree root .gitattributes */
  121. private File dotGitattributes;
  122. /** file containing CRLF */
  123. private File fileCRLF;
  124. /** file containing LF */
  125. private File fileLF;
  126. /** file containing mixed CRLF and LF */
  127. private File fileMixed;
  128. /** this values are set in {@link #collectRepositoryState()} */
  129. private static class ActualEntry {
  130. private String attrs;
  131. private String file;
  132. private String index;
  133. private int indexContentLength;
  134. }
  135. private ActualEntry entryCRLF = new ActualEntry();
  136. private ActualEntry entryLF = new ActualEntry();
  137. private ActualEntry entryMixed = new ActualEntry();
  138. private DirCache dc;
  139. @Test
  140. public void testDefaultSetup() throws Exception {
  141. // for EOL to work, the text attribute must be set
  142. setupGitAndDoHardReset(null, null, null, null, "* text=auto");
  143. collectRepositoryState();
  144. assertEquals("text=auto", entryCRLF.attrs);
  145. checkEntryContent(entryCRLF, CONTENT_LF, CONTENT_LF);
  146. checkEntryContent(entryLF, CONTENT_LF, CONTENT_LF);
  147. checkEntryContent(entryMixed, CONTENT_LF, CONTENT_LF);
  148. }
  149. public void checkEntryContent(ActualEntry entry, String fileContent,
  150. String indexContent) {
  151. assertEquals(fileContent, entry.file);
  152. assertEquals(indexContent, entry.index);
  153. assertEquals(fileContent.length(), entry.indexContentLength);
  154. }
  155. @Test
  156. public void test_ConfigAutoCRLF_false() throws Exception {
  157. // for EOL to work, the text attribute must be set
  158. setupGitAndDoHardReset(AutoCRLF.FALSE, null, null, null, "* text=auto");
  159. collectRepositoryState();
  160. assertEquals("text=auto", entryCRLF.attrs);
  161. checkEntryContent(entryCRLF, CONTENT_LF, CONTENT_LF);
  162. checkEntryContent(entryLF, CONTENT_LF, CONTENT_LF);
  163. checkEntryContent(entryMixed, CONTENT_LF, CONTENT_LF);
  164. }
  165. @Test
  166. public void test_ConfigAutoCRLF_true() throws Exception {
  167. // for EOL to work, the text attribute must be set
  168. setupGitAndDoHardReset(AutoCRLF.TRUE, null, null, null, "* text=auto");
  169. collectRepositoryState();
  170. assertEquals("text=auto", entryCRLF.attrs);
  171. checkEntryContent(entryCRLF, CONTENT_CRLF, CONTENT_LF);
  172. checkEntryContent(entryLF, CONTENT_CRLF, CONTENT_LF);
  173. checkEntryContent(entryMixed, CONTENT_CRLF, CONTENT_LF);
  174. }
  175. @Test
  176. public void test_ConfigAutoCRLF_input() throws Exception {
  177. // for EOL to work, the text attribute must be set
  178. setupGitAndDoHardReset(AutoCRLF.INPUT, null, null, null, "* text=auto");
  179. collectRepositoryState();
  180. assertEquals("text=auto", entryCRLF.attrs);
  181. checkEntryContent(entryCRLF, CONTENT_LF, CONTENT_LF);
  182. checkEntryContent(entryLF, CONTENT_LF, CONTENT_LF);
  183. checkEntryContent(entryMixed, CONTENT_LF, CONTENT_LF);
  184. }
  185. @Test
  186. public void test_ConfigEOL_lf() throws Exception {
  187. // for EOL to work, the text attribute must be set
  188. setupGitAndDoHardReset(null, EOL.LF, "*.txt text", null, null);
  189. collectRepositoryState();
  190. assertEquals("text", entryCRLF.attrs);
  191. checkEntryContent(entryCRLF, CONTENT_LF, CONTENT_LF);
  192. checkEntryContent(entryLF, CONTENT_LF, CONTENT_LF);
  193. checkEntryContent(entryMixed, CONTENT_LF, CONTENT_LF);
  194. }
  195. @Test
  196. public void test_ConfigEOL_crlf() throws Exception {
  197. // for EOL to work, the text attribute must be set
  198. setupGitAndDoHardReset(null, EOL.CRLF, "*.txt text", null, null);
  199. collectRepositoryState();
  200. assertEquals("text", entryCRLF.attrs);
  201. checkEntryContent(entryCRLF, CONTENT_CRLF, CONTENT_LF);
  202. checkEntryContent(entryLF, CONTENT_CRLF, CONTENT_LF);
  203. checkEntryContent(entryMixed, CONTENT_CRLF, CONTENT_LF);
  204. }
  205. @Test
  206. public void test_ConfigEOL_native_windows() throws Exception {
  207. String origLineSeparator = System.getProperty("line.separator", "\n");
  208. System.setProperty("line.separator", "\r\n");
  209. try {
  210. // for EOL to work, the text attribute must be set
  211. setupGitAndDoHardReset(null, EOL.NATIVE, "*.txt text", null, null);
  212. collectRepositoryState();
  213. assertEquals("text", entryCRLF.attrs);
  214. checkEntryContent(entryCRLF, CONTENT_LF, CONTENT_LF);
  215. checkEntryContent(entryLF, CONTENT_LF, CONTENT_LF);
  216. } finally {
  217. System.setProperty("line.separator", origLineSeparator);
  218. }
  219. }
  220. @Test
  221. public void test_ConfigEOL_native_xnix() throws Exception {
  222. String origLineSeparator = System.getProperty("line.separator", "\n");
  223. System.setProperty("line.separator", "\n");
  224. try {
  225. // for EOL to work, the text attribute must be set
  226. setupGitAndDoHardReset(null, EOL.NATIVE, "*.txt text", null, null);
  227. collectRepositoryState();
  228. assertEquals("text", entryCRLF.attrs);
  229. checkEntryContent(entryCRLF, CONTENT_LF, CONTENT_LF);
  230. checkEntryContent(entryLF, CONTENT_LF, CONTENT_LF);
  231. } finally {
  232. System.setProperty("line.separator", origLineSeparator);
  233. }
  234. }
  235. @Test
  236. public void test_ConfigAutoCRLF_false_ConfigEOL_lf() throws Exception {
  237. // for EOL to work, the text attribute must be set
  238. setupGitAndDoHardReset(AutoCRLF.FALSE, EOL.LF, "*.txt text", null, null);
  239. collectRepositoryState();
  240. assertEquals("text", entryCRLF.attrs);
  241. checkEntryContent(entryCRLF, CONTENT_LF, CONTENT_LF);
  242. checkEntryContent(entryLF, CONTENT_LF, CONTENT_LF);
  243. checkEntryContent(entryMixed, CONTENT_LF, CONTENT_LF);
  244. }
  245. @Test
  246. public void test_ConfigAutoCRLF_false_ConfigEOL_native() throws Exception {
  247. // for EOL to work, the text attribute must be set
  248. setupGitAndDoHardReset(AutoCRLF.FALSE, EOL.NATIVE, "*.txt text", null, null);
  249. collectRepositoryState();
  250. assertEquals("text", entryCRLF.attrs);
  251. checkEntryContent(entryCRLF, CONTENT_LF, CONTENT_LF);
  252. checkEntryContent(entryLF, CONTENT_LF, CONTENT_LF);
  253. checkEntryContent(entryMixed, CONTENT_LF, CONTENT_LF);
  254. }
  255. @Test
  256. public void test_ConfigAutoCRLF_true_ConfigEOL_lf() throws Exception {
  257. // for EOL to work, the text attribute must be set
  258. setupGitAndDoHardReset(AutoCRLF.TRUE, EOL.LF, "*.txt text", null, null);
  259. collectRepositoryState();
  260. assertEquals("text", entryCRLF.attrs);
  261. checkEntryContent(entryCRLF, CONTENT_CRLF, CONTENT_LF);
  262. checkEntryContent(entryLF, CONTENT_CRLF, CONTENT_LF);
  263. checkEntryContent(entryMixed, CONTENT_CRLF, CONTENT_LF);
  264. }
  265. @Test
  266. public void test_switchToBranchWithTextAttributes()
  267. throws Exception {
  268. Git git = Git.wrap(db);
  269. // for EOL to work, the text attribute must be set
  270. setupGitAndDoHardReset(AutoCRLF.FALSE, EOL.CRLF, null, null,
  271. "file1.txt text\nfile2.txt text\nfile3.txt text");
  272. collectRepositoryState();
  273. assertEquals("text", entryCRLF.attrs);
  274. checkEntryContent(entryCRLF, CONTENT_CRLF, CONTENT_LF);
  275. checkEntryContent(entryLF, CONTENT_CRLF, CONTENT_LF);
  276. checkEntryContent(entryMixed, CONTENT_CRLF, CONTENT_LF);
  277. // switch to binary for file1
  278. dotGitattributes = createAndAddFile(git, Constants.DOT_GIT_ATTRIBUTES,
  279. "file1.txt binary\nfile2.txt text\nfile3.txt text");
  280. gitCommit(git, "switchedToBinaryFor1");
  281. recreateWorktree(git);
  282. collectRepositoryState();
  283. assertEquals("binary -diff -merge -text", entryCRLF.attrs);
  284. checkEntryContent(entryCRLF, CONTENT_LF, CONTENT_LF);
  285. assertEquals("text", entryLF.attrs);
  286. checkEntryContent(entryLF, CONTENT_CRLF, CONTENT_LF);
  287. assertEquals("text", entryMixed.attrs);
  288. checkEntryContent(entryMixed, CONTENT_CRLF, CONTENT_LF);
  289. // checkout the commit which has text for file1
  290. gitCheckout(git, "HEAD^");
  291. recreateWorktree(git);
  292. collectRepositoryState();
  293. assertEquals("text", entryCRLF.attrs);
  294. checkEntryContent(entryCRLF, CONTENT_CRLF, CONTENT_LF);
  295. checkEntryContent(entryLF, CONTENT_CRLF, CONTENT_LF);
  296. checkEntryContent(entryMixed, CONTENT_CRLF, CONTENT_LF);
  297. }
  298. @Test
  299. public void test_switchToBranchWithBinaryAttributes() throws Exception {
  300. Git git = Git.wrap(db);
  301. // for EOL to work, the text attribute must be set
  302. setupGitAndDoHardReset(AutoCRLF.FALSE, EOL.LF, null, null,
  303. "file1.txt binary\nfile2.txt binary\nfile3.txt binary");
  304. collectRepositoryState();
  305. assertEquals("binary -diff -merge -text", entryCRLF.attrs);
  306. checkEntryContent(entryCRLF, CONTENT_CRLF, CONTENT_CRLF);
  307. checkEntryContent(entryLF, CONTENT_LF, CONTENT_LF);
  308. checkEntryContent(entryMixed, CONTENT_MIXED, CONTENT_MIXED);
  309. // switch to text for file1
  310. dotGitattributes = createAndAddFile(git, Constants.DOT_GIT_ATTRIBUTES,
  311. "file1.txt text\nfile2.txt binary\nfile3.txt binary");
  312. gitCommit(git, "switchedToTextFor1");
  313. recreateWorktree(git);
  314. collectRepositoryState();
  315. assertEquals("text", entryCRLF.attrs);
  316. checkEntryContent(entryCRLF, CONTENT_CRLF, CONTENT_LF);
  317. assertEquals("binary -diff -merge -text", entryLF.attrs);
  318. checkEntryContent(entryLF, CONTENT_LF, CONTENT_LF);
  319. assertEquals("binary -diff -merge -text", entryMixed.attrs);
  320. checkEntryContent(entryMixed, CONTENT_MIXED, CONTENT_MIXED);
  321. // checkout the commit which has text for file1
  322. gitCheckout(git, "HEAD^");
  323. recreateWorktree(git);
  324. collectRepositoryState();
  325. assertEquals("binary -diff -merge -text", entryCRLF.attrs);
  326. checkEntryContent(entryCRLF, CONTENT_CRLF, CONTENT_CRLF);
  327. checkEntryContent(entryLF, CONTENT_LF, CONTENT_LF);
  328. checkEntryContent(entryMixed, CONTENT_MIXED, CONTENT_MIXED);
  329. }
  330. @Test
  331. public void test_ConfigAutoCRLF_input_ConfigEOL_lf() throws Exception {
  332. // for EOL to work, the text attribute must be set
  333. setupGitAndDoHardReset(AutoCRLF.INPUT, EOL.LF, "*.txt text", null, null);
  334. collectRepositoryState();
  335. assertEquals("text", entryCRLF.attrs);
  336. checkEntryContent(entryCRLF, CONTENT_LF, CONTENT_LF);
  337. checkEntryContent(entryLF, CONTENT_LF, CONTENT_LF);
  338. checkEntryContent(entryMixed, CONTENT_LF, CONTENT_LF);
  339. }
  340. @Test
  341. public void test_ConfigAutoCRLF_true_GlobalEOL_lf() throws Exception {
  342. setupGitAndDoHardReset(AutoCRLF.TRUE, EOL.LF, "*.txt eol=lf", null, null);
  343. collectRepositoryState();
  344. assertEquals("eol=lf", entryCRLF.attrs);
  345. checkEntryContent(entryCRLF, CONTENT_LF, CONTENT_LF);
  346. checkEntryContent(entryLF, CONTENT_LF, CONTENT_LF);
  347. checkEntryContent(entryMixed, CONTENT_LF, CONTENT_LF);
  348. }
  349. @Test
  350. public void test_ConfigAutoCRLF_false_GlobalEOL_lf() throws Exception {
  351. setupGitAndDoHardReset(AutoCRLF.FALSE, EOL.LF, "*.txt eol=lf", null, null);
  352. collectRepositoryState();
  353. assertEquals("eol=lf", entryCRLF.attrs);
  354. checkEntryContent(entryCRLF, CONTENT_LF, CONTENT_LF);
  355. checkEntryContent(entryLF, CONTENT_LF, CONTENT_LF);
  356. checkEntryContent(entryMixed, CONTENT_LF, CONTENT_LF);
  357. }
  358. @Test
  359. public void test_ConfigAutoCRLF_input_GlobalEOL_lf() throws Exception {
  360. setupGitAndDoHardReset(AutoCRLF.INPUT, EOL.LF, "*.txt eol=lf", null, null);
  361. collectRepositoryState();
  362. assertEquals("eol=lf", entryCRLF.attrs);
  363. checkEntryContent(entryCRLF, CONTENT_LF, CONTENT_LF);
  364. checkEntryContent(entryLF, CONTENT_LF, CONTENT_LF);
  365. checkEntryContent(entryMixed, CONTENT_LF, CONTENT_LF);
  366. }
  367. @Test
  368. public void test_ConfigAutoCRLF_true_GlobalEOL_crlf() throws Exception {
  369. setupGitAndDoHardReset(AutoCRLF.TRUE, EOL.LF, "*.txt eol=crlf", null, null);
  370. collectRepositoryState();
  371. assertEquals("eol=crlf", entryCRLF.attrs);
  372. checkEntryContent(entryCRLF, CONTENT_CRLF, CONTENT_LF);
  373. checkEntryContent(entryLF, CONTENT_CRLF, CONTENT_LF);
  374. checkEntryContent(entryMixed, CONTENT_CRLF, CONTENT_LF);
  375. }
  376. @Test
  377. public void test_ConfigAutoCRLF_false_GlobalEOL_crlf() throws Exception {
  378. setupGitAndDoHardReset(AutoCRLF.FALSE, EOL.LF, "*.txt eol=crlf", null, null);
  379. collectRepositoryState();
  380. assertEquals("eol=crlf", entryCRLF.attrs);
  381. checkEntryContent(entryCRLF, CONTENT_CRLF, CONTENT_LF);
  382. checkEntryContent(entryLF, CONTENT_CRLF, CONTENT_LF);
  383. checkEntryContent(entryMixed, CONTENT_CRLF, CONTENT_LF);
  384. }
  385. @Test
  386. public void test_ConfigAutoCRLF_input_GlobalEOL_crlf() throws Exception {
  387. setupGitAndDoHardReset(AutoCRLF.INPUT, EOL.LF, "*.txt eol=crlf", null, null);
  388. collectRepositoryState();
  389. assertEquals("eol=crlf", entryCRLF.attrs);
  390. checkEntryContent(entryCRLF, CONTENT_CRLF, CONTENT_LF);
  391. checkEntryContent(entryLF, CONTENT_CRLF, CONTENT_LF);
  392. checkEntryContent(entryMixed, CONTENT_CRLF, CONTENT_LF);
  393. }
  394. @Test
  395. public void test_ConfigAutoCRLF_true_GlobalEOL_lf_InfoEOL_crlf()
  396. throws Exception {
  397. setupGitAndDoHardReset(AutoCRLF.TRUE, null, "*.txt eol=lf", "*.txt eol=crlf", null);
  398. // info decides
  399. collectRepositoryState();
  400. assertEquals("eol=crlf", entryCRLF.attrs);
  401. checkEntryContent(entryCRLF, CONTENT_CRLF, CONTENT_LF);
  402. checkEntryContent(entryLF, CONTENT_CRLF, CONTENT_LF);
  403. checkEntryContent(entryMixed, CONTENT_CRLF, CONTENT_LF);
  404. }
  405. @Test
  406. public void test_ConfigAutoCRLF_false_GlobalEOL_crlf_InfoEOL_lf()
  407. throws Exception {
  408. setupGitAndDoHardReset(AutoCRLF.FALSE, null, "*.txt eol=crlf", "*.txt eol=lf", null);
  409. // info decides
  410. collectRepositoryState();
  411. assertEquals("eol=lf", entryCRLF.attrs);
  412. checkEntryContent(entryCRLF, CONTENT_LF, CONTENT_LF);
  413. checkEntryContent(entryLF, CONTENT_LF, CONTENT_LF);
  414. checkEntryContent(entryMixed, CONTENT_LF, CONTENT_LF);
  415. }
  416. @Test
  417. public void test_GlobalEOL_lf_RootEOL_crlf() throws Exception {
  418. setupGitAndDoHardReset(null, null, "*.txt eol=lf", null, "*.txt eol=crlf");
  419. // root over global
  420. collectRepositoryState();
  421. assertEquals("eol=crlf", entryCRLF.attrs);
  422. checkEntryContent(entryCRLF, CONTENT_CRLF, CONTENT_LF);
  423. checkEntryContent(entryLF, CONTENT_CRLF, CONTENT_LF);
  424. checkEntryContent(entryMixed, CONTENT_CRLF, CONTENT_LF);
  425. }
  426. @Test
  427. public void test_GlobalEOL_lf_InfoEOL_crlf_RootEOL_lf() throws Exception {
  428. setupGitAndDoHardReset(null, null, "*.txt eol=lf", "*.txt eol=crlf", "*.txt eol=lf");
  429. // info overrides all
  430. collectRepositoryState();
  431. assertEquals("eol=crlf", entryCRLF.attrs);
  432. checkEntryContent(entryCRLF, CONTENT_CRLF, CONTENT_LF);
  433. checkEntryContent(entryLF, CONTENT_CRLF, CONTENT_LF);
  434. checkEntryContent(entryMixed, CONTENT_CRLF, CONTENT_LF);
  435. }
  436. @Test
  437. public void test_GlobalEOL_lf_InfoEOL_crlf_RootEOL_unspec()
  438. throws Exception {
  439. setupGitAndDoHardReset(null, null, "*.txt eol=lf", "*.txt eol=crlf",
  440. "*.txt text !eol");
  441. // info overrides all
  442. collectRepositoryState();
  443. assertEquals("eol=crlf text", entryCRLF.attrs);
  444. checkEntryContent(entryCRLF, CONTENT_CRLF, CONTENT_LF);
  445. checkEntryContent(entryLF, CONTENT_CRLF, CONTENT_LF);
  446. checkEntryContent(entryMixed, CONTENT_CRLF, CONTENT_LF);
  447. }
  448. @Test
  449. public void test_GlobalEOL_lf_InfoEOL_unspec_RootEOL_crlf()
  450. throws Exception {
  451. setupGitAndDoHardReset(null, null, "*.txt eol=lf", "*.txt !eol",
  452. "*.txt text eol=crlf");
  453. // info overrides all
  454. collectRepositoryState();
  455. assertEquals("text", entryCRLF.attrs);
  456. checkEntryContent(entryCRLF, CONTENT_LF, CONTENT_LF);
  457. checkEntryContent(entryLF, CONTENT_LF, CONTENT_LF);
  458. checkEntryContent(entryMixed, CONTENT_LF, CONTENT_LF);
  459. }
  460. @Test
  461. public void testBinary1() throws Exception {
  462. setupGitAndDoHardReset(AutoCRLF.TRUE, EOL.CRLF, "*.txt text", "*.txt binary",
  463. "*.txt eol=crlf");
  464. // info overrides all
  465. collectRepositoryState();
  466. assertEquals("binary -diff -merge -text eol=crlf", entryCRLF.attrs);
  467. checkEntryContent(entryCRLF, CONTENT_CRLF, CONTENT_CRLF);
  468. checkEntryContent(entryLF, CONTENT_LF, CONTENT_LF);
  469. checkEntryContent(entryMixed, CONTENT_MIXED, CONTENT_MIXED);
  470. }
  471. @Test
  472. public void testBinary2() throws Exception {
  473. setupGitAndDoHardReset(AutoCRLF.TRUE, EOL.CRLF, "*.txt text eol=crlf", null,
  474. "*.txt binary");
  475. // root over global
  476. collectRepositoryState();
  477. assertEquals("binary -diff -merge -text eol=crlf", entryCRLF.attrs);
  478. checkEntryContent(entryCRLF, CONTENT_CRLF, CONTENT_CRLF);
  479. checkEntryContent(entryLF, CONTENT_LF, CONTENT_LF);
  480. checkEntryContent(entryMixed, CONTENT_MIXED, CONTENT_MIXED);
  481. }
  482. // create new repo with
  483. // global .gitattributes
  484. // info .git/config/info/.gitattributes
  485. // workdir root .gitattributes
  486. // text file lf.txt CONTENT_LF
  487. // text file crlf.txt CONTENT_CRLF
  488. //
  489. // commit files (checkin)
  490. // delete working dir files
  491. // reset hard (checkout)
  492. private void setupGitAndDoHardReset(AutoCRLF autoCRLF, EOL eol,
  493. String globalAttributesContent, String infoAttributesContent,
  494. String workDirRootAttributesContent) throws Exception {
  495. Git git = new Git(db);
  496. FileBasedConfig config = db.getConfig();
  497. if (autoCRLF != null) {
  498. config.setEnum(ConfigConstants.CONFIG_CORE_SECTION, null,
  499. ConfigConstants.CONFIG_KEY_AUTOCRLF, autoCRLF);
  500. }
  501. if (eol != null) {
  502. config.setEnum(ConfigConstants.CONFIG_CORE_SECTION, null,
  503. ConfigConstants.CONFIG_KEY_EOL, eol);
  504. }
  505. if (globalAttributesContent != null) {
  506. File f = new File(db.getDirectory(), "global/attrs");
  507. write(f, globalAttributesContent);
  508. config.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
  509. ConfigConstants.CONFIG_KEY_ATTRIBUTESFILE,
  510. f.getAbsolutePath());
  511. }
  512. if (infoAttributesContent != null) {
  513. File f = new File(db.getDirectory(), Constants.INFO_ATTRIBUTES);
  514. write(f, infoAttributesContent);
  515. }
  516. config.save();
  517. if (workDirRootAttributesContent != null) {
  518. dotGitattributes = createAndAddFile(git,
  519. Constants.DOT_GIT_ATTRIBUTES, workDirRootAttributesContent);
  520. } else {
  521. dotGitattributes = null;
  522. }
  523. fileCRLF = createAndAddFile(git, "file1.txt", CONTENT_CRLF);
  524. fileLF = createAndAddFile(git, "file2.txt", CONTENT_LF);
  525. fileMixed = createAndAddFile(git, "file3.txt", CONTENT_MIXED);
  526. gitCommit(git, "addFiles");
  527. recreateWorktree(git);
  528. }
  529. private void recreateWorktree(Git git)
  530. throws GitAPIException, CheckoutConflictException,
  531. InterruptedException, IOException, NoFilepatternException {
  532. // re-create file from the repo
  533. for (File f : new File[] { dotGitattributes, fileCRLF, fileLF, fileMixed }) {
  534. if (f == null)
  535. continue;
  536. f.delete();
  537. Assert.assertFalse(f.exists());
  538. }
  539. gitResetHard(git);
  540. fsTick(db.getIndexFile());
  541. gitAdd(git, ".");
  542. }
  543. protected void gitCommit(Git git, String msg) throws GitAPIException {
  544. git.commit().setMessage(msg).call();
  545. }
  546. protected void gitAdd(Git git, String path) throws GitAPIException {
  547. git.add().addFilepattern(path).call();
  548. }
  549. protected void gitResetHard(Git git) throws GitAPIException {
  550. git.reset().setMode(ResetType.HARD).call();
  551. }
  552. protected void gitCheckout(Git git, String revstr)
  553. throws GitAPIException, RevisionSyntaxException, IOException {
  554. git.checkout().setName(db.resolve(revstr).getName()).call();
  555. }
  556. // create a file and add it to the repo
  557. private File createAndAddFile(Git git, String path, String content)
  558. throws Exception {
  559. File f;
  560. int pos = path.lastIndexOf('/');
  561. if (pos < 0) {
  562. f = writeTrashFile(path, content);
  563. } else {
  564. f = writeTrashFile(path.substring(0, pos), path.substring(pos + 1),
  565. content);
  566. }
  567. gitAdd(git, path);
  568. Assert.assertTrue(f.exists());
  569. return f;
  570. }
  571. private void collectRepositoryState() throws Exception {
  572. dc = db.readDirCache();
  573. walk = beginWalk();
  574. if (dotGitattributes != null)
  575. collectEntryContentAndAttributes(F, ".gitattributes", null);
  576. collectEntryContentAndAttributes(F, fileCRLF.getName(), entryCRLF);
  577. collectEntryContentAndAttributes(F, fileLF.getName(), entryLF);
  578. collectEntryContentAndAttributes(F, fileMixed.getName(), entryMixed);
  579. endWalk();
  580. }
  581. private TreeWalk beginWalk() throws Exception {
  582. TreeWalk newWalk = new TreeWalk(db);
  583. newWalk.addTree(new FileTreeIterator(db));
  584. newWalk.addTree(new DirCacheIterator(db.readDirCache()));
  585. return newWalk;
  586. }
  587. private void endWalk() throws IOException {
  588. assertFalse("Not all files tested", walk.next());
  589. }
  590. private void collectEntryContentAndAttributes(FileMode type, String pathName,
  591. ActualEntry e) throws IOException {
  592. assertTrue("walk has entry", walk.next());
  593. assertEquals(pathName, walk.getPathString());
  594. assertEquals(type, walk.getFileMode(0));
  595. if (e != null) {
  596. e.attrs = "";
  597. for (Attribute a : walk.getAttributes().getAll()) {
  598. e.attrs += " " + a.toString();
  599. }
  600. e.attrs = e.attrs.trim();
  601. e.file = new String(
  602. IO.readFully(new File(db.getWorkTree(), pathName)));
  603. DirCacheEntry dce = dc.getEntry(pathName);
  604. ObjectLoader open = walk.getObjectReader().open(dce.getObjectId());
  605. e.index = new String(open.getBytes());
  606. e.indexContentLength = dce.getLength();
  607. }
  608. if (D.equals(type))
  609. walk.enterSubtree();
  610. }
  611. }