How do I build a version tree for a Git repository using LibGit2(Sharp)

江枫思渺然 提交于 2020-01-04 02:38:06

问题


Some background:

I'm looking at the possibility of using Git as a data storage layer. Basically I need to keep all versions of some XML files which describe application state. The users needs a "Time Machine" to be able to revert to previous states, as well as branch off previous states etc. This will be hidden behind a service layer, but I am looking at using Git at the back end.

I need to present the evolution of the application's state over time, so I want to build a network diagram showing the changes, the branches etc.

To do this I need to build a version tree visually. Basically, I want to do this:

git log --oneline --graph --decorate --all

I'm using LibGit2Sharp. I've poked around the API, but I'm not seeing anything immediately helpful. I'm also pretty knew to Git, which isn't helping.


回答1:


Using LibGit2Sharp, you can simulate the git log command just by enumerating the Commits. You can also filter the list of commits, or specify some sorting options (like having the list sorted in topological order). More information can be found on the LibGit2Sharp wiki page related to git log.

Using a graph library like QuickGraph (but there are others, as can be found on this other SO question), you can just build the graph like this:

var graph = new AdjacencyGraph<Commit, Edge<Commit>>();

using (var repo = new Repository(path_to_your_repo))
{
    foreach (var c in repo.Commits.Take(20))
    {
        graph.AddVerticesAndEdgeRange(c.Parents.Select(p => new Edge<Commit>(c, p)));
    }
}

You can then output this graph to several visualization formats (graphML, Glee, graphviz). To output it in the graphviz (.dot) format:

var graphviz = new GraphvizAlgorithm<Commit,Edge<Commit>>(graph);
graphviz.FormatVertex +=
    (o, e) =>
        {
            e.VertexFormatter.Label = string.Format("{0} {1}", e.Vertex.Id.ToString(7),
                                                    e.Vertex.MessageShort.Replace("\"", "\\\""));
        };

graphviz.Generate(new FileDotEngine(), @"d:\graph");



回答2:


libgit2 is about the underlying Git concepts. If you want to show a graph, you need to look at the commits and how they relate to each other. Then you can draw the graph with whatever graphics library you prefer.

If what you want is the output of that command, then running that command is the simplest way.



来源:https://stackoverflow.com/questions/15203844/how-do-i-build-a-version-tree-for-a-git-repository-using-libgit2sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!