GitCommands.GitModule.GetAllBranchesWhichContainGivenCommit C# (CSharp) Method

GetAllBranchesWhichContainGivenCommit() public method

Gets branches which contain the given commit. If both local and remote branches are requested, remote branches are prefixed with "remotes/" (as returned by git branch -a)
public GetAllBranchesWhichContainGivenCommit ( string sha1, bool getLocal, bool getRemote ) : IEnumerable
sha1 string The sha1.
getLocal bool Pass true to include local branches
getRemote bool Pass true to include remote branches
return IEnumerable
        public IEnumerable<string> GetAllBranchesWhichContainGivenCommit(string sha1, bool getLocal, bool getRemote)
        {
            string args = "--contains " + sha1;
            if (getRemote && getLocal)
                args = "-a " + args;
            else if (getRemote)
                args = "-r " + args;
            else if (!getLocal)
                return new string[] { };
            string info = RunGitCmd("branch " + args);
            if (info.Trim().StartsWith("fatal") || info.Trim().StartsWith("error:"))
                return new List<string>();

            string[] result = info.Split(new[] { '\r', '\n', '*' }, StringSplitOptions.RemoveEmptyEntries);

            // Remove symlink targets as in "origin/HEAD -> origin/master"
            for (int i = 0; i < result.Length; i++)
            {
                string item = result[i].Trim();
                int idx;
                if (getRemote && ((idx = item.IndexOf(" ->")) >= 0))
                {
                    item = item.Substring(0, idx);
                }
                result[i] = item;
            }

            return result;
        }

Usage Example

        public void GetAllBranchesWhichContainGivenCommitTestReturnsEmptyList()
        {
            var module = new GitModule("");
            var actualResult = module.GetAllBranchesWhichContainGivenCommit("fakesha1", false, false);

            Assert.IsNotNull(actualResult);
            Assert.IsTrue(!actualResult.Any());
        }
GitModule