Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force Composer to use https:// instead of git://?

I have something like this

"repositories": [
    {
        "type": "package",
        "package": {
            "name": "myrepo",
            "version": "dev-master",
            "source": {
                "url": "https://github.com/me/myrepo.git",
                "type": "git",
                "reference": "master"
            }
        }
    },

But when Composer pulls the repo, the remotes (origin and composer) in .git/config are set up as git://github.com/me/myrepo.git.

[remote "origin"]
    url = git://github.com/me/myrepo.git
    fetch = +refs/heads/*:refs/remotes/origin/*
    pushurl = [email protected]:me/myrepo.git
[branch "master"]
    remote = composer
    merge = refs/heads/master
[remote "composer"]
    url = git://github.com/me/myrepo.git
    fetch = +refs/heads/*:refs/remotes/composer/*

I can't push to it, because Github doesn't work with git://. I have to manually change this to https:// and then push. I specified https:// in the URL, but why isn't this respected?

like image 355
Patrick Yan Avatar asked Sep 05 '13 13:09

Patrick Yan


1 Answers

The github-protocols option only works with GitHub, but there's another way to solve the problem that is not dependent on using GitHub for your Git server.

You just tell Composer to use a different URL, the same way you would if you had forked a package and you wanted to make sure Composer used your fork instead of the default package.

Here's an example where I use my own fork for one package, and I force the protocol in the URL to http instead of git for another:

"repositories": [{
    "type": "vcs",
    "url": "https://github.com/brandondrew/laravel4-PHPExcel"
},{
    "type": "vcs",
    "url": "http://git.code.sf.net/p/tcpdf/code"
}],

In the case of Laravel4-PHPExcel, I had fixed a bug early on, and needed my fix while I waited for the maintainer to accept my pull request. (As an aside, he has since made a ton of improvements to his code, so I'm dropping the use of my fork, but I'm showing it here since it's useful as an example, even if my code is no longer useful in practice.)

In the case of TCPDF, they are the only package that my app uses that specifies the Git protocol, so it seemed ridiculous to beg and plead with the security team at my client location to open up port 9418 when I could just use HTTP instead, which (in my opinion) they should have used to begin with. All I changed in the URL is replacing git with http, but it works just like any other user-specified URL, allowing you to specify the URL that Composer uses for a given package.


UPDATE:

Just for the sake of being complete, I feel I should mention that you can also solve the problem at the Git level as well. In the case of the app where I faced this issue, I felt that doing it in Composer was more appropriate, but your situation may differ, and so may your views on what is the best approach for the situation.

The Git approach can be made much more global, which you may see as an advantage (or not). The simple case is to tell Git to substitute one URL for another:

git config --global url."http://git.code.sf.net/p/tcpdf/code".insteadOf "git://git.code.sf.net/p/tcpdf/code"

But you can also tell Git to always replace 'git://' with 'https://':

git config --global url."https://".insteadOf "git://"

In the examples I put it in the user's global config file, but you can replace --global with --local to put it in the repository's config file (or --system to put it in the system's config file).

like image 140
iconoclast Avatar answered Sep 28 '22 20:09

iconoclast