Search code examples
composer-phpphpstormdiff

How To Easily Show A Diff Between Two Versions Of A Composer Package


Is there an easy way to show a diff between two different versions of one Composer package? Of course, I could manually or semi-automatically download the two versions and then manually run a diff on them. However, it would be quite handy to have a command like

composer diff namespace/module 1.0.0 1.0.1

This would show a diff between the versions 1.0.0 and 1.0.1 of package namespace/module, so that I can easily review what has changed.

Is there any smart way to do that?

It would even be nicer if I could see the diff in PhpStorm.


Solution

  • Assuming you are in the project directory, JQ must be installed.

    It installs both package versions in temp folders and compares them.

    Usage: ./composer-diff.sh foo/bar 1.0.0 2.0.0

    #!/bin/bash
    
    PACKAGE=$1 # namespace/module
    OLD=$2
    NEW=$3
    
    PROJECT=`pwd`
    
    TMPBASE=/tmp/$$
    
    mkdir -p $TMPBASE/$OLD
    mkdir -p $TMPBASE/$NEW
    
    jq '{repositories}' composer.json > $TMPBASE/$OLD/composer.json
    jq '{repositories}' composer.json > $TMPBASE/$NEW/composer.json
    cp auth.json $TMPBASE/$OLD
    cp auth.json $TMPBASE/$NEW
    
    cd $TMPBASE/$OLD
    composer require $PACKAGE $OLD
    
    cd $TMPBASE/$NEW
    composer require $PACKAGE $NEW
    
    set +x
    diff -ur $TMPBASE/$OLD/vendor/$PACKAGE $TMPBASE/$NEW/vendor/$PACKAGE > $PROJECT/composer-package.diff
    
    

    Now the resulting composer-package.diff file can be openend in any editor.