xref: /JGit/org.eclipse.jgit.lfs.server.test/tst/org/eclipse/jgit/lfs/server/fs/PushTest.java (revision 5c5f7c6b146b24f2bd4afae1902df85ad6e57ea3)
1 /*
2  * Copyright (C) 2018, Markus Duft <markus.duft@ssi-schaefer.com> 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.server.fs;
11 
12 import static java.nio.charset.StandardCharsets.UTF_8;
13 import static org.junit.Assert.assertEquals;
14 
15 import java.io.InputStream;
16 import java.nio.file.Files;
17 import java.nio.file.Path;
18 
19 import org.eclipse.jgit.api.Git;
20 import org.eclipse.jgit.api.RemoteAddCommand;
21 import org.eclipse.jgit.junit.JGitTestUtil;
22 import org.eclipse.jgit.junit.TestRepository;
23 import org.eclipse.jgit.lfs.BuiltinLFS;
24 import org.eclipse.jgit.lib.Constants;
25 import org.eclipse.jgit.lib.ObjectId;
26 import org.eclipse.jgit.lib.ObjectLoader;
27 import org.eclipse.jgit.lib.Repository;
28 import org.eclipse.jgit.lib.StoredConfig;
29 import org.eclipse.jgit.revwalk.RevCommit;
30 import org.eclipse.jgit.revwalk.RevWalk;
31 import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
32 import org.eclipse.jgit.transport.URIish;
33 import org.eclipse.jgit.treewalk.TreeWalk;
34 import org.eclipse.jgit.treewalk.filter.PathFilter;
35 import org.eclipse.jgit.util.FileUtils;
36 import org.eclipse.jgit.util.IO;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 
41 public class PushTest extends LfsServerTest {
42 
43 	Git git;
44 
45 	private TestRepository localDb;
46 
47 	private Repository remoteDb;
48 
49 	@Override
50 	@Before
setup()51 	public void setup() throws Exception {
52 		super.setup();
53 
54 		BuiltinLFS.register();
55 
56 		Path rtmp = Files.createTempDirectory("jgit_test_");
57 		remoteDb = FileRepositoryBuilder.create(rtmp.toFile());
58 		remoteDb.create(true);
59 
60 		Path tmp = Files.createTempDirectory("jgit_test_");
61 		Repository db = FileRepositoryBuilder
62 				.create(tmp.resolve(".git").toFile());
63 		db.create(false);
64 		StoredConfig cfg = db.getConfig();
65 		cfg.setString("filter", "lfs", "usejgitbuiltin", "true");
66 		cfg.setString("lfs", null, "url", server.getURI().toString() + "/lfs");
67 		cfg.save();
68 
69 		localDb = new TestRepository<>(db);
70 		localDb.branch("master").commit().add(".gitattributes",
71 				"*.bin filter=lfs diff=lfs merge=lfs -text ").create();
72 		git = Git.wrap(db);
73 
74 		URIish uri = new URIish(
75 				"file://" + remoteDb.getDirectory());
76 		RemoteAddCommand radd = git.remoteAdd();
77 		radd.setUri(uri);
78 		radd.setName(Constants.DEFAULT_REMOTE_NAME);
79 		radd.call();
80 
81 		git.checkout().setName("master").call();
82 		git.push().call();
83 	}
84 
85 	@After
cleanup()86 	public void cleanup() throws Exception {
87 		remoteDb.close();
88 		localDb.getRepository().close();
89 		FileUtils.delete(localDb.getRepository().getWorkTree(),
90 				FileUtils.RECURSIVE);
91 		FileUtils.delete(remoteDb.getDirectory(), FileUtils.RECURSIVE);
92 	}
93 
94 	@Test
testPushSimple()95 	public void testPushSimple() throws Exception {
96 		JGitTestUtil.writeTrashFile(localDb.getRepository(), "a.bin",
97 				"1234567");
98 		git.add().addFilepattern("a.bin").call();
99 		RevCommit commit = git.commit().setMessage("add lfs blob").call();
100 		git.push().call();
101 
102 		// check object in remote db, should be LFS pointer
103 		ObjectId id = commit.getId();
104 		try (RevWalk walk = new RevWalk(remoteDb)) {
105 			RevCommit rc = walk.parseCommit(id);
106 			try (TreeWalk tw = new TreeWalk(walk.getObjectReader())) {
107 				tw.addTree(rc.getTree());
108 				tw.setFilter(PathFilter.create("a.bin"));
109 				tw.next();
110 
111 				assertEquals(tw.getPathString(), "a.bin");
112 				ObjectLoader ldr = walk.getObjectReader()
113 						.open(tw.getObjectId(0), Constants.OBJ_BLOB);
114 				try(InputStream is = ldr.openStream()) {
115 					assertEquals(
116 							"version https://git-lfs.github.com/spec/v1\noid sha256:8bb0cf6eb9b17d0f7d22b456f121257dc1254e1f01665370476383ea776df414\nsize 7\n",
117 							new String(IO
118 									.readWholeStream(is,
119 											(int) ldr.getSize())
120 									.array(), UTF_8));
121 				}
122 			}
123 
124 		}
125 
126 		assertEquals(
127 				"[POST /lfs/objects/batch 200, PUT /lfs/objects/8bb0cf6eb9b17d0f7d22b456f121257dc1254e1f01665370476383ea776df414 200]",
128 				server.getRequests().toString());
129 	}
130 
131 }
132