yipo
8/19/2019 - 2:49 PM

yaml-diff.md

Properly diff YAML while version controlling (by Git)

  • Why: Inconsistent key order messes up the diff result when version controlling YAML files.
  • Platform: Windows

Any tool to sort the keys?

Setup the command

  • node.js
    • Option 'Add to PATH' is not required. (Just embedded in the script later on)
  • npm install -g yml-sorter
    • Installed under %AppData%\npm.

Access the command from Git Bash

  • ~/bin (/c/Users/<user-name>/bin) is built in the PATH environment variable of Git Bash.
  • Thus, place the following script in that directory, and named as yaml-sort.bat.
    • %ProgramFiles%\nodejs for node.
    • %AppData%\npm for yml-sorter.
@path %ProgramFiles%\nodejs;%AppData%\npm
@yml-sorter -di "%~1" 2> nul

Config your Git

  • git config diff.yaml.textconv yaml-sort.bat
  • In .gitattribute: *.yml diff=yaml

Check Customizing Git - Git Attributes for more details.