View Javadoc
1   /*
2    * Copyright (C) 2020, Google LLC. 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.revwalk;
12  
13  import java.io.IOException;
14  
15  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
16  import org.eclipse.jgit.errors.MissingObjectException;
17  
18  /** Sorts commits in topological order without intermixing lines of history. */
19  class TopoNonIntermixSortGenerator extends Generator {
20  	private static final int TOPO_QUEUED = RevWalk.TOPO_QUEUED;
21  
22  	private final FIFORevQueue pending;
23  
24  	private final int outputType;
25  
26  	/**
27  	 * Create a new sorter and completely spin the generator.
28  	 * <p>
29  	 * When the constructor completes the supplied generator will have no
30  	 * commits remaining, as all of the commits will be held inside of this
31  	 * generator's internal buffer.
32  	 *
33  	 * @param s
34  	 *            generator to pull all commits out of, and into this buffer.
35  	 * @throws MissingObjectException
36  	 * @throws IncorrectObjectTypeException
37  	 * @throws IOException
38  	 */
39  	TopoNonIntermixSortGenerator(Generator s) throws MissingObjectException,
40  			IncorrectObjectTypeException, IOException {
41  		super(s.firstParent);
42  		pending = new FIFORevQueue(firstParent);
43  		outputType = s.outputType() | SORT_TOPO;
44  		s.shareFreeList(pending);
45  		for (;;) {
46  			final RevCommit c = s.next();
47  			if (c == null) {
48  				break;
49  			}
50  			if ((c.flags & TOPO_QUEUED) == 0) {
51  				for (RevCommit p : c.parents) {
52  					p.inDegree++;
53  
54  					if (firstParent) {
55  						break;
56  					}
57  				}
58  			}
59  			c.flags |= TOPO_QUEUED;
60  			pending.add(c);
61  		}
62  	}
63  
64  	@Override
65  	int outputType() {
66  		return outputType;
67  	}
68  
69  	@Override
70  	void shareFreeList(BlockRevQueue q) {
71  		q.shareFreeList(pending);
72  	}
73  
74  	@Override
75  	RevCommit next() throws MissingObjectException,
76  			IncorrectObjectTypeException, IOException {
77  		for (;;) {
78  			final RevCommit c = pending.next();
79  			if (c == null) {
80  				return null;
81  			}
82  
83  			if (c.inDegree > 0) {
84  				// At least one of our children is missing. We delay
85  				// production until all of our children are output.
86  				//
87  				continue;
88  			}
89  
90  			if ((c.flags & TOPO_QUEUED) == 0) {
91  				// c is a parent that already produced or a parent that
92  				// was never in the priority queue and should never produce.
93  				//
94  				continue;
95  			}
96  
97  			for (RevCommit p : c.parents) {
98  				if (--p.inDegree == 0 && (p.flags & TOPO_QUEUED) != 0) {
99  					// The parent has no unproduced interesting children. unpop
100 					// the parent so it goes right behind this child. This means
101 					// that this parent commit may appear in "pending" more than
102 					// once, but this is safe since upon the second and
103 					// subsequent iterations with this commit, it will no longer
104 					// have TOPO_QUEUED set, and thus will be skipped.
105 					//
106 					pending.unpop(p);
107 				}
108 				if (firstParent) {
109 					break;
110 				}
111 			}
112 
113 			c.flags &= ~TOPO_QUEUED;
114 			return c;
115 		}
116 	}
117 }