|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Script to get changes between feature branch and the mainline |
| 5 | + * |
| 6 | + * @category dev |
| 7 | + * @package build |
| 8 | + * Copyright © 2015 Magento. All rights reserved. |
| 9 | + * See COPYING.txt for license details. |
| 10 | + */ |
| 11 | + |
| 12 | +// @codingStandardsIgnoreFile |
| 13 | + |
| 14 | +define( |
| 15 | +'USAGE', |
| 16 | +<<<USAGE |
| 17 | + php -f get_github_changes.php -- |
| 18 | + --output-file="<output_file>" |
| 19 | + --base-path="<base_path>" |
| 20 | + --repo="<main_repo>" |
| 21 | + [--file-formats="<comma_separated_list_of_formats>"] |
| 22 | +
|
| 23 | +USAGE |
| 24 | +); |
| 25 | + |
| 26 | +$options = getopt('', ['output-file:', 'base-path:', 'repo:', 'file-formats:']); |
| 27 | + |
| 28 | +$requiredOptions = ['output-file', 'base-path', 'repo']; |
| 29 | +if (!validateInput($options, $requiredOptions)) { |
| 30 | + echo USAGE; |
| 31 | + exit(1); |
| 32 | +} |
| 33 | + |
| 34 | +$fileFormats = explode(',', isset($options['file-formats']) ? $options['file-formats'] : 'php'); |
| 35 | + |
| 36 | +$mainline = 'mainline_' . (string)rand(0, 9999); |
| 37 | +$repo = getRepo($options, $mainline); |
| 38 | +$changes = retrieveChangesAcrossForks($mainline, $repo); |
| 39 | +$changedFiles = getChangedFiles($changes, $fileFormats); |
| 40 | +generateChangedFilesList($options['output-file'], $changedFiles); |
| 41 | +cleanup($repo, $mainline); |
| 42 | + |
| 43 | +/** |
| 44 | + * Generates a file containing changed files |
| 45 | + * |
| 46 | + * @param string $outputFile |
| 47 | + * @param array $changedFiles |
| 48 | + * @return void |
| 49 | + */ |
| 50 | +function generateChangedFilesList($outputFile, $changedFiles) |
| 51 | +{ |
| 52 | + $changedFilesList = fopen($outputFile, 'w'); |
| 53 | + foreach ($changedFiles as $file) { |
| 54 | + fwrite($changedFilesList, $file . PHP_EOL); |
| 55 | + } |
| 56 | + fclose($changedFilesList); |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Gets list of changed files |
| 61 | + * |
| 62 | + * @param array $changes |
| 63 | + * @param array $fileFormats |
| 64 | + * @return array |
| 65 | + */ |
| 66 | +function getChangedFiles($changes, $fileFormats) |
| 67 | +{ |
| 68 | + $files = []; |
| 69 | + foreach ($changes as $fileName) { |
| 70 | + foreach ($fileFormats as $format) { |
| 71 | + $isFileFormat = strpos($fileName, '.' . $format); |
| 72 | + if ($isFileFormat) { |
| 73 | + $files[] = $fileName; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return $files; |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Retrieves changes across forks |
| 83 | + * |
| 84 | + * @param array $options |
| 85 | + * @return array |
| 86 | + * @throws Exception |
| 87 | + */ |
| 88 | +function getRepo($options, $mainline) |
| 89 | +{ |
| 90 | + $repo = new GitRepo($options['base-path']); |
| 91 | + $repo->addRemote($mainline, $options['repo']); |
| 92 | + $repo->fetch($mainline); |
| 93 | + return $repo; |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * @param $repo |
| 98 | + * @return array |
| 99 | + */ |
| 100 | +function retrieveChangesAcrossForks($mainline, $repo) |
| 101 | +{ |
| 102 | + return $repo->compareChanges($mainline, 'develop'); |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Deletes temporary "base" repo |
| 107 | + * |
| 108 | + * @param GitRepo $repo |
| 109 | + * @param string $repo |
| 110 | + */ |
| 111 | +function cleanup($repo, $mainline) |
| 112 | +{ |
| 113 | + $repo->removeRemote($mainline); |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * Validates input options based on required options |
| 118 | + * |
| 119 | + * @param array $options |
| 120 | + * @param array $requiredOptions |
| 121 | + * @return bool |
| 122 | + */ |
| 123 | +function validateInput(array $options, array $requiredOptions) |
| 124 | +{ |
| 125 | + foreach ($requiredOptions as $requiredOption) { |
| 126 | + if (!isset($options[$requiredOption]) || empty($options[$requiredOption])) { |
| 127 | + return false; |
| 128 | + } |
| 129 | + } |
| 130 | + return true; |
| 131 | +} |
| 132 | + |
| 133 | + |
| 134 | +class GitRepo |
| 135 | +{ |
| 136 | + /** |
| 137 | + * Absolute path to git project |
| 138 | + * |
| 139 | + * @var string |
| 140 | + */ |
| 141 | + private $workTree; |
| 142 | + |
| 143 | + /** |
| 144 | + * @var array |
| 145 | + */ |
| 146 | + private $remoteList = []; |
| 147 | + |
| 148 | + /** |
| 149 | + * @param string $workTree absolute path to git project |
| 150 | + */ |
| 151 | + public function __construct($workTree) |
| 152 | + { |
| 153 | + if (empty($workTree) || !is_dir($workTree)) { |
| 154 | + throw new UnexpectedValueException('Working tree should be a valid path to directory'); |
| 155 | + } |
| 156 | + $this->workTree = $workTree; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Adds remote |
| 161 | + * |
| 162 | + * @param string $alias |
| 163 | + * @param string $url |
| 164 | + */ |
| 165 | + public function addRemote($alias, $url) |
| 166 | + { |
| 167 | + if (isset($this->remoteList[$alias])) { |
| 168 | + return; |
| 169 | + } |
| 170 | + $this->remoteList[$alias] = $url; |
| 171 | + |
| 172 | + $this->call(sprintf('remote add %s %s', $alias, $url)); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Remove remote |
| 177 | + * |
| 178 | + * @param string $alias |
| 179 | + */ |
| 180 | + public function removeRemote($alias) |
| 181 | + { |
| 182 | + if (isset($this->remoteList[$alias])) { |
| 183 | + $this->call(sprintf('remote remove %s', $alias)); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Fetches remote |
| 189 | + * |
| 190 | + * @param string $remoteAlias |
| 191 | + */ |
| 192 | + public function fetch($remoteAlias) |
| 193 | + { |
| 194 | + if (!isset($this->remoteList[$remoteAlias])) { |
| 195 | + throw new LogicException('Alias is not defined'); |
| 196 | + } |
| 197 | + |
| 198 | + $this->call(sprintf('fetch %s', $remoteAlias)); |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Returns files changes between branch |
| 203 | + * |
| 204 | + * @param string $remoteAlias |
| 205 | + * @param string $remoteBranch |
| 206 | + * @return array |
| 207 | + */ |
| 208 | + public function compareChanges($remoteAlias, $remoteBranch) |
| 209 | + { |
| 210 | + if (!isset($this->remoteList[$remoteAlias])) { |
| 211 | + throw new LogicException('Alias is not defined'); |
| 212 | + } |
| 213 | + |
| 214 | + $result = $this->call(sprintf('log %s/%s..HEAD --name-status --oneline', $remoteAlias, $remoteBranch)); |
| 215 | + |
| 216 | + return is_array($result) |
| 217 | + ? $this->filterChangedBackFiles( |
| 218 | + $this->filterChangedFiles($result), |
| 219 | + $remoteAlias, |
| 220 | + $remoteBranch |
| 221 | + ) |
| 222 | + : []; |
| 223 | + } |
| 224 | + |
| 225 | + /** |
| 226 | + * Filters git cli output for changed files |
| 227 | + * |
| 228 | + * @param array $changes |
| 229 | + * @return array |
| 230 | + */ |
| 231 | + protected function filterChangedFiles(array $changes) |
| 232 | + { |
| 233 | + $changedFilesMasks = [ |
| 234 | + 'M' => "M\t", |
| 235 | + 'A' => "A\t" |
| 236 | + ]; |
| 237 | + $filteredChanges = []; |
| 238 | + foreach ($changes as $fileName) { |
| 239 | + foreach ($changedFilesMasks as $mask) { |
| 240 | + if (strpos($fileName, $mask) === 0) { |
| 241 | + $fileName = str_replace($mask, '', $fileName); |
| 242 | + $fileName = trim($fileName); |
| 243 | + if (!in_array($fileName, $filteredChanges) && is_file($this->workTree . '/' . $fileName)) { |
| 244 | + $filteredChanges[] = $fileName; |
| 245 | + } |
| 246 | + break; |
| 247 | + } |
| 248 | + } |
| 249 | + } |
| 250 | + return $filteredChanges; |
| 251 | + } |
| 252 | + |
| 253 | + /** |
| 254 | + * Makes a diff of file for specified remote/branch and filters only those have real changes |
| 255 | + * |
| 256 | + * @param array $changes |
| 257 | + * @param string $remoteAlias |
| 258 | + * @param string $remoteBranch |
| 259 | + * @return array |
| 260 | + */ |
| 261 | + protected function filterChangedBackFiles(array $changes, $remoteAlias, $remoteBranch) |
| 262 | + { |
| 263 | + $filteredChanges = []; |
| 264 | + foreach ($changes as $fileName) { |
| 265 | + $result = $this->call(sprintf( |
| 266 | + 'diff HEAD %s/%s -- %s', $remoteAlias, $remoteBranch, $this->workTree .'/'. $fileName) |
| 267 | + ); |
| 268 | + if ($result) { |
| 269 | + $filteredChanges[] = $fileName; |
| 270 | + } |
| 271 | + } |
| 272 | + |
| 273 | + return $filteredChanges; |
| 274 | + } |
| 275 | + |
| 276 | + /** |
| 277 | + * Makes call ro git cli |
| 278 | + * |
| 279 | + * @param string $command |
| 280 | + * @return mixed |
| 281 | + */ |
| 282 | + private function call($command) |
| 283 | + { |
| 284 | + $gitCmd = sprintf( |
| 285 | + 'git --git-dir %s --work-tree %s', |
| 286 | + escapeshellarg("{$this->workTree}/.git"), |
| 287 | + escapeshellarg($this->workTree) |
| 288 | + ); |
| 289 | + $tmp = sprintf('%s %s', $gitCmd, $command); |
| 290 | + exec(escapeshellcmd($tmp), $output); |
| 291 | + return $output; |
| 292 | + } |
| 293 | +} |
0 commit comments