View Javadoc
1   /*
2    * Copyright (C) 2016, Christian Halstrick <christian.halstrick@sap.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 org.junit.Assert.assertEquals;
13  import static org.junit.Assert.assertFalse;
14  
15  import java.nio.file.Files;
16  import java.nio.file.Path;
17  
18  import org.eclipse.jgit.api.Git;
19  import org.eclipse.jgit.api.errors.JGitInternalException;
20  import org.eclipse.jgit.junit.JGitTestUtil;
21  import org.eclipse.jgit.junit.TestRepository;
22  import org.eclipse.jgit.lfs.BuiltinLFS;
23  import org.eclipse.jgit.lfs.lib.LongObjectId;
24  import org.eclipse.jgit.lib.ConfigConstants;
25  import org.eclipse.jgit.lib.Repository;
26  import org.eclipse.jgit.lib.StoredConfig;
27  import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
28  import org.eclipse.jgit.util.FileUtils;
29  import org.junit.After;
30  import org.junit.Before;
31  import org.junit.Test;
32  
33  public class CheckoutTest extends LfsServerTest {
34  
35  	Git git;
36  	private TestRepository tdb;
37  
38  	@Override
39  	@Before
40  	public void setup() throws Exception {
41  		super.setup();
42  
43  		BuiltinLFS.register();
44  
45  		Path tmp = Files.createTempDirectory("jgit_test_");
46  		Repository db = FileRepositoryBuilder
47  				.create(tmp.resolve(".git").toFile());
48  		db.create();
49  		StoredConfig cfg = db.getConfig();
50  		cfg.setBoolean(ConfigConstants.CONFIG_FILTER_SECTION,
51  				ConfigConstants.CONFIG_SECTION_LFS,
52  				ConfigConstants.CONFIG_KEY_USEJGITBUILTIN, true);
53  		cfg.setBoolean(ConfigConstants.CONFIG_FILTER_SECTION,
54  				ConfigConstants.CONFIG_SECTION_LFS,
55  				ConfigConstants.CONFIG_KEY_REQUIRED, false);
56  		cfg.setString(ConfigConstants.CONFIG_SECTION_LFS, null, "url",
57  				server.getURI().toString() + "/lfs");
58  		cfg.save();
59  
60  		tdb = new TestRepository<>(db);
61  		tdb.branch("test").commit()
62  				.add(".gitattributes",
63  						"*.bin filter=lfs diff=lfs merge=lfs -text ")
64  				.add("a.bin",
65  						"version https://git-lfs.github.com/spec/v1\noid sha256:8bb0cf6eb9b17d0f7d22b456f121257dc1254e1f01665370476383ea776df414\nsize 7\n")
66  				.create();
67  		git = Git.wrap(db);
68  		tdb.branch("test2").commit().add(".gitattributes",
69  				"*.bin filter=lfs diff=lfs merge=lfs -text ").create();
70  	}
71  
72  	@After
73  	public void cleanup() throws Exception {
74  		tdb.getRepository().close();
75  		FileUtils.delete(tdb.getRepository().getWorkTree(),
76  				FileUtils.RECURSIVE);
77  	}
78  
79  	@Test
80  	public void testUnknownContent() throws Exception {
81  		git.checkout().setName("test").call();
82  		// unknown content. We will see the pointer file
83  		assertEquals(
84  				"version https://git-lfs.github.com/spec/v1\noid sha256:8bb0cf6eb9b17d0f7d22b456f121257dc1254e1f01665370476383ea776df414\nsize 7\n",
85  				JGitTestUtil.read(git.getRepository(), "a.bin"));
86  		assertEquals("[POST /lfs/objects/batch 200]",
87  				server.getRequests().toString());
88  	}
89  
90  	@Test(expected = JGitInternalException.class)
91  	public void testUnknownContentRequired() throws Exception {
92  		StoredConfig cfg = tdb.getRepository().getConfig();
93  		cfg.setBoolean(ConfigConstants.CONFIG_FILTER_SECTION,
94  				ConfigConstants.CONFIG_SECTION_LFS,
95  				ConfigConstants.CONFIG_KEY_REQUIRED, true);
96  		cfg.save();
97  
98  		// must throw
99  		git.checkout().setName("test").call();
100 	}
101 
102 	@Test
103 	public void testKnownContent() throws Exception {
104 		putContent(
105 				LongObjectId.fromString(
106 						"8bb0cf6eb9b17d0f7d22b456f121257dc1254e1f01665370476383ea776df414"),
107 				"1234567");
108 		git.checkout().setName("test").call();
109 		// known content. we will see the actual content of the LFS blob.
110 		assertEquals(
111 				"1234567",
112 				JGitTestUtil.read(git.getRepository(), "a.bin"));
113 		assertEquals(
114 				"[PUT /lfs/objects/8bb0cf6eb9b17d0f7d22b456f121257dc1254e1f01665370476383ea776df414 200"
115 						+ ", POST /lfs/objects/batch 200"
116 						+ ", GET /lfs/objects/8bb0cf6eb9b17d0f7d22b456f121257dc1254e1f01665370476383ea776df414 200]",
117 				server.getRequests().toString());
118 
119 		git.checkout().setName("test2").call();
120 		assertFalse(JGitTestUtil.check(git.getRepository(), "a.bin"));
121 		git.checkout().setName("test").call();
122 		// unknown content. We will see the pointer file
123 		assertEquals("1234567",
124 				JGitTestUtil.read(git.getRepository(), "a.bin"));
125 		assertEquals(3, server.getRequests().size());
126 	}
127 
128 }