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 java.nio.charset.StandardCharsets.UTF_8;
46 import static org.junit.Assert.assertEquals;
47
48 import java.io.InputStream;
49 import java.nio.file.Files;
50 import java.nio.file.Path;
51
52 import org.eclipse.jgit.api.Git;
53 import org.eclipse.jgit.api.RemoteAddCommand;
54 import org.eclipse.jgit.junit.JGitTestUtil;
55 import org.eclipse.jgit.junit.TestRepository;
56 import org.eclipse.jgit.lfs.BuiltinLFS;
57 import org.eclipse.jgit.lib.Constants;
58 import org.eclipse.jgit.lib.ObjectId;
59 import org.eclipse.jgit.lib.ObjectLoader;
60 import org.eclipse.jgit.lib.Repository;
61 import org.eclipse.jgit.lib.StoredConfig;
62 import org.eclipse.jgit.revwalk.RevCommit;
63 import org.eclipse.jgit.revwalk.RevWalk;
64 import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
65 import org.eclipse.jgit.transport.URIish;
66 import org.eclipse.jgit.treewalk.TreeWalk;
67 import org.eclipse.jgit.treewalk.filter.PathFilter;
68 import org.eclipse.jgit.util.FileUtils;
69 import org.eclipse.jgit.util.IO;
70 import org.junit.After;
71 import org.junit.Before;
72 import org.junit.Test;
73
74 public class PushTest extends LfsServerTest {
75
76 Git git;
77
78 private TestRepository localDb;
79
80 private Repository remoteDb;
81
82 @Override
83 @Before
84 public void setup() throws Exception {
85 super.setup();
86
87 BuiltinLFS.register();
88
89 Path rtmp = Files.createTempDirectory("jgit_test_");
90 remoteDb = FileRepositoryBuilder.create(rtmp.toFile());
91 remoteDb.create(true);
92
93 Path tmp = Files.createTempDirectory("jgit_test_");
94 Repository db = FileRepositoryBuilder
95 .create(tmp.resolve(".git").toFile());
96 db.create(false);
97 StoredConfig cfg = db.getConfig();
98 cfg.setString("filter", "lfs", "usejgitbuiltin", "true");
99 cfg.setString("lfs", null, "url", server.getURI().toString() + "/lfs");
100 cfg.save();
101
102 localDb = new TestRepository<>(db);
103 localDb.branch("master").commit().add(".gitattributes",
104 "*.bin filter=lfs diff=lfs merge=lfs -text ").create();
105 git = Git.wrap(db);
106
107 URIish uri = new URIish(
108 "file://" + remoteDb.getDirectory());
109 RemoteAddCommand radd = git.remoteAdd();
110 radd.setUri(uri);
111 radd.setName(Constants.DEFAULT_REMOTE_NAME);
112 radd.call();
113
114 git.checkout().setName("master").call();
115 git.push().call();
116 }
117
118 @After
119 public void cleanup() throws Exception {
120 remoteDb.close();
121 localDb.getRepository().close();
122 FileUtils.delete(localDb.getRepository().getWorkTree(),
123 FileUtils.RECURSIVE);
124 FileUtils.delete(remoteDb.getDirectory(), FileUtils.RECURSIVE);
125 }
126
127 @Test
128 public void testPushSimple() throws Exception {
129 JGitTestUtil.writeTrashFile(localDb.getRepository(), "a.bin",
130 "1234567");
131 git.add().addFilepattern("a.bin").call();
132 RevCommit commit = git.commit().setMessage("add lfs blob").call();
133 git.push().call();
134
135
136 ObjectId id = commit.getId();
137 try (RevWalk walk = new RevWalk(remoteDb)) {
138 RevCommit rc = walk.parseCommit(id);
139 try (TreeWalk tw = new TreeWalk(walk.getObjectReader())) {
140 tw.addTree(rc.getTree());
141 tw.setFilter(PathFilter.create("a.bin"));
142 tw.next();
143
144 assertEquals(tw.getPathString(), "a.bin");
145 ObjectLoader ldr = walk.getObjectReader()
146 .open(tw.getObjectId(0), Constants.OBJ_BLOB);
147 try(InputStream is = ldr.openStream()) {
148 assertEquals(
149 "version https://git-lfs.github.com/spec/v1\noid sha256:8bb0cf6eb9b17d0f7d22b456f121257dc1254e1f01665370476383ea776df414\nsize 7\n",
150 new String(IO
151 .readWholeStream(is,
152 (int) ldr.getSize())
153 .array(), UTF_8));
154 }
155 }
156
157 }
158
159 assertEquals(
160 "[POST /lfs/objects/batch 200, PUT /lfs/objects/8bb0cf6eb9b17d0f7d22b456f121257dc1254e1f01665370476383ea776df414 200]",
161 server.getRequests().toString());
162 }
163
164 }