简体   繁体   English

使用 PHP composer 克隆 git repo

[英]Use PHP composer to clone git repo

I'm trying to use composer to automatically clone a git repository from github that isn't in packagist but it's not working and I can't figure out what am I doing wrong.我正在尝试使用composer从 github 自动克隆 git 存储库,该存储库不在packagegist中,但它不起作用,我不知道我做错了什么。

I think I have to include it among "repositories" like so:我认为我必须将它包含在“存储库”中,如下所示:

"repositories": [
    {
        "url": "https://github.com/l3pp4rd/DoctrineExtensions.git",
        "type": "git"
    }
],

and then probably list it in "require" section.然后可能在“要求”部分列出它。 It should be similar to this example but it doesn't work.它应该类似于此示例,但它不起作用。 It just gives this error:它只是给出了这个错误:

Your requirements could not be resolved to an installable set of packages.您的要求无法解决为一组可安装的软件包。

Have anyone tried to do something like this already?有没有人尝试过做这样的事情?

That package in fact is available through packagist .该软件包实际上可以通过 packagist 获得 You don't need a custom repository definition in this case.在这种情况下,您不需要自定义存储库定义。 Just make sure you add a require (which is always needed) with a matching version constraint.只需确保添加具有匹配版本约束的require (始终需要)。

In general, if a package is available on packagist, do not add a VCS repo.一般而言,如果 packagist 上有可用的包,则不要添加 VCS 存储库。 It will just slow things down.它只会减慢速度。


For packages that are not available via packagist, use a VCS (or git) repository, as shown in your question.对于无法通过 packagist 获得的软件包,请使用VCS (或 git)存储库,如您的问题所示。 When you do, make sure that:这样做时,请确保:

  • The "repositories" field is specified in the root composer.json (it's a root-only field, repository definitions from required packages are ignored) “repositories”字段在根 composer.json 中指定(它是一个仅限根的字段,来自所需包的存储库定义被忽略)
  • The repositories definition points to a valid VCS repo存储库定义指向有效的 VCS 存储库
  • If the type is "git" instead of "vcs" (as in your question), make sure it is in fact a git repo如果类型是“git”而不是“vcs”(如您的问题),请确保它实际上是一个 git repo
  • You have a require for the package in question您对相关软件包有require
  • The constraint in the require matches the versions provided by the VCS repo. require中的约束与 VCS 存储库提供的版本匹配。 You can use composer show <packagename> to find the available versions.您可以使用composer show <packagename>来查找可用版本。 In this case ~2.3 would be a good option.在这种情况下, ~2.3将是一个不错的选择。
  • The name in the require matches the name in the remote composer.json . require中的名称与远程composer.json中的名称匹配。 In this case, it is gedmo/doctrine-extensions .在这种情况下,它是gedmo/doctrine-extensions

Here is a sample composer.json that installs the same package via a VCS repo:这是通过 VCS 存储库安装相同软件包的示例composer.json

{
    "repositories": [
        {
            "url": "https://github.com/l3pp4rd/DoctrineExtensions.git",
            "type": "git"
        }
    ],
    "require": {
        "gedmo/doctrine-extensions": "~2.3"
    }
}

The VCS repo docs explain all of this quite well. VCS repo 文档很好地解释了所有这些。


If there is a git (or other VCS) repository with a composer.json available, do not use a "package" repo.如果有可用的composer.json的 git(或其他 VCS)存储库,请不要使用“包”存储库。 Package repos require you to provide all of the metadata in the definition and will completely ignore any composer.json present in the provided dist and source.包 repos 要求您提供定义中的所有元数据,并且将完全忽略提供的 dist 和源中存在的任何composer.json They also have additional limitations, such as not allowing for proper updates in most cases.它们还有其他限制,例如在大多数情况下不允许进行适当的更新。

Avoid package repos ( see also the docs ).避免包回购(另见文档)。

At the time of writing in 2013, this was one way to do it.在 2013 年撰写本文时,这是一种方法。 Composer has added support for better ways: See @igorw 's answer Composer 增加了对更好方法的支持:见@igorw 的回答

DO YOU HAVE A REPOSITORY?你有存储库吗?

Git, Mercurial and SVN is supported by Composer. Composer 支持 Git、Mercurial 和 SVN。

DO YOU HAVE WRITE ACCESS TO THE REPOSITORY?你有对存储库的写访问权限吗?

Yes?是的?

DOES THE REPOSITORY HAVE A composer.json FILE存储库是否有composer.json文件

If you have a repository you can write to: Add a composer.json file, or fix the existing one, and DON'T use the solution below.如果你有一个存储库,你可以写入:添加一个composer.json文件,或修复现有的文件,不要使用下面的解决方案。

Go to @igorw 's answer转到@igorw 的回答

ONLY USE THIS IF YOU DON'T HAVE A REPOSITORY仅当您没有存储库时才使用它
OR IF THE REPOSITORY DOES NOT HAVE A composer.json AND YOU CANNOT ADD IT或者如果存储库没有composer.json并且您无法添加它

This will override everything that Composer may be able to read from the original repository's composer.json , including the dependencies of the package and the autoloading.这将覆盖 Composer 可能能够从原始存储库的composer.json读取的所有内容,包括包的依赖项和自动加载。

Using the package type will transfer the burden of correctly defining everything onto you.使用package类型会将正确定义所有内容的负担转移到您身上。 The easier way is to have a composer.json file in the repository, and just use it.更简单的方法是在存储库中有一个composer.json文件,然后直接使用它。

This solution really only is for the rare cases where you have an abandoned ZIP download that you cannot alter, or a repository you can only read, but it isn't maintained anymore.此解决方案实际上仅适用于您拥有无法更改的废弃 ZIP 下载或只能读取但不再维护的存储库的极少数情况。

"repositories": [
    {
        "type":"package",
        "package": {
          "name": "l3pp4rd/doctrine-extensions",
          "version":"master",
          "source": {
              "url": "https://github.com/l3pp4rd/DoctrineExtensions.git",
              "type": "git",
              "reference":"master"
            }
        }
    }
],
"require": {
    "l3pp4rd/doctrine-extensions": "master"
}

You can include git repository to composer.json like this:您可以像这样将 git 存储库包含到 composer.json 中:

"repositories": [
{
    "type": "package",
    "package": {
        "name": "example-package-name", //give package name to anything, must be unique
        "version": "1.0",
        "source": {
            "url": "https://github.com/example-package-name.git", //git url
            "type": "git",
            "reference": "master" //git branch-name
        }
    }
}],
"require" : {
  "example-package-name": "1.0"
}

Just tell composer to use source if available:如果可用,只需告诉作曲家使用源代码:

composer update --prefer-source

Or:或者:

composer install --prefer-source

Then you will get packages as cloned repositories instead of extracted tarballs, so you can make some changes and commit them back.然后您将获得作为克隆存储库的包而不是提取的 tarball,因此您可以进行一些更改并将它们提交回来。 Of course, assuming you have write/push permissions to the repository and Composer knows about project's repository.当然,假设您对存储库具有写入/推送权限并且 Composer 知道项目的存储库。

Disclaimer: I think I may answered a little bit different question, but this was what I was looking for when I found this question, so I hope it will be useful to others as well.免责声明:我想我可能回答了一个有点不同的问题,但这是我发现这个问题时所寻找的,所以我希望它对其他人也有用。

If Composer does not know, where the project's repository is, or the project does not have proper composer.json, situation is a bit more complicated, but others answered such scenarios already.如果 Composer 不知道项目的存储库在哪里,或者项目没有正确的 composer.json,情况会更复杂一些,但其他人已经回答了这种情况。

I was encountering the following error: The requested package my-foo/bar could not be found in any version, there may be a typo in the package name.我遇到了以下错误: The requested package my-foo/bar could not be found in any version, there may be a typo in the package name.

If you're forking another repo to make your own changes you will end up with a new repository.如果您要分叉另一个存储库以进行自己的更改,您最终将获得一个新存储库。

Eg:例如:

https://github.com/foo/bar.git
=>
https://github.com/my-foo/bar.git

The new url will need to go into your repositories section of your composer.json.新 url 需要进入 composer.json 的存储库部分。

Remember if you want refer to your fork as my-foo/bar in your require section, you will have to rename the package in the composer.json file inside of your new repo.请记住,如果您想在 require 部分将您的 fork 引用为my-foo/bar ,则必须在新存储库内的composer.json文件中重命名该包。

{
    "name":         "foo/bar",

=>

{
    "name":         "my-foo/bar",

If you've just forked the easiest way to do this is edit it right inside github.如果你刚刚分叉,最简单的方法就是在 github 中编辑它。

In my case, I use Symfony2.3.x and the minimum-stability parameter is by default "stable" (which is good).就我而言,我使用 Symfony2.3.​​x 并且最小稳定性参数默认为“稳定”(这很好)。 I wanted to import a repo not in packagist but had the same issue "Your requirements could not be resolved to an installable set of packages.".我想导入一个不在 packagist 中的存储库,但遇到了同样的问题“您的要求无法解析为一组可安装的软件包。”。 It appeared that the composer.json in the repo I tried to import use a minimum-stability "dev".我尝试导入的 repo 中的 composer.json 似乎使用了最低稳定性“dev”。

So to resolve this issue, don't forget to verify the minimum-stability .所以要解决这个问题,不要忘记验证minimum-stability I solved it by requiring a dev-master version instead of master as stated in this post .我通过要求解决它dev-master版本,而不是master在本说明

If you want to use a composer.json from GitHub you would look at this example (under the VCS section).如果您想使用来自 GitHub 的composer.json ,您可以查看此示例(在 VCS 部分下)。

The package section is for packages that do not have the composer.json . package 部分用于没有composer.json However, you didn't follow that example as well or it would also have worked.但是,您也没有遵循该示例,否则它也会起作用。 Do read what it says about package repositories:请阅读它关于包存储库的内容:

Basically, you define the same information that is included in the composer repository's packages.json , but only for a single package.基本上,您定义包含在 composer 存储库的packages.json的相同信息,但仅适用于单个包。 Again, the minimum required fields are name, version, and either of dist or source.同样,最少需要的字段是名称、版本以及 dist 或 source。

I try to join the solutions mention here as there are some important points to it needed to list.我尝试加入这里提到的解决方案,因为它需要列出一些要点。

  1. As mentioned in the @igorw's answer the URL to repository must be in that case specified in the composer.json file, however since in both cases the composer.json must exist (unlike the 2nd way @Mike Graf) publishing it on the Packagist is not that much different (furthermore also Github currently provides packages services as npm packages), only difference instead of literally inputting the URL at the packagist interface once being signed up.正如@igorw 的回答中提到的,在这种情况下,存储库的 URL 必须在 composer.json 文件中指定,但是因为在这两种情况下,composer.json 都必须存在(与@Mike Graf 的第二种方式不同)将它发布到 Packagist 上是没有太大的不同(此外,Github 目前也提供包服务作为 npm 包),唯一的区别是注册后在 packagist 界面上逐字输入 URL。

  2. Moreover, it has a shortcoming that it cannot rely on an external library that uses this approach as recursive repository definitions do not work in Composer.此外,它有一个缺点,它不能依赖使用这种方法的外部库,因为递归存储库定义在 Composer 中不起作用。 Furthermore, due to it, there seems to be a "bug" upon it, since the recursive definition failed at the dependency, respecifying the repositories explicitly in the root does not seem to be enough but also all the dependencies from the packages would have to be respecified.此外,由于它,似乎有一个“错误”,因为递归定义在依赖项上失败,在根中明确地重新指定存储库似乎还不够,而且包中的所有依赖项都必须被重新指定。

With a composer file (answered Oct 18 '12 at 15:13 igorw)使用作曲家文件(2012 年 10 月 18 日 15:13 igorw 回答)

{
    "repositories": [
        {
            "url": "https://github.com/l3pp4rd/DoctrineExtensions.git",
            "type": "git"
        }
    ],
    "require": {
        "gedmo/doctrine-extensions": "~2.3"
    }
}

Without a composer file (answered Jan 23 '13 at 17:28 Mike Graf)没有作曲家文件(Mike Graf 于 13 年 1 月 23 日 17:28 回答)

"repositories": [
    {
        "type":"package",
        "package": {
          "name": "l3pp4rd/doctrine-extensions",
          "version":"master",
          "source": {
              "url": "https://github.com/l3pp4rd/DoctrineExtensions.git",
              "type": "git",
              "reference":"master"
            }
        }
    }
],
"require": {
    "l3pp4rd/doctrine-extensions": "master"
}

https://stackoverflow.com/a/14485706/11716408 https://stackoverflow.com/a/14485706/11716408

{
    "repositories": [
        {
            "type":"package",
            "package": {
              "name": "tiagof2/materializecss-laravel-pagination",
              "version":"v1.0.3",
              "source": {
                  "url": "https://github.com/tiagofrancafernandes/materializecss-laravel-pagination.git",
                  "type": "git",
                  "reference":"v1.0.3"
                }
            }
        }
    ],
    "require": {
        "tiagof2/materializecss-laravel-pagination": "v1.0.3"
    },
}
composer update

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM