Vivek Nuna
Vivek Nuna

Reputation: 1

How to check whether a commit is present in a Branch in Azure Dev Ops?

I have a commit id, I want to check whether is it present in a branch or in which branches this commit id is present.

GitBranchStats doesn't have any properties related to commit info in

Microsoft.TeamFoundation.SourceControl.WebApi library.
Uri orgUrl = new Uri("https://example.com");         // Organization URL            
String personalAccessToken = "XXXXXXXXX"; 
VssConnection connection = new VssConnection(orgUrl, new VssBasicCredential(string.Empty, personalAccessToken));
GitHttpClient gitClient = connection.GetClient<GitHttpClient>();
GitBranchStats branch = gitClient.GetBranchAsync("repo", "name").Result;

FYI, I am not looking for a git command, I already know this.

Edit:

It's giving exceptions in method check_whether_present_in_branches in a few cases when a branch is in any folder like below.

Exception message:

TF401175:The version descriptor <Branch: Release > could not be resolved to a version in the repository

enter image description here

Upvotes: 1

Views: 1163

Answers (1)

Bowman Zhu
Bowman Zhu

Reputation: 7146

Update:

using Microsoft.TeamFoundation.SourceControl.WebApi;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;

namespace CheckWhetherCommitExistInCurrentBranch
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Uri orgUrl = new Uri("https://dev.azure.com/xxx/");         // Organization URL
            string project = "xxx";
            string repository = "xxx";
            String personalAccessToken = "xxx";
            string commit_id = "xxx";
            string branch_name = "xxx";

            //var commitref = GetLastestCommit(orgUrl, personalAccessToken, project, repository, branch_name);

            //Console.WriteLine(commitref.CommitId);

            //check whether the commit id exists in specific branch.
            bool exists = check_whether_present_in_branch(orgUrl, personalAccessToken, project, repository, commit_id, branch_name);
            Console.WriteLine("Whether commit id "+commit_id+" exists in "+branch_name+"? "+exists+"\n");

            //check what branches the commit id exists.
            List<string> branches = check_whether_present_in_branches(orgUrl, personalAccessToken, project, repository, commit_id);
            foreach (var branch in branches)
            {
                Console.WriteLine("exists in "+branch);
            }
        }
        public static bool check_whether_present_in_branch(Uri orgUrl, string personalAccessToken, string project, string repository, string commit_id, string branch)
        {
            bool exists = false;
            VssConnection connection = new VssConnection(orgUrl, new VssBasicCredential(string.Empty, personalAccessToken));
            GitHttpClient gitClient = connection.GetClient<GitHttpClient>();
            //check whether specific commit id present which branch
            var commits = gitClient.GetCommitsAsync(project: project, repositoryId: repository, new GitQueryCommitsCriteria
            {
                ItemVersion = new GitVersionDescriptor
                {
                    Version = branch,
                    VersionType = GitVersionType.Branch
                }
            }).Result;
            foreach (var commit in commits)
            {
                Console.WriteLine(commit.CommitId);
                if (commit.CommitId == commit_id)
                {
                    //Console.WriteLine("Commit id present in branch");
                    exists = true;
                    return exists;
                }
            }
            return exists;
        }


        public static List<string> check_whether_present_in_branches(Uri orgUrl, string personalAccessToken, string project, string repository, string commit_id)
        {
            List<string> branches = new List<string>();
            VssConnection connection = new VssConnection(orgUrl, new VssBasicCredential(string.Empty, personalAccessToken));
            GitHttpClient gitClient = connection.GetClient<GitHttpClient>();
            var refs = gitClient.GetRefsAsync(project: project, repositoryId: repository).Result;
            foreach (var ref_ in refs)
            {
                //Console.WriteLine(ref_.Name);
                if (ref_.Name.StartsWith("refs/heads/"))
                {
                    var commits = gitClient.GetCommitsAsync(project: project, repositoryId: repository, new GitQueryCommitsCriteria
                    {
                        ItemVersion = new GitVersionDescriptor
                        {
                            //Version = ref_.Name,
                            //Version = (ref_.Name.Split('/'))[(ref_.Name.Split('/')).Length - 1],
                            Version = ref_.Name.Substring(11),
                           
                            VersionType = GitVersionType.Branch
                        }
                    }).Result;
                    foreach (var commit in commits)
                    {
                        //Console.WriteLine(commit.CommitId);
                        if (commit.CommitId == commit_id)
                        {
                            //Console.WriteLine("Commit id present in branch");
                            branches.Add(ref_.Name);
                        }
                    }
                }
                else
                {
                    continue;
                }
            }
            return branches;
        }

        //get lastest commit
        public static GitCommitRef GetLastestCommit(Uri orgUrl, string personalAccessToken, string project, string repoId, string branchName)
        {
            List<string> branches = new List<string>();
            VssConnection connection = new VssConnection(orgUrl, new VssBasicCredential(string.Empty, personalAccessToken));
            GitHttpClient gitClient = connection.GetClient<GitHttpClient>();
            var commit = gitClient.GetCommitsAsync(project: project, repositoryId: repoId, new GitQueryCommitsCriteria()
            {
                ItemVersion = new GitVersionDescriptor()
                {
                    Version = branchName,
                    VersionType = GitVersionType.Branch
                },
                Top = 1
            }).Result.FirstOrDefault();
            return commit;
        }
    }
}

enter image description here

Original Answer:

Provide a C# demo for you(I think I should have already considered all the situations):

using Microsoft.TeamFoundation.SourceControl.WebApi;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;
using System;
using System.Collections.Generic;

namespace CheckWhetherCommitExistInCurrentBranch
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri orgUrl = new Uri("https://dev.azure.com/xxx/");         // Organization URL
            string project = "xxx";
            string repository = "xxx";
            String personalAccessToken = "xxx";
            string commit_id = "xxx";
            string branch_name = "xxx";

            //check whether the commit id exists in specific branch.
            bool exists = check_whether_present_in_branch(orgUrl,personalAccessToken,project,repository,commit_id,branch_name);
            Console.WriteLine("Whether commit id "+commit_id+" exists in "+branch_name+"? "+exists+"\n");

            //check what branches the commit id exists.
            List<string>  branches = check_whether_present_in_branches(orgUrl, personalAccessToken, project, repository, commit_id);
            foreach (var branch in branches)
            {
                Console.WriteLine("exists in "+branch);
            }
        }
        public static bool check_whether_present_in_branch(Uri orgUrl, string personalAccessToken, string project, string repository, string commit_id, string branch) {
            bool exists = false;
            VssConnection connection = new VssConnection(orgUrl, new VssBasicCredential(string.Empty, personalAccessToken));
            GitHttpClient gitClient = connection.GetClient<GitHttpClient>();
            //check whether specific commit id present which branch
            var commits = gitClient.GetCommitsAsync(project: project, repositoryId: repository, new GitQueryCommitsCriteria
            {
                ItemVersion = new GitVersionDescriptor
                {
                    Version = branch,
                    VersionType = GitVersionType.Branch
                }
            }).Result;
            foreach (var commit in commits)
            {
                Console.WriteLine(commit.CommitId);
                if (commit.CommitId == commit_id)
                {
                    //Console.WriteLine("Commit id present in branch");
                    exists = true;
                    return exists;
                }
            }
            return exists;
        }


        public static List<string> check_whether_present_in_branches(Uri orgUrl, string personalAccessToken, string project, string repository, string commit_id)
        {
            List<string> branches = new List<string>();
            VssConnection connection = new VssConnection(orgUrl, new VssBasicCredential(string.Empty, personalAccessToken));
            GitHttpClient gitClient = connection.GetClient<GitHttpClient>();
            var refs = gitClient.GetRefsAsync(project: project, repositoryId: repository).Result;
            foreach (var ref_ in refs)
            {
                //Console.WriteLine(ref_.Name);
                if (ref_.Name.StartsWith("refs/heads/"))
                {
                    var commits = gitClient.GetCommitsAsync(project: project, repositoryId: repository, new GitQueryCommitsCriteria
                    {
                        ItemVersion = new GitVersionDescriptor
                        {
                            //Version = ref_.Name,
                            Version = (ref_.Name.Split('/'))[(ref_.Name.Split('/')).Length - 1],
                            VersionType = GitVersionType.Branch
                        }
                    }).Result;
                    foreach (var commit in commits)
                    {
                        //Console.WriteLine(commit.CommitId);
                        if (commit.CommitId == commit_id)
                        {
                            //Console.WriteLine("Commit id present in branch");
                            branches.Add(ref_.Name);
                        }
                    }
                }
                else
                {
                    continue;
                }
            }
            return branches;
        }
    }
}

Results:

enter image description here

Upvotes: 1

Related Questions