View Javadoc
1   /*
2    * Copyright (C) 2018, Markus Duft <markus.duft@ssi-schaefer.com>
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 		// check object in remote db, should be LFS pointer
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 }