Use libgit2sharp, how to calculate ahead or behind metrics. like this page https://github.com/libgit2/libgit2sharp/branches

有帮助吗?

解决方案

How to calculate ahead or behind metrics

Each Branch bears a TrackingDetails property. This property exposes AheadBy and BehindBy nullable values (null will be returned when the branch has no upstream configuration or if the upstream branch does not exist).

Those values will represent the number of commits the local branch is ahead/behind compared to the upstream branch (ie. the remote branch being tracked).

This outputs similar results than git status -sb

like this page https://github.com/libgit2/libgit2sharp/branches

This page actually compares each branch of the upstream (ie. the one hosted on GitHub) repository against the current tip of the remote HEAD. This feature (comparing two local branches) is not available in LibGit2Sharp.

Provided you're interested with it, please feel free to open a feature request.

Update

A pull request (see #564) introducing a new method repo.ObjectDatabase.CalculateHistoryDivergence(Commit, Commit) is cooking up.

This will allow the user to determine the ahead-by and behind-by counts, along with the merge-base that's been used to calculate those distances.

其他提示

For those searching (as of pygit2 v 0.27.4), the API is ahead_behind.

Sample code gist:


    import pygit2
    repo = pygit2.Repository('your-repo-path')

    upstream_head = repo.revparse_single('origin/HEAD')
    local_head    = repo.revparse_single('HEAD')

    diff = repo.ahead_behind(local_head.id, upstream_head.id)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top