Skip to content

Commit 707d849

Browse files
authored
Add commands to reset local repositories
1 parent 6562e4e commit 707d849

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
},
9292
"scripts": {
9393
"drupal-scaffold": "DrupalComposer\\DrupalScaffold\\Plugin::scaffold",
94+
"reset-repositories": "ThunderDevelop\\composer\\ScriptHandler::resetLocalRepositories",
9495
"pre-update-cmd": [
9596
"ThunderDevelop\\composer\\ScriptHandler::downloadDevelopPackages"
9697
],

scripts/composer/ScriptHandler.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,79 @@ public static function downloadDevelopPackages(Event $event) {
8888
}
8989
}
9090

91+
/**
92+
* Reset local repositories to the default branch.
93+
*
94+
* @param \Composer\Script\Event $event
95+
* The script event.
96+
*/
97+
public static function resetLocalRepositories(Event $event) {
98+
$io = $event->getIO();
99+
$repositoriesInfo = self::getLocalRepositoriesInfo($event);
100+
$drupalFinder = new DrupalFinder();
101+
$drupalFinder->locateRoot(getcwd());
102+
$composerRoot = $drupalFinder->getComposerRoot();
103+
104+
$io->write(PHP_EOL);
105+
foreach ($repositoriesInfo as $key => $info) {
106+
$gitCommand = 'git -C ' . $composerRoot . '/' . $info['install_path'];
107+
108+
$localBranch = trim(shell_exec($gitCommand . ' rev-parse --abbrev-ref HEAD'));
109+
110+
exec($gitCommand . ' fetch --quiet');
111+
$gitStatus = shell_exec($gitCommand . ' status --porcelain');
112+
if (!empty($gitStatus)) {
113+
$io->write('Stash local changes in ' . $info['package'] . ':' . $localBranch, TRUE);
114+
exec($gitCommand . ' stash --include-untracked');
115+
}
116+
117+
if ($localBranch !== $info['branch']) {
118+
$io->write('Checkout ' . $info['package'] . ':' . $info['branch'], TRUE);
119+
exec($gitCommand . ' checkout --quiet ' . $info['branch']);
120+
}
121+
122+
$io->write('Merge remote changes into ' . $info['package'] . ':' . $info['branch'], TRUE);
123+
exec($gitCommand . ' merge --quiet');
124+
}
125+
}
126+
127+
/**
128+
* Collect information about local repositories.
129+
*
130+
* Retrieve available informazion about the repositories defined in the
131+
* local-develop-packages key of the composer.json.
132+
*
133+
* @param \Composer\Script\Event $event
134+
* The script event.
135+
*
136+
* @return array
137+
* The collected repositories.
138+
*/
139+
protected static function getLocalRepositoriesInfo(Event $event) {
140+
$repositoriesInfo = [];
141+
$composer = $event->getComposer();
142+
$rootExtra = $composer->getPackage()->getExtra();
143+
$packages = $rootExtra['local-develop-packages'];
144+
145+
foreach ($packages as $packageString => $packageVersion) {
146+
$package = $composer->getRepositoryManager()->findPackage($packageString, $packageVersion);
147+
if (!$package) {
148+
continue;
149+
}
150+
151+
$repository = $package->getRepository();
152+
if ($gitDriver = $repository->getDriver()) {
153+
$info = [];
154+
$info['package'] = $packageString;
155+
$info['install_path'] = self::getInstallPath($package, $composer);
156+
$info['url'] = $gitDriver->getUrl();
157+
$info['branch'] = (0 === strpos($packageVersion, 'dev-')) ? substr($packageVersion, strlen('dev-')) : '';
158+
$repositoriesInfo[] = $info;
159+
}
160+
}
161+
return $repositoriesInfo;
162+
}
163+
91164
/**
92165
* Return the install path based on package type.
93166
*

0 commit comments

Comments
 (0)