ChatGPT解决这个技术问题 Extra ChatGPT

Composer require branch name

For example I want to require:

{
  "repositories": [
    {
      "type": "git",
      "url": "https://github.com/google/google-api-php-client.git"
    }
  ],

  "require": {
    "google/apiclient": "v1-master"
  }
}

In this example I try require google/apiclient on branch v1-master. I get error:

  [UnexpectedValueException]                                                        
  Could not parse version constraint v1-master: Invalid version string "v1-master"

T
Tomas Votruba

You need to prefix all dev branches (= non tagged) by dev-.

To install the branch you need, use:

composer require google/apiclient:dev-v1-master

See composer docs.


Handy info. Thanks!
I am trying that on a windows machine but unable to find correct command to run composer to download a package from the git repository. So you please help on this?
Add the 'dev-' prefix is only in the composer command. Do you don't need to change anything in repository branches name
Can somebody explain why 'dev-' is needed in branch name to checkout branches which are not in development?
It's just composer convention. See "If you alias a non-comparable version (such as dev-develop) dev- must prefix the branch name. You may also alias a comparable version (i.e. start with numbers, and end with .x-dev), but only as a more specific version. For example, 1.x-dev could be aliased as 1.2.x-dev." - getcomposer.org/doc/articles/aliases.md#branch-alias
f
fico7489

this will work :

{
  "repositories": [
    {
      "type": "git",
      "url": "https://github.com/google/google-api-php-client.git"
    }
  ],

  "require": {
    "google/apiclient": "dev-BRANCH_NAME"
  }
}

so pattern is "dev-*", if you branch name is "bug-fix" then "dev-bug-fix"

with command line :

composer require google/apiclient:dev-BRANCH_NAME

If you forked the main repo do not change vendor name in the require part. Fork: "url": "https://github.com/your-username/google-api-php-client.git" but require stays: google vendor.
R
Remigiusz Samborski

I was trying to the same for a different Google repository which contains several packages and it took me some time to figure it out. Therefore I am sharing my solution below.

My goal is to pull latest google/cloud-compute from https://github.com/googleapis/google-cloud-php.git within master branch.

Following steps worked for me:

Clone the repository

git clone https://github.com/googleapis/google-cloud-php.git google-cloud-php

Set composer.json to use the right package from local folder:

{
    "repositories": [
        {
            "type": "path",
            "url": "/Users/USERNAME/projects/google-cloud-php/Compute"
        }
    ],

    "require": {
        "google/cloud-compute": "dev-master"
    }
}

Please note that in step 2 the url is pointing to the Compute subfolder where the actual google/cloud-compute package exists.

My solution could be easily tweaked for any branch, you would just need to git checkout the appropriate branch in step 1 and then change 'dev-master' to 'dev-YOUR_BRANCH' in step 2.