independents method

Future<List<GitCommit>> independents(
  1. List<GitCommit> commits
)

Independents returns a subset of the passed commits, that are not reachable the others It mimics the behavior of git merge-base --independent commit....

Implementation

Future<List<GitCommit>> independents(List<GitCommit> commits) async {
  commits.sort(_commitDateDec);
  _removeDuplicates(commits);

  if (commits.length < 2) {
    return commits;
  }

  var seen = <GitHash>{};
  var isLimit = (GitCommit commit) => seen.contains(commit.hash);

  var pos = 0;
  while (true) {
    var from = commits[pos];

    var others = List<GitCommit>.from(commits);
    others.remove(from);

    var fromHistoryIter = commitIteratorBFSFiltered(
      objStorage: objStorage,
      from: from,
      isLimit: isLimit,
    );

    await for (var fromAncestor in fromHistoryIter) {
      others.removeWhere((other) {
        if (fromAncestor.hash == other.hash) {
          commits.remove(other);
          return true;
        }
        return false;
      });

      if (commits.length == 1) {
        throw Exception('Stop?');
      }

      seen.add(fromAncestor.hash);
    }

    pos = commits.indexOf(from) + 1;
    if (pos >= commits.length) {
      break;
    }
  }

  return commits;
}