How to calculate ahead or behind branchs - libgit2sharp

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)

Related

Bitbucket - how to set branch permissions for given source/dest pair?

In Bitbucket cloud it's straightforward to set a branch permission to control who and how changes can be made, for example, consider who can modify the 'main' branch. We need more granularity and would like to be able to set up a rule based on both the source and destination branch.
Imagine we had the following branches:
releaseX
releaseY
featureA
featureB
Is it possible to specify a branching pattern or something that would apply different rules from release* -> main than feature* -> main? Maybe the 'Select branch By pattern' input takes a special pattern like s:release*,d:main etc..? Is this possible?
We also have access to the BB API if that helps somehow.

Using API how to find the parent branch in Bitbucket

I want to find the parent branches for a particular branch. Suppose I have created A branch from master and branch B from A. Now I want to find the parents for B like B->A->Master. I checked the Bitbucket API but there is no such a method available. When I pull the data for a branch there is no field which shows the parent branch details.
You can use regular git commands for that:
git branch --merged HEAD --sort=authordate
This lists the ancestor branches of the current working directory in chronological order (you can of course specify any other ref instead of HEAD) and would be great to use in a script or some automated tooling.
A very quick and dirty alternative way would be to just look at the regular output of git log:
git log --graph --decorate | egrep 'commit .* \('
This variation would maybe be interesting for a human to watch but too noisy for a script.

How do I preserve an approval in Gerrit with new patchset

I'm setting up a Gerrit server for the team. I've used Gerrit in the past, but I've never set one up.
One feature I know I've used in the past is, once I get a CL approved, I can rebase the branch without losing the approval. I assume this is a setting somewhere, but I can't find it.
You need to set the "Submit Type" of the repository (in the repository configuration page) to one of these:
Rebase If Necessary
If the change being submitted is a strict superset of the destination branch, then the branch is fast-forwarded to the change. If not, then the change is automatically rebased and then the branch is fast-forwarded to the change.
When Gerrit tries to do a merge, by default the merge will only succeed if there is no path conflict. A path conflict occurs when the same file has also been changed on the other side of the merge.
Rebase Always
Basically, the same as Rebase If Necessary, but it creates a new patchset even if fast forward is possible AND like Cherry Pick it ensures footers such as Change-Id, Reviewed-On, and others are present in resulting commit that is merged.
Thus, Rebase Always can be considered similar to Cherry Pick, but with the important distinction that Rebase Always does not ignore dependencies.
See more details in the Gerrit documentation here.
I think you are looking for the Copy All Score On Trivial Rebase labels.
The example below is from our project.config. For the Verified label the scores will be copied if there is a Trivial Rebase or a No Code Change patchset added, the No Code Change basically means an updated commit message.
[label "Verified"]
function = MaxWithBlock
value = -1 Fails
value = 0 No score
value = +1 Verified
copyAllScoresIfNoCodeChange = true
copyAllScoresOnTrivialRebase = true
defaultValue = 0

Download file at specific commit with Bitbucket REST API

We are trying to find a way to download a single file from a Bitbucket project using the REST API at a specific commit. Currently, we have the ability to download a file at a specific branch:
https://stash.domain.com:8443/rest/api/1.0/projects/our_project/src/main/java/com/SomeFile.java?at=refs%2Fheads%2Fmaster
Note that the end of the URL, when decoded, contains the query parameter at=refs/heads/master, which refers to the master branch. This also works for specific tags:
https://stash.domain.com:8443/rest/api/1.0/projects/our_project/src/main/java/com/SomeFile.java?at=refs%2Ftags%2Ftesttag1
Here the query parameter at=refs/tags/testtag1 refers to the tag (commit) testtag1.
But because of the nature of our implementation, we would like to refer directly to a commit SHA-1 hash via the Bitbucket REST API. Is this possible?
Obviously, one ugly workaround would be to just add a tag to every commit. But this could bloat the repository and it also feels like an unnecessary hack.
With the help of this SO question, I found one of the answers which tipped me off to the correct syntax. Use this:
<URL>?at=commit_hash
For example:
https://stash.domain.com:8443/rest/api/1.0/projects/our_project/src/main/java/com/SomeFile.java?at=bed2dda5
Here is a table of three main endpoint types with the Bitbucket REST API:
query parameter | role
---------------------------------------------
refs/heads/master | specify master branch
refs/tags/someTag | specify 'someTag' tag
at=bed2dda5 | specify commit #bed2dda5

Needing a little help performing operations on remote repository

I've been tinkering around, and reading through what little Wiki information that I could find, and looked through tests that I thought may be relevant, however I'm having problems coming up with a working solution for a few things.
Mostly, I'm looking for a way to list commits that have been pushed to the server ahead of your working repository / local commits. I've tried using Fetch, and FetchHeads, but looking through all of the documentation for the Network class doesn't seem to yield anything that I can understand, I guess. Ideally, I'd be interested in seeing something like:
IQueriableCommitLog Repository.Network.GetCommitsAhead
OR
IQueriableCommitLog Repository.Remote.GetCommitsAhead
Then, just use the results like you would in:
IQueriableCommitLog Repository.Commits
Also, perhaps a method like Commit.UpdateTo() for the remote commit.
Am I too far off base for what I'm looking for? Is it possible at this time to perform actions such as this? Is it supported by libgit2?
Mostly, I'm looking for a way to list commits that have been pushed to the server ahead of your working repository / local commits.
A Branch is either local or remote. Local Branches which are tracking other ones expose a TrackedBranch property.
Considering a local branch with a not null TrackedBranch property
Commits that have been performed on this local branch and aren't known from the tracked one.
repo.Commits.QueryBy(new Filter { Since = branch.Tip, Until = branch.TrackedBranch });
Commits that exist in the tracked branch and aren't known from the local one
repo.Commits.QueryBy(new Filter { Since = TrackedBranch, Until = Tip })

Resources