Home > Enterprise >  On Azure DevOps check if branch is only ahead by merge commits
On Azure DevOps check if branch is only ahead by merge commits

Time:10-14

The Behind | Ahead on the ADO branches view is very useful to see at a glance, whether there are any features implemented in develop that yet need to be merged to main. When merging develop to main, we use merge-commits (no fast-forward) to allow GitVersion to automatically bump the minor SemVer-version of our tools. Thus main is always ahead of develop by an ever increasing number of merge-commits. Those merge-commits however never contain any file changes.

I'd like to also see at a glance, whether there are any file-changes that need to be merged back from main to develop (e.g. hotfixes).

How can I assure that developers remember to merge back hotfixes from main to develop?

enter image description here

CodePudding user response:

I created a ticket on the Visual Studio developer community for this.

As a workaround, I currently use a nightly pipeline to check, whether main is ahead of develop only by merge-commits. This way, developers remember to merge hotfixes back to develop.

schedules:
- cron: "45 1 * * Mon,Tue,Wed,Thu,Fri"
  branches:
    include:
    - main

steps:
- task: PowerShell@2
  displayName: Check develop ahead of main
    inputs:
      filePath: check-develop-ahead-of-main.ps1
# check-develop-ahead-of-main.ps1

$commitHashes = git rev-list --left-right origin/develop..origin/main | %{ $_.Substring(1, 6)};

foreach ($commitHash in $commitHashes) {
    $parentCommits = (git show --no-patch --format="%P" $commitHash).Split(" ");
    $isMergeCommit = $parentCommits.Count -ne 1;

    if (!$isMergeCommit) {
        throw "There is at least one commit ($($commitHash)) that is on the main branch but NOT on the develop branch. Please create a pull request from main to develop."
    }
}

Write-Host "The main branch is only ahead of the develop branch by merge commits ($($commitHashes))." -ForegroundColor Green

CodePudding user response:

I'd like to also see at a glance, whether there are any file-changes that need to be merged back from main to develop (e.g. hotfixes).

One way to accomplish this is with this single command:

git diff origin/develop...origin/main --name-only

Note there are 3 dots there, which (specifically for git diff) means, "Show me the changes that are only on main."

If you get no output, then you have nothing only on main. If you get one or more files listed in the output, then you have some changes that need to be merged back into develop.

Note the --name-only flag just lists the files instead of the full diff of those files, but it's completely optional. I think I would prefer that for your use case to just get a "yes or no" answer.

Side note about dots: The dot notation is kind of confusing in Git. With git diff, if you use 2 dots instead of 3 dots, you would get the full diff of the two branches. This is sort of the opposite of what 2 and 3 dots means with git log. In git log, two dots is "show me only new commits on the right side", whereas three dots is "show me new commits on either side".

  • Related