forked from LastCallMedia/ComposerExtraFiles
-
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
Open
jmcclelland
wants to merge
10
commits into
civicrm:master
Choose a base branch
from
progressivetech:git-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b0b1240
add support for git repos
jmcclelland ed47064
more sensible way to get the URL.
jmcclelland c339ff8
fix deprecation warnings.
jmcclelland 12abc9f
always purge download directory.
jmcclelland e8aab4f
update documentation.
jmcclelland 3cfb161
add test for git type.
jmcclelland 6f94fa2
use composer functions when available.
jmcclelland 8ae9f60
specifying depth limits the commits we can checkout
jmcclelland a943cd0
remove extra argument to sprintf
jmcclelland 1a2da1b
we have to get all the branches
jmcclelland File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?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)) { | ||
$cfs->removeDirectory($wd); | ||
} | ||
// Make the directory recursively. | ||
$cfs->ensureDirectoryExists($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 init && git fetch %s "+refs/heads/*:refs/remotes/origin/*" && git checkout %s', | ||
ProcessExecutor::escape($url), | ||
ProcessExecutor::escape($version) | ||
); | ||
}; | ||
$git->runCommand($gitCallable, $urlAndVersion, $wd); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.