I am working on proof of concept for my project. One of the requirement is to get name of the file latest committed to the main branch. I am calling below command from Azure pipeline.
$a = git diff-tree --no-commit-id --name-only -r $(Build.SourceVersion)
It works fine when I am directly committing file to the main branch.
When I create another branch and add file to it and then merge it with main branch and when i try the same command it does not fetch me any value. Alternatively I also tried git diff-tree --no-commit-id --name-only -r HEAD
, but this command don't return any value when is file is added to main branch during merge process. Any pointer or help will be appreciated.
CodePudding user response:
You can get the latest commit hash, from the main
branch, regardless of whether that's a merge or not using this command:
git log -n 1 --pretty=format:"%H" main
You should then be able to plumb this in to your existing command like:
git diff-tree --no-commit-id --name-only -r $(git log -n 1 --pretty=format:"%H" main)
P.S. you can restrict it to merges, or non-merges by adding the --merge
or --no-merges
switches, respectively.