Eugene Podskal
Eugene Podskal

Reputation: 10401

How to determine whether a branch contains a commit with TFS WebApi client library?

Basically I'd like to find an API alternative to git branch --contains with TFS WebApi client libraries:

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

Is there a way to accomplish it?

Or should I rather operate on a local clone with git.exe/libgit instead (the repository in question is a bit too large and, if possible, I'd much prefer to avoid cloning it)?

Upvotes: 0

Views: 1107

Answers (2)

Eugene Podskal
Eugene Podskal

Reputation: 10401

The best way to do it now is with GetBranchStatsBatchAsync.

The GetCommits solution is not really reliable - we had some cases where API failed to find specific commits in the range falsely declaring that a commit wasn't in the branch (we then replaced the logic with GetBranchStatsBatchAsync as it became available and had no problems since).


Old/unreliable solution

There is a way to achieve it without GetBranchStatsBatchAsync - get commit time, then check whether such commit is in the branch at that time interval, though it requires two separate calls and may have some issues with:

  1. Committer date vs Author date - I use committer date, but didn't spend enough time trying to truly check it.

The usage is:

GitHttpClient git = ...;
var isInBranch = git.BranchContains(
    project: "project",
    repositoryId: "repository",
    branch: "master",
    commitId: "12345678910...")

The code is:

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.: The code currently is a sync wrapper over async methods, as it is what was unfortunately required in the current project. Rework it into a proper async version if it suits you.

Upvotes: 0

PatrickLu-MSFT
PatrickLu-MSFT

Reputation: 51093

There is not any WebApi Client library corresponding to git branch-- contains command.

However as a workaround, you could directly run git command in your C# code.

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

You could contain your certification, more details please refer Run git commands from a C# function

Another way is using powershell script to run the git command and call TFS API.

Upvotes: 1

Related Questions