26

Ideal Situation

Often while working on a Symfony2 project I will spot something I want to change in one of my dependencies. If I could find a way to simply change it in vendor and then easily push the changes as a pull request then I would probably contribute more often (rather than overriding the part with a local child bundle).

The Problem

I can't change a vendor directory without composer freaking out on the next update. If I submit a pull request then it may take quite some time before I can actually use the code in vendors, which is actually a deterrent from contributing my new functionality.

How I do it now

The way I typically contribute to a bundle is to make a fork, put the fork in a barebones symfony standard-edition app, make the change and then submit a pull request.

Put fork in composer.json?

The only solution I can think of, is removing the packagist dependency of the bundle I am editing, and then including my fork with composer (as a package) from github. That way I get my code immediately and can still contribute.

Is this the only solution? How do you do it?

Any tips/advice for contributing to a bundle while working on a different project at the same time would be appreciated!

j0k
  • 22,600
  • 28
  • 79
  • 90
Chris Tickner
  • 2,008
  • 2
  • 20
  • 24
  • 2
    I think you have it right. There's 2 parts: submitting the patch and using the patch. I usually fork/branch the package in my working project to make the fix. Commit it and push it to my own repo. Then if i want to keep using that fix i'll add that reference to my fork/branch to composer. – MDrollette Nov 10 '12 at 19:22
  • We use the same approach as @MDrollette. First we use the standard packages. If something needs fixing (like the DBAL QueryBuilder - https://github.com/doctrine/dbal/pull/175), we fork it, use our fork instead of the standard one, modify code, write tests and open a pull request hoping that the fix gets included. – wimvds Nov 11 '12 at 10:23

4 Answers4

55

Nah... this is broken.

I've tried the official way to include a fork, here's an example (original:kitano, fork: jstoeffler) of the composer.json :

(For those who are in a hurry: THIS DOESNT WORK)

"repositories": [
//...
    {
        "type": "vcs",
        "url": "https://github.com/jstoeffler/KitanoConnectionBundle",

    },
//...
],

It keeps using the original bundle. Don't know what the problem is, and I don't get how everything works, but here's how I successfully add a fork to a project.

"repositories": [
//...
    {
        "type": "package",
        "package": {
            "name": "kitano/connection-bundle",
            "version": "dev-master",
            "source": {
                "url": "https://github.com/jstoeffler/KitanoConnectionBundle.git",
                "type": "git",
                "reference": "master"
            },
            "autoload": {
                "classmap": [""]
            }
        }
    },
//...
],
Julien
  • 9,312
  • 10
  • 63
  • 86
  • 1
    Thanks for this alternate method. – Chris Tickner Jan 16 '15 at 14:26
  • 10
    Just want to add that it is nice to have an alternate method, but **the official method does work** (I've just use it today with an up-to-date composer installation). I've just needed to append the *dev-* prefix in front of my forked branch name and it worked like a charm. – mTorres Jan 17 '15 at 11:03
  • 5
    @mTorres that did not work for me, unfortunately. Julien's did. – Hugh Guiney Sep 16 '15 at 08:50
  • 2
    From what Im understanding, if your branch is out of date with the master branch and that other modules require the up to date original, the official method doesn't work. – delmalki Feb 09 '16 at 01:07
  • 1
    Just to clarify for others (since this slipped me up), the `reference` value should be the name of your branch excluding the `dev-` prefix. – Pete Sep 23 '16 at 23:41
  • The original method works for me when I "tagged" the forked version and incremented the version - so I could specifically target it with my composer. i.e. from 1.1.0 to 1.2.0 – Laurence Jan 20 '17 at 01:30
  • The "For those who are in a hurry" method works fine for me when also specifying that you want the `dev-master` version. This pulls in the fork. – BusterX Jan 25 '18 at 08:52
  • Unfortunately this just clones the repository but doesn't require the compose.json dependencies in that repository – Vaishnav Mhetre Mar 12 '19 at 18:25
  • if the original repo already has a branch with a `dev-`-prefix or if its a fork itself, this method is not working. instead, do an own fork of the repo, create a new branch like "mynewbranch" from the branch you need, and require it with `dev-mynewbranch` – cari Aug 26 '19 at 11:55
8

[UPDATE: Answer Not Valid Anymore]

As pointed out in one of the comments, this answer is a couple years old and not correct anymore. See answers below for the correct way to proceed.

[Original answer below]

This is the approach recommended by Jordi Boggiano (@Seldaek), creator of composer. See from his talk at Symfony Live San Francisco earlier this year (at the 2 minutes mark): http://www.youtube.com/watch?list=PLo7mBDsRHu11ChvScWUE7MN1Qo5QVHQEz&feature=player_detailpage&v=P3NwF8RV1lY#t=120s

Khepin
  • 931
  • 9
  • 16
  • Thanks for sharing. This is the only way I've tried that retrieves the fork and works with dependencies. Rather than using the https form for the repository as shown in the video I use this form. It doesn't require a password- git@github.com:/.git The key to satisfying dependencies is inline aliasing. – cmc May 05 '15 at 18:41
  • outdated information, see below for correct answer for this specific issue as of Oct 2016 – Joey T Oct 26 '16 at 01:11
7

As of 2017 the proper way to do it is:

  1. Add your GitHub branch link to the repositories

    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/crimson-med/yii2-link-preview"
        }
    ],
    
  2. Add the source to the require of your composer.json

    "require": {
        "yii2mod/yii2-link-preview": "dev-master"
    },
    
rob006
  • 21,383
  • 5
  • 53
  • 74
Mederic
  • 1,949
  • 4
  • 19
  • 36
2

FYI, I just tried the very first option:

    "repositories": [{
    "type": "vcs",
    "url": "https://github.com/thujohn/twitter"
}],
"require": {
    "laravel/framework": "4.2.*",
    "thujohn/twitter": "dev-master",
    "anahkiasen/flickering": "^0.1.2",
    "fairholm/elasticquent": "dev-master",
    "facebook/php-sdk-v4" : "~5.0"
},

An it worked fine.

vagrant@dev:/var/www$ sudo php composer.phar update
Loading composer repositories with package information                                                                                                                                                         Updating dependencies (including require-dev)         
  - Removing thujohn/twitter (2.0.4)
  - Installing thujohn/twitter (dev-master 7a92118)
    Downloading: 100%         

Writing lock file
Generating autoload files
> php artisan clear-compiled
> php artisan optimize
Generating optimized class loader

I just needed to specify the "master" branch name as "dev-master".

Eduardo Chongkan
  • 752
  • 7
  • 12