How can I sort a set of git commit IDs in topological order?

git

Solution

Here's one way of speeding it up:

git rev-list --topo-order $(cat SET_OF_SHA1S) \
   | grep --file SET_OF_SHA1S --max-count $(wc -l SET_OF_SHA1S)

Optimisations:

- Only ask `rev-list` to list all commits reachable from your set of SHA1s.

- As soon as `rev-list` prints enough commits that include the set of SHA1s you're interested in, tell `grep` to stop grepping using the `--max-count` parameter. `grep` will in turn close its input, and `rev-list` will stop needlessly printing out further SHA1s.

Problem

I have a set of commit SHA1s, in no particular order. I would like to pipe this set to a command, and have those commits returned in topological order. Here's one way of doing this: ``` git rev-list --all --topo-order | grep --file SET_OF_SHA1S ``` As you can imagine, this is a very slow way of doing it, as `git rev-list` is having to print out all of the commit SHA1s, not just the ones in my set. Is there a better and faster way to do this? Use case: My test framework tests certain Git commits and stores the result in a database. I'm writing a web page that summarises these results, and it would be nice to display the results in order. Sorting by commit date is not ideal as some rebased commits will have exactly the same commit date.

Original source