51

I've got some packages host on the Gitlab of my company. I want to request a specific version of these packages but each time I try, composer download the latest commit of the master branch.

composer.json :

{
   "name" : "MyProject",
   "require" : {
      "user/project" : "v0.5.0"
   },
   "type" : "project",
   "repositories" : [
      {
         "url" : "git@gitlab.XXXX.fr:user/project.git",
         "type" : "vcs"
      }
   ],
   "config" : {
      "vendor-dir" : "private/class"
   }
}

The structure of the repository of my package :

  • tag v0.5.0 : commit dd6ed3c8...
  • commit X,Y,Z
  • tag v0.7.0 : commit 15293ac6...
  • last commit f15600a1...

When I execute "composer install" :

Loading composer repositories with package information

Installing dependencies (including require-dev)

Analyzed 69 packages to resolve dependencies

Analyzed 67 rules to resolve dependencies

  • Installing user/project (dev-master f15600a)

    Cloning f15600a1

It downloads the last commit only.

How can I configure the composer.json file of my project to use a specific tag ?

Community
  • 1
  • 1
Airmanbzh
  • 615
  • 1
  • 5
  • 11

4 Answers4

70

How to require a specific Git tag?

Change the version requirement to dev-master, followed by a hash # and the Git tag name, e.g. v0.5.0, like so:

"require": {
    "vendor/package": "dev-master#v0.5.0"
}

How to require a specific Git commit?

Change the version requirement to dev-master, followed by a hash # and the Git commit reference, e.g. dd6ed3c8, like so:

"require": {
    "vendor/package": "dev-master#dd6ed3c8"
}

Referencing: https://getcomposer.org/doc/04-schema.md#package-links


Define your own package and set version and reference

An alternative to working with repositories of "type": "vcs" is to define a custom package "type": "package" inside repositories and work with a reference.

The reference is either a Git commit hash, or a tag or branch name, like origin/master.

This will tie the version to a specific commit reference, in this case dd6ed3c8.

"repositories": [
  # ...
  {
    "type": "package",
    "package": {
      "name": "vendor/package",
      "version": "v0.5.0",
      "source": {
        "url": "git@gitlab.server.com:vendor/project.git",
        "type": "git",
        "reference": "dd6ed3c8"
      }
    }
  }
]
Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
  • 11
    I already try this and each time it downloads the last commit. Not the requested tag/commit – Airmanbzh Jan 15 '16 at 08:30
  • 1
    I don't know why type vcs doesn't work out for you. I've added an example for defining your own package inside repositories. This allows to set a version and a reference. – Jens A. Koch Jan 15 '16 at 13:15
  • 1
    I tried your last proposition and still the same result. It clones the whole repository (and uses the last commit) I don't understand why this happens. Maybe I need to use something similar to packagist... – Airmanbzh Jan 15 '16 at 16:28
  • 3
    It seems that Gitlab repositories are a problem for Composer and that it will only use Github and Bitbucket's APIs to download the zip archive.While in Gitlab,it will not pick the reference up on a checkout, nor will it fetch a dist(archive of the source) - it will just git clone. – Jens A. Koch Jan 15 '16 at 17:02
  • 2
    Maybe this helps: https://github.com/wemakecustom/gitlab-composer Its a branch/tag indexer for Gitlab repositories. Untested. – Jens A. Koch Jan 15 '16 at 17:06
  • Thanks for your answer, I'll give a try to the branch/tag indexer :) – Airmanbzh Jan 18 '16 at 16:20
  • 1
    In my case, `dev-master#v0.5.0` didn't work but `v0.5.0` worked. – maliayas Jun 18 '20 at 20:11
31

Pull by git tag:

{
  "repositories": [
    {
      "type": "git",
      "url": "https://gitlab.xxx.com/some/repo.git"
    }
  ],
  "require": {
    "some/repo": "1.0.2"
  }
}

Pull by latest git commit:

{
  "repositories": [
    {
      "type": "git",
      "url": "https://gitlab.xxx.com/some/repo.git"
    }
  ],
  "require": {
    "some/repo": "dev-master"
  }
}

Pull by specific git commit:

{
  "repositories": [
    {
      "type": "git",
      "url": "https://gitlab.xxx.com/some/repo.git"
    }
  ],
  "require": {
    "some/repo": "dev-master#68696f39"
  }
}

Add repository via Composer Cli and pull latest git commit:

composer config repositories.repo git https://gitlab.xxx.com/some/repo.git
composer require "some/repo:dev-master"
composer update
TonyB
  • 423
  • 4
  • 6
11

To require a specific branch you can also just dev-branchname like this:

"require": {
    "user/project" : "dev-my-branch"
},
totas
  • 10,288
  • 6
  • 35
  • 32
  • Enough people saying `dev-master` made enough sense, but thank you for clarifying this easily with the whole `dev-` bit... :) – rabbitt May 05 '20 at 22:43
2

You can use a specific version hash after '#', for example:

require": {
    "user/project": "dev-master#31454f258f10329ae7c48763eb898a75c39e0a9f"
}

see: https://getcomposer.org/doc/04-schema.md#package-links

lstonon
  • 121
  • 3