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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. in.close();
  79. out.close();
  80. return b;
  81. }
  82. out.write(prefix);
  83. out.write(b);
  84. return 1;
  85. }
  86. };
  87. return cmd;
  88. }
  89. }
  90. @Override
  91. @Before
  92. public void setUp() throws Exception {
  93. super.setUp();
  94. git = new Git(db);
  95. // commit something
  96. writeTrashFile("Test.txt", "Hello world");
  97. git.add().addFilepattern("Test.txt").call();
  98. initialCommit = git.commit().setMessage("Initial commit").call();
  99. // create a master branch and switch to it
  100. git.branchCreate().setName("test").call();
  101. RefUpdate rup = db.updateRef(Constants.HEAD);
  102. rup.link("refs/heads/test");
  103. // commit something on the test branch
  104. writeTrashFile("Test.txt", "Some change");
  105. git.add().addFilepattern("Test.txt").call();
  106. secondCommit = git.commit().setMessage("Second commit").call();
  107. }
  108. @Test
  109. public void testBuiltinCleanFilter()
  110. throws IOException, GitAPIException {
  111. String builtinCommandName = "jgit://builtin/test/clean";
  112. FilterCommandRegistry.register(builtinCommandName,
  113. new TestCommandFactory('c'));
  114. StoredConfig config = git.getRepository().getConfig();
  115. config.setString("filter", "test", "clean", builtinCommandName);
  116. config.save();
  117. writeTrashFile(".gitattributes", "*.txt filter=test");
  118. git.add().addFilepattern(".gitattributes").call();
  119. git.commit().setMessage("add filter").call();
  120. writeTrashFile("Test.txt", "Hello again");
  121. git.add().addFilepattern("Test.txt").call();
  122. assertEquals(
  123. "[.gitattributes, mode:100644, content:*.txt filter=test][Test.txt, mode:100644, content:cHceclclcoc cacgcacicn]",
  124. indexState(CONTENT));
  125. writeTrashFile("Test.bin", "Hello again");
  126. git.add().addFilepattern("Test.bin").call();
  127. assertEquals(
  128. "[.gitattributes, mode:100644, content:*.txt filter=test][Test.bin, mode:100644, content:Hello again][Test.txt, mode:100644, content:cHceclclcoc cacgcacicn]",
  129. indexState(CONTENT));
  130. config.setString("filter", "test", "clean", null);
  131. config.save();
  132. git.add().addFilepattern("Test.txt").call();
  133. assertEquals(
  134. "[.gitattributes, mode:100644, content:*.txt filter=test][Test.bin, mode:100644, content:Hello again][Test.txt, mode:100644, content:Hello again]",
  135. indexState(CONTENT));
  136. config.setString("filter", "test", "clean", null);
  137. config.save();
  138. }
  139. @Test
  140. public void testBuiltinSmudgeFilter() throws IOException, GitAPIException {
  141. String builtinCommandName = "jgit://builtin/test/smudge";
  142. FilterCommandRegistry.register(builtinCommandName,
  143. new TestCommandFactory('s'));
  144. StoredConfig config = git.getRepository().getConfig();
  145. config.setString("filter", "test", "smudge", builtinCommandName);
  146. config.save();
  147. writeTrashFile(".gitattributes", "*.txt filter=test");
  148. git.add().addFilepattern(".gitattributes").call();
  149. git.commit().setMessage("add filter").call();
  150. writeTrashFile("Test.txt", "Hello again");
  151. git.add().addFilepattern("Test.txt").call();
  152. assertEquals(
  153. "[.gitattributes, mode:100644, content:*.txt filter=test][Test.txt, mode:100644, content:Hello again]",
  154. indexState(CONTENT));
  155. assertEquals("Hello again", read("Test.txt"));
  156. deleteTrashFile("Test.txt");
  157. git.checkout().addPath("Test.txt").call();
  158. assertEquals("sHseslslsos sasgsasisn", read("Test.txt"));
  159. writeTrashFile("Test.bin", "Hello again");
  160. git.add().addFilepattern("Test.bin").call();
  161. assertEquals(
  162. "[.gitattributes, mode:100644, content:*.txt filter=test][Test.bin, mode:100644, content:Hello again][Test.txt, mode:100644, content:Hello again]",
  163. indexState(CONTENT));
  164. deleteTrashFile("Test.bin");
  165. git.checkout().addPath("Test.bin").call();
  166. assertEquals("Hello again", read("Test.bin"));
  167. config.setString("filter", "test", "clean", null);
  168. config.save();
  169. git.add().addFilepattern("Test.txt").call();
  170. assertEquals(
  171. "[.gitattributes, mode:100644, content:*.txt filter=test][Test.bin, mode:100644, content:Hello again][Test.txt, mode:100644, content:sHseslslsos sasgsasisn]",
  172. indexState(CONTENT));
  173. config.setString("filter", "test", "clean", null);
  174. config.save();
  175. }
  176. @Test
  177. public void testBuiltinCleanAndSmudgeFilter() throws IOException, GitAPIException {
  178. String builtinCommandPrefix = "jgit://builtin/test/";
  179. FilterCommandRegistry.register(builtinCommandPrefix + "smudge",
  180. new TestCommandFactory('s'));
  181. FilterCommandRegistry.register(builtinCommandPrefix + "clean",
  182. new TestCommandFactory('c'));
  183. StoredConfig config = git.getRepository().getConfig();
  184. config.setString("filter", "test", "smudge", builtinCommandPrefix+"smudge");
  185. config.setString("filter", "test", "clean",
  186. builtinCommandPrefix + "clean");
  187. config.save();
  188. writeTrashFile(".gitattributes", "*.txt filter=test");
  189. git.add().addFilepattern(".gitattributes").call();
  190. git.commit().setMessage("add filter").call();
  191. writeTrashFile("Test.txt", "Hello again");
  192. git.add().addFilepattern("Test.txt").call();
  193. assertEquals(
  194. "[.gitattributes, mode:100644, content:*.txt filter=test][Test.txt, mode:100644, content:cHceclclcoc cacgcacicn]",
  195. indexState(CONTENT));
  196. assertEquals("Hello again", read("Test.txt"));
  197. deleteTrashFile("Test.txt");
  198. git.checkout().addPath("Test.txt").call();
  199. assertEquals("scsHscsescslscslscsoscs scsascsgscsascsiscsn",
  200. read("Test.txt"));
  201. writeTrashFile("Test.bin", "Hello again");
  202. git.add().addFilepattern("Test.bin").call();
  203. assertEquals(
  204. "[.gitattributes, mode:100644, content:*.txt filter=test][Test.bin, mode:100644, content:Hello again][Test.txt, mode:100644, content:cHceclclcoc cacgcacicn]",
  205. indexState(CONTENT));
  206. deleteTrashFile("Test.bin");
  207. git.checkout().addPath("Test.bin").call();
  208. assertEquals("Hello again", read("Test.bin"));
  209. config.setString("filter", "test", "clean", null);
  210. config.save();
  211. git.add().addFilepattern("Test.txt").call();
  212. assertEquals(
  213. "[.gitattributes, mode:100644, content:*.txt filter=test][Test.bin, mode:100644, content:Hello again][Test.txt, mode:100644, content:scsHscsescslscslscsoscs scsascsgscsascsiscsn]",
  214. indexState(CONTENT));
  215. config.setString("filter", "test", "clean", null);
  216. config.save();
  217. }
  218. }