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