现在最好的方法是使用GetBranchStatsBatchAsync。
GetCommits 解决方案并不真正可靠 - 我们在某些情况下 API 未能找到范围内的特定提交,错误地声明提交不在分支中(然后我们用 GetBranchStatsBatchAsync 替换了逻辑,因为它变得可用从那以后就没有问题了)。
旧的/不可靠的解决方案
有一种方法可以在没有GetBranchStatsBatchAsync 的情况下实现它 - 获取提交时间,然后检查该时间间隔是否在分支中,尽管它需要两个单独的调用并且可能有一些问题 与:
-
Committer date vs Author date - 我使用提交者日期,但没有花足够的时间尝试真正检查它。
用法是:
GitHttpClient git = ...;
var isInBranch = git.BranchContains(
project: "project",
repositoryId: "repository",
branch: "master",
commitId: "12345678910...")
代码是:
public static class GitHttpClientExt
{
/// <summary>
/// Gets a value indicating whether a branch with name <paramref name="branch"/> (like 'master', 'dev') contains the commit with specified <paramref name="commitId"/>.
/// Just like the <code>git branch --contains</code> it doesn't take possible reversions into account.
/// </summary>
public static Boolean BranchContains(this GitHttpClient git, String project, String repositoryId, String branch, String commitId)
{
var commitToFind = git.TryGetCommit(project: project, repositoryId: repositoryId, commitId: commitId);
if (commitToFind == null)
{
return false;
}
var committedDate = commitToFind.Committer.Date; // TODO: It will usually be the same as the author's, but I have failed to check what date TFS actually uses in date queries.
var criteria = new GitQueryCommitsCriteria
{
ItemVersion = new GitVersionDescriptor
{
Version = branch,
VersionType = GitVersionType.Branch
},
FromDate = DateToString(committedDate.AddSeconds(-1)), // Zero length interval seems to work, but just in case
ToDate = DateToString(committedDate.AddSeconds(1)),
};
var commitIds = git
.GetAllCommits(
project: project,
repositoryId: repositoryId,
searchCriteria: criteria)
.Select(c => c.CommitId);
return commitIds.Contains(commitId);
}
/// <summary>
/// Gets the string representation of <paramref name="dateTime"/> usable in query objects for <see cref="GitHttpClient"/>.
/// </summary>
public static String DateToString(DateTimeOffset dateTime)
{
return dateTime.ToString(CultureInfo.InvariantCulture);
}
/// <summary>Tries to retrieve git commit with specified <paramref name="commitId"/> for a project.</summary>
public static GitCommitRef TryGetCommit(this GitHttpClient git, String project, String repositoryId, String commitId)
{
return git
.GetAllCommits(
project: project,
repositoryId: repositoryId,
searchCriteria: new GitQueryCommitsCriteria
{
Ids = new List<String>
{
commitId
}
})
.SingleOrDefault();
}
/// <summary>Retrieve all(up to <see cref="Int32.MaxValue"/>) git (unless <paramref name="top"/> is set) commits for a project</summary>
public static List<GitCommitRef> GetAllCommits(
this GitHttpClient git,
String project,
String repositoryId,
GitQueryCommitsCriteria searchCriteria,
Int32? skip = null,
Int32? top = (Int32.MaxValue - 1)) // Current API somehow fails (silently!) on Int32.MaxValue;
{
return git
.GetCommitsAsync(
project: project,
repositoryId: repositoryId,
searchCriteria: searchCriteria,
skip: skip,
top: top)
.GetAwaiter()
.GetResult();
}
}
P.S.:目前的代码是异步方法的同步包装器,不幸的是当前项目需要它。如果适合您,请将其重新制作成合适的异步版本。