View Javadoc
1   /*
2    * Copyright (C) 2012, Christian Halstrick <christian.halstrick@sap.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  
44  package org.eclipse.jgit.internal.storage.file;
45  
46  import static org.junit.Assert.assertTrue;
47  
48  import java.util.Collections;
49  
50  import org.eclipse.jgit.junit.TestRepository.CommitBuilder;
51  import org.eclipse.jgit.lib.ObjectId;
52  import org.eclipse.jgit.lib.RefUpdate;
53  import org.eclipse.jgit.merge.MergeStrategy;
54  import org.eclipse.jgit.merge.Merger;
55  import org.eclipse.jgit.revwalk.RevCommit;
56  import org.eclipse.jgit.revwalk.RevTree;
57  import org.junit.Test;
58  
59  public class GcBranchPrunedTest extends GcTestCase {
60  
61  	@Test
62  	public void branch_historyNotPruned() throws Exception {
63  		RevCommit tip = commitChain(10);
64  		tr.branch("b").update(tip);
65  		gc.setExpireAgeMillis(0);
66  		fsTick();
67  		gc.prune(Collections.<ObjectId> emptySet());
68  		do {
69  			assertTrue(repo.hasObject(tip));
70  			tr.parseBody(tip);
71  			RevTree t = tip.getTree();
72  			assertTrue(repo.hasObject(t));
73  			assertTrue(repo.hasObject(tr.get(t, "a")));
74  			tip = tip.getParentCount() > 0 ? tip.getParent(0) : null;
75  		} while (tip != null);
76  	}
77  
78  	@Test
79  	public void deleteBranch_historyPruned() throws Exception {
80  		RevCommit tip = commitChain(10);
81  		tr.branch("b").update(tip);
82  		RefUpdate update = repo.updateRef("refs/heads/b");
83  		update.setForceUpdate(true);
84  		update.delete();
85  		gc.setExpireAgeMillis(0);
86  		fsTick();
87  		gc.prune(Collections.<ObjectId> emptySet());
88  		assertTrue(gc.getStatistics().numberOfLooseObjects == 0);
89  	}
90  
91  	@Test
92  	public void deleteMergedBranch_historyNotPruned() throws Exception {
93  		RevCommit parent = tr.commit().create();
94  		RevCommit b1Tip = tr.branch("b1").commit().parent(parent).add("x", "x")
95  				.create();
96  		RevCommit b2Tip = tr.branch("b2").commit().parent(parent).add("y", "y")
97  				.create();
98  
99  		// merge b1Tip and b2Tip and update refs/heads/b1 to the merge commit
100 		Merger merger = MergeStrategy.SIMPLE_TWO_WAY_IN_CORE.newMerger(repo);
101 		merger.merge(b1Tip, b2Tip);
102 		CommitBuilder cb = tr.commit();
103 		cb.parent(b1Tip).parent(b2Tip);
104 		cb.setTopLevelTree(merger.getResultTreeId());
105 		RevCommit mergeCommit = cb.create();
106 		RefUpdate u = repo.updateRef("refs/heads/b1");
107 		u.setNewObjectId(mergeCommit);
108 		u.update();
109 
110 		RefUpdate update = repo.updateRef("refs/heads/b2");
111 		update.setForceUpdate(true);
112 		update.delete();
113 
114 		gc.setExpireAgeMillis(0);
115 		fsTick();
116 		gc.prune(Collections.<ObjectId> emptySet());
117 		assertTrue(repo.hasObject(b2Tip));
118 	}
119 }