View Javadoc
1   /*
2    * Copyright (C) 2017, Matthias Sohn <matthias.sohn@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.internal.storage.file;
11  
12  import static org.eclipse.jgit.lib.Constants.INFO_ALTERNATES;
13  import static org.junit.Assert.assertTrue;
14  
15  import java.io.File;
16  import java.io.IOException;
17  import java.nio.file.Path;
18  
19  import org.eclipse.jgit.api.Git;
20  import org.eclipse.jgit.api.errors.AbortedByHookException;
21  import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
22  import org.eclipse.jgit.api.errors.GitAPIException;
23  import org.eclipse.jgit.api.errors.NoFilepatternException;
24  import org.eclipse.jgit.api.errors.NoHeadException;
25  import org.eclipse.jgit.api.errors.NoMessageException;
26  import org.eclipse.jgit.api.errors.UnmergedPathsException;
27  import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
28  import org.eclipse.jgit.junit.JGitTestUtil;
29  import org.eclipse.jgit.lib.ObjectId;
30  import org.eclipse.jgit.revwalk.RevCommit;
31  import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
32  import org.junit.Test;
33  
34  public class AlternatesTest extends SampleDataRepositoryTestCase {
35  
36  	private FileRepository db2;
37  
38  	@Override
39  	public void setUp() throws Exception {
40  		super.setUp();
41  		db2 = createWorkRepository();
42  	}
43  
44  	private void setAlternate(FileRepository from, FileRepository to)
45  			throws IOException {
46  		File alt = new File(from.getObjectDatabase().getDirectory(),
47  				INFO_ALTERNATES);
48  		alt.getParentFile().mkdirs();
49  		File fromDir = from.getObjectDatabase().getDirectory();
50  		File toDir = to.getObjectDatabase().getDirectory();
51  		Path relative = fromDir.toPath().relativize(toDir.toPath());
52  		write(alt, relative.toString() + "\n");
53  	}
54  
55  	@Test
56  	public void testAlternate() throws Exception {
57  		setAlternate(db2, db);
58  		RevCommit c = createCommit();
59  		assertCommit(c);
60  		assertAlternateObjects(db2);
61  	}
62  
63  	@Test
64  	public void testAlternateCyclic2() throws Exception {
65  		setAlternate(db2, db);
66  		setAlternate(db, db2);
67  		RevCommit c = createCommit();
68  		assertCommit(c);
69  		assertAlternateObjects(db2);
70  	}
71  
72  	@Test
73  	public void testAlternateCyclic3() throws Exception {
74  		FileRepository db3 = createBareRepository();
75  		setAlternate(db2, db3);
76  		setAlternate(db3, db);
77  		setAlternate(db, db2);
78  		RevCommit c = createCommit();
79  		assertCommit(c);
80  		assertAlternateObjects(db2);
81  	}
82  
83  	private RevCommit createCommit() throws IOException, GitAPIException,
84  			NoFilepatternException, NoHeadException, NoMessageException,
85  			UnmergedPathsException, ConcurrentRefUpdateException,
86  			WrongRepositoryStateException, AbortedByHookException {
87  		JGitTestUtil.writeTrashFile(db, "test", "test");
88  		Git git = Git.wrap(db2);
89  		git.add().addFilepattern("test").call();
90  		RevCommit c = git.commit().setMessage("adding test").call();
91  		return c;
92  	}
93  
94  	private void assertCommit(RevCommit c) {
95  		ObjectDirectory od = db2.getObjectDatabase();
96  		assertTrue("can't find expected commit" + c.name(),
97  				od.has(c.toObjectId()));
98  	}
99  
100 	private void assertAlternateObjects(FileRepository repo) {
101 		// check some objects in alternate
102 		final ObjectId alternateObjects[] = new ObjectId[] {
103 				ObjectId.fromString("49322bb17d3acc9146f98c97d078513228bbf3c0"),
104 				ObjectId.fromString("d0114ab8ac326bab30e3a657a0397578c5a1af88"),
105 				ObjectId.fromString("f73b95671f326616d66b2afb3bdfcdbbce110b44"),
106 				ObjectId.fromString("6020a3b8d5d636e549ccbd0c53e2764684bb3125"),
107 				ObjectId.fromString("0a3d7772488b6b106fb62813c4d6d627918d9181"),
108 				ObjectId.fromString("da0f8ed91a8f2f0f067b3bdf26265d5ca48cf82c"),
109 				ObjectId.fromString(
110 						"cd4bcfc27da62c6b840de700be1c60a7e69952a5") };
111 		ObjectDirectory od = repo.getObjectDatabase();
112 		for (ObjectId o : alternateObjects) {
113 			assertTrue(String.format("can't find object %s in alternate",
114 					o.getName()), od.has(o));
115 		}
116 	}
117 }