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