You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TopoSortGenerator.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. package org.eclipse.jgit.revwalk;
  11. import java.io.IOException;
  12. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  13. import org.eclipse.jgit.errors.MissingObjectException;
  14. /** Sorts commits in topological order. */
  15. class TopoSortGenerator extends Generator {
  16. private static final int TOPO_DELAY = RevWalk.TOPO_DELAY;
  17. private final FIFORevQueue pending;
  18. private final int outputType;
  19. /**
  20. * Create a new sorter and completely spin the generator.
  21. * <p>
  22. * When the constructor completes the supplied generator will have no
  23. * commits remaining, as all of the commits will be held inside of this
  24. * generator's internal buffer.
  25. *
  26. * @param s
  27. * generator to pull all commits out of, and into this buffer.
  28. * @throws MissingObjectException
  29. * @throws IncorrectObjectTypeException
  30. * @throws IOException
  31. */
  32. TopoSortGenerator(Generator s) throws MissingObjectException,
  33. IncorrectObjectTypeException, IOException {
  34. super(s.firstParent);
  35. pending = new FIFORevQueue(firstParent);
  36. outputType = s.outputType() | SORT_TOPO;
  37. s.shareFreeList(pending);
  38. for (;;) {
  39. final RevCommit c = s.next();
  40. if (c == null) {
  41. break;
  42. }
  43. for (RevCommit p : c.parents) {
  44. p.inDegree++;
  45. if (firstParent) {
  46. break;
  47. }
  48. }
  49. pending.add(c);
  50. }
  51. }
  52. @Override
  53. int outputType() {
  54. return outputType;
  55. }
  56. @Override
  57. void shareFreeList(BlockRevQueue q) {
  58. q.shareFreeList(pending);
  59. }
  60. @Override
  61. RevCommit next() throws MissingObjectException,
  62. IncorrectObjectTypeException, IOException {
  63. for (;;) {
  64. final RevCommit c = pending.next();
  65. if (c == null)
  66. return null;
  67. if (c.inDegree > 0) {
  68. // At least one of our children is missing. We delay
  69. // production until all of our children are output.
  70. //
  71. c.flags |= TOPO_DELAY;
  72. continue;
  73. }
  74. // All of our children have already produced,
  75. // so it is OK for us to produce now as well.
  76. //
  77. for (RevCommit p : c.parents) {
  78. if (--p.inDegree == 0 && (p.flags & TOPO_DELAY) != 0) {
  79. // This parent tried to come before us, but we are
  80. // his last child. unpop the parent so it goes right
  81. // behind this child.
  82. //
  83. p.flags &= ~TOPO_DELAY;
  84. pending.unpop(p);
  85. }
  86. if (firstParent) {
  87. break;
  88. }
  89. }
  90. return c;
  91. }
  92. }
  93. }