How does 'git log --graph' or 'hg graphlog' work?

后端 未结 4 1820
一向
一向 2020-12-04 23:57

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

4条回答
  •  死守一世寂寞
    2020-12-05 00:31

    This particular problem isn't that hard, compared to graph display in general. Because you want to keep the nodes in the order they were committed the problem gets much simpler.

    Also note that the display model is grid based, rows are commits and columns are edges into the past/future.

    While I didn't read the git source you probably just walk the list of commits, starting from the newest, and maintain a list of open edges into the past. Following the edges naturally leads to splitting/merging columns and you end up with the kind of tree git/hg display.

    When merging edges you want to avoid crossing other edges, so you'll have to try to order your columns ahead of time. This is actally the only part that may not be straightforward. For example one could do a two-pass algorithm, making up a column order for the edges in the first pass and doing the drawing in the second pass.

提交回复
热议问题