View Javadoc
1   /*
2    * Copyright (C) 2016 Google Inc. 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  
11  package org.eclipse.jgit.internal.storage.reftree;
12  
13  import static org.eclipse.jgit.lib.Constants.HEAD;
14  import static org.eclipse.jgit.lib.Constants.MASTER;
15  import static org.eclipse.jgit.lib.Constants.ORIG_HEAD;
16  import static org.eclipse.jgit.lib.Constants.R_HEADS;
17  import static org.eclipse.jgit.lib.RefDatabase.ALL;
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertFalse;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.List;
24  
25  import org.eclipse.jgit.internal.storage.file.FileRepository;
26  import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
27  import org.eclipse.jgit.junit.TestRepository;
28  import org.eclipse.jgit.lib.ObjectId;
29  import org.eclipse.jgit.lib.Ref;
30  import org.eclipse.jgit.lib.RefDatabase;
31  import org.eclipse.jgit.lib.RefUpdate;
32  import org.eclipse.jgit.revwalk.RevCommit;
33  import org.eclipse.jgit.storage.file.FileBasedConfig;
34  import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
35  import org.junit.Before;
36  import org.junit.Test;
37  
38  public class LocalDiskRefTreeDatabaseTest extends LocalDiskRepositoryTestCase {
39  	private FileRepository repo;
40  	private RefTreeDatabase refdb;
41  	private RefDatabase bootstrap;
42  
43  	private TestRepository<FileRepository> testRepo;
44  	private RevCommit A;
45  	private RevCommit B;
46  
47  	@Override
48  	@Before
49  	public void setUp() throws Exception {
50  		super.setUp();
51  		FileRepository init = createWorkRepository();
52  		FileBasedConfig cfg = init.getConfig();
53  		cfg.setInt("core", null, "repositoryformatversion", 1);
54  		cfg.setString("extensions", null, "refStorage", "reftree");
55  		cfg.save();
56  
57  		repo = (FileRepository) new FileRepositoryBuilder()
58  				.setGitDir(init.getDirectory())
59  				.build();
60  		refdb = (RefTreeDatabase) repo.getRefDatabase();
61  		bootstrap = refdb.getBootstrap();
62  		addRepoToClose(repo);
63  
64  		RefUpdate head = refdb.newUpdate(HEAD, true);
65  		head.link(R_HEADS + MASTER);
66  
67  		testRepo = new TestRepository<>(init);
68  		A = testRepo.commit().create();
69  		B = testRepo.commit(testRepo.getRevWalk().parseCommit(A));
70  	}
71  
72  	@Test
73  	public void testHeadOrigHead() throws IOException {
74  		RefUpdate master = refdb.newUpdate(HEAD, false);
75  		master.setExpectedOldObjectId(ObjectId.zeroId());
76  		master.setNewObjectId(A);
77  		assertEquals(RefUpdate.Result.NEW, master.update());
78  		assertEquals(A, refdb.exactRef(HEAD).getObjectId());
79  
80  		RefUpdate orig = refdb.newUpdate(ORIG_HEAD, true);
81  		orig.setNewObjectId(B);
82  		assertEquals(RefUpdate.Result.NEW, orig.update());
83  
84  		File origFile = new File(repo.getDirectory(), ORIG_HEAD);
85  		assertEquals(B.name() + '\n', read(origFile));
86  		assertEquals(B, bootstrap.exactRef(ORIG_HEAD).getObjectId());
87  		assertEquals(B, refdb.exactRef(ORIG_HEAD).getObjectId());
88  		assertFalse(refdb.getRefs(ALL).containsKey(ORIG_HEAD));
89  
90  		List<Ref> addl = refdb.getAdditionalRefs();
91  		assertEquals(2, addl.size());
92  		assertEquals(ORIG_HEAD, addl.get(1).getName());
93  		assertEquals(B, addl.get(1).getObjectId());
94  	}
95  }