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 package org.eclipse.jgit.lfs.server.fs;
44
45 import static org.junit.Assert.assertEquals;
46 import static org.junit.Assert.assertFalse;
47
48 import java.nio.file.Files;
49 import java.nio.file.Path;
50
51 import org.eclipse.jgit.api.Git;
52 import org.eclipse.jgit.api.errors.JGitInternalException;
53 import org.eclipse.jgit.junit.JGitTestUtil;
54 import org.eclipse.jgit.junit.TestRepository;
55 import org.eclipse.jgit.lfs.BuiltinLFS;
56 import org.eclipse.jgit.lfs.lib.LongObjectId;
57 import org.eclipse.jgit.lib.ConfigConstants;
58 import org.eclipse.jgit.lib.Repository;
59 import org.eclipse.jgit.lib.StoredConfig;
60 import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
61 import org.eclipse.jgit.util.FileUtils;
62 import org.junit.After;
63 import org.junit.Before;
64 import org.junit.Test;
65
66 public class CheckoutTest extends LfsServerTest {
67
68 Git git;
69 private TestRepository tdb;
70
71 @Override
72 @Before
73 public void setup() throws Exception {
74 super.setup();
75
76 BuiltinLFS.register();
77
78 Path tmp = Files.createTempDirectory("jgit_test_");
79 Repository db = FileRepositoryBuilder
80 .create(tmp.resolve(".git").toFile());
81 db.create();
82 StoredConfig cfg = db.getConfig();
83 cfg.setBoolean(ConfigConstants.CONFIG_FILTER_SECTION,
84 ConfigConstants.CONFIG_SECTION_LFS,
85 ConfigConstants.CONFIG_KEY_USEJGITBUILTIN, true);
86 cfg.setBoolean(ConfigConstants.CONFIG_FILTER_SECTION,
87 ConfigConstants.CONFIG_SECTION_LFS,
88 ConfigConstants.CONFIG_KEY_REQUIRED, false);
89 cfg.setString(ConfigConstants.CONFIG_SECTION_LFS, null, "url",
90 server.getURI().toString() + "/lfs");
91 cfg.save();
92
93 tdb = new TestRepository<>(db);
94 tdb.branch("test").commit()
95 .add(".gitattributes",
96 "*.bin filter=lfs diff=lfs merge=lfs -text ")
97 .add("a.bin",
98 "version https://git-lfs.github.com/spec/v1\noid sha256:8bb0cf6eb9b17d0f7d22b456f121257dc1254e1f01665370476383ea776df414\nsize 7\n")
99 .create();
100 git = Git.wrap(db);
101 tdb.branch("test2").commit().add(".gitattributes",
102 "*.bin filter=lfs diff=lfs merge=lfs -text ").create();
103 }
104
105 @After
106 public void cleanup() throws Exception {
107 tdb.getRepository().close();
108 FileUtils.delete(tdb.getRepository().getWorkTree(),
109 FileUtils.RECURSIVE);
110 }
111
112 @Test
113 public void testUnknownContent() throws Exception {
114 git.checkout().setName("test").call();
115
116 assertEquals(
117 "version https://git-lfs.github.com/spec/v1\noid sha256:8bb0cf6eb9b17d0f7d22b456f121257dc1254e1f01665370476383ea776df414\nsize 7\n",
118 JGitTestUtil.read(git.getRepository(), "a.bin"));
119 assertEquals("[POST /lfs/objects/batch 200]",
120 server.getRequests().toString());
121 }
122
123 @Test(expected = JGitInternalException.class)
124 public void testUnknownContentRequired() throws Exception {
125 StoredConfig cfg = tdb.getRepository().getConfig();
126 cfg.setBoolean(ConfigConstants.CONFIG_FILTER_SECTION,
127 ConfigConstants.CONFIG_SECTION_LFS,
128 ConfigConstants.CONFIG_KEY_REQUIRED, true);
129 cfg.save();
130
131
132 git.checkout().setName("test").call();
133 }
134
135 @Test
136 public void testKnownContent() throws Exception {
137 putContent(
138 LongObjectId.fromString(
139 "8bb0cf6eb9b17d0f7d22b456f121257dc1254e1f01665370476383ea776df414"),
140 "1234567");
141 git.checkout().setName("test").call();
142
143 assertEquals(
144 "1234567",
145 JGitTestUtil.read(git.getRepository(), "a.bin"));
146 assertEquals(
147 "[PUT /lfs/objects/8bb0cf6eb9b17d0f7d22b456f121257dc1254e1f01665370476383ea776df414 200"
148 + ", POST /lfs/objects/batch 200"
149 + ", GET /lfs/objects/8bb0cf6eb9b17d0f7d22b456f121257dc1254e1f01665370476383ea776df414 200]",
150 server.getRequests().toString());
151
152 git.checkout().setName("test2").call();
153 assertFalse(JGitTestUtil.check(git.getRepository(), "a.bin"));
154 git.checkout().setName("test").call();
155
156 assertEquals("1234567",
157 JGitTestUtil.read(git.getRepository(), "a.bin"));
158 assertEquals(3, server.getRequests().size());
159 }
160
161 }