1 /* 2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> 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.revwalk; 45 46 import java.io.IOException; 47 48 import org.eclipse.jgit.errors.IncorrectObjectTypeException; 49 import org.eclipse.jgit.errors.MissingObjectException; 50 51 /** 52 * Replaces a RevCommit's parents until not colored with REWRITE. 53 * <p> 54 * Before a RevCommit is returned to the caller its parents are updated to 55 * create a dense DAG. Instead of reporting the actual parents as recorded when 56 * the commit was created the returned commit will reflect the next closest 57 * commit that matched the revision walker's filters. 58 * <p> 59 * This generator is the second phase of a path limited revision walk and 60 * assumes it is receiving RevCommits from {@link TreeRevFilter}, 61 * after they have been fully buffered by {@link AbstractRevQueue}. The full 62 * buffering is necessary to allow the simple loop used within our own 63 * {@link #rewrite(RevCommit)} to pull completely through a strand of 64 * {@link RevWalk#REWRITE} colored commits and come up with a simplification 65 * that makes the DAG dense. Not fully buffering the commits first would cause 66 * this loop to abort early, due to commits not being parsed and colored 67 * correctly. 68 * 69 * @see TreeRevFilter 70 */ 71 class RewriteGenerator extends Generator { 72 private static final int REWRITE = RevWalk.REWRITE; 73 74 /** For {@link #cleanup(RevCommit[])} to remove duplicate parents. */ 75 private static final int DUPLICATE = RevWalk.TEMP_MARK; 76 77 private final Generator source; 78 79 RewriteGenerator(Generator s) { 80 source = s; 81 } 82 83 @Override 84 void shareFreeList(BlockRevQueue q) { 85 source.shareFreeList(q); 86 } 87 88 @Override 89 int outputType() { 90 return source.outputType() & ~NEEDS_REWRITE; 91 } 92 93 @Override 94 RevCommit next() throws MissingObjectException, 95 IncorrectObjectTypeException, IOException { 96 final RevCommit c = source.next(); 97 if (c == null) { 98 return null; 99 } 100 boolean rewrote = false; 101 final RevCommit[] pList = c.parents; 102 final int nParents = pList.length; 103 for (int i = 0; i < nParents; i++) { 104 final RevCommit oldp = pList[i]; 105 final RevCommit newp = rewrite(oldp); 106 if (oldp != newp) { 107 pList[i] = newp; 108 rewrote = true; 109 } 110 } 111 if (rewrote) { 112 c.parents = cleanup(pList); 113 } 114 return c; 115 } 116 117 private RevCommit./../../../org/eclipse/jgit/revwalk/RevCommit.html#RevCommit">RevCommit rewrite(RevCommit p) { 118 for (;;) { 119 final RevCommit[] pList = p.parents; 120 if (pList.length > 1) { 121 // This parent is a merge, so keep it. 122 // 123 return p; 124 } 125 126 if ((p.flags & RevWalk.UNINTERESTING) != 0) { 127 // Retain uninteresting parents. They show where the 128 // DAG was cut off because it wasn't interesting. 129 // 130 return p; 131 } 132 133 if ((p.flags & REWRITE) == 0) { 134 // This parent was not eligible for rewriting. We 135 // need to keep it in the DAG. 136 // 137 return p; 138 } 139 140 if (pList.length == 0) { 141 // We can't go back any further, other than to 142 // just delete the parent entirely. 143 // 144 return null; 145 } 146 147 p = pList[0]; 148 } 149 } 150 151 private RevCommit../../../org/eclipse/jgit/revwalk/RevCommit.html#RevCommit">RevCommit[] cleanup(RevCommit[] oldList) { 152 // Remove any duplicate parents caused due to rewrites (e.g. a merge 153 // with two sides that both simplified back into the merge base). 154 // We also may have deleted a parent by marking it null. 155 // 156 int newCnt = 0; 157 for (int o = 0; o < oldList.length; o++) { 158 final RevCommit p = oldList[o]; 159 if (p == null) 160 continue; 161 if ((p.flags & DUPLICATE) != 0) { 162 oldList[o] = null; 163 continue; 164 } 165 p.flags |= DUPLICATE; 166 newCnt++; 167 } 168 169 if (newCnt == oldList.length) { 170 for (RevCommit p : oldList) 171 p.flags &= ~DUPLICATE; 172 return oldList; 173 } 174 175 final RevCommit.html#RevCommit">RevCommit[] newList = new RevCommit[newCnt]; 176 newCnt = 0; 177 for (RevCommit p : oldList) { 178 if (p != null) { 179 newList[newCnt++] = p; 180 p.flags &= ~DUPLICATE; 181 } 182 } 183 184 return newList; 185 } 186 }