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.

FilterCommandsTest.java 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (C) 2016, 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. package org.eclipse.jgit.util;
  44. import static org.junit.Assert.assertEquals;
  45. import java.io.IOException;
  46. import java.io.InputStream;
  47. import java.io.OutputStream;
  48. import org.eclipse.jgit.api.Git;
  49. import org.eclipse.jgit.api.errors.GitAPIException;
  50. import org.eclipse.jgit.attributes.FilterCommand;
  51. import org.eclipse.jgit.attributes.FilterCommandFactory;
  52. import org.eclipse.jgit.attributes.FilterCommandRegistry;
  53. import org.eclipse.jgit.junit.RepositoryTestCase;
  54. import org.eclipse.jgit.lib.Constants;
  55. import org.eclipse.jgit.lib.RefUpdate;
  56. import org.eclipse.jgit.lib.Repository;
  57. import org.eclipse.jgit.lib.StoredConfig;
  58. import org.eclipse.jgit.revwalk.RevCommit;
  59. import org.junit.Before;
  60. import org.junit.Test;
  61. public class FilterCommandsTest extends RepositoryTestCase {
  62. private Git git;
  63. RevCommit initialCommit;
  64. RevCommit secondCommit;
  65. class TestCommandFactory implements FilterCommandFactory {
  66. private int prefix;
  67. public TestCommandFactory(int prefix) {
  68. this.prefix = prefix;
  69. }
  70. @Override
  71. public FilterCommand create(Repository repo, InputStream in,
  72. final OutputStream out) {
  73. FilterCommand cmd = new FilterCommand(in, out) {
  74. @Override
  75. public int run() throws IOException {
  76. int b = in.read();
  77. if (b == -1) {
  78. return b;
  79. }
  80. out.write(prefix);
  81. out.write(b);
  82. return 1;
  83. }
  84. };
  85. return cmd;
  86. }
  87. }
  88. @Override
  89. @Before
  90. public void setUp() throws Exception {
  91. super.setUp();
  92. git = new Git(db);
  93. // commit something
  94. writeTrashFile("Test.txt", "Hello world");
  95. git.add().addFilepattern("Test.txt").call();
  96. initialCommit = git.commit().setMessage("Initial commit").call();
  97. // create a master branch and switch to it
  98. git.branchCreate().setName("test").call();
  99. RefUpdate rup = db.updateRef(Constants.HEAD);
  100. rup.link("refs/heads/test");
  101. // commit something on the test branch
  102. writeTrashFile("Test.txt", "Some change");
  103. git.add().addFilepattern("Test.txt").call();
  104. secondCommit = git.commit().setMessage("Second commit").call();
  105. }
  106. @Test
  107. public void testBuiltinCleanFilter()
  108. throws IOException, GitAPIException {
  109. String builtinCommandName = "jgit://builtin/test/clean";
  110. FilterCommandRegistry.register(builtinCommandName,
  111. new TestCommandFactory('c'));
  112. StoredConfig config = git.getRepository().getConfig();
  113. config.setString("filter", "test", "clean", builtinCommandName);
  114. config.save();
  115. writeTrashFile(".gitattributes", "*.txt filter=test");
  116. git.add().addFilepattern(".gitattributes").call();
  117. git.commit().setMessage("add filter").call();
  118. writeTrashFile("Test.txt", "Hello again");
  119. git.add().addFilepattern("Test.txt").call();
  120. assertEquals(
  121. "[.gitattributes, mode:100644, content:*.txt filter=test][Test.txt, mode:100644, content:cHceclclcoc cacgcacicn]",
  122. indexState(CONTENT));
  123. writeTrashFile("Test.bin", "Hello again");
  124. git.add().addFilepattern("Test.bin").call();
  125. assertEquals(
  126. "[.gitattributes, mode:100644, content:*.txt filter=test][Test.bin, mode:100644, content:Hello again][Test.txt, mode:100644, content:cHceclclcoc cacgcacicn]",
  127. indexState(CONTENT));
  128. config.setString("filter", "test", "clean", null);
  129. config.save();
  130. git.add().addFilepattern("Test.txt").call();
  131. assertEquals(
  132. "[.gitattributes, mode:100644, content:*.txt filter=test][Test.bin, mode:100644, content:Hello again][Test.txt, mode:100644, content:Hello again]",
  133. indexState(CONTENT));
  134. config.setString("filter", "test", "clean", null);
  135. config.save();
  136. }
  137. @Test
  138. public void testBuiltinSmudgeFilter() throws IOException, GitAPIException {
  139. String builtinCommandName = "jgit://builtin/test/smudge";
  140. FilterCommandRegistry.register(builtinCommandName,
  141. new TestCommandFactory('s'));
  142. StoredConfig config = git.getRepository().getConfig();
  143. config.setString("filter", "test", "smudge", builtinCommandName);
  144. config.save();
  145. writeTrashFile(".gitattributes", "*.txt filter=test");
  146. git.add().addFilepattern(".gitattributes").call();
  147. git.commit().setMessage("add filter").call();
  148. writeTrashFile("Test.txt", "Hello again");
  149. git.add().addFilepattern("Test.txt").call();
  150. assertEquals(
  151. "[.gitattributes, mode:100644, content:*.txt filter=test][Test.txt, mode:100644, content:Hello again]",
  152. indexState(CONTENT));
  153. assertEquals("Hello again", read("Test.txt"));
  154. deleteTrashFile("Test.txt");
  155. git.checkout().addPath("Test.txt").call();
  156. assertEquals("sHseslslsos sasgsasisn", read("Test.txt"));
  157. writeTrashFile("Test.bin", "Hello again");
  158. git.add().addFilepattern("Test.bin").call();
  159. assertEquals(
  160. "[.gitattributes, mode:100644, content:*.txt filter=test][Test.bin, mode:100644, content:Hello again][Test.txt, mode:100644, content:Hello again]",
  161. indexState(CONTENT));
  162. deleteTrashFile("Test.bin");
  163. git.checkout().addPath("Test.bin").call();
  164. assertEquals("Hello again", read("Test.bin"));
  165. config.setString("filter", "test", "clean", null);
  166. config.save();
  167. git.add().addFilepattern("Test.txt").call();
  168. assertEquals(
  169. "[.gitattributes, mode:100644, content:*.txt filter=test][Test.bin, mode:100644, content:Hello again][Test.txt, mode:100644, content:sHseslslsos sasgsasisn]",
  170. indexState(CONTENT));
  171. config.setString("filter", "test", "clean", null);
  172. config.save();
  173. }
  174. @Test
  175. public void testBuiltinCleanAndSmudgeFilter() throws IOException, GitAPIException {
  176. String builtinCommandPrefix = "jgit://builtin/test/";
  177. FilterCommandRegistry.register(builtinCommandPrefix + "smudge",
  178. new TestCommandFactory('s'));
  179. FilterCommandRegistry.register(builtinCommandPrefix + "clean",
  180. new TestCommandFactory('c'));
  181. StoredConfig config = git.getRepository().getConfig();
  182. config.setString("filter", "test", "smudge", builtinCommandPrefix+"smudge");
  183. config.setString("filter", "test", "clean",
  184. builtinCommandPrefix + "clean");
  185. config.save();
  186. writeTrashFile(".gitattributes", "*.txt filter=test");
  187. git.add().addFilepattern(".gitattributes").call();
  188. git.commit().setMessage("add filter").call();
  189. writeTrashFile("Test.txt", "Hello again");
  190. git.add().addFilepattern("Test.txt").call();
  191. assertEquals(
  192. "[.gitattributes, mode:100644, content:*.txt filter=test][Test.txt, mode:100644, content:cHceclclcoc cacgcacicn]",
  193. indexState(CONTENT));
  194. assertEquals("Hello again", read("Test.txt"));
  195. deleteTrashFile("Test.txt");
  196. git.checkout().addPath("Test.txt").call();
  197. assertEquals("scsHscsescslscslscsoscs scsascsgscsascsiscsn",
  198. read("Test.txt"));
  199. writeTrashFile("Test.bin", "Hello again");
  200. git.add().addFilepattern("Test.bin").call();
  201. assertEquals(
  202. "[.gitattributes, mode:100644, content:*.txt filter=test][Test.bin, mode:100644, content:Hello again][Test.txt, mode:100644, content:cHceclclcoc cacgcacicn]",
  203. indexState(CONTENT));
  204. deleteTrashFile("Test.bin");
  205. git.checkout().addPath("Test.bin").call();
  206. assertEquals("Hello again", read("Test.bin"));
  207. config.setString("filter", "test", "clean", null);
  208. config.save();
  209. git.add().addFilepattern("Test.txt").call();
  210. assertEquals(
  211. "[.gitattributes, mode:100644, content:*.txt filter=test][Test.bin, mode:100644, content:Hello again][Test.txt, mode:100644, content:scsHscsescslscslscsoscs scsascsgscsascsiscsn]",
  212. indexState(CONTENT));
  213. config.setString("filter", "test", "clean", null);
  214. config.save();
  215. }
  216. }