Home > database >  Detect diff to specific yaml file field between two branches with Git and Bash
Detect diff to specific yaml file field between two branches with Git and Bash

Time:09-13

I have a yaml file that has a field data.version which I want to detect changes from main branch.\

The yaml looks something like this:

# ...
data:
  version: 1.2.3
# ...

There are more fields which are not relevant for this purpose.

I am writing a GitLab-CI script where I have my current commit checked out.
I am able to see the changes in general by using this command:\

git fetch origin main
git diff origin/main HEAD -- my_yaml_file

But this does not allow me to detect changes to this specific field...

Is there a way to get and parse the original file from main branch?

Note that I am trying to avoid checking out the entire repository on a temp directory just for that purpose :)

CodePudding user response:

You can get a specific version of a file with git show

git show origin/main:my_yaml_file

After that you need to parse the yaml file to get the diff

For example using yq

git show origin/main:my_yaml_file|yq eval ".data.version"

Will give out the value of data.version

  • Related