Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get name of branch into code

I have a question about passing the branch name to my code as a string.

So we are using a git repository and the branch number also refers to the staging environment where the build is placed. Meaning, if my branch is 001, my url is 001.test.myapplication.com.

I am writing automated tests which are executed on the staging environment of the branch. My question is, is it possible to pass the branch number to my code so I can make it part of the URL I want to test on?

I am using visual studio 2017, selenium and specflow.

like image 482
Anand Avatar asked Jan 24 '18 11:01

Anand


People also ask

How do I find my remote branch name?

Command #1: git branch -r This Git command will show you remote branches. The -r flag here is short for --remotes . This is the command I use personally. So if you want, you can just stop reading here and use git branch -r whenever you want to list remote git branches.


1 Answers

I actually found a great solution which perfectly works. Sharing so in the future, others can use it too if they need to.

        ProcessStartInfo startInfo = new ProcessStartInfo("git.exe");

        startInfo.UseShellExecute = false;
        startInfo.WorkingDirectory = "dir Here";
        startInfo.RedirectStandardInput = true;
        startInfo.RedirectStandardOutput = true;
        startInfo.Arguments = "rev-parse --abbrev-ref HEAD";

        Process process = new Process();
        process.StartInfo = startInfo;
        process.Start();

        string branchname = process.StandardOutput.ReadLine();
like image 199
Anand Avatar answered Oct 26 '22 20:10

Anand