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