How to remove a remote using LibGit2Sharp? - libgit2sharp

I've checked libgit2sharp v0.15.0 and it seems like there is no method for removing a remote.
Is there any way to do it?

I've checked libgit2sharp v0.15.0 and it seems like there is no method for removing a remote.
That's completely correct. Such a method doesn't exist yet as it's not supported by libgit2 yet.
There's a work in progress (see PR #1199) to implement this. Subscribing to this PR would let you know about it's future progress.
Is there any way to do it?
You can do it by hand this way:
Remove the whole config section that describes the remote to be deleted
Delete from the config every branch (remote, merge) tuple that depends on the remote to be deleted
Delete from .git/refs/remotes/ every remote tracking branch reference
For instance, if you're willing to drop the remote "useless"
Drop this whole section from the config
[remote "useless"]
url = https://github.com/useless/project.git
fetch = +refs/heads/*:refs/remotes/useless/*
Remove remote and merge entries from the two following branches
[branch "vNext"]
remote = useless
merge = refs/heads/vNext
[branch "topic/awesome_feature"]
remote = useless
merge = refs/heads/topic/awesome_feature
Delete the following references
- .git/refs/remotes/useless/vNext
- .git/refs/remotes/useless/topic/awesome_feature
Update
Pull Request #731 just made possible the removal of remotes through the API.
Syntax: repo.Network.Remotes.Remove(remoteName)

Related

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

How to calculate ahead or behind branchs

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)

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 })

Git merge from a specific folder only

I've created a rails website for client X. I now have a client, Y, who wants a website that does the exact same thing as client X, but with a different skin.
I made a git branch from clientXcode and called it clientYcode. I then did all the changes to the views to make it look different, and lala, the same website with a different skin.
Now here's what I don't understand about git: I've made many changes to clientXcode in the views, models, and controllers; and now I want to merge those changes into clientYcode, excluding any view changes. Since views, models, and controllers each have their own folder in rails I was hoping to be able to do something like:
git merge client_x_code 'app/controllers/*', 'app/models/*'
QUESTION 1: Is something like that possible with git? If so, how would I do it?
QUESTION 2: Was branching the best solution to make a copy of my project?
Well I found the easiest solution to my problem...
git checkout clientYcode
git checkout clientXcode "app/controllers/"
git checkout clientXcode "app/models/"
And that does what I want!
The simplest course of action is to merge everything, except the content of the directory you want to keep.
You can do that by adding a merge driver in that directory, as the How do I tell git to always select my local version for conflicted merges on a specific file? question details.
With that merge driver in place, Branching is a good solution in this case.
Extract:
We have a .gitattributes file defined in the dirWithCopyMerge directory (defined only in the branch where the merge will occurs: myBranch), and we have a .git\config file which now contains a merge driver.
[merge "keepMine"]
name = always keep mine during merge
driver = keepMine.sh %O %A %B

Resources