I know that the history in Git is stored in a data structure called a DAG. I've heard about DFS and know it's somewhat related.
I'm curious, how do programs such as git log --graph
or hg graphlog
draw the history? I always thought it's quite complicated to draw the lanes and everything in such a nice way.
Could someone write some pseudo code that demonstrates it?
note: I tried looking around Git or hg's code but it's very hard to follow and get a general idea of what's going on.
The git log graph command creates a graphic overview of how a developer's various development pipelines have branched and merged over time.
The colors are merely meant to help you view the lines as distinct from other lines. To answer question #1, they are assigned not pseudo-randomly, but rather sequentially, each time git log --graph picks a new "column number".
Git stores history as a graph of snapshots of the entire repository. These snapshots, called commits in Git, can have multiple parents, creating a history that looks like a graph instead of a straight line.
First, one obtains a list of commits (as with git rev-list
), and parents of each commit. A "column reservation list" is kept in memory.
For each commit then:
Example showing output of git-forest
on aufs2-util with an extra commit to have more than one branch).
With lookahead, one can anticipate how far down the merge point will be and squeeze the wood between two columns to give a more aesthetically pleasing result.
I tried looking around Git or hg's code but it's very hard to follow and get a general idea of what's going on.
For hg, did you try to follow the code in hg itself, or in graphlog?
Because the code of graphlog is pretty short. You can find it in hgext/graphlog.py, and really the important part is the top ~200 lines, the rest is the extension's bootstrapping and finding the revision graph selected. The code generation function is ascii
, with its last parameter being the result of a call to asciiedge
(the call itself is performed on the last line of generate
, the function being provided to generate
by graphlog
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With