How to allow unrelated histories on git pull with libgit2sharp? - libgit2sharp

What is libgit2sharp's equivalent to the following git command?
git pull origin master --allow-unrelated-histories
I have a scenario where I have to merge a branch from another remote with an unrelated history. When i want to push the merged result to this remote, I'll get a NonFastForwardException.
So far I implemented the following code:
using (var repo = new Repository(repoPath))
{
Remote remote = repo.Network.Remotes.Add("newRemote", targetRepositoryUrl);
repo.Branches.Update(repo.Branches["wikiMaster"], b => b.Remote = "newRemote");
Commands.Pull(repo, sig, pullOptions);
// Will throw "NonFastForwardException"
repo.Network.Push(repo.Branches["wikiMaster"], pushOptions);
}
pullOptions and pushOptions do only contain the credentials.

Related

How access message of git commits from Microsoft.TeamFoundation.WorkItemTracking.Client?

I'm using TFS 2017 update 1 on premises. I'm using #ID in log comments of commits in order to associate workitem ID (of User Story, Task etc.) with GIT commits of source code. It properly works (I can see links to commit from workitem interface).
I'd like to use TFS SDK API with tfs aggregator in order to better manage GIT commits (e.g. automatic transition to custom state of workitem when a specific custom git commit message is done by programmers).
How can access message/log of git commits from Microsoft.TeamFoundation.WorkItemTracking.Client in order be able to parser custom message in addition to those described here (e.g. "Fixes #123" or "Close #123")?
You can't get the commit comment only with WorkItemHttpClient, you can get it along with GitHttpClient. first of all get the work item links with WorkItemHttpClient, than get the commit id and get the comment with GitHttpClient.
A working example:
VssClientCredentials cred = new VssClientCredentials();
VssConnection tfs = new VssConnection(new Uri("http://tfs-server:8080/tfs/collection"), cred);
var workItemClient = tfs.GetClient<WorkItemTrackingHttpClient>();
var gitClient = tfs.GetClient<GitHttpClient>();
int workItemId = 1213;
var workItem = workItemClient.GetWorkItemAsync("Project-Name", workItemId, expand: WorkItemExpand.Relations).Result;
// We need to retrieve the commit id from the links, debug the following line to understand what I did
var commitId = wit.Relations.Where(r => r.Url.Contains("Git")).FirstOrDefault().Url.Split('%')[2].Remove(0,2);
var commit = gitClient.GetCommitAsync("Project-Name", commitId, "Repo-Name").Result;
string comment = commit.comment;
By the way, you can't use the Fixes #123 syntax because is not supported in TFS 2017.

How access git commits from Microsoft.TeamFoundation.WorkItemTracking.Client

I'm using TFS 2017 update 1 on premises. I'm using #ID in log comments of commits in order to associate workitem ID (of User Story, Task etc.) with GIT commits of source code. It properly works (I can see links to commit from workitem interface) but I'd like to use TFS SDK API with tfs aggregator in order to better manage GIT commits (e.g. dashboards using custom fields) . How can access git commits from Microsoft.TeamFoundation.WorkItemTracking.Client ?
The git commits are linked to workitems with "ExternalLink" type. So you can get the links of the work item to query that information.
WorkItemStore wis = ttpc.GetService<WorkItemStore>();
int workitemid = 1;
WorkItem wi = wis.GetWorkItem(workitemid);
foreach (Link w in wi.Links)
{
if (w.GetType().ToString() == "Microsoft.TeamFoundation.WorkItemTracking.Client.ExternalLink")
{
ExternalLink el = (ExternalLink)w;
Console.WriteLine(el.LinkedArtifactUri);
}
}
The LinkedArtifactUri will include the ID of the git commit. And then you can get the commit information via:
GitHttpClient ght = ttpc.GetClient<GitHttpClient>();
GitCommit gc = ght.GetCommitAsync("CommitID", "RepoID",99).Result;
Console.WriteLine(gc.Committer.Date);
Console.WriteLine(gc.Committer.Name);
foreach (GitChange gch in gc.Changes)
{
Console.WriteLine(gch.Item.Path);
}
Console.ReadLine();

Bitbucket Jenkins plugin constructs wrong push URL

We use Bitbucket server and want to trigger a Jenkins build whenever something is pushed to Bitbucket.
I tried to set up everything according to this page:
https://wiki.jenkins.io/display/JENKINS/BitBucket+Plugin
So I created a Post Webhook in Bitbucket, pointing at the Jenkins Bitbucket plugin's endpoint.
Bitbucket successfully notifies the plugin when a push occurs. According to the Jenkins logs, the plugin then iterates over all jobs where "Build when a change is pushed to BitBucket" is checked, and tries to match that job's repo URL to the URL of the push that occurred.
So, if the repo URL is
https://jira.mycompany.com/stash/scm/PROJ/project.git, the plugin tries to match it against
https://jira.mycompany.com/stash/PROJ/project, which obviously fails.
As per official info from Atlassian, Bitbucket cannot be prevented from inserting the "/scm/" part in the path.
The corresponding code in the Bitbucket Jenkins plugin is in class com.cloudbees.jenkins.plugins.BitbucketPayloadProcessor:
private void processWebhookPayloadBitBucketServer(JSONObject payload) {
JSONObject repo = payload.getJSONObject("repository");
String user = payload.getJSONObject("actor").getString("username");
String url = "";
if (repo.getJSONObject("links").getJSONArray("self").size() != 0) {
try {
URL pushHref = new URL(repo.getJSONObject("links").getJSONArray("self").getJSONObject(0).getString("href"));
url = pushHref.toString().replaceFirst(new String("projects.*"), new String(repo.getString("fullName").toLowerCase()));
String scm = repo.has("scmId") ? repo.getString("scmId") : "git";
probe.triggerMatchingJobs(user, url, scm, payload.toString());
} catch (MalformedURLException e) {
LOGGER.log(Level.WARNING, String.format("URL %s is malformed", url), e);
}
}
}
In the JSON payload that Bitbucket sends to the plugin, the actual checkout URL doesn't appear, only the link to the repository's Bitbucket page. The above method from the plugin appears to construct the checkout URL from that URL by removing everything after and including projects/ and adding the "full name" of the repo, resulting in the above wrong URL.
Official info from Atlassian is that Bitbucket cannot be prevented from adding the "scm" part to the checkout URL.
Is this a bug in the Jenkins plugin? If so, how can the plugin work for anyone?
I found the reason for the failure.
The issue is that the Bitbucket plugin for Jenkins does account for the /scm part in the path, but only if it's the first part after the host name.
If your Bitbucket server instance is configured not under its own domain but under a path of another service, matching the checkout URLs will fail.
Example:
https://bitbucket.foobar.com/scm/PROJ/myproject.git will work,
https://jira.foobar.com/stash/scm/PROJ/myproject.git will not work.
Someone who also had this problem has already created a fix for the plugin, the pull request for which is pending: JENKINS-49177: Now removing first occurrence of /scm

Complete TFS Pull Request programmatically

Using the Microsoft.TeamFoundationServer.Client (15.112.1) to connect to a TFS 2017 Update 2 server we can get details about an existing PR like this:
var connection = new VssConnection(collectionUri, credentials);
var client = connection.GetClient<GitHttpClient>();
var pr = await client.GetPullRequestByIdAsync(pullRequestId);
Also, we can create new PR like this:
var pr = await client.CreatePullRequestAsync(
new GitPullRequest
{
SourceRefName = "master",
TargetRefName = "develop",
Title = "[Automatic Merge]"
},
projectName, repositoryName);
In addition, we can vote on the PR like this:
var pr = await client.CreatePullRequestReviewerAsync(
reviewer, projectName, repositoryName, pullRequestId, authorizedIdenity.Id.ToString());
Is there any way to complete the PR (overriding or not existing branch
policies) and proceed with the merge operation?
The GitHttpClient has an UpdatePullRequestAsync method.
To complete the pull request you need to update the Status property of your pull request. and use the UpdatePullRequestAsync method to complete your PR.
Please make sure that to set the the CompletionOptions property to specify whether you are merging the commit, delete the source branch etc.
So your code would look like following
pr.Status = PullRequestStatus.Completed
pr.CompletionOption = new GitPullRequestCompletionOption() { SquashMerge = true };
client.UpdatePullRequest(pr, repositoryId, pullRequestId);
EDIT:
The ByPassPolicy is not available for the released version of Microsoft.TeamFoundationServer.ExtendedClient yet.
However, if you install the pre-release NuGet Package v15.122.1-preview of library Microsoft.TeamFoundationServer.ExtendedClient, you will see the option ByPassPolicy as a property in the GitPullrequestCompletionOptions class. You can set it to true to by pass policy.

Gerrit Replication.config

I have a remote server ABC with 3 repositories code jellybean, Kitkat and lollipop.
Now I want to do replication only Lollipop repository on Another server XYZ.
Can anyone help me with the replication.config.
How do i write replication.config.
Gerrit document says
[remote "host-one"]
url = gerrit2#host-one.example.com:/some/path/${name}.git
[remote "pubmirror"]
url = mirror1.us.some.org:/pub/git/${name}.git
url = mirror2.us.some.org:/pub/git/${name}.git
url = mirror3.us.some.org:/pub/git/${name}.git
push = +refs/heads/*
push = +refs/tags/*
threads = 3
Should i mention both host ABC and mirror XYZ URL??
Please explain with example
To enable replication.
First we need to make the mirror of existing repository on slave server.
Then in replication.config in master use projects: to mention repository name.

Resources