libgit2sharp get all commits since the last push - c#

I would like to view all of the commits since the last time the user pushed from their machine.
using (var repo = new Repository(repositoryDirectory))
{
var c = repo.Lookup<Commit>(shaHashOfCommit);
// Let's only consider the refs that lead to this commit...
var refs = repo.Refs.ReachableFrom(new []{c});
//...and create a filter that will retrieve all the commits...
var cf = new CommitFilter
{
Since = refs, // ...reachable from all those refs...
Until = c // ...until this commit is met
};
var cs = repo.Commits.QueryBy(cf);
foreach (var co in cs)
{
Console.WriteLine("{0}: {1}", co.Id.ToString(7), co.MessageShort);
}
}
I got this code from another post, but I am not sure how to modify it to get the commits since the date of the last push.

You want the commits that are reachable from c, excluding the ones that are reachable from the remote commit.
If you're talking about master, in a typical setup, the tracking branch for this will be remotes/origin/master. refs/remotes/origin/master will be updated when you push to the remote master branch.
So your CommitFilter should look like:
new CommitFilter { Since = repo.Refs["refs/remotes/origin/master"], Until = c }
Which is equivalent to git log refs/remotes/origin/master..c.

Related

How to get git branch information about each of commit by using 'LibGit2sharp'

I'd like to make 'Commit History Graph' by using libgit2sharp library like below.
There was a similar question in stackoverflow, but I couldn't get the specific answer.
How do I build a version tree for a Git repository using LibGit2(Sharp)
Here is my source code.
string path = "c:\github\git-gui-app";
var repo = Repository(path);
var commits = repo.Commits();
var list = new List<CommitItem>();
foreach (var item in commits)
{
var commitItem = new CommitItem();
commitItem.Name = item.Message;
commitItem.Sha = item.Sha;
commitItem.ParentSha = item.Parents.FirstOrDefault().Sha;
list.Add(commitItem);
}
foreach (var item in list)
{
item.BranchInfo = GetBranchInfo(item);
}
...
private BranchItem GetBranchInfo(CommitItem item)
{
// How to get branch information in current commit?
}
I succeeded in getting the git repository commit information,
but I still don't know how to get branch information of each commit.

Is there a way to create git push/commit with linked workitems through TFS client libraries/API in a single operation?

I want to create a push using TFS git client libraries that contains the specified workitems.
The code is basically:
public static async Task<GitPush> CreatePush(
this GitHttpClient git,
GitRepository repo,
GitRefUpdate branchUpdateRef,
GitChange change,
String comment,
IEnumerable<Int32> workitems = null)
{
workitems = (workitems ?? Enumerable.Empty<Int32>()).ToList();
var commit = new GitCommitRef
{
Comment = comment,
Changes = new[]
{
change
},
WorkItems = workitems
.Select(wi => new ResourceRef
{
Id = wi.ToString()
// Perhaps one should also set an URL of some kind
})
.ToList()
};
var push = new GitPush
{
Commits = new[]
{
commit
},
RefUpdates = new[]
{
branchUpdateRef
},
Repository = repo
};
return await git.CreatePushAsync(
push,
project: repo.ProjectReference.Id,
repositoryId: repo.Id);
}
The push is created successfully, though there are no workitem links on the created commit.
I have tried
Setting the URL to the one from https://learn.microsoft.com/en-us/rest/api/vsts/wit/work%20items/get%20work%20item?view=vsts-rest-4.1#workitem .
Settings it to the slightly different workitem URL that is actually returned in GitCommitRef.WorkItems ResourceRef for commits that have related workitems.
But to no avail.
I actually can achieve the desired final result by
Either appending $"#{workitemId}" to the commit comment string
Or by adding commit link to the workitem in question
Still, both of those solutions have minor drawbacks(1 - changing commit message, 2 - possible race condition with CI and etc.) that I'd prefer to avoid.
Basically, is there a way to way to create GitPush with associated workitems in a single operation, or do I have to use the aforementioned workarounds?
No, there isn't any way to achieve this. Even when you create a commit/push from TFS Web UI, TFS still create the commit/push first and then update the work item to link to the push.

using libgit2sharp to pull latest from a branch

I am using libgit2sharp in a c# solution to switch to a branch and pull in the latest changes. Here is the code I am using:
public void FetchAll()
{
using (var repo = new Repository(_LocalGitPath))
{
foreach (Remote remote in repo.Network.Remotes)
{
FetchOptions options = new FetchOptions();
options.CredentialsProvider = new CredentialsHandler((url, usernameFromUrl, types) => new UsernamePasswordCredentials()
{
Username = _UserName,
Password = _Password
});
repo.Network.Fetch(remote, options);
}
}
}
public string CheckoutBranch(string branchName)
{
using (var repo = new Repository(_LocalGitPath))
{
var trackingBranch = repo.Branches[branchName];
if (trackingBranch.IsRemote)
{
branchName = branchName.Replace("origin/", string.Empty);
var branch = repo.CreateBranch(branchName, trackingBranch.Tip);
repo.Branches.Update(branch, b => b.TrackedBranch = trackingBranch.CanonicalName);
Commands.Checkout(repo, branch, new CheckoutOptions { CheckoutModifiers = CheckoutModifiers.Force });
}
else
{
Commands.Checkout(repo, trackingBranch, new CheckoutOptions { CheckoutModifiers = CheckoutModifiers.Force });
}
return branchName;
}
}
public void PullBranch(string branchName)
{
using (var repo = new Repository(_LocalGitPath))
{
PullOptions options = new PullOptions();
options.MergeOptions = new MergeOptions();
options.MergeOptions.FailOnConflict = true;
options.FetchOptions = new FetchOptions();
options.FetchOptions.CredentialsProvider = new CredentialsHandler((url, usernameFromUrl, types) => new UsernamePasswordCredentials()
{
Username = _UserName,
Password = _Password
});
repo.Network.Pull(new Signature(_UserName, _Password, new DateTimeOffset(DateTime.Now)), options);
}
}
I have no problem fetching, and checking out a branch. When I try to pull latest, I get an error saying, 'There is no tracking information for the current branch.' I believe that this means that the local branch doesn't know what the correct remote repository is to pull changes from, but I haven't been able to figure out how to tell libgit2sharp what the remote repo path is. Anyone have any suggestions?
While doing research on this problem I found this: https://github.com/libgit2/libgit2sharp/issues/1235. Essentially, a libgit2sharp dev describes the exact problem I am seeing, but doesn't provide any code for the fix.
One additional note: I will never be merging or pushing any changes back from this repository. I am pulling it for an automated build, so we can ignore or overwrite any local changes. I just need to get the latest code.
SOLUTION:
I have updated the code above with the solution that I got working. You need to be really careful to make sure that when you checkout a branch, you check the isRemote flag on the branch you are checking out is set to true. If you checkout a branch that isn't a remote it will set the remote to '.' in your git config file, and you need to manually fix it. If there isn't a valid remote you will not be able to pull the branch changes.
Do not use the code sample on the libgit2sharp wiki unless they add this check in.
You can setup the tracking brach information on the local branch by using the Refspec:
using (var repo = new Repository("/Users/sushi/code/redux/mono"))
{
var trackingBranch = repo.Branches["remotes/origin/work-btls"];
if (trackingBranch.IsRemote)
{
var branch = repo.CreateBranch("SomeLocalBranchName", trackingBranch.Tip);
repo.Branches.Update(branch, b => b.TrackedBranch = trackingBranch.CanonicalName);
repo.Checkout(branch, new CheckoutOptions { CheckoutModifiers = CheckoutModifiers.Force });
}
}
You can use git to verify that SomeLocalBranchName is now tracking remotes/origin/work-btls:
>>>git for-each-ref --format='%(refname:short) <- %(upstream:short)' refs/heads
SomeLocalBranchName <- remotes/origin/work-btls
master <- origin/master
>>>git status
On branch SomeLocalBranchName
Your branch is up-to-date with 'remotes/origin/work-btls'.

How to get all installations when using Azure Notification Hubs installation model?

Using NotificationHubClient I can get all registered devices using GetAllRegistrationsAsync(). But if I do not use the registration model but the installation model instead, how can I get all installations? There are methods to retrieve a specific installation but none to get everything.
You're correct, as of July 2016 there's no way to get all installations for a hub. In the future, the product team is planning to add this feature to the installations model, but it will work in a different way. Instead of making it a runtime operation, you'll provide your storage connection string and you'll get a blob with everything associated with the hub.
Sorry for visiting an old thread... but in theory you could use the GetAllRegistrationsAsyc to get all the installations. I guess this will return everything without an installation id as well, but you could just ignore those if you choose.
Could look something like this
var allRegistrations = await _hub.GetAllRegistrationsAsync(0);
var continuationToken = allRegistrations.ContinuationToken;
var registrationDescriptionsList = new List<RegistrationDescription>(allRegistrations);
while (!string.IsNullOrWhiteSpace(continuationToken))
{
var otherRegistrations = await _hub.GetAllRegistrationsAsync(continuationToken, 0);
registrationDescriptionsList.AddRange(otherRegistrations);
continuationToken = otherRegistrations.ContinuationToken;
}
// Put into DeviceInstallation object
var deviceInstallationList = new List<DeviceInstallation>();
foreach (var registration in registrationDescriptionsList)
{
var deviceInstallation = new DeviceInstallation();
var tags = registration.Tags;
foreach(var tag in tags)
{
if (tag.Contains("InstallationId:"))
{
deviceInstallation.InstallationId = new Guid(tag.Substring(tag.IndexOf(":")+1));
}
}
deviceInstallation.PushHandle = registration.PnsHandle;
deviceInstallation.Tags = new List<string>(registration.Tags);
deviceInstallationList.Add(deviceInstallation);
}
I am not suggesting this to be the cleanest chunk of code written, but it does the trick for us. We only use this for debugging type purposes anyways

TFS 2010: How to produce a changelog (ie. list of work items) between two releases of the application using labels?

I'm looking for a way to automatically produce a changelog (actually a list of workitems) between two releases of my application. I have two versions of my application, v1 and v2, each is identified by a label in TFS 2010 (LABEL1 and LABEL2) that I manually created before building the setups of my app.
I have a branching system, which means I have a trunk were most of bugs are fixed, and a branch where patches are applied mostly using merges from the trunk (but there are also some fixes on the branch only that do not concern the trunk). The two versions of my application (v1 and v2) are versions from the branch.
I would like TFS 2010 to be able to return the list of bugs that were fixed (ie. the list of work items with type = Bug that are closed and verified) between these two labels.
I tried to achieve this using the web UI of TFS 2010, or using Visual Studio, but I didn't find any way.
Then I tried to ask tf.exe for a history using the following command line:
tf history /server:http://server_url/collection_name "$/project_path" /version:LLABEL1~LLABEL2 /recursive /noprompt /format:brief
where LABEL1 is the label that has been associated with the source code of the v1 of the application, and LABEL2 the label that has been associated with the source code of the v2 of the application.
It actually fails in two ways:
- the command line only returns a list of changesets, not a list of associated closed work items
- the list of changesets only contains the changesets that I applied on the branch itself, not the changesets that I also applied and the trunk and then merged to the branch. Setting or not the "/slotmode" parameter doesn't change anything.
There I tried to write a piece of C# code to retrieve the list of workitems (not the list of changesets):
var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://server_url/collection_name"));
VersionControlServer controlServer = tfs.GetService<VersionControlServer>();
VersionControlServer vcs = tfs.GetService<VersionControlServer>();
VersionSpec sFrom = VersionSpec.ParseSingleSpec("LLABEL1", null);
VersionSpec sTo = VersionSpec.ParseSingleSpec("LLABEL2", null);
var changesets = vcs.QueryHistory(
"$/project_path",
sTo,
0,
RecursionType.Full,
null,
sFrom,
sTo,
int.MaxValue,
true,
false); // Slotmode to false
Dictionary<int, WorkItem> dico = new Dictionary<int, WorkItem>();
foreach (Changeset set in changesets)
{
foreach (WorkItem zz in set.WorkItems)
{
if (!dico.ContainsKey(zz.Id))
{
dico.Add(zz.Id, zz);
}
}
}
foreach (KeyValuePair<int, WorkItem> pair in dico.OrderBy(z => z.Key))
{
Console.WriteLine(string.Format("ID: {0}, Title: {1}", pair.Key, pair.Value.Title));
}
This actually works, I get the list of workitems between my two labels which is actually what I wanted. But only workitems associated to changesets that were committed on the branch itself are taken into account: the workitems of type "Bug" that were solved on the trunk then merged to the branch don't appear. Slotmode doesn't change anything.
Then I finally tried to replace VersionSpecs that were defined by a label with VersionSpecs that are defined by changesets:
VersionSpec sFrom = VersionSpec.ParseSingleSpec("C5083", null);
VersionSpec sTo = VersionSpec.ParseSingleSpec("C5276", null);
And my code finally works.
So my question is: how could I get the same result with labels, which are the TFS objects I use to identify a version? If it's not possible, how should I identify a version in TFS 2010?
Thx.
Btw I found some questions on stackoverflow, but none of them gave me answers with labels. For instance:
Question example
I think http://tfschangelog.codeplex.com/ can possibly help you here.
TFS ChangeLog applicatoin allows users to automatically generate release notes from TFS. Users will have to provide information on thier project, branch and changeset range and then TFS ChangeLog application will extract information from each changeset in a given range and all the associated workitems to such changesets. i.e. it will travel from starting changeset upto ending changeset and will extract data about each changeset along with associated workitems in an XML file.
Users can then use their own transformation logic including filter, sorting, styling, output formatting, etc. to generate Release Notes Report.
Another thing I would like to add here will be related to Labels in TFS. Labels are basically assigned / associated with changesets. Currently, TFS ChangeLog application does not support Labels to define starting and ending point but it does support changeset which can be used as a workaround solution.
Hope this is useful.
In general, the absolute method of defining points in time in any SCM is clearly the checkin-id. Using labels to abstract this, is in TFS not the optimum as discussed here & here. A better approach is to use builds instead, especially in a modern CI environment.
In order to retrieve the max changeset that was contained in a given build you 'd have to do something like this:
using System;
using System.Collections.Generic;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Client;
namespace GetChangesetsFromBuild
{
class Program
{
static void Main()
{
TfsTeamProjectCollection tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://TFSServer:8080/Name"));
IBuildServer bs = (IBuildServer)tpc.GetService(typeof(IBuildServer));
IBuildDetail build = bs.GetAllBuildDetails(new Uri("vstfs:///..."));
List<IChangesetSummary> associatedChangesets = InformationNodeConverters.GetAssociatedChangesets(build);
int idMax = associatedChangesets[0].ChangesetId;
}
}
}
A difficulty with the above is to retrieve the BuildUri of the builds you are interested in. In order to get this information you could do something like this:
IBuildDetail[] builds = bs.QueryBuilds("TeamPorjectName", "yourBuildDefinitionName")
and then retrieve the Uri's that are important to you.
This is also a good vehicle if you eventually insist on using labels: Besides Uri, each build[] has also a LabelName.
I have been in the same situation as you. I also want Work Items from merged changesets included. I only include Work Items that are Done. Also if the same Work Item is linked to multiple changesets, only the last changeset is reported. I use this in a CI setup; and create a changelog for each build. The List<ChangeInfo> can then be exported to a XML/HTML/TXT-file. Here is my solution:
namespace TFSChangelog
{
public class TFSChangelogGenerator
{
private const string workItemDoneText = "Done";
/// <summary>
/// This class describes a change by:
/// Changeset details
/// and
/// WorkItem details
/// </summary>
public class ChangeInfo
{
#region Changeset details
public DateTime ChangesetCreationDate { get; set; }
public int ChangesetId { get; set; }
#endregion
#region WorkItem details
public string WorkItemTitle { get; set; }
public int WorkItemId { get; set; }
#endregion
}
public static List<ChangeInfo> GetChangeinfo(string tfsServer, string serverPath, string from, string to)
{
// Connect to server
var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(tfsServer));
tfs.Connect(ConnectOptions.None);
var vcs = tfs.GetService<VersionControlServer>();
// Create versionspec's
VersionSpec versionFrom = null;
if (!string.IsNullOrEmpty(from))
versionFrom = VersionSpec.ParseSingleSpec(from, null);
VersionSpec versionTo = VersionSpec.Latest;
if (!string.IsNullOrEmpty(to))
versionTo = VersionSpec.ParseSingleSpec(to, null);
// Internally used dictionary
var changes = new Dictionary<int, ChangeInfo>();
// Find Changesets that are checked into the branch
var directChangesets = vcs.QueryHistory(
serverPath,
VersionSpec.Latest,
0,
RecursionType.Full,
null,
versionFrom,
versionTo,
Int32.MaxValue,
true,
false
).Cast<Changeset>();
foreach (var changeset in directChangesets)
{
foreach (var workItem in changeset.WorkItems.Where(workItem => workItem.State == workItemDoneText))
{
if (changes.ContainsKey(workItem.Id))
{
if (changeset.ChangesetId < changes[workItem.Id].ChangesetId) continue;
}
changes[workItem.Id] = new ChangeInfo { ChangesetId = changeset.ChangesetId, ChangesetCreationDate = changeset.CreationDate, WorkItemId = workItem.Id, WorkItemTitle = workItem.Title };
}
}
// Find Changesets that are merged into the branch
var items = vcs.GetItems(serverPath, RecursionType.Full);
foreach (var item in items.Items)
{
var changesetMergeDetails = vcs.QueryMergesWithDetails(
null,
null,
0,
item.ServerItem,
VersionSpec.Latest,
0,
versionFrom,
versionTo,
RecursionType.Full
);
foreach (var merge in changesetMergeDetails.Changesets)
{
foreach (var workItem in merge.WorkItems.Where(workItem => workItem.State == workItemDoneText))
{
if (changes.ContainsKey(workItem.Id))
{
if (merge.ChangesetId < changes[workItem.Id].ChangesetId) continue;
}
changes[workItem.Id] = new ChangeInfo { ChangesetId = merge.ChangesetId, ChangesetCreationDate = merge.CreationDate, WorkItemId = workItem.Id, WorkItemTitle = workItem.Title };
}
}
}
// Return a list sorted by ChangesetId
return (from entry in changes orderby entry.Value.ChangesetId descending select entry.Value).ToList();
}
}
}
This question got me closer to solving a similar problem I was having.
Use the type LabelVersionSpec instead of VersionSpec for label versions.
Replace:
VersionSpec sFrom = VersionSpec.ParseSingleSpec("LLABEL1", null);
VersionSpec sTo = VersionSpec.ParseSingleSpec("LLABEL2", null);
with:
LabelVersionSpec sFrom = new LabelVersionSpec("LLABEL1");
LabelVersionSpec sTo = new LabelVersionSpec("LLABEL2");

Categories

Resources