-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add support for git repos #5
base: master
Are you sure you want to change the base?
Changes from 2 commits
b0b1240
ed47064
c339ff8
12abc9f
e8aab4f
3cfb161
6f94fa2
8ae9f60
a943cd0
1a2da1b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace LastCall\DownloadsPlugin\Handler; | ||
|
||
use Composer\Composer; | ||
use Composer\Config; | ||
use Composer\IO\IOInterface; | ||
use Composer\Util\Git; | ||
use Composer\Util\ProcessExecutor; | ||
use Composer\Util\Filesystem; | ||
use React\Promise\PromiseInterface; | ||
|
||
class GitHandler extends BaseHandler | ||
{ | ||
const TMP_PREFIX = '.composer-extra-tmp-'; | ||
|
||
public function createSubpackage() | ||
{ | ||
$pkg = parent::createSubpackage(); | ||
$pkg->setDistType('git'); | ||
return $pkg; | ||
} | ||
|
||
public function getTrackingFile() | ||
{ | ||
$file = basename($this->extraFile['id']) . '-' . md5($this->extraFile['id']) . '.json'; | ||
return | ||
dirname($this->getTargetPath()) . | ||
DIRECTORY_SEPARATOR . self::DOT_DIR . | ||
DIRECTORY_SEPARATOR . $file; | ||
} | ||
|
||
/** | ||
* @param Composer $composer | ||
* @param IOInterface $io | ||
*/ | ||
public function download(Composer $composer, IOInterface $io) { | ||
$urlAndVersion = $this->getSubpackage()->getDistUrl(); | ||
$config = $composer->getConfig(); | ||
$wd = $this->getTargetPath(); | ||
$process = new ProcessExecutor($io); | ||
$cfs = new Filesystem(); | ||
$git = new Git($io, $config, $process, $cfs); | ||
if (file_exists($wd)) { | ||
$gitCallable = static function ($urlAndVersion): string { | ||
$parts = explode('@', $urlAndVersion); | ||
$url = $parts[0]; | ||
if (count($parts) > 1) { | ||
$version = $parts[1]; | ||
} | ||
else { | ||
$version = 'master'; | ||
} | ||
return sprintf('git remote update --prune origin && git checkout %s', ProcessExecutor::escape($version)); | ||
}; | ||
} | ||
else { | ||
if (!file_exists($wd)) { | ||
mkdir($wd); | ||
} | ||
$gitCallable = static function ($urlAndVersion): string { | ||
print_r($urlAndVersion); | ||
$parts = explode('@', $urlAndVersion); | ||
$url = $parts[0]; | ||
if (count($parts) > 1) { | ||
$version = $parts[1]; | ||
} | ||
else { | ||
$version = 'master'; | ||
} | ||
return sprintf('git init && git remote add origin %s && git remote update --prune origin && git checkout %s', ProcessExecutor::escape($url), ProcessExecutor::escape($version)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For both L71 and L54 -- I think
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup - makes sense. |
||
}; | ||
} | ||
$git->runCommand($gitCallable, $urlAndVersion, $wd); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't tested, but I see these two code-paths (based on
file_exists()
), and it makes me imagine the following use-case:composer.json
, addextra.downloads
definition with agit
URL.composer install
.composer.json
, editextra.downloads
and change thegit
commit/tag.composer install
.I guess the idea is that step 2 makes a new folder, and step 4 updates that same folder.
But what if that folder doesn't have any
.git
data? For example:composer.json
, addextra.downloads
definition with azip
URL.composer install
.composer.json
, editextra.downloads
definition to agit
URL.composer install
.At step 4, shouldn't we clear out the folder before doing any git operations?
(IIRC, this only matters for
extra.downloads
in the top-level folder. In other contexts, there's a higher levelrm -rf
.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback! And yes, I agree, I think adding git support here is a better way forward than adding the alternative.
I can work on extending the test coverage.
Regarding the
file_exists
code paths - I added it without much thought in the interest of speed and efficiency (if we already have the directory in place and it's checked out to the same commit, we move on very quickly). However, it does sacrifice correctness in the use case you provide (and perhaps others we haven't even thought of).I think, in the context of this extension, the
.composer-downloads/<id>-<hash>.json
is kinda like thecomposer.lock
file. So, oncomposer install
we could test for the existance of that file. If it doesn't exist and we have a folder, thanrm -rf
the folder and install from scratch. If it does exist, we could assume the contents properly describe the folder and rungit fetch
(if someone mucked with the folder git will return an error and that seems reasonable).On
composer update
we would need to comparecomposer.json
with.composer-downloads/<id>-<hash>.json
. If we are moving from zip to git, thenrm -rf
the folder and start from scratch. If the repo URL changes,rm -rf
and start from scratch, and otherwisegit fetch
.Having spelled all this out, I'm leaning towards your initial suggestion of just
rm -rf
. I'm not sure all the added complexity is worth the small efficiency gain. Open to writing the more complex approach though if you prefer.