如何确定分支是否包含TFS WebApi客户端库的提交?

时间:2018-05-28 16:45:45

标签: c# git tfs git-commit tfs-sdk

基本上我想通过TFS WebApi git branch --contains 找到client libraries的替代API:

var commitId = "123456789abcdef";
var branchName = "branch";
var tpc = new TfsTeamProjectCollection(new Uri(""));
var git = tpc.GetClient<GitHttpClient>();
var isInBranch = git.?????(branchName, commitId);

有办法实现吗?

或者我应该使用git.exe / libgit来操作本地克隆(相关的存储库有点太大,如果可能的话,我更愿意避免克隆它?)

2 个答案:

答案 0 :(得分:1)

没有任何与git branch-- contains命令对应的WebApi客户端库。

但是作为一种解决方法,您可以直接在C#代码中运行git命令。

string gitCommand = "git";
string gitAddArgument = @"add -A" ;
string gitCommitArgument = @"commit ""explanations_of_changes"" "
string gitPushArgument = @"push our_remote"

Process.Start(gitCommand, gitAddArgument );
Process.Start(gitCommand, gitCommitArgument );
Process.Start(gitCommand, gitPushArgument );

您可以包含您的认证,更多详情请参阅Run git commands from a C# function

另一种方法是使用powershell脚本来运行git命令并调用TFS API。

答案 1 :(得分:0)

有一种方法可以通过当前可访问的API实现它 - 获取提交时间,然后检查此时间隔内是否在分支中,尽管它需要两个单独的调用并且可能有一些问题用:

  1. Committer date vs Author date - 我使用提交日期,但没有花足够的时间尝试真正检查它。
  2. 用法是:

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

    PS:还有一种方法可以使GetBranchStatsBatchAsync更可靠 - 我们遇到一些情况,api无法在范围内找到特定的提交,错误地声明提交不在分支中(我们用GetBranchStatsBatchAsync替换了逻辑,因此没有问题。)

    P.S。:代码当前是异步方法的同步包装器,因为它是当前项目中不幸需要的。如果它适合你,请将其修改为适当的异步版本。

相关问题