View Javadoc
1   /*
2    * Copyright (C) 2021, Thomas Wolf <thomas.wolf@paranor.ch> 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.lfs;
11  
12  import static org.junit.Assert.assertEquals;
13  import static org.junit.Assert.assertTrue;
14  
15  import java.io.File;
16  import java.nio.charset.StandardCharsets;
17  import java.nio.file.Files;
18  
19  import org.eclipse.jgit.api.Git;
20  import org.eclipse.jgit.api.ResetCommand.ResetType;
21  import org.eclipse.jgit.attributes.FilterCommandRegistry;
22  import org.eclipse.jgit.junit.RepositoryTestCase;
23  import org.eclipse.jgit.lfs.lib.Constants;
24  import org.eclipse.jgit.lib.StoredConfig;
25  import org.junit.AfterClass;
26  import org.junit.Before;
27  import org.junit.BeforeClass;
28  import org.junit.Test;
29  
30  public class LfsGitTest extends RepositoryTestCase {
31  
32  	private static final String SMUDGE_NAME = org.eclipse.jgit.lib.Constants.BUILTIN_FILTER_PREFIX
33  			+ Constants.ATTR_FILTER_DRIVER_PREFIX
34  			+ org.eclipse.jgit.lib.Constants.ATTR_FILTER_TYPE_SMUDGE;
35  
36  	private static final String CLEAN_NAME = org.eclipse.jgit.lib.Constants.BUILTIN_FILTER_PREFIX
37  			+ Constants.ATTR_FILTER_DRIVER_PREFIX
38  			+ org.eclipse.jgit.lib.Constants.ATTR_FILTER_TYPE_CLEAN;
39  
40  	@BeforeClass
41  	public static void installLfs() {
42  		FilterCommandRegistry.register(SMUDGE_NAME, SmudgeFilter.FACTORY);
43  		FilterCommandRegistry.register(CLEAN_NAME, CleanFilter.FACTORY);
44  	}
45  
46  	@AfterClass
47  	public static void removeLfs() {
48  		FilterCommandRegistry.unregister(SMUDGE_NAME);
49  		FilterCommandRegistry.unregister(CLEAN_NAME);
50  	}
51  
52  	private Git git;
53  
54  	@Override
55  	@Before
56  	public void setUp() throws Exception {
57  		super.setUp();
58  		git = new Git(db);
59  		// commit something
60  		writeTrashFile("Test.txt", "Hello world");
61  		git.add().addFilepattern("Test.txt").call();
62  		git.commit().setMessage("Initial commit").call();
63  		// prepare the config for LFS
64  		StoredConfig config = git.getRepository().getConfig();
65  		config.setString("filter", "lfs", "clean", CLEAN_NAME);
66  		config.setString("filter", "lfs", "smudge", SMUDGE_NAME);
67  		config.save();
68  	}
69  
70  	@Test
71  	public void checkoutNonLfsPointer() throws Exception {
72  		String content = "size_t\nsome_function(void* ptr);\n";
73  		File smallFile = writeTrashFile("Test.txt", content);
74  		StringBuilder largeContent = new StringBuilder(
75  				LfsPointer.SIZE_THRESHOLD * 4);
76  		while (largeContent.length() < LfsPointer.SIZE_THRESHOLD * 4) {
77  			largeContent.append(content);
78  		}
79  		File largeFile = writeTrashFile("large.txt", largeContent.toString());
80  		fsTick(largeFile);
81  		git.add().addFilepattern("Test.txt").addFilepattern("large.txt").call();
82  		git.commit().setMessage("Text files").call();
83  		writeTrashFile(".gitattributes", "*.txt filter=lfs");
84  		git.add().addFilepattern(".gitattributes").call();
85  		git.commit().setMessage("attributes").call();
86  		assertTrue(smallFile.delete());
87  		assertTrue(largeFile.delete());
88  		// This reset will run the two text files through the smudge filter
89  		git.reset().setMode(ResetType.HARD).call();
90  		assertTrue(smallFile.exists());
91  		assertTrue(largeFile.exists());
92  		checkFile(smallFile, content);
93  		checkFile(largeFile, largeContent.toString());
94  		// Modify the large file
95  		largeContent.append(content);
96  		writeTrashFile("large.txt", largeContent.toString());
97  		// This should convert largeFile to an LFS pointer
98  		git.add().addFilepattern("large.txt").call();
99  		git.commit().setMessage("Large modified").call();
100 		String lfsPtr = "version https://git-lfs.github.com/spec/v1\n"
101 				+ "oid sha256:d041ab19bd7edd899b3c0450d0f61819f96672f0b22d26c9753abc62e1261614\n"
102 				+ "size 858\n";
103 		assertEquals("[.gitattributes, mode:100644, content:*.txt filter=lfs]"
104 				+ "[Test.txt, mode:100644, content:" + content + ']'
105 						+ "[large.txt, mode:100644, content:" + lfsPtr + ']',
106 				indexState(CONTENT));
107 		// Verify the file has been saved
108 		File savedFile = new File(db.getDirectory(), "lfs");
109 		savedFile = new File(savedFile, "objects");
110 		savedFile = new File(savedFile, "d0");
111 		savedFile = new File(savedFile, "41");
112 		savedFile = new File(savedFile,
113 				"d041ab19bd7edd899b3c0450d0f61819f96672f0b22d26c9753abc62e1261614");
114 		String saved = new String(Files.readAllBytes(savedFile.toPath()),
115 				StandardCharsets.UTF_8);
116 		assertEquals(saved, largeContent.toString());
117 
118 		assertTrue(smallFile.delete());
119 		assertTrue(largeFile.delete());
120 		git.reset().setMode(ResetType.HARD).call();
121 		assertTrue(smallFile.exists());
122 		assertTrue(largeFile.exists());
123 		checkFile(smallFile, content);
124 		checkFile(largeFile, largeContent.toString());
125 		assertEquals("[.gitattributes, mode:100644, content:*.txt filter=lfs]"
126 				+ "[Test.txt, mode:100644, content:" + content + ']'
127 						+ "[large.txt, mode:100644, content:" + lfsPtr + ']',
128 				indexState(CONTENT));
129 		git.add().addFilepattern("Test.txt").call();
130 		git.commit().setMessage("Small committed again").call();
131 		String lfsPtrSmall = "version https://git-lfs.github.com/spec/v1\n"
132 				+ "oid sha256:9110463275fb0e2f0e9fdeaf84e598e62915666161145cf08927079119cc7814\n"
133 				+ "size 33\n";
134 		assertEquals("[.gitattributes, mode:100644, content:*.txt filter=lfs]"
135 				+ "[Test.txt, mode:100644, content:" + lfsPtrSmall + ']'
136 						+ "[large.txt, mode:100644, content:" + lfsPtr + ']',
137 				indexState(CONTENT));
138 
139 		assertTrue(git.status().call().isClean());
140 	}
141 }