summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.lfs.test/tst/org/eclipse/jgit/lfs/LfsGitTest.java
blob: 3e83c8ef49d9e39d7ea92b4874150343afddadb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * Copyright (C) 2021, 2022 Thomas Wolf <thomas.wolf@paranor.ch> and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */
package org.eclipse.jgit.lfs;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ResetCommand.ResetType;
import org.eclipse.jgit.attributes.FilterCommandRegistry;
import org.eclipse.jgit.junit.RepositoryTestCase;
import org.eclipse.jgit.lfs.lib.Constants;
import org.eclipse.jgit.lib.StoredConfig;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class LfsGitTest extends RepositoryTestCase {

	private static final String SMUDGE_NAME = org.eclipse.jgit.lib.Constants.BUILTIN_FILTER_PREFIX
			+ Constants.ATTR_FILTER_DRIVER_PREFIX
			+ org.eclipse.jgit.lib.Constants.ATTR_FILTER_TYPE_SMUDGE;

	private static final String CLEAN_NAME = org.eclipse.jgit.lib.Constants.BUILTIN_FILTER_PREFIX
			+ Constants.ATTR_FILTER_DRIVER_PREFIX
			+ org.eclipse.jgit.lib.Constants.ATTR_FILTER_TYPE_CLEAN;

	@BeforeClass
	public static void installLfs() {
		FilterCommandRegistry.register(SMUDGE_NAME, SmudgeFilter.FACTORY);
		FilterCommandRegistry.register(CLEAN_NAME, CleanFilter.FACTORY);
	}

	@AfterClass
	public static void removeLfs() {
		FilterCommandRegistry.unregister(SMUDGE_NAME);
		FilterCommandRegistry.unregister(CLEAN_NAME);
	}

	private Git git;

	@Override
	@Before
	public void setUp() throws Exception {
		super.setUp();
		git = new Git(db);
		// commit something
		writeTrashFile("Test.txt", "Hello world");
		git.add().addFilepattern("Test.txt").call();
		git.commit().setMessage("Initial commit").call();
		// prepare the config for LFS
		StoredConfig config = git.getRepository().getConfig();
		config.setString("filter", "lfs", "clean", CLEAN_NAME);
		config.setString("filter", "lfs", "smudge", SMUDGE_NAME);
		config.save();
	}

	@Test
	public void testBranchSwitch() throws Exception {
		git.branchCreate().setName("abranch").call();
		git.checkout().setName("abranch").call();
		File aFile = writeTrashFile("a.bin", "aaa");
		writeTrashFile(".gitattributes", "a.bin filter=lfs");
		git.add().addFilepattern(".").call();
		git.commit().setMessage("acommit").call();
		git.checkout().setName("master").call();
		git.branchCreate().setName("bbranch").call();
		git.checkout().setName("bbranch").call();
		File bFile = writeTrashFile("b.bin", "bbb");
		writeTrashFile(".gitattributes", "b.bin filter=lfs");
		git.add().addFilepattern(".").call();
		git.commit().setMessage("bcommit").call();
		git.checkout().setName("abranch").call();
		checkFile(aFile, "aaa");
		git.checkout().setName("bbranch").call();
		checkFile(bFile, "bbb");
	}

	@Test
	public void checkoutNonLfsPointer() throws Exception {
		String content = "size_t\nsome_function(void* ptr);\n";
		File smallFile = writeTrashFile("Test.txt", content);
		StringBuilder largeContent = new StringBuilder(
				LfsPointer.SIZE_THRESHOLD * 4);
		while (largeContent.length() < LfsPointer.SIZE_THRESHOLD * 4) {
			largeContent.append(content);
		}
		File largeFile = writeTrashFile("large.txt", largeContent.toString());
		fsTick(largeFile);
		git.add().addFilepattern("Test.txt").addFilepattern("large.txt").call();
		git.commit().setMessage("Text files").call();
		writeTrashFile(".gitattributes", "*.txt filter=lfs");
		git.add().addFilepattern(".gitattributes").call();
		git.commit().setMessage("attributes").call();
		assertTrue(smallFile.delete());
		assertTrue(largeFile.delete());
		// This reset will run the two text files through the smudge filter
		git.reset().setMode(ResetType.HARD).call();
		assertTrue(smallFile.exists());
		assertTrue(largeFile.exists());
		checkFile(smallFile, content);
		checkFile(largeFile, largeContent.toString());
		// Modify the large file
		largeContent.append(content);
		writeTrashFile("large.txt", largeContent.toString());
		// This should convert largeFile to an LFS pointer
		git.add().addFilepattern("large.txt").call();
		git.commit().setMessage("Large modified").call();
		String lfsPtr = "version https://git-lfs.github.com/spec/v1\n"
				+ "oid sha256:d041ab19bd7edd899b3c0450d0f61819f96672f0b22d26c9753abc62e1261614\n"
				+ "size 858\n";
		assertEquals("[.gitattributes, mode:100644, content:*.txt filter=lfs]"
				+ "[Test.txt, mode:100644, content:" + content + ']'
						+ "[large.txt, mode:100644, content:" + lfsPtr + ']',
				indexState(CONTENT));
		// Verify the file has been saved
		File savedFile = new File(db.getDirectory(), "lfs");
		savedFile = new File(savedFile, "objects");
		savedFile = new File(savedFile, "d0");
		savedFile = new File(savedFile, "41");
		savedFile = new File(savedFile,
				"d041ab19bd7edd899b3c0450d0f61819f96672f0b22d26c9753abc62e1261614");
		String saved = new String(Files.readAllBytes(savedFile.toPath()),
				StandardCharsets.UTF_8);
		assertEquals(saved, largeContent.toString());

		assertTrue(smallFile.delete());
		assertTrue(largeFile.delete());
		git.reset().setMode(ResetType.HARD).call();
		assertTrue(smallFile.exists());
		assertTrue(largeFile.exists());
		checkFile(smallFile, content);
		checkFile(largeFile, largeContent.toString());
		assertEquals("[.gitattributes, mode:100644, content:*.txt filter=lfs]"
				+ "[Test.txt, mode:100644, content:" + content + ']'
						+ "[large.txt, mode:100644, content:" + lfsPtr + ']',
				indexState(CONTENT));
		git.add().addFilepattern("Test.txt").call();
		git.commit().setMessage("Small committed again").call();
		String lfsPtrSmall = "version https://git-lfs.github.com/spec/v1\n"
				+ "oid sha256:9110463275fb0e2f0e9fdeaf84e598e62915666161145cf08927079119cc7814\n"
				+ "size 33\n";
		assertEquals("[.gitattributes, mode:100644, content:*.txt filter=lfs]"
				+ "[Test.txt, mode:100644, content:" + lfsPtrSmall + ']'
						+ "[large.txt, mode:100644, content:" + lfsPtr + ']',
				indexState(CONTENT));

		assertTrue(git.status().call().isClean());
	}
}