View Javadoc
1   /*
2    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> 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. */
19  class TopoSortGenerator extends Generator {
20  	private static final int TOPO_DELAY = RevWalk.TOPO_DELAY;
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  	TopoSortGenerator(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  			for (RevCommit p : c.parents) {
51  				p.inDegree++;
52  				if (firstParent) {
53  					break;
54  				}
55  			}
56  			pending.add(c);
57  		}
58  	}
59  
60  	@Override
61  	int outputType() {
62  		return outputType;
63  	}
64  
65  	@Override
66  	void shareFreeList(BlockRevQueue q) {
67  		q.shareFreeList(pending);
68  	}
69  
70  	@Override
71  	RevCommit next() throws MissingObjectException,
72  			IncorrectObjectTypeException, IOException {
73  		for (;;) {
74  			final RevCommit c = pending.next();
75  			if (c == null)
76  				return null;
77  
78  			if (c.inDegree > 0) {
79  				// At least one of our children is missing. We delay
80  				// production until all of our children are output.
81  				//
82  				c.flags |= TOPO_DELAY;
83  				continue;
84  			}
85  
86  			// All of our children have already produced,
87  			// so it is OK for us to produce now as well.
88  			//
89  			for (RevCommit p : c.parents) {
90  				if (--p.inDegree == 0 && (p.flags & TOPO_DELAY) != 0) {
91  					// This parent tried to come before us, but we are
92  					// his last child. unpop the parent so it goes right
93  					// behind this child.
94  					//
95  					p.flags &= ~TOPO_DELAY;
96  					pending.unpop(p);
97  				}
98  				if (firstParent) {
99  					break;
100 				}
101 			}
102 			return c;
103 		}
104 	}
105 }