View Javadoc
1   /*
2    * Copyright (C) 2019, Google LLC.
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.revwalk;
44  
45  import static org.junit.Assert.assertEquals;
46  import static org.junit.Assert.assertFalse;
47  import static org.junit.Assert.assertTrue;
48  
49  import org.eclipse.jgit.internal.storage.file.FileRepository;
50  import org.eclipse.jgit.internal.storage.file.GC;
51  import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
52  import org.eclipse.jgit.junit.TestRepository;
53  import org.eclipse.jgit.lib.BitmapIndex.BitmapBuilder;
54  import org.eclipse.jgit.lib.NullProgressMonitor;
55  import org.eclipse.jgit.lib.ProgressMonitor;
56  import org.junit.Before;
57  import org.junit.Test;
58  
59  public class BitmapCalculatorTest extends LocalDiskRepositoryTestCase {
60  	TestRepository<FileRepository> repo;
61  
62  	/** {@inheritDoc} */
63  	@Override
64  	@Before
65  	public void setUp() throws Exception {
66  		super.setUp();
67  		FileRepository db = createWorkRepository();
68  		repo = new TestRepository<>(db);
69  	}
70  
71  	@Test
72  	public void addOnlyCommits() throws Exception {
73  		RevBlob abBlob = repo.blob("a_b_content");
74  		RevCommit root = repo.commit().add("a/b", abBlob).create();
75  		repo.update("refs/heads/master", root);
76  
77  		// GC creates bitmap index with ALL objects
78  		GC gc = new GC(repo.getRepository());
79  		gc.setAuto(false);
80  		gc.gc();
81  
82  		// These objects are not in the bitmap index.
83  		RevBlob acBlob = repo.blob("a_c_content");
84  		RevCommit head = repo.commit().parent(root).add("a/c", acBlob).create();
85  		repo.update("refs/heads/master", head);
86  
87  		BitmapCalculator bitmapWalker = new BitmapCalculator(repo.getRevWalk());
88  		BitmapBuilder bitmap = bitmapWalker
89  				.getBitmap(head, NullProgressMonitor.INSTANCE);
90  
91  		assertTrue(bitmap.contains(root.getId()));
92  		assertTrue(bitmap.contains(root.getTree().getId()));
93  		assertTrue(bitmap.contains(abBlob.getId()));
94  
95  		// BitmapCalculator added only the commit, no other objects.
96  		assertTrue(bitmap.contains(head.getId()));
97  		assertFalse(bitmap.contains(head.getTree().getId()));
98  		assertFalse(bitmap.contains(acBlob.getId()));
99  	}
100 
101 	@Test
102 	public void walkUntilBitmap() throws Exception {
103 		RevCommit root = repo.commit().create();
104 		repo.update("refs/heads/master", root);
105 
106 		// GC creates bitmap index with ALL objects
107 		GC gc = new GC(repo.getRepository());
108 		gc.setAuto(false);
109 		gc.gc();
110 
111 		// These objects are not in the bitmap index.
112 		RevCommit commit1 = repo.commit(root);
113 		RevCommit commit2 = repo.commit(commit1);
114 		repo.update("refs/heads/master", commit2);
115 
116 		CounterProgressMonitor monitor = new CounterProgressMonitor();
117 		BitmapCalculator bitmapWalker = new BitmapCalculator(repo.getRevWalk());
118 		BitmapBuilder bitmap = bitmapWalker.getBitmap(commit2, monitor);
119 
120 		assertTrue(bitmap.contains(root));
121 		assertTrue(bitmap.contains(commit1));
122 		assertTrue(bitmap.contains(commit2));
123 		assertEquals(2, monitor.getUpdates());
124 	}
125 
126 	@Test
127 	public void noNeedToWalk() throws Exception {
128 		RevCommit root = repo.commit().create();
129 		RevCommit commit1 = repo.commit(root);
130 		RevCommit commit2 = repo.commit(commit1);
131 		repo.update("refs/heads/master", commit2);
132 
133 		// GC creates bitmap index with ALL objects
134 		GC gc = new GC(repo.getRepository());
135 		gc.setAuto(false);
136 		gc.gc();
137 
138 		CounterProgressMonitor monitor = new CounterProgressMonitor();
139 		BitmapCalculator bitmapWalker = new BitmapCalculator(repo.getRevWalk());
140 		BitmapBuilder bitmap = bitmapWalker.getBitmap(commit2, monitor);
141 
142 		assertTrue(bitmap.contains(root));
143 		assertTrue(bitmap.contains(commit1));
144 		assertTrue(bitmap.contains(commit2));
145 		assertEquals(0, monitor.getUpdates());
146 	}
147 
148 	private static class CounterProgressMonitor implements ProgressMonitor {
149 
150 		private int counter;
151 
152 		@Override
153 		public void start(int totalTasks) {
154 			// Nothing to do in tests
155 		}
156 
157 		@Override
158 		public void beginTask(String title, int totalWork) {
159 			// Nothing to to in tests
160 		}
161 
162 		@Override
163 		public void update(int completed) {
164 			counter += 1;
165 		}
166 
167 		@Override
168 		public void endTask() {
169 			// Nothing to do in tests
170 		}
171 
172 		@Override
173 		public boolean isCancelled() {
174 			return false;
175 		}
176 
177 		int getUpdates() {
178 			return counter;
179 		}
180 	}
181 }