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 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 		// check object in remote db, should be LFS pointer
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 }