Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a branch name with a slash in Azure DevOps?

Azure Devops offers two variables containing information about the current git branch name: $(Build.SourceBranchName) and $(Build.SourceBranch).

While SourceBranch contains the full reference to the branch, SourceBranchName is expected to contain only the short branch name.

Unfortunately, the behavior is a bit unexpected when the branch name contains a slash (/):

+---------------------------------------------------------------------------------------------------------+
| Situation                     | Git branch name  | Build.SourceBranch          | Build.SourceBranchName |
|---------------------------------------------------------------------------------------------------------|
| branch name contains no slash | mybranch         | refs/heads/mybranch         | mybranch               |
| branch name contains slash    | release/mybranch | refs/heads/release/mybranch | mybranch               |
+---------------------------------------------------------------------------------------------------------+

The part of the branch name before the slash is not considered as part of the branch name. My colleague pointed out that this is the documented behavior of Azure Devops:

Git repo branch or pull request: The last path segment in the ref. For example, in refs/heads/master this value is master. In refs/heads/feature/tools this value is tools.

I am not sure if this behavior is particularly useful: I want to checkout the branch, and need the branch name to include the slash. Also, If the part before the slash is stripped off, there might well be confusion about the actual path, as the name could be ambiguous.

I need the branch name including the slash. Is there any simple way to get it? Do I always have to work with the full ref in order to be on the safe side?

like image 841
hey Avatar asked Jan 28 '20 20:01

hey


People also ask

How do I pull a branch from Azure DevOps?

From the Pull Requests view, select New Pull Request. Select the source and target branches, enter a title and optional description, and select Create. After the PR is created, select Open in browser to open the new PR in the Azure DevOps web portal.

How do you tag a branch in Azure?

Select Create Tag from the Tags view in the web portal to create a new annotated tag. Specify a Name, select the branch to Tag from, enter a Description (required since you are creating an annotated tag), and select Create. The new tag is displayed in the tag list.

How do I find my Azure DevOps branches?

Select the settings button in your project to open the project administration page. Select Version Control. Select your Git repository. Your branches are displayed under your repo.


1 Answers

I always use Build.SourceBranch in my scripts. Just assign it to a new variable and remove refs/heads/ from the start. I use only for CI and PR:

  1. For CI. I use Build.SourceBranch variable without refs/heads. I work with PowerShell:

$branchSource = "$(Build.SourceBranch)"
$branchSourcePath = $branchSource -replace "refs/heads/", ""

  1. For PRs. I use System.PullRequest.SourceBranch variable without refs/heads because Build.SourceBranch contains the path to the remote PR. The replacement is the same as in the first option just use the right variable.

$branchSource = "$(System.PullRequest.SourceBranch)"
$branchSourcePath = $branchSource -replace "refs/heads/", ""

like image 169
Shamrai Aleksander Avatar answered Oct 09 '22 03:10

Shamrai Aleksander