Skip to content

Fast Computation of Lowest Common Ancestor in Version Control Commit Graph

Stepan Polokhin, Ruslan Savchenko, Yuriy Chernyshov

cs.DSarXiv:2611.25912

Abstract

The problem of finding the lowest common ancestor in the commit graph arises when a user wants to merge two (or more) development branches: for example, to add their own development into a common branch. At that moment, the version control system must determine the merge base - the common ancestor of the latest commits of the branches, in order to show the mutual difference between the branches. The problem is complicated by the fact that the commit graph is not a tree but an arbitrary acyclic graph. Moreover, if there is no common ancestor, searching for one may require traversing the entire graph. Traversing a graph with several million vertices takes tens of seconds, which becomes noticeable to users. We show that the commit graph has additional structure - supporting branches - and that this can be used to speed up the algorithm. By extracting supporting branches, the graph can be preprocessed and the search for the lowest common ancestor can be significantly accelerated. Moreover, if removing the supporting branches decomposes the graph into connected components of bounded size, the lowest common ancestor query can be answered in time comparable to searching for a common ancestor within a single component only. We present an algorithm running in time O(δGmc + ns2 ns + ms ns) and requiring O(d) preprocessed data per vertex, where δG is the maximum out-degree of a vertex in G, nc and mc are the maximum numbers of vertices and edges in a connected component formed after removing the supporting branches, ns is the number of supporting branches, ms is the number of edges in the reachability graph between the supporting branches, and d is the maximum number of supporting branches adjacent to any single component.

Create a lesson