Skip to content

Instantly share code, notes, and snippets.

@AydinHassan
Last active February 15, 2024 08:18
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save AydinHassan/6ed0bf2219ea0f122402 to your computer and use it in GitHub Desktop.
Save AydinHassan/6ed0bf2219ea0f122402 to your computer and use it in GitHub Desktop.
Add a new version of Magento to a repository compatible with https://github.com/AydinHassan/magento-core-composer-installer
<?php
/**
* Script to manage to version of Magento in a git mirror
* php $argv[0] core-repository new-version-of-magento
*
* It will look for Mage.php in new-version-of-magento and parse the version
* and edition from it.
*
* It will copy the code, remove the old files, create new branches if necessary
* and tag the release.
*
* Branches are major.minor
*
* so 1.9.0.0, 1.9.0.1, 1.9.10 all go in the 1.9 branch
* 1.10.0.0 would cause a new 1.10 branch to be created.
*
* 1.10 will be branched of 1.9 so you can diff them easily
*
* @author Aydin Hassan <aydin@hotmail.co.uk>
*/
function cp($from, $to) {
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($from, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $item) {
if ($item->isDir()) {
mkdir($to . "/" . $iterator->getSubPathName());
} else {
copy($item, $to . "/" . $iterator->getSubPathName());
}
}
}
function rmOldMagento()
{
exec("rm -rf *");
exec("rm -rf *.ht");
}
function commitAndTag($version, $edition)
{
exec("git add --all");
exec("git commit -m 'Magento $edition $version'");
exec("git tag $version");
}
function parseCore($location) {
if (!file_exists(rtrim($location, "/") . "/app/Mage.php")) {
die("$location does not look like a Magento code base");
}
require_once rtrim($location, "/") . "/app/Mage.php";
$edition = Mage::getEdition();
$version = Mage::getVersionInfo();
$versionStr = Mage::getVersion();
return array('edition' => $edition, 'version' => $version, 'versionStr' => $versionStr);
}
if (!isset($argv[1]) || !isset($argv[2])) {
die("{$argv[0]} core-repo magento-code-location");
}
$coreRepoLocation = $argv[1];
$coreLocation = $argv[2];
$packageName = isset($argv[3]) ? $argv[3] : 'magento/magento';
if (!file_exists($coreRepoLocation)) {
die("Core Repo Location is invalid");
}
$info = parseCore($coreLocation);
$coreRepoLocation = realpath($coreRepoLocation);
chdir($coreRepoLocation);
exec("git for-each-ref refs/heads/ --format='%(refname:short)'", $branches);
usort($branches, function ($a, $b) {
return version_compare($a, $b);
});
$composerFile = '{
"name": "' . $packageName . '",
"description": "Magento ' . $info['edition'] . '",
"type": "magento-core"
}';
$major = $info['version']['major'];
$minor = $info['version']['minor'];
$version = $info['versionStr'];
$branch = $major . "." . $minor;
if (!count($branches)) {
exec("git checkout -b $branch");
cp($coreLocation, $coreRepoLocation);
file_put_contents("composer.json", $composerFile);
commitAndTag($version, $info['edition']);
exit("Created branch: $branch and tagged: $version");
}
$earliestBranch = $branches[0];
if (version_compare($version, $earliestBranch, '<')) {
die("Adding previous releases not supported");
}
$foundBranch = false;
foreach ($branches as $b) {
if ($b === $branch) {
$foundBranch = true;
}
}
if ($foundBranch) {
exec("git checkout $branch");
rmOldMagento();
cp($coreLocation, $coreRepoLocation);
file_put_contents("composer.json", $composerFile);
commitAndTag($version, $info['edition']);
exit("Added to branch: $branch and tagged: $version");
}
$mostRecentVersion = end($branches);
if (version_compare($version, $mostRecentVersion, '<')) {
die("Adding new previous minors is unsupported");
}
exec("git checkout -b $branch $mostRecentVersion");
rmOldMagento();
cp($coreLocation, $coreRepoLocation);
file_put_contents("composer.json", $composerFile);
commitAndTag($version, $info['edition']);
exit("Created new branch: $branch and tagged: $version");
@Webmasterei
Copy link

Great Scripts. Thanks.
I had errors with this until I changed paths in command line to absolute. MAybes you should mention it.

@djallits
Copy link

For Magento Enterprise 1.9.1.1

php add-magento-version.php ~/Projects/ REDACTED /magento-enterprise ~/Projects/ REDACTED /1.9.1.1

...and the following error is the result.

PHP Fatal error:  Uncaught Error: Call to undefined method Mage::getEdition() in /Users/Dan/add-magento-version.php:56
Stack trace:
#0 /Users/Dan/add-magento-version.php(73): parseCore('/Users/Dan/Proj...')
#1 {main}
  thrown in /Users/Dan/add-magento-version.php on line 56

Fatal error: Uncaught Error: Call to undefined method Mage::getEdition() in /Users/Dan/add-magento-version.php:56
Stack trace:
#0 /Users/Dan/add-magento-version.php(73): parseCore('/Users/Dan/Proj...')
#1 {main}
  thrown in /Users/Dan/add-magento-version.php on line 56

@djallits
Copy link

djallits commented Feb 1, 2016

Just got back to this. A quick search returns that the getEdition method wasn't available until Magento Community 1.7 or EE 1.12.

@SageRiley2015
Copy link

This confused me

@SageRiley2015
Copy link

This confused me

@SageRiley2015
Copy link

This confused me

@SageRiley2015
Copy link

This confused me

@Webmasterei
Copy link

@SageRiley2015 obviously!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment