diff --git a/src/configuration/ArcanistConfiguration.php b/src/configuration/ArcanistConfiguration.php index d232da3d..5755cc5e 100644 --- a/src/configuration/ArcanistConfiguration.php +++ b/src/configuration/ArcanistConfiguration.php @@ -1,267 +1,272 @@ buildAllWorkflows(), $command); } public function buildAllWorkflows() { $workflows_by_name = array(); $workflows_by_class_name = id(new PhutilSymbolLoader()) ->setAncestorClass('ArcanistWorkflow') ->loadObjects(); foreach ($workflows_by_class_name as $class => $workflow) { $name = $workflow->getWorkflowName(); if (isset($workflows_by_name[$name])) { $other = get_class($workflows_by_name[$name]); throw new Exception( - "Workflows {$class} and {$other} both implement workflows named ". - "{$name}."); + pht( + 'Workflows %s and %s both implement workflows named %s.', + $class, + $other, + $name)); } $workflows_by_name[$name] = $workflow; } return $workflows_by_name; } final public function isValidWorkflow($workflow) { return (bool)$this->buildWorkflow($workflow); } public function willRunWorkflow($command, ArcanistWorkflow $workflow) { // This is a hook. } public function didRunWorkflow($command, ArcanistWorkflow $workflow, $err) { // This is a hook. } public function didAbortWorkflow($command, $workflow, Exception $ex) { // This is a hook. } public function getCustomArgumentsForCommand($command) { return array(); } final public function selectWorkflow( &$command, array &$args, ArcanistConfigurationManager $configuration_manager, PhutilConsole $console) { // First, try to build a workflow with the exact name provided. We always // pick an exact match, and do not allow aliases to override it. $workflow = $this->buildWorkflow($command); if ($workflow) { return $workflow; } // If the user has an alias, like 'arc alias dhelp diff help', look it up // and substitute it. We do this only after trying to resolve the workflow // normally to prevent you from doing silly things like aliasing 'alias' // to something else. $aliases = ArcanistAliasWorkflow::getAliases($configuration_manager); list($new_command, $args) = ArcanistAliasWorkflow::resolveAliases( $command, $this, $args, $configuration_manager); $full_alias = idx($aliases, $command, array()); $full_alias = implode(' ', $full_alias); // Run shell command aliases. if (ArcanistAliasWorkflow::isShellCommandAlias($new_command)) { $shell_cmd = substr($full_alias, 1); $console->writeLog( - "[alias: 'arc %s' -> $ %s]", + "[%s: 'arc %s' -> $ %s]", + pht('alias'), $command, $shell_cmd); if ($args) { $err = phutil_passthru('%C %Ls', $shell_cmd, $args); } else { $err = phutil_passthru('%C', $shell_cmd); } exit($err); } // Run arc command aliases. if ($new_command) { $workflow = $this->buildWorkflow($new_command); if ($workflow) { $console->writeLog( - "[alias: 'arc %s' -> 'arc %s']\n", + "[%s: 'arc %s' -> 'arc %s']\n", + pht('alias'), $command, $full_alias); $command = $new_command; return $workflow; } } $all = array_keys($this->buildAllWorkflows()); // We haven't found a real command or an alias, so try to locate a command // by unique prefix. $prefixes = $this->expandCommandPrefix($command, $all); if (count($prefixes) == 1) { $command = head($prefixes); return $this->buildWorkflow($command); } else if (count($prefixes) > 1) { $this->raiseUnknownCommand($command, $prefixes); } // We haven't found a real command, alias, or unique prefix. Try similar // spellings. $corrected = self::correctCommandSpelling($command, $all, 2); if (count($corrected) == 1) { $console->writeErr( pht( "(Assuming '%s' is the British spelling of '%s'.)", $command, head($corrected))."\n"); $command = head($corrected); return $this->buildWorkflow($command); } else if (count($corrected) > 1) { $this->raiseUnknownCommand($command, $corrected); } $this->raiseUnknownCommand($command); } private function raiseUnknownCommand($command, array $maybe = array()) { - $message = pht("Unknown command '%s'. Try 'arc help'.", $command); + $message = pht("Unknown command '%s'. Try '%s'.", $command, 'arc help'); if ($maybe) { $message .= "\n\n".pht('Did you mean:')."\n"; sort($maybe); foreach ($maybe as $other) { $message .= " ".$other."\n"; } } throw new ArcanistUsageException($message); } private function expandCommandPrefix($command, array $options) { $is_prefix = array(); foreach ($options as $option) { if (strncmp($option, $command, strlen($command)) == 0) { $is_prefix[$option] = true; } } return array_keys($is_prefix); } public static function correctCommandSpelling( $command, array $options, $max_distance) { // Adjust to the scaled edit costs we use below, so "2" roughly means // "2 edits". $max_distance = $max_distance * 3; // These costs are somewhat made up, but the theory is that it is far more // likely you will mis-strike a key ("lans" for "land") or press two keys // out of order ("alnd" for "land") than omit keys or press extra keys. $matrix = id(new PhutilEditDistanceMatrix()) ->setInsertCost(4) ->setDeleteCost(4) ->setReplaceCost(3) ->setTransposeCost(2); return self::correctSpelling($command, $options, $matrix, $max_distance); } public static function correctArgumentSpelling($command, array $options) { $max_distance = 1; // We are stricter with arguments - we allow only one inserted or deleted // character. It is mainly to handle cases like --no-lint versus --nolint // or --reviewer versus --reviewers. $matrix = id(new PhutilEditDistanceMatrix()) ->setInsertCost(1) ->setDeleteCost(1) ->setReplaceCost(10); return self::correctSpelling($command, $options, $matrix, $max_distance); } public static function correctSpelling( $input, array $options, PhutilEditDistanceMatrix $matrix, $max_distance) { $distances = array(); $inputv = str_split($input); foreach ($options as $option) { $optionv = str_split($option); $matrix->setSequences($optionv, $inputv); $distances[$option] = $matrix->getEditDistance(); } asort($distances); $best = min($max_distance, reset($distances)); foreach ($distances as $option => $distance) { if ($distance > $best) { unset($distances[$option]); } } // Before filtering, check if we have multiple equidistant matches and // return them if we do. This prevents us from, e.g., matching "alnd" with // both "land" and "amend", then dropping "land" for being too short, and // incorrectly completing to "amend". if (count($distances) > 1) { return array_keys($distances); } foreach ($distances as $option => $distance) { if (strlen($option) < $distance) { unset($distances[$option]); } } return array_keys($distances); } } diff --git a/src/configuration/ArcanistConfigurationManager.php b/src/configuration/ArcanistConfigurationManager.php index ef9b0b4f..6195a1ec 100644 --- a/src/configuration/ArcanistConfigurationManager.php +++ b/src/configuration/ArcanistConfigurationManager.php @@ -1,338 +1,344 @@ workingCopy = $working_copy; } /* -( Get config )--------------------------------------------------------- */ const CONFIG_SOURCE_RUNTIME = 'runtime'; const CONFIG_SOURCE_LOCAL = 'local'; const CONFIG_SOURCE_PROJECT = 'project'; const CONFIG_SOURCE_USER = 'user'; const CONFIG_SOURCE_SYSTEM = 'system'; const CONFIG_SOURCE_DEFAULT = 'default'; public function getProjectConfig($key) { if ($this->workingCopy) { return $this->workingCopy->getProjectConfig($key); } return null; } public function getLocalConfig($key) { if ($this->workingCopy) { return $this->workingCopy->getLocalConfig($key); } return null; } public function getWorkingCopyIdentity() { return $this->workingCopy; } /** * Read a configuration directive from any available configuration source. * This includes the directive in local, user and system configuration in * addition to project configuration, and configuration provided as command * arguments ("runtime"). * The precedence is runtime > local > project > user > system * * @param key Key to read. * @param wild Default value if key is not found. * @return wild Value, or default value if not found. * * @task config */ public function getConfigFromAnySource($key, $default = null) { $all = $this->getConfigFromAllSources($key); return empty($all) ? $default : head($all); } /** * For the advanced case where you want customized configuration handling. * * Reads the configuration from all available sources, returning a map (array) * of results, with the source as key. Missing values will not be in the map, * so an empty array will be returned if no results are found. * * The map is ordered by the canonical sources precedence, which is: * runtime > local > project > user > system * * @param key Key to read * @return array Mapping of source => value read. Sources with no value are * not in the array. * * @task config */ public function getConfigFromAllSources($key) { $results = array(); $settings = new ArcanistSettings(); $pval = idx($this->runtimeConfig, $key); if ($pval !== null) { $results[self::CONFIG_SOURCE_RUNTIME] = $settings->willReadValue($key, $pval); } $pval = $this->getLocalConfig($key); if ($pval !== null) { $results[self::CONFIG_SOURCE_LOCAL] = $settings->willReadValue($key, $pval); } $pval = $this->getProjectConfig($key); if ($pval !== null) { $results[self::CONFIG_SOURCE_PROJECT] = $settings->willReadValue($key, $pval); } $user_config = $this->readUserArcConfig(); $pval = idx($user_config, $key); if ($pval !== null) { $results[self::CONFIG_SOURCE_USER] = $settings->willReadValue($key, $pval); } $system_config = $this->readSystemArcConfig(); $pval = idx($system_config, $key); if ($pval !== null) { $results[self::CONFIG_SOURCE_SYSTEM] = $settings->willReadValue($key, $pval); } $default_config = $this->readDefaultConfig(); if (array_key_exists($key, $default_config)) { $results[self::CONFIG_SOURCE_DEFAULT] = $default_config[$key]; } return $results; } /** * Sets a runtime config value that takes precedence over any static * config values. * * @param key Key to set. * @param value The value of the key. * * @task config */ public function setRuntimeConfig($key, $value) { $this->runtimeConfig[$key] = $value; return $this; } /* -( Read/write config )--------------------------------------------------- */ public function readLocalArcConfig() { if ($this->workingCopy) { return $this->workingCopy->readLocalArcConfig(); } return array(); } public function writeLocalArcConfig(array $config) { if ($this->workingCopy) { return $this->workingCopy->writeLocalArcConfig($config); } throw new Exception(pht('No working copy to write config to!')); } /** * This is probably not the method you're looking for; try * @{method:readUserArcConfig}. */ public function readUserConfigurationFile() { if ($this->userConfigCache === null) { $user_config = array(); $user_config_path = $this->getUserConfigurationFileLocation(); $console = PhutilConsole::getConsole(); if (Filesystem::pathExists($user_config_path)) { $console->writeLog( "%s\n", pht( 'Config: Reading user configuration file "%s"...', $user_config_path)); if (!phutil_is_windows()) { $mode = fileperms($user_config_path); if (!$mode) { throw new Exception( pht( 'Unable to read file permissions for "%s"!', $user_config_path)); } if ($mode & 0177) { // Mode should allow only owner access. - $prompt = "File permissions on your ~/.arcrc are too open. ". - "Fix them by chmod'ing to 600?"; + $prompt = pht( + "File permissions on your %s are too open. ". + "Fix them by chmod'ing to 600?", + '~/.arcrc'); if (!phutil_console_confirm($prompt, $default_no = false)) { throw new ArcanistUsageException( - 'Set ~/.arcrc to file mode 600.'); + pht('Set %s to file mode 600.', '~/.arcrc')); } execx('chmod 600 %s', $user_config_path); // Drop the stat cache so we don't read the old permissions if // we end up here again. If we don't do this, we may prompt the user // to fix permissions multiple times. clearstatcache(); } } $user_config_data = Filesystem::readFile($user_config_path); try { $user_config = phutil_json_decode($user_config_data); } catch (PhutilJSONParserException $ex) { throw new PhutilProxyException( - "Your '~/.arcrc' file is not a valid JSON file.", + pht("Your '%s' file is not a valid JSON file.", '~/.arcrc'), $ex); } } else { $console->writeLog( "%s\n", pht( 'Config: Did not find user configuration at "%s".', $user_config_path)); } $this->userConfigCache = $user_config; } return $this->userConfigCache; } /** * This is probably not the method you're looking for; try * @{method:writeUserArcConfig}. */ public function writeUserConfigurationFile($config) { $json_encoder = new PhutilJSON(); $json = $json_encoder->encodeFormatted($config); $path = $this->getUserConfigurationFileLocation(); Filesystem::writeFile($path, $json); if (!phutil_is_windows()) { execx('chmod 600 %s', $path); } } public function setUserConfigurationFileLocation($custom_arcrc) { if (!Filesystem::pathExists($custom_arcrc)) { throw new Exception( - 'Custom arcrc file was specified, but it was not found!'); + pht('Custom %s file was specified, but it was not found!', 'arcrc')); } $this->customArcrcFilename = $custom_arcrc; $this->userConfigCache = null; } public function getUserConfigurationFileLocation() { if (strlen($this->customArcrcFilename)) { return $this->customArcrcFilename; } if (phutil_is_windows()) { return getenv('APPDATA').'/.arcrc'; } else { return getenv('HOME').'/.arcrc'; } } public function readUserArcConfig() { return idx($this->readUserConfigurationFile(), 'config', array()); } public function writeUserArcConfig(array $options) { $config = $this->readUserConfigurationFile(); $config['config'] = $options; $this->writeUserConfigurationFile($config); } public function getSystemArcConfigLocation() { if (phutil_is_windows()) { return Filesystem::resolvePath( 'Phabricator/Arcanist/config', getenv('ProgramData')); } else { return '/etc/arcconfig'; } } public function readSystemArcConfig() { static $system_config; if ($system_config === null) { $system_config = array(); $system_config_path = $this->getSystemArcConfigLocation(); $console = PhutilConsole::getConsole(); if (Filesystem::pathExists($system_config_path)) { $console->writeLog( "%s\n", pht( 'Config: Reading system configuration file "%s"...', $system_config_path)); $file = Filesystem::readFile($system_config_path); try { $system_config = phutil_json_decode($file); } catch (PhutilJSONParserException $ex) { throw new PhutilProxyException( pht( "Your '%s' file is not a valid JSON file.", $system_config_path), $ex); } } else { $console->writeLog( "%s\n", pht( 'Config: Did not find system configuration at "%s".', $system_config_path)); } } return $system_config; } public function applyRuntimeArcConfig($args) { $arcanist_settings = new ArcanistSettings(); $options = $args->getArg('config'); foreach ($options as $opt) { $opt_config = preg_split('/=/', $opt, 2); if (count($opt_config) !== 2) { - throw new ArcanistUsageException("Argument was '{$opt}', but must be ". - "'name=value'. For example, history.immutable=true"); + throw new ArcanistUsageException( + pht( + "Argument was '%s', but must be '%s'. For example, %s", + $opt, + 'name=value', + 'history.immutable=true')); } list($key, $value) = $opt_config; $value = $arcanist_settings->willWriteValue($key, $value); $this->setRuntimeConfig($key, $value); } return $this->runtimeConfig; } public function readDefaultConfig() { $settings = new ArcanistSettings(); return $settings->getDefaultSettings(); } } diff --git a/src/configuration/ArcanistSettings.php b/src/configuration/ArcanistSettings.php index ad5f7626..b6ede180 100644 --- a/src/configuration/ArcanistSettings.php +++ b/src/configuration/ArcanistSettings.php @@ -1,347 +1,347 @@ array( 'type' => 'string', 'help' => pht( 'The URI of a Phabricator install to connect to by default, if '. '%s is run in a project without a Phabricator URI or run outside '. 'of a project.', 'arc'), 'example' => '"http://phabricator.example.com/"', ), 'base' => array( 'type' => 'string', 'help' => pht( 'Base commit ruleset to invoke when determining the start of a '. 'commit range. See "Arcanist User Guide: Commit Ranges" for '. 'details.'), 'example' => '"arc:amended, arc:prompt"', ), 'load' => array( 'type' => 'list', 'legacy' => 'phutil_libraries', 'help' => pht( 'A list of paths to phutil libraries that should be loaded at '. 'startup. This can be used to make classes available, like lint '. 'or unit test engines.'), 'default' => array(), 'example' => '["/var/arc/customlib/src"]', ), 'repository.callsign' => array( 'type' => 'string', 'example' => '"X"', 'help' => pht( 'Associate the working copy with a specific Phabricator repository. '. 'Normally, %s can figure this association out on its own, but if '. 'your setup is unusual you can use this option to tell it what the '. 'desired value is.', 'arc'), ), 'phabricator.uri' => array( 'type' => 'string', 'legacy' => 'conduit_uri', 'example' => '"https://phabricator.mycompany.com/"', 'help' => pht( 'Associates this working copy with a specific installation of '. 'Phabricator.'), ), 'project.name' => array( 'type' => 'string', 'legacy' => 'project_id', 'example' => '"arcanist"', 'help' => pht( 'Associates this working copy with a named Arcanist Project. '. 'This is primarily useful if you use SVN and have several different '. 'projects in the same repository.'), ), 'lint.engine' => array( 'type' => 'string', 'legacy' => 'lint_engine', 'help' => pht( 'The name of a default lint engine to use, if no lint engine is '. 'specified by the current project.'), 'example' => '"ExampleLintEngine"', ), 'unit.engine' => array( 'type' => 'string', 'legacy' => 'unit_engine', 'help' => pht( 'The name of a default unit test engine to use, if no unit test '. 'engine is specified by the current project.'), 'example' => '"ExampleUnitTestEngine"', ), 'arc.feature.start.default' => array( 'type' => 'string', 'help' => pht( 'The name of the default branch to create the new feature branch '. 'off of.'), 'example' => '"develop"', ), 'arc.land.onto.default' => array( 'type' => 'string', 'help' => pht( 'The name of the default branch to land changes onto when '. '`%s` is run.', - 'arc land'), + 'arc land'), 'example' => '"develop"', ), 'arc.land.update.default' => array( 'type' => 'string', 'help' => pht( 'The default strategy to use when arc land updates the feature '. 'branch. Supports "rebase" and "merge" strategies.'), 'example' => '"rebase"', ), 'arc.lint.cache' => array( 'type' => 'bool', 'help' => pht( 'Enable the lint cache by default. When enabled, `%s` attempts to '. 'use cached results if possible. Currently, the cache is not always '. 'invalidated correctly and may cause `%s` to report incorrect '. 'results, particularly while developing linters. This is probably '. 'worth enabling only if your linters are very slow.', 'arc lint', 'arc lint'), 'default' => false, 'example' => 'false', ), 'history.immutable' => array( 'type' => 'bool', 'legacy' => 'immutable_history', 'help' => pht( 'If true, %s will never change repository history (e.g., through '. 'amending or rebasing). Defaults to true in Mercurial and false in '. 'Git. This setting has no effect in Subversion.', 'arc'), 'example' => 'false', ), 'editor' => array( 'type' => 'string', 'help' => pht( 'Command to use to invoke an interactive editor, like `%s` or `%s`. '. 'This setting overrides the %s environmental variable.', 'nano', 'vim', 'EDITOR'), 'example' => '"nano"', ), 'https.cabundle' => array( 'type' => 'string', 'help' => pht( "Path to a custom CA bundle file to be used for arcanist's cURL ". "calls. This is used primarily when your conduit endpoint is ". "behind HTTPS signed by your organization's internal CA."), 'example' => 'support/yourca.pem', ), 'https.blindly-trust-domains' => array( 'type' => 'list', 'help' => pht( 'List of domains to blindly trust SSL certificates for. '. 'Disables peer verification.'), 'default' => array(), 'example' => '["secure.mycompany.com"]', ), 'browser' => array( 'type' => 'string', 'help' => pht('Command to use to invoke a web browser.'), 'example' => '"gnome-www-browser"', ), 'events.listeners' => array( 'type' => 'list', 'help' => pht('List of event listener classes to install at startup.'), 'default' => array(), 'example' => '["ExampleEventListener"]', ), 'http.basicauth.user' => array( 'type' => 'string', 'help' => pht('Username to use for basic auth over HTTP transports.'), 'example' => '"bob"', ), 'http.basicauth.pass' => array( 'type' => 'string', 'help' => pht('Password to use for basic auth over HTTP transports.'), 'example' => '"bobhasasecret"', ), 'arc.autostash' => array( 'type' => 'bool', 'help' => pht( 'Whether %s should permit the automatic stashing of changes in the '. 'working directory when requiring a clean working copy. This option '. 'should only be used when users understand how to restore their '. 'working directory from the local stash if an Arcanist operation '. 'causes an unrecoverable error.', 'arc'), 'default' => false, 'example' => 'false', ), ); } private function getOption($key) { return idx($this->getOptions(), $key, array()); } public function getAllKeys() { return array_keys($this->getOptions()); } public function getHelp($key) { return idx($this->getOption($key), 'help'); } public function getExample($key) { return idx($this->getOption($key), 'example'); } public function getType($key) { return idx($this->getOption($key), 'type', 'wild'); } public function getLegacyName($key) { return idx($this->getOption($key), 'legacy'); } public function getDefaultSettings() { $defaults = array(); foreach ($this->getOptions() as $key => $option) { if (array_key_exists('default', $option)) { $defaults[$key] = $option['default']; } } return $defaults; } public function willWriteValue($key, $value) { $type = $this->getType($key); switch ($type) { case 'bool': if (strtolower($value) === 'false' || strtolower($value) === 'no' || strtolower($value) === 'off' || $value === '' || $value === '0' || $value === 0 || $value === false) { $value = false; } else if (strtolower($value) === 'true' || strtolower($value) === 'yes' || strtolower($value) === 'on' || $value === '1' || $value === 1 || $value === true) { $value = true; } else { throw new ArcanistUsageException( pht( "Type of setting '%s' must be boolean, like 'true' or 'false'.", $key)); } break; case 'list': if (is_array($value)) { break; } if (is_string($value)) { $list = json_decode($value, true); if (is_array($list)) { $value = $list; break; } } throw new ArcanistUsageException( pht( "Type of setting '%s' must be list. You can specify a list ". "in JSON, like: %s", $key, '["apple", "banana", "cherry"]')); case 'string': if (!is_scalar($value)) { throw new ArcanistUsageException( pht( "Type of setting '%s' must be string.", $key)); } $value = (string)$value; break; case 'wild': break; } return $value; } public function willReadValue($key, $value) { $type = $this->getType($key); switch ($type) { case 'string': if (!is_string($value)) { throw new ArcanistUsageException( pht( "Type of setting '%s' must be string.", $key)); } break; case 'bool': if ($value !== true && $value !== false) { throw new ArcanistUsageException( pht( "Type of setting '%s' must be boolean.", $key)); } break; case 'list': if (!is_array($value)) { throw new ArcanistUsageException( pht( "Type of setting '%s' must be list.", $key)); } break; case 'wild': break; } return $value; } public function formatConfigValueForDisplay($key, $value) { if ($value === false) { return 'false'; } if ($value === true) { return 'true'; } if ($value === null) { return 'null'; } if (is_string($value)) { return '"'.$value.'"'; } if (is_array($value)) { // TODO: Both json_encode() and PhutilJSON do a bad job with one-liners. // PhutilJSON splits them across a bunch of lines, while json_encode() // escapes all kinds of stuff like "/". It would be nice if PhutilJSON // had a mode for pretty one-liners. $value = json_encode($value); // json_encode() unnecessarily escapes "/" to prevent "" stuff, // optimistically unescape it for display to improve readability. $value = preg_replace('@(?assertCommandCompletion( array('land'), 'alnd', array('land', 'amend')); $this->assertCommandCompletion( array('branch'), 'brnach', array('branch', 'browse')); $this->assertCommandCompletion( array(), 'test', array('list', 'unit')); $this->assertCommandCompletion( array('list'), 'lists', array('list')); $this->assertCommandCompletion( array('diff'), 'dfif', array('diff')); $this->assertCommandCompletion( array('unit'), 'uint', array('unit', 'lint', 'list')); $this->assertCommandCompletion( array('list', 'lint'), 'nilt', array('unit', 'lint', 'list')); } private function assertCommandCompletion($expect, $input, $commands) { $result = ArcanistConfiguration::correctCommandSpelling( $input, $commands, 2); sort($result); sort($expect); $commands = implode(', ', $commands); $this->assertEqual( $expect, $result, - "Correction of {$input} against: {$commands}"); + pht('Correction of %s against: %s', $input, $commands)); } public function testArgumentCompletion() { $this->assertArgumentCompletion( array('nolint'), 'no-lint', array('nolint', 'nounit')); $this->assertArgumentCompletion( array('reviewers'), 'reviewer', array('reviewers', 'cc')); $this->assertArgumentCompletion( array(), 'onlint', array('nolint')); $this->assertArgumentCompletion( array(), 'nolind', array('nolint')); } private function assertArgumentCompletion($expect, $input, $arguments) { $result = ArcanistConfiguration::correctArgumentSpelling( $input, $arguments); sort($result); sort($expect); $arguments = implode(', ', $arguments); $this->assertEqual( $expect, $result, - "Correction of {$input} against: {$arguments}"); + pht('Correction of %s against: %s', $input, $arguments)); } } diff --git a/src/difference/ArcanistDiffUtils.php b/src/difference/ArcanistDiffUtils.php index c789cddc..f3486d6f 100644 --- a/src/difference/ArcanistDiffUtils.php +++ b/src/difference/ArcanistDiffUtils.php @@ -1,254 +1,254 @@ '; $highlight_c = ''; $is_html = false; if ($str instanceof PhutilSafeHTML) { $is_html = true; $str = $str->getHTMLContent(); } $n = strlen($str); for ($i = 0; $i < $n; $i++) { if ($p == $e) { do { if (empty($intra_stack)) { $buf .= substr($str, $i); break 2; } $stack = array_shift($intra_stack); $s = $e; $e += $stack[1]; } while ($stack[0] == 0); } if (!$highlight && !$tag && !$ent && $p == $s) { $buf .= $highlight_o; $highlight = true; } if ($str[$i] == '<') { $tag = true; if ($highlight) { $buf .= $highlight_c; } } if (!$tag) { if ($str[$i] == '&') { $ent = true; } if ($ent && $str[$i] == ';') { $ent = false; } if (!$ent) { $p++; } } $buf .= $str[$i]; if ($tag && $str[$i] == '>') { $tag = false; if ($highlight) { $buf .= $highlight_o; } } if ($highlight && ($p == $e || $i == $n - 1)) { $buf .= $highlight_c; $highlight = false; } } if ($is_html) { return phutil_safe_html($buf); } return $buf; } private static function collapseIntralineRuns($runs) { $count = count($runs); for ($ii = 0; $ii < $count - 1; $ii++) { if ($runs[$ii][0] == $runs[$ii + 1][0]) { $runs[$ii + 1][1] += $runs[$ii][1]; unset($runs[$ii]); } } return array_values($runs); } public static function generateEditString(array $ov, array $nv, $max = 80) { return id(new PhutilEditDistanceMatrix()) ->setComputeString(true) ->setAlterCost(1 / ($max * 2)) ->setReplaceCost(2) ->setMaximumLength($max) ->setSequences($ov, $nv) ->getEditString(); } public static function computeIntralineEdits($o, $n) { if (preg_match('/[\x80-\xFF]/', $o.$n)) { $ov = phutil_utf8v_combined($o); $nv = phutil_utf8v_combined($n); $multibyte = true; } else { $ov = str_split($o); $nv = str_split($n); $multibyte = false; } $result = self::generateEditString($ov, $nv); // Smooth the string out, by replacing short runs of similar characters // with 'x' operations. This makes the result more readable to humans, since // there are fewer choppy runs of short added and removed substrings. do { $original = $result; $result = preg_replace( '/([xdi])(s{3})([xdi])/', '$1xxx$3', $result); $result = preg_replace( '/([xdi])(s{2})([xdi])/', '$1xx$3', $result); $result = preg_replace( '/([xdi])(s{1})([xdi])/', '$1x$3', $result); } while ($result != $original); // Now we have a character-based description of the edit. We need to // convert into a byte-based description. Walk through the edit string and // adjust each operation to reflect the number of bytes in the underlying // character. $o_pos = 0; $n_pos = 0; $result_len = strlen($result); $o_run = array(); $n_run = array(); $old_char_len = 1; $new_char_len = 1; for ($ii = 0; $ii < $result_len; $ii++) { $c = $result[$ii]; if ($multibyte) { $old_char_len = strlen($ov[$o_pos]); $new_char_len = strlen($nv[$n_pos]); } switch ($c) { case 's': case 'x': $byte_o = $old_char_len; $byte_n = $new_char_len; $o_pos++; $n_pos++; break; case 'i': $byte_o = 0; $byte_n = $new_char_len; $n_pos++; break; case 'd': $byte_o = $old_char_len; $byte_n = 0; $o_pos++; break; } if ($byte_o) { if ($c == 's') { $o_run[] = array(0, $byte_o); } else { $o_run[] = array(1, $byte_o); } } if ($byte_n) { if ($c == 's') { $n_run[] = array(0, $byte_n); } else { $n_run[] = array(1, $byte_n); } } } $o_run = self::collapseIntralineRuns($o_run); $n_run = self::collapseIntralineRuns($n_run); return array($o_run, $n_run); } } diff --git a/src/differential/constants/ArcanistDifferentialRevisionStatus.php b/src/differential/constants/ArcanistDifferentialRevisionStatus.php index 017000e9..62c3eec7 100644 --- a/src/differential/constants/ArcanistDifferentialRevisionStatus.php +++ b/src/differential/constants/ArcanistDifferentialRevisionStatus.php @@ -1,27 +1,27 @@ pht('Needs Review'), self::NEEDS_REVISION => pht('Needs Revision'), self::ACCEPTED => pht('Accepted'), self::CLOSED => pht('Closed'), self::ABANDONED => pht('Abandoned'), self::CHANGES_PLANNED => pht('Changes Planned'), self::IN_PREPARATION => pht('In Preparation'), ); - return idx($map, coalesce($status, '?'), 'Unknown'); + return idx($map, coalesce($status, '?'), pht('Unknown')); } } diff --git a/src/exception/usage/ArcanistUserAbortException.php b/src/exception/usage/ArcanistUserAbortException.php index d1aa1ee1..f87951f7 100644 --- a/src/exception/usage/ArcanistUserAbortException.php +++ b/src/exception/usage/ArcanistUserAbortException.php @@ -1,13 +1,13 @@ '); * * We encode it like this: * * o * 1234 # Length, as a 4-byte unsigned long. * * * For a detailed description of the cmdserver protocol, see * @{class:ArcanistHgServerChannel}. * * @param pair The pair to encode. * @return string Encoded string for transmission to the client. * * @task protocol */ protected function encodeMessage($argv) { if (!is_array($argv) || count($argv) !== 2) { - throw new Exception('Message should be .'); + throw new Exception(pht('Message should be %s.', '')); } $channel = head($argv); $data = last($argv); $len = strlen($data); $len = pack('N', $len); return "{$channel}{$len}{$data}"; } /** * Decode a message received from the client. The message looks like this: * * runcommand\n * 8 # Length, as a 4-byte unsigned long. * log\0 * -l\0 * 5 * * We decode it into a list in PHP, which looks like this: * * array( * 'runcommand', * 'log', * '-l', * '5', * ); * * @param string Bytes from the server. * @return list> Zero or more complete commands. * * @task protocol */ protected function decodeStream($data) { $this->buf .= $data; // The first part is terminated by "\n", so we don't always know how many // bytes we need to look for. This makes parsing a bit of a pain. $messages = array(); do { $continue_parsing = false; switch ($this->mode) { case self::MODE_COMMAND: // We're looking for "\n", which indicates the end of the command // name, like "runcommand". Next, we'll expect a length. $pos = strpos($this->buf, "\n"); if ($pos === false) { break; } $this->command = substr($this->buf, 0, $pos); $this->buf = substr($this->buf, $pos + 1); $this->mode = self::MODE_LENGTH; $continue_parsing = true; break; case self::MODE_LENGTH: // We're looking for a byte length, as a 4-byte big-endian unsigned // integer. Next, we'll expect that many bytes of data. if (strlen($this->buf) < 4) { break; } $len = substr($this->buf, 0, 4); $len = unpack('N', $len); $len = head($len); $this->buf = substr($this->buf, 4); $this->mode = self::MODE_ARGUMENTS; $this->byteLengthOfNextChunk = $len; $continue_parsing = true; break; case self::MODE_ARGUMENTS: // We're looking for the data itself, which is a block of bytes // of the given length. These are arguments delimited by "\0". Next // we'll expect another command. if (strlen($this->buf) < $this->byteLengthOfNextChunk) { break; } $data = substr($this->buf, 0, $this->byteLengthOfNextChunk); $this->buf = substr($this->buf, $this->byteLengthOfNextChunk); $message = array_merge(array($this->command), explode("\0", $data)); $this->mode = self::MODE_COMMAND; $this->command = null; $this->byteLengthOfNextChunk = null; $messages[] = $message; $continue_parsing = true; break; } } while ($continue_parsing); return $messages; } } diff --git a/src/hgdaemon/ArcanistHgProxyClient.php b/src/hgdaemon/ArcanistHgProxyClient.php index 62c32b41..86053fc4 100644 --- a/src/hgdaemon/ArcanistHgProxyClient.php +++ b/src/hgdaemon/ArcanistHgProxyClient.php @@ -1,197 +1,200 @@ executeCommand($command); * * The advantage of using this complex mechanism is that commands run in this * way do not need to pay the startup overhead for hg and the Python runtime, * which is often on the order of 100ms or more per command. * * @task construct Construction * @task config Configuration * @task exec Executing Mercurial Commands * @task internal Internals */ final class ArcanistHgProxyClient { private $workingCopy; private $server; private $skipHello; /* -( Construction )------------------------------------------------------- */ /** * Build a new client. This client is bound to a working copy. A server * must already be running on this working copy for the client to work. * * @param string Path to a Mercurial working copy. * * @task construct */ public function __construct($working_copy) { $this->workingCopy = Filesystem::resolvePath($working_copy); } /* -( Configuration )------------------------------------------------------ */ /** * When connecting, do not expect the "capabilities" message. * * @param bool True to skip the "capabilities" message. * @return this * * @task config */ public function setSkipHello($skip) { $this->skipHello = $skip; return $this; } /* -( Executing Merucurial Commands )-------------------------------------- */ /** * Execute a command (given as a list of arguments) via the command server. * * @param list A list of command arguments, like "log", "-l", "5". * @return tuple Return code, stdout and stderr. * * @task exec */ public function executeCommand(array $argv) { if (!$this->server) { try { $server = $this->connectToDaemon(); } catch (Exception $ex) { $this->launchDaemon(); $server = $this->connectToDaemon(); } $this->server = $server; } $server = $this->server; // Note that we're adding "runcommand" to make the server run the command. // Theoretically the server supports other capabilities, but in practice // we are only concerned with "runcommand". $server->write(array_merge(array('runcommand'), $argv)); // We'll get back one or more blocks of response data, ending with an 'r' // block which indicates the return code. Reconstitute these into stdout, // stderr and a return code. $stdout = ''; $stderr = ''; $err = 0; $done = false; while ($message = $server->waitForMessage()) { // The $server channel handles decoding of the wire format and gives us // messages which look like this: // // array('o', ''); list($channel, $data) = $message; switch ($channel) { case 'o': $stdout .= $data; break; case 'e': $stderr .= $data; break; case 'd': // TODO: Do something with this? This is the 'debug' channel. break; case 'r': // NOTE: This little dance is because the value is emitted as a // big-endian signed 32-bit long. PHP has no flag to unpack() that // can unpack these, so we unpack a big-endian unsigned long, then // repack it as a machine-order unsigned long, then unpack it as // a machine-order signed long. This appears to produce the desired // result. $err = head(unpack('N', $data)); $err = pack('L', $err); $err = head(unpack('l', $err)); $done = true; break; } if ($done) { break; } } return array($err, $stdout, $stderr); } /* -( Internals )---------------------------------------------------------- */ /** * @task internal */ private function connectToDaemon() { $errno = null; $errstr = null; $socket_path = ArcanistHgProxyServer::getPathToSocket($this->workingCopy); $socket = @stream_socket_client('unix://'.$socket_path, $errno, $errstr); if ($errno || !$socket) { throw new Exception( - "Unable to connect socket! Error #{$errno}: {$errstr}"); + pht( + 'Unable to connect socket! Error #%d: %s', + $errno, + $errstr)); } $channel = new PhutilSocketChannel($socket); $server = new ArcanistHgServerChannel($channel); if (!$this->skipHello) { // The protocol includes a "hello" message with capability and encoding // information. Read and discard it, we use only the "runcommand" // capability which is guaranteed to be available. $hello = $server->waitForMessage(); } return $server; } /** * @task internal */ private function launchDaemon() { $root = dirname(phutil_get_library_root('arcanist')); $bin = $root.'/scripts/hgdaemon/hgdaemon_server.php'; $proxy = new ExecFuture( '%s %s --idle-limit 15 --quiet %C', $bin, $this->workingCopy, $this->skipHello ? '--skip-hello' : null); $proxy->resolvex(); } } diff --git a/src/hgdaemon/ArcanistHgProxyServer.php b/src/hgdaemon/ArcanistHgProxyServer.php index a6e19a25..9a2e8eb0 100644 --- a/src/hgdaemon/ArcanistHgProxyServer.php +++ b/src/hgdaemon/ArcanistHgProxyServer.php @@ -1,487 +1,496 @@ workingCopy = Filesystem::resolvePath($working_copy); } /* -( Configuration )------------------------------------------------------ */ /** * Disable status messages to stdout. Controlled with `--quiet`. * * @param bool True to disable status messages. * @return this * * @task config */ public function setQuiet($quiet) { $this->quiet = $quiet; return $this; } /** * Configure a client limit. After serving this many clients, the server * will exit. Controlled with `--client-limit`. * * You can use `--client-limit 1` with `--xprofile` and `--do-not-daemonize` * to profile the server. * * @param int Client limit, or 0 to disable limit. * @return this * * @task config */ public function setClientLimit($limit) { $this->clientLimit = $limit; return $this; } /** * Configure an idle time limit. After this many seconds idle, the server * will exit. Controlled with `--idle-limit`. * * @param int Idle limit, or 0 to disable limit. * @return this * * @task config */ public function setIdleLimit($limit) { $this->idleLimit = $limit; return $this; } /** * When clients connect, do not send the "capabilities" message expected by * the Mercurial protocol. This deviates from the protocol and will only work * if the clients are also configured not to expect the message, but slightly * improves performance. Controlled with --skip-hello. * * @param bool True to skip the "capabilities" message. * @return this * * @task config */ public function setSkipHello($skip) { $this->skipHello = $skip; return $this; } /** * Configure whether the server runs in the foreground or daemonizes. * Controlled by --do-not-daemonize. Primarily useful for debugging. * * @param bool True to run in the foreground. * @return this * * @task config */ public function setDoNotDaemonize($do_not_daemonize) { $this->doNotDaemonize = $do_not_daemonize; return $this; } /* -( Serving Requests )--------------------------------------------------- */ /** * Start the server. This method returns after the client limit or idle * limit are exceeded. If neither limit is configured, this method does not * exit. * * @return null * * @task server */ public function start() { // Create the unix domain socket in the working copy to listen for clients. $socket = $this->startWorkingCopySocket(); $this->socket = $socket; if (!$this->doNotDaemonize) { $this->daemonize(); } // Start the Mercurial process which we'll forward client requests to. $hg = $this->startMercurialProcess(); $clients = array(); - $this->log(null, 'Listening'); + $this->log(null, pht('Listening')); $this->idleSince = time(); while (true) { // Wait for activity on any active clients, the Mercurial process, or // the listening socket where new clients connect. PhutilChannel::waitForAny( array_merge($clients, array($hg)), array( 'read' => $socket ? array($socket) : array(), 'except' => $socket ? array($socket) : array(), )); if (!$hg->update()) { - throw new Exception('Server exited unexpectedly!'); + throw new Exception(pht('Server exited unexpectedly!')); } // Accept any new clients. while ($socket && ($client = $this->acceptNewClient($socket))) { $clients[] = $client; $key = last_key($clients); $client->setName($key); - $this->log($client, 'Connected'); + $this->log($client, pht('Connected')); $this->idleSince = time(); // Check if we've hit the client limit. If there's a configured // client limit and we've hit it, stop accepting new connections // and close the socket. $this->lifetimeClientCount++; if ($this->clientLimit) { if ($this->lifetimeClientCount >= $this->clientLimit) { $this->closeSocket(); $socket = null; } } } // Update all the active clients. foreach ($clients as $key => $client) { if ($this->updateClient($client, $hg)) { // In this case, the client is still connected so just move on to // the next one. Otherwise we continue below and handle the // disconnect. continue; } - $this->log($client, 'Disconnected'); + $this->log($client, pht('Disconnected')); unset($clients[$key]); // If we have a client limit and we've served that many clients, exit. if ($this->clientLimit) { if ($this->lifetimeClientCount >= $this->clientLimit) { if (!$clients) { - $this->log(null, 'Exiting (Client Limit)'); + $this->log(null, pht('Exiting (Client Limit)')); return; } } } } // If we have an idle limit and haven't had any activity in at least // that long, exit. if ($this->idleLimit) { $remaining = $this->idleLimit - (time() - $this->idleSince); if ($remaining <= 0) { - $this->log(null, 'Exiting (Idle Limit)'); + $this->log(null, pht('Exiting (Idle Limit)')); return; } if ($remaining <= 5) { - $this->log(null, 'Exiting in '.$remaining.' seconds'); + $this->log(null, pht('Exiting in %d seconds', $remaining)); } } } } /** * Update one client, processing any commands it has sent us. We fully * process all commands we've received here before returning to the main * server loop. * * @param ArcanistHgClientChannel The client to update. * @param ArcanistHgServerChannel The Mercurial server. * * @task server */ private function updateClient( ArcanistHgClientChannel $client, ArcanistHgServerChannel $hg) { if (!$client->update()) { // Client has disconnected, don't bother proceeding. return false; } // Read a command from the client if one is available. Note that we stop // updating other clients or accepting new connections while processing a // command, since there isn't much we can do with them until the server // finishes executing this command. $message = $client->read(); if (!$message) { return true; } $this->log($client, '$ '.$message[0].' '.$message[1]); $t_start = microtime(true); // Forward the command to the server. $hg->write($message); while (true) { PhutilChannel::waitForAny(array($client, $hg)); if (!$client->update() || !$hg->update()) { // If either the client or server has exited, bail. return false; } $response = $hg->read(); if (!$response) { continue; } // Forward the response back to the client. $client->write($response); // If the response was on the 'r'esult channel, it indicates the end // of the command output. We can process the next command (if any // remain) or go back to accepting new connections and servicing // other clients. if ($response[0] == 'r') { // Update the client immediately to try to get the bytes on the wire // as quickly as possible. This gives us slightly more throughput. $client->update(); break; } } // Log the elapsed time. $t_end = microtime(true); $t = 1000000 * ($t_end - $t_start); - $this->log($client, '< '.number_format($t, 0).'us'); + $this->log($client, pht('< %sus', number_format($t, 0))); $this->idleSince = time(); return true; } /* -( Managing Clients )--------------------------------------------------- */ /** * @task client */ public static function getPathToSocket($working_copy) { return $working_copy.'/.hg/hgdaemon-socket'; } /** * @task client */ private function startWorkingCopySocket() { $errno = null; $errstr = null; $socket_path = self::getPathToSocket($this->workingCopy); $socket_uri = 'unix://'.$socket_path; $socket = @stream_socket_server($socket_uri, $errno, $errstr); if ($errno || !$socket) { Filesystem::remove($socket_path); $socket = @stream_socket_server($socket_uri, $errno, $errstr); } if ($errno || !$socket) { throw new Exception( - "Unable to start socket! Error #{$errno}: {$errstr}"); + pht( + 'Unable to start socket! Error #%d: %s', + $errno, + $errstr)); } $ok = stream_set_blocking($socket, 0); if ($ok === false) { - throw new Exception('Unable to set socket nonblocking!'); + throw new Exception(pht('Unable to set socket nonblocking!')); } return $socket; } /** * @task client */ private function acceptNewClient($socket) { // NOTE: stream_socket_accept() always blocks, even when the socket has // been set nonblocking. $new_client = @stream_socket_accept($socket, $timeout = 0); if (!$new_client) { return null; } $channel = new PhutilSocketChannel($new_client); $client = new ArcanistHgClientChannel($channel); if (!$this->skipHello) { $client->write($this->hello); } return $client; } /* -( Managing Mercurial )------------------------------------------------- */ /** * Starts a Mercurial process which can actually handle requests. * * @return ArcanistHgServerChannel Channel to the Mercurial server. * @task hg */ private function startMercurialProcess() { // NOTE: "cmdserver.log=-" makes Mercurial use the 'd'ebug channel for // log messages. $future = new ExecFuture( 'HGPLAIN=1 hg --config cmdserver.log=- serve --cmdserver pipe'); $future->setCWD($this->workingCopy); $channel = new PhutilExecChannel($future); $hg = new ArcanistHgServerChannel($channel); // The server sends a "hello" message with capability and encoding // information. Save it and forward it to clients when they connect. $this->hello = $hg->waitForMessage(); return $hg; } /* -( Internals )---------------------------------------------------------- */ /** * Close and remove the unix domain socket in the working copy. * * @task internal */ public function __destruct() { $this->closeSocket(); } private function closeSocket() { if ($this->socket) { @stream_socket_shutdown($this->socket, STREAM_SHUT_RDWR); @fclose($this->socket); Filesystem::remove(self::getPathToSocket($this->workingCopy)); $this->socket = null; } } private function log($client, $message) { if ($this->quiet) { return; } if ($client) { - $message = '[Client '.$client->getName().'] '.$message; + $message = sprintf( + '[%s] %s', + pht('Client %s', $client->getName()), + $message); } else { - $message = '[Server] '.$message; + $message = sprintf( + '[%s] %s', + pht('Server'), + $message); } echo $message."\n"; } private function daemonize() { // Keep stdout if it's been redirected somewhere, otherwise shut it down. $keep_stdout = false; $keep_stderr = false; if (function_exists('posix_isatty')) { if (!posix_isatty(STDOUT)) { $keep_stdout = true; } if (!posix_isatty(STDERR)) { $keep_stderr = true; } } $pid = pcntl_fork(); if ($pid === -1) { - throw new Exception('Unable to fork!'); + throw new Exception(pht('Unable to fork!')); } else if ($pid) { // We're the parent; exit. First, drop our reference to the socket so // our __destruct() doesn't tear it down; the child will tear it down // later. $this->socket = null; exit(0); } // We're the child; continue. fclose(STDIN); if (!$keep_stdout) { fclose(STDOUT); $this->quiet = true; } if (!$keep_stderr) { fclose(STDERR); } } } diff --git a/src/hgdaemon/ArcanistHgServerChannel.php b/src/hgdaemon/ArcanistHgServerChannel.php index b16f3bd1..1850c9af 100644 --- a/src/hgdaemon/ArcanistHgServerChannel.php +++ b/src/hgdaemon/ArcanistHgServerChannel.php @@ -1,177 +1,178 @@ * * The first character in a message from the server is the "channel". Mercurial * channels have nothing to do with Phutil channels; they are more similar to * stdout/stderr. Mercurial has four primary channels: * * 'o'utput, like stdout * 'e'rror, like stderr * 'r'esult, like return codes * 'd'ebug, like an external log file * * In PHP, the format of these messages is a pair, with the channel and then * the data: * * array('o', ''); * * In general, we send "runcommand" requests, and the server responds with * a series of messages on the "output" channel and then a single response * on the "result" channel to indicate that output is complete. * * @task protocol Protocol Implementation */ final class ArcanistHgServerChannel extends PhutilProtocolChannel { const MODE_CHANNEL = 'channel'; const MODE_LENGTH = 'length'; const MODE_BLOCK = 'block'; private $mode = self::MODE_CHANNEL; private $byteLengthOfNextChunk = 1; private $buf = ''; /* -( Protocol Implementation )-------------------------------------------- */ /** * Encode a message for transmission to the server. The message should be * formatted as an array, like this: * * array( * 'runcommand', * 'log', * '-l', * '5', * ); * * * We will return the cmdserver version of this: * * runcommand\n * 8 # Length, as a 4-byte unsigned long. * log\0 * -l\0 * 5 * * @param list List of command arguments. * @return string Encoded string for transmission to the server. * * @task protocol */ protected function encodeMessage($argv) { if (!is_array($argv)) { - throw new Exception('Message to Mercurial server should be an array.'); + throw new Exception( + pht('Message to Mercurial server should be an array.')); } $command = head($argv); $args = array_slice($argv, 1); $args = implode("\0", $args); $len = strlen($args); $len = pack('N', $len); return "{$command}\n{$len}{$args}"; } /** * Decode a message received from the server. The message looks like this: * * o * 1234 # Length, as a 4-byte unsigned long. * * * ...where 'o' is the "channel" the message is being sent over. * * We decode into a pair in PHP, which looks like this: * * array('o', ''); * * @param string Bytes from the server. * @return list> Zero or more complete messages. * * @task protocol */ protected function decodeStream($data) { $this->buf .= $data; // We always know how long the next chunk is, so this parser is fairly // easy to implement. $messages = array(); while ($this->byteLengthOfNextChunk <= strlen($this->buf)) { $chunk = substr($this->buf, 0, $this->byteLengthOfNextChunk); $this->buf = substr($this->buf, $this->byteLengthOfNextChunk); switch ($this->mode) { case self::MODE_CHANNEL: // We've received the channel name, one of 'o', 'e', 'r' or 'd' for // 'output', 'error', 'result' or 'debug' respectively. This is a // single byte long. Next, we'll expect a length. $this->channel = $chunk; $this->byteLengthOfNextChunk = 4; $this->mode = self::MODE_LENGTH; break; case self::MODE_LENGTH: // We've received the length of the data, as a 4-byte big-endian // unsigned integer. Next, we'll expect the data itself. $this->byteLengthOfNextChunk = head(unpack('N', $chunk)); $this->mode = self::MODE_BLOCK; break; case self::MODE_BLOCK: // We've received the data itself, which is a block of bytes of the // given length. We produce a message from the channel and the data // and return it. Next, we expect another channel name. $message = array($this->channel, $chunk); $this->byteLengthOfNextChunk = 1; $this->mode = self::MODE_CHANNEL; $this->channel = null; $messages[] = $message; break; } } // Return zero or more messages, which might look something like this: // // array( // array('o', '<...>'), // array('o', '<...>'), // array('r', '<...>'), // ); return $messages; } } diff --git a/src/lint/ArcanistLintPatcher.php b/src/lint/ArcanistLintPatcher.php index 1cea8dc3..5bbd2503 100644 --- a/src/lint/ArcanistLintPatcher.php +++ b/src/lint/ArcanistLintPatcher.php @@ -1,141 +1,143 @@ lintResult = $result; return $obj; } public function getUnmodifiedFileContent() { return $this->lintResult->getData(); } public function getModifiedFileContent() { if ($this->modifiedData === null) { $this->buildModifiedFile(); } return $this->modifiedData; } public function writePatchToDisk() { $path = $this->lintResult->getFilePathOnDisk(); $data = $this->getModifiedFileContent(); $ii = null; do { $lint = $path.'.linted'.($ii++); } while (file_exists($lint)); // Copy existing file to preserve permissions. 'chmod --reference' is not // supported under OSX. if (Filesystem::pathExists($path)) { // This path may not exist if we're generating a new file. execx('cp -p %s %s', $path, $lint); } Filesystem::writeFile($lint, $data); list($err) = exec_manual('mv -f %s %s', $lint, $path); if ($err) { throw new Exception( - "Unable to overwrite path `{$path}', patched version was left ". - "at `{$lint}'."); + pht( + "Unable to overwrite path '%s', patched version was left at '%s'.", + $path, + $lint)); } foreach ($this->applyMessages as $message) { $message->didApplyPatch(); } } private function __construct() {} private function buildModifiedFile() { $data = $this->getUnmodifiedFileContent(); foreach ($this->lintResult->getMessages() as $lint) { if (!$lint->isPatchable()) { continue; } $orig_offset = $this->getCharacterOffset($lint->getLine() - 1); $orig_offset += $lint->getChar() - 1; $dirty = $this->getDirtyCharacterOffset(); if ($dirty > $orig_offset) { continue; } // Adjust the character offset by the delta *after* checking for // dirtiness. The dirty character cursor is a cursor on the original file, // and should be compared with the patch position in the original file. $working_offset = $orig_offset + $this->getCharacterDelta(); $old_str = $lint->getOriginalText(); $old_len = strlen($old_str); $new_str = $lint->getReplacementText(); $new_len = strlen($new_str); if ($working_offset == strlen($data)) { // Temporary hack to work around a destructive hphpi issue, see #451031. $data .= $new_str; } else { $data = substr_replace($data, $new_str, $working_offset, $old_len); } $this->changeCharacterDelta($new_len - $old_len); $this->setDirtyCharacterOffset($orig_offset + $old_len); $this->applyMessages[] = $lint; } $this->modifiedData = $data; } private function getCharacterOffset($line_num) { if ($this->lineOffsets === null) { $lines = explode("\n", $this->getUnmodifiedFileContent()); $this->lineOffsets = array(0); $last = 0; foreach ($lines as $line) { $this->lineOffsets[] = $last + strlen($line) + 1; $last += strlen($line) + 1; } } if ($line_num >= count($this->lineOffsets)) { - throw new Exception("Data has fewer than `{$line}' lines."); + throw new Exception(pht('Data has fewer than %d lines.', $line)); } return idx($this->lineOffsets, $line_num); } private function setDirtyCharacterOffset($offset) { $this->dirtyUntil = $offset; return $this; } private function getDirtyCharacterOffset() { return $this->dirtyUntil; } private function changeCharacterDelta($change) { $this->characterDelta += $change; return $this; } private function getCharacterDelta() { return $this->characterDelta; } } diff --git a/src/lint/ArcanistLintSeverity.php b/src/lint/ArcanistLintSeverity.php index c34756d9..a8da738e 100644 --- a/src/lint/ArcanistLintSeverity.php +++ b/src/lint/ArcanistLintSeverity.php @@ -1,50 +1,50 @@ 'Advice', - self::SEVERITY_AUTOFIX => 'Auto-Fix', - self::SEVERITY_WARNING => 'Warning', - self::SEVERITY_ERROR => 'Error', - self::SEVERITY_DISABLED => 'Disabled', + self::SEVERITY_ADVICE => pht('Advice'), + self::SEVERITY_AUTOFIX => pht('Auto-Fix'), + self::SEVERITY_WARNING => pht('Warning'), + self::SEVERITY_ERROR => pht('Error'), + self::SEVERITY_DISABLED => pht('Disabled'), ); } public static function getStringForSeverity($severity_code) { $map = self::getLintSeverities(); if (!array_key_exists($severity_code, $map)) { - throw new Exception("Unknown lint severity '{$severity_code}'!"); + throw new Exception(pht("Unknown lint severity '%s'!", $severity_code)); } return $map[$severity_code]; } public static function isAtLeastAsSevere($message_sev, $level) { static $map = array( self::SEVERITY_DISABLED => 10, self::SEVERITY_ADVICE => 20, self::SEVERITY_AUTOFIX => 25, self::SEVERITY_WARNING => 30, self::SEVERITY_ERROR => 40, ); if (empty($map[$message_sev])) { return true; } return $map[$message_sev] >= idx($map, $level, 0); } } diff --git a/src/lint/engine/ArcanistLintEngine.php b/src/lint/engine/ArcanistLintEngine.php index 29cf13d6..e320fc0c 100644 --- a/src/lint/engine/ArcanistLintEngine.php +++ b/src/lint/engine/ArcanistLintEngine.php @@ -1,610 +1,612 @@ configurationManager = $configuration_manager; return $this; } final public function getConfigurationManager() { return $this->configurationManager; } final public function setWorkingCopy( ArcanistWorkingCopyIdentity $working_copy) { $this->workingCopy = $working_copy; return $this; } final public function getWorkingCopy() { return $this->workingCopy; } final public function setPaths($paths) { $this->paths = $paths; return $this; } public function getPaths() { return $this->paths; } final public function setPathChangedLines($path, $changed) { if ($changed === null) { $this->changedLines[$path] = null; } else { $this->changedLines[$path] = array_fill_keys($changed, true); } return $this; } final public function getPathChangedLines($path) { return idx($this->changedLines, $path); } final public function setFileData($data) { $this->fileData = $data + $this->fileData; return $this; } final public function setEnableAsyncLint($enable_async_lint) { $this->enableAsyncLint = $enable_async_lint; return $this; } final public function getEnableAsyncLint() { return $this->enableAsyncLint; } final public function loadData($path) { if (!isset($this->fileData[$path])) { $disk_path = $this->getFilePathOnDisk($path); $this->fileData[$path] = Filesystem::readFile($disk_path); } return $this->fileData[$path]; } public function pathExists($path) { $disk_path = $this->getFilePathOnDisk($path); return Filesystem::pathExists($disk_path); } final public function isDirectory($path) { $disk_path = $this->getFilePathOnDisk($path); return is_dir($disk_path); } final public function isBinaryFile($path) { try { $data = $this->loadData($path); } catch (Exception $ex) { return false; } return ArcanistDiffUtils::isHeuristicBinaryFile($data); } final public function isSymbolicLink($path) { return is_link($this->getFilePathOnDisk($path)); } final public function getFilePathOnDisk($path) { return Filesystem::resolvePath( $path, $this->getWorkingCopy()->getProjectRoot()); } final public function setMinimumSeverity($severity) { $this->minimumSeverity = $severity; return $this; } final public function run() { $linters = $this->buildLinters(); if (!$linters) { - throw new ArcanistNoEffectException('No linters to run.'); + throw new ArcanistNoEffectException(pht('No linters to run.')); } foreach ($linters as $key => $linter) { $linter->setLinterID($key); } $linters = msort($linters, 'getLinterPriority'); foreach ($linters as $linter) { $linter->setEngine($this); } $have_paths = false; foreach ($linters as $linter) { if ($linter->getPaths()) { $have_paths = true; break; } } if (!$have_paths) { - throw new ArcanistNoEffectException('No paths are lintable.'); + throw new ArcanistNoEffectException(pht('No paths are lintable.')); } $versions = array($this->getCacheVersion()); foreach ($linters as $linter) { $version = get_class($linter).':'.$linter->getCacheVersion(); $symbols = id(new PhutilSymbolLoader()) ->setType('class') ->setName(get_class($linter)) ->selectSymbolsWithoutLoading(); $symbol = idx($symbols, 'class$'.get_class($linter)); if ($symbol) { $version .= ':'.md5_file( phutil_get_library_root($symbol['library']).'/'.$symbol['where']); } $versions[] = $version; } $this->cacheVersion = crc32(implode("\n", $versions)); $runnable = $this->getRunnableLinters($linters); $this->stopped = array(); $exceptions = $this->executeLinters($runnable); foreach ($runnable as $linter) { foreach ($linter->getLintMessages() as $message) { if (!$this->isSeverityEnabled($message->getSeverity())) { continue; } if (!$this->isRelevantMessage($message)) { continue; } $message->setGranularity($linter->getCacheGranularity()); $result = $this->getResultForPath($message->getPath()); $result->addMessage($message); } } if ($this->cachedResults) { foreach ($this->cachedResults as $path => $messages) { $messages = idx($messages, $this->cacheVersion, array()); $repository_version = idx($messages, 'repository_version'); unset($messages['stopped']); unset($messages['repository_version']); foreach ($messages as $message) { $use_cache = $this->shouldUseCache( idx($message, 'granularity'), $repository_version); if ($use_cache) { $this->getResultForPath($path)->addMessage( ArcanistLintMessage::newFromDictionary($message)); } } } } foreach ($this->results as $path => $result) { $disk_path = $this->getFilePathOnDisk($path); $result->setFilePathOnDisk($disk_path); if (isset($this->fileData[$path])) { $result->setData($this->fileData[$path]); } else if ($disk_path && Filesystem::pathExists($disk_path)) { // TODO: this may cause us to, e.g., load a large binary when we only // raised an error about its filename. We could refine this by looking // through the lint messages and doing this load only if any of them // have original/replacement text or something like that. try { $this->fileData[$path] = Filesystem::readFile($disk_path); $result->setData($this->fileData[$path]); } catch (FilesystemException $ex) { // Ignore this, it's noncritical that we access this data and it // might be unreadable or a directory or whatever else for plenty // of legitimate reasons. } } } if ($exceptions) { - throw new PhutilAggregateException('Some linters failed:', $exceptions); + throw new PhutilAggregateException( + pht('Some linters failed:'), + $exceptions); } return $this->results; } final public function isSeverityEnabled($severity) { $minimum = $this->minimumSeverity; return ArcanistLintSeverity::isAtLeastAsSevere($severity, $minimum); } final private function shouldUseCache( $cache_granularity, $repository_version) { switch ($cache_granularity) { case ArcanistLinter::GRANULARITY_FILE: return true; case ArcanistLinter::GRANULARITY_DIRECTORY: case ArcanistLinter::GRANULARITY_REPOSITORY: return ($this->repositoryVersion == $repository_version); default: return false; } } /** * @param dict>> * @return this */ final public function setCachedResults(array $results) { $this->cachedResults = $results; return $this; } final public function getResults() { return $this->results; } final public function getStoppedPaths() { return $this->stopped; } abstract public function buildLinters(); final public function setRepositoryVersion($version) { $this->repositoryVersion = $version; return $this; } final private function isRelevantMessage(ArcanistLintMessage $message) { // When a user runs "arc lint", we default to raising only warnings on // lines they have changed (errors are still raised anywhere in the // file). The list of $changed lines may be null, to indicate that the // path is a directory or a binary file so we should not exclude // warnings. if (!$this->changedLines || $message->isError() || $message->shouldBypassChangedLineFiltering()) { return true; } $locations = $message->getOtherLocations(); $locations[] = $message->toDictionary(); foreach ($locations as $location) { $path = idx($location, 'path', $message->getPath()); if (!array_key_exists($path, $this->changedLines)) { continue; } $changed = $this->getPathChangedLines($path); if ($changed === null || !$location['line']) { return true; } $last_line = $location['line']; if (isset($location['original'])) { $last_line += substr_count($location['original'], "\n"); } for ($l = $location['line']; $l <= $last_line; $l++) { if (!empty($changed[$l])) { return true; } } } return false; } final protected function getResultForPath($path) { if (empty($this->results[$path])) { $result = new ArcanistLintResult(); $result->setPath($path); $result->setCacheVersion($this->cacheVersion); $this->results[$path] = $result; } return $this->results[$path]; } final public function getLineAndCharFromOffset($path, $offset) { if (!isset($this->charToLine[$path])) { $char_to_line = array(); $line_to_first_char = array(); $lines = explode("\n", $this->loadData($path)); $line_number = 0; $line_start = 0; foreach ($lines as $line) { $len = strlen($line) + 1; // Account for "\n". $line_to_first_char[] = $line_start; $line_start += $len; for ($ii = 0; $ii < $len; $ii++) { $char_to_line[] = $line_number; } $line_number++; } $this->charToLine[$path] = $char_to_line; $this->lineToFirstChar[$path] = $line_to_first_char; } $line = $this->charToLine[$path][$offset]; $char = $offset - $this->lineToFirstChar[$path][$line]; return array($line, $char); } final public function getPostponedLinters() { return $this->postponedLinters; } final public function setPostponedLinters(array $linters) { $this->postponedLinters = $linters; return $this; } protected function getCacheVersion() { return 1; } /** * Get a named linter resource shared by another linter. * * This mechanism allows linters to share arbitrary resources, like the * results of computation. If several linters need to perform the same * expensive computation step, they can use a named resource to synchronize * construction of the result so it doesn't need to be built multiple * times. * * @param string Resource identifier. * @param wild Optionally, default value to return if resource does not * exist. * @return wild Resource, or default value if not present. */ public function getLinterResource($key, $default = null) { return idx($this->linterResources, $key, $default); } /** * Set a linter resource that other linters can access. * * See @{method:getLinterResource} for a description of this mechanism. * * @param string Resource identifier. * @param wild Resource. * @return this */ public function setLinterResource($key, $value) { $this->linterResources[$key] = $value; return $this; } private function getRunnableLinters(array $linters) { assert_instances_of($linters, 'ArcanistLinter'); // TODO: The canRun() mechanism is only used by one linter, and just // silently disables the linter. Almost every other linter handles this // by throwing `ArcanistMissingLinterException`. Both mechanisms are not // ideal; linters which can not run should emit a message, get marked as // "skipped", and allow execution to continue. See T7045. $runnable = array(); foreach ($linters as $key => $linter) { if ($linter->canRun()) { $runnable[$key] = $linter; } } return $runnable; } private function executeLinters(array $runnable) { $all_paths = $this->getPaths(); $path_chunks = array_chunk($all_paths, 32, $preserve_keys = true); $exception_lists = array(); foreach ($path_chunks as $chunk) { $exception_lists[] = $this->executeLintersOnChunk($runnable, $chunk); } return array_mergev($exception_lists); } private function executeLintersOnChunk(array $runnable, array $path_list) { assert_instances_of($runnable, 'ArcanistLinter'); $path_map = array_fuse($path_list); $exceptions = array(); $did_lint = array(); foreach ($runnable as $linter) { $linter_id = $linter->getLinterID(); $paths = $linter->getPaths(); foreach ($paths as $key => $path) { // If we aren't running this path in the current chunk of paths, // skip it completely. if (empty($path_map[$path])) { unset($paths[$key]); continue; } // Make sure each path has a result generated, even if it is empty // (i.e., the file has no lint messages). $result = $this->getResultForPath($path); // If a linter has stopped all other linters for this path, don't // actually run the linter. if (isset($this->stopped[$path])) { unset($paths[$key]); continue; } // If we have a cached result for this path, don't actually run the // linter. if (isset($this->cachedResults[$path][$this->cacheVersion])) { $cached_result = $this->cachedResults[$path][$this->cacheVersion]; $use_cache = $this->shouldUseCache( $linter->getCacheGranularity(), idx($cached_result, 'repository_version')); if ($use_cache) { unset($paths[$key]); if (idx($cached_result, 'stopped') == $linter_id) { $this->stopped[$path] = $linter_id; } } } } $paths = array_values($paths); if (!$paths) { continue; } try { $this->executeLinterOnPaths($linter, $paths); $did_lint[] = array($linter, $paths); } catch (Exception $ex) { $exceptions[] = $ex; } } foreach ($did_lint as $info) { list($linter, $paths) = $info; try { $this->executeDidLintOnPaths($linter, $paths); } catch (Exception $ex) { $exceptions[] = $ex; } } return $exceptions; } private function beginLintServiceCall(ArcanistLinter $linter, array $paths) { $profiler = PhutilServiceProfiler::getInstance(); return $profiler->beginServiceCall( array( 'type' => 'lint', 'linter' => $linter->getInfoName(), 'paths' => $paths, )); } private function endLintServiceCall($call_id) { $profiler = PhutilServiceProfiler::getInstance(); $profiler->endServiceCall($call_id, array()); } private function executeLinterOnPaths(ArcanistLinter $linter, array $paths) { $call_id = $this->beginLintServiceCall($linter, $paths); try { $linter->willLintPaths($paths); foreach ($paths as $path) { $linter->setActivePath($path); $linter->lintPath($path); if ($linter->didStopAllLinters()) { $this->stopped[$path] = $linter->getLinterID(); } } } catch (Exception $ex) { $this->endLintServiceCall($call_id); throw $ex; } $this->endLintServiceCall($call_id); } private function executeDidLintOnPaths(ArcanistLinter $linter, array $paths) { $call_id = $this->beginLintServiceCall($linter, $paths); try { $linter->didLintPaths($paths); } catch (Exception $ex) { $this->endLintServiceCall($call_id); throw $ex; } $this->endLintServiceCall($call_id); } } diff --git a/src/lint/linter/ArcanistBaseXHPASTLinter.php b/src/lint/linter/ArcanistBaseXHPASTLinter.php index a6279745..3549a353 100644 --- a/src/lint/linter/ArcanistBaseXHPASTLinter.php +++ b/src/lint/linter/ArcanistBaseXHPASTLinter.php @@ -1,212 +1,213 @@ getVersion(); $version = PhutilXHPASTBinary::getVersion(); if ($version) { $parts[] = $version; } return implode('-', $parts); } final protected function raiseLintAtToken( XHPASTToken $token, $code, $desc, $replace = null) { return $this->raiseLintAtOffset( $token->getOffset(), $code, $desc, $token->getValue(), $replace); } final protected function raiseLintAtNode( XHPASTNode $node, $code, $desc, $replace = null) { return $this->raiseLintAtOffset( $node->getOffset(), $code, $desc, $node->getConcreteString(), $replace); } final protected function buildFutures(array $paths) { return $this->getXHPASTLinter()->buildSharedFutures($paths); } protected function didResolveLinterFutures(array $futures) { $this->getXHPASTLinter()->releaseSharedFutures(array_keys($futures)); } /* -( Sharing Parse Trees )------------------------------------------------ */ /** * Get the linter object which is responsible for building parse trees. * * When the engine specifies that several XHPAST linters should execute, * we designate one of them as the one which will actually build parse trees. * The other linters share trees, so they don't have to recompute them. * * Roughly, the first linter to execute elects itself as the builder. * Subsequent linters request builds and retrieve results from it. * * @return ArcanistBaseXHPASTLinter Responsible linter. * @task sharing */ final protected function getXHPASTLinter() { $resource_key = 'xhpast.linter'; // If we're the first linter to run, share ourselves. Otherwise, grab the // previously shared linter. $engine = $this->getEngine(); $linter = $engine->getLinterResource($resource_key); if (!$linter) { $linter = $this; $engine->setLinterResource($resource_key, $linter); } $base_class = __CLASS__; if (!($linter instanceof $base_class)) { throw new Exception( pht( 'Expected resource "%s" to be an instance of "%s"!', $resource_key, $base_class)); } return $linter; } /** * Build futures on this linter, for use and to share with other linters. * * @param list Paths to build futures for. * @return list Futures. * @task sharing */ final protected function buildSharedFutures(array $paths) { foreach ($paths as $path) { if (!isset($this->futures[$path])) { $this->futures[$path] = PhutilXHPASTBinary::getParserFuture( $this->getData($path)); $this->refcount[$path] = 1; } else { $this->refcount[$path]++; } } return array_select_keys($this->futures, $paths); } /** * Release futures on this linter which are no longer in use elsewhere. * * @param list Paths to release futures for. * @return void * @task sharing */ final protected function releaseSharedFutures(array $paths) { foreach ($paths as $path) { if (empty($this->refcount[$path])) { throw new Exception( pht( 'Imbalanced calls to shared futures: each call to '. - 'buildSharedFutures() for a path must be paired with a call to '. - 'releaseSharedFutures().')); + '%s for a path must be paired with a call to %s.', + 'buildSharedFutures()', + 'releaseSharedFutures()')); } $this->refcount[$path]--; if (!$this->refcount[$path]) { unset($this->refcount[$path]); unset($this->futures[$path]); unset($this->trees[$path]); unset($this->exceptions[$path]); } } } /** * Get a path's tree from the responsible linter. * * @param string Path to retrieve tree for. * @return XHPASTTree|null Tree, or null if unparseable. * @task sharing */ final protected function getXHPASTTreeForPath($path) { // If we aren't the linter responsible for actually building the parse // trees, go get the tree from that linter. if ($this->getXHPASTLinter() !== $this) { return $this->getXHPASTLinter()->getXHPASTTreeForPath($path); } if (!array_key_exists($path, $this->trees)) { $this->trees[$path] = null; try { $this->trees[$path] = XHPASTTree::newFromDataAndResolvedExecFuture( $this->getData($path), $this->futures[$path]->resolve()); $root = $this->trees[$path]->getRootNode(); $root->buildSelectCache(); $root->buildTokenCache(); } catch (Exception $ex) { $this->exceptions[$path] = $ex; } } return $this->trees[$path]; } /** * Get a path's parse exception from the responsible linter. * * @param string Path to retrieve exception for. * @return Exeption|null Parse exception, if available. * @task sharing */ final protected function getXHPASTExceptionForPath($path) { if ($this->getXHPASTLinter() !== $this) { return $this->getXHPASTLinter()->getXHPASTExceptionForPath($path); } return idx($this->exceptions, $path); } public function getSuperGlobalNames() { return array( '$GLOBALS', '$_SERVER', '$_GET', '$_POST', '$_FILES', '$_COOKIE', '$_SESSION', '$_REQUEST', '$_ENV', ); } } diff --git a/src/lint/linter/ArcanistCSharpLinter.php b/src/lint/linter/ArcanistCSharpLinter.php index 89c1f632..6120a07b 100644 --- a/src/lint/linter/ArcanistCSharpLinter.php +++ b/src/lint/linter/ArcanistCSharpLinter.php @@ -1,249 +1,261 @@ 'map>', 'help' => pht('Provide a discovery map.'), ); // TODO: This should probably be replaced with "bin" when this moves // to extend ExternalLinter. $options['binary'] = array( 'type' => 'string', 'help' => pht('Override default binary.'), ); return $options; } public function setLinterConfigurationValue($key, $value) { switch ($key) { case 'discovery': $this->discoveryMap = $value; return; case 'binary': $this->cslintHintPath = $value; return; } parent::setLinterConfigurationValue($key, $value); } protected function getLintCodeFromLinterConfigurationKey($code) { return $code; } public function setCustomSeverityMap(array $map) { foreach ($map as $code => $severity) { if (substr($code, 0, 2) === 'SA' && $severity == 'disabled') { throw new Exception( - "In order to keep StyleCop integration with IDEs and other tools ". - "consistent with Arcanist results, you aren't permitted to ". - "disable StyleCop rules within '.arclint'. ". - "Instead configure the severity using the StyleCop settings dialog ". - "(usually accessible from within your IDE). StyleCop settings ". - "for your project will be used when linting for Arcanist."); + pht( + "In order to keep StyleCop integration with IDEs and other tools ". + "consistent with Arcanist results, you aren't permitted to ". + "disable StyleCop rules within '%s'. Instead configure the ". + "severity using the StyleCop settings dialog (usually accessible ". + "from within your IDE). StyleCop settings for your project will ". + "be used when linting for Arcanist.", + '.arclint')); } } return parent::setCustomSeverityMap($map); } /** * Determines what executables and lint paths to use. Between platforms * this also changes whether the lint engine is run under .NET or Mono. It * also ensures that all of the required binaries are available for the lint * to run successfully. * * @return void */ private function loadEnvironment() { if ($this->loaded) { return; } // Determine runtime engine (.NET or Mono). if (phutil_is_windows()) { $this->runtimeEngine = ''; } else if (Filesystem::binaryExists('mono')) { $this->runtimeEngine = 'mono '; } else { - throw new Exception('Unable to find Mono and you are not on Windows!'); + throw new Exception( + pht('Unable to find Mono and you are not on Windows!')); } // Determine cslint path. $cslint = $this->cslintHintPath; if ($cslint !== null && file_exists($cslint)) { $this->cslintEngine = Filesystem::resolvePath($cslint); } else if (Filesystem::binaryExists('cslint.exe')) { $this->cslintEngine = 'cslint.exe'; } else { - throw new Exception('Unable to locate cslint.'); + throw new Exception(pht('Unable to locate %s.', 'cslint')); } // Determine cslint version. $ver_future = new ExecFuture( '%C -v', $this->runtimeEngine.$this->cslintEngine); list($err, $stdout, $stderr) = $ver_future->resolve(); if ($err !== 0) { throw new Exception( - 'You are running an old version of cslint. Please '. - 'upgrade to version '.self::SUPPORTED_VERSION.'.'); + pht( + 'You are running an old version of %s. Please '. + 'upgrade to version %s.', + 'cslint', + self::SUPPORTED_VERSION)); } $ver = (int)$stdout; if ($ver < self::SUPPORTED_VERSION) { throw new Exception( - 'You are running an old version of cslint. Please '. - 'upgrade to version '.self::SUPPORTED_VERSION.'.'); + pht( + 'You are running an old version of %s. Please '. + 'upgrade to version %s.', + 'cslint', + self::SUPPORTED_VERSION)); } else if ($ver > self::SUPPORTED_VERSION) { throw new Exception( - 'Arcanist does not support this version of cslint (it is '. - 'newer). You can try upgrading Arcanist with `arc upgrade`.'); + pht( + 'Arcanist does not support this version of %s (it is newer). '. + 'You can try upgrading Arcanist with `%s`.', + 'cslint', + 'arc upgrade')); } $this->loaded = true; } public function lintPath($path) {} public function willLintPaths(array $paths) { $this->loadEnvironment(); $futures = array(); // Bulk linting up into futures, where the number of files // is based on how long the command is. $current_paths = array(); foreach ($paths as $path) { // If the current paths for the command, plus the next path // is greater than 6000 characters (less than the Windows // command line limit), then finalize this future and add it. $total = 0; foreach ($current_paths as $current_path) { $total += strlen($current_path) + 3; // Quotes and space. } if ($total + strlen($path) > 6000) { // %s won't pass through the JSON correctly // under Windows. This is probably because not only // does the JSON have quotation marks in the content, // but because there'll be a lot of escaping and // double escaping because the JSON also contains // regular expressions. cslint supports passing the // settings JSON through base64-encoded to mitigate // this issue. $futures[] = new ExecFuture( '%C --settings-base64=%s -r=. %Ls', $this->runtimeEngine.$this->cslintEngine, base64_encode(json_encode($this->discoveryMap)), $current_paths); $current_paths = array(); } // Append the path to the current paths array. $current_paths[] = $this->getEngine()->getFilePathOnDisk($path); } // If we still have paths left in current paths, then we need to create // a future for those too. if (count($current_paths) > 0) { $futures[] = new ExecFuture( '%C --settings-base64=%s -r=. %Ls', $this->runtimeEngine.$this->cslintEngine, base64_encode(json_encode($this->discoveryMap)), $current_paths); $current_paths = array(); } $this->futures = $futures; } public function didLintPaths(array $paths) { if ($this->futures) { $futures = id(new FutureIterator($this->futures)) ->limit(8); foreach ($futures as $future) { $this->resolveFuture($future); } $this->futures = array(); } } protected function resolveFuture(Future $future) { list($stdout) = $future->resolvex(); $all_results = json_decode($stdout); foreach ($all_results as $results) { if ($results === null || $results->Issues === null) { return; } foreach ($results->Issues as $issue) { $message = new ArcanistLintMessage(); $message->setPath($results->FileName); $message->setLine($issue->LineNumber); $message->setCode($issue->Index->Code); $message->setName($issue->Index->Name); $message->setChar($issue->Column); $message->setOriginalText($issue->OriginalText); $message->setReplacementText($issue->ReplacementText); $desc = @vsprintf($issue->Index->Message, $issue->Parameters); if ($desc === false) { $desc = $issue->Index->Message; } $message->setDescription($desc); $severity = ArcanistLintSeverity::SEVERITY_ADVICE; switch ($issue->Index->Severity) { case 0: $severity = ArcanistLintSeverity::SEVERITY_ADVICE; break; case 1: $severity = ArcanistLintSeverity::SEVERITY_AUTOFIX; break; case 2: $severity = ArcanistLintSeverity::SEVERITY_WARNING; break; case 3: $severity = ArcanistLintSeverity::SEVERITY_ERROR; break; case 4: $severity = ArcanistLintSeverity::SEVERITY_DISABLED; break; } $severity_override = $this->getLintMessageSeverity($issue->Index->Code); if ($severity_override !== null) { $severity = $severity_override; } $message->setSeverity($severity); $this->addLintMessage($message); } } } protected function getDefaultMessageSeverity($code) { return null; } } diff --git a/src/lint/linter/ArcanistCppcheckLinter.php b/src/lint/linter/ArcanistCppcheckLinter.php index 830452a5..d5df5acb 100644 --- a/src/lint/linter/ArcanistCppcheckLinter.php +++ b/src/lint/linter/ArcanistCppcheckLinter.php @@ -1,114 +1,118 @@ getDeprecatedConfiguration('lint.cppcheck.prefix'); $bin = $this->getDeprecatedConfiguration('lint.cppcheck.bin', 'cppcheck'); if ($prefix) { return $prefix.'/'.$bin; } else { return $bin; } } public function getVersion() { list($stdout) = execx('%C --version', $this->getExecutableCommand()); $matches = array(); $regex = '/^Cppcheck (?P\d+\.\d+)$/'; if (preg_match($regex, $stdout, $matches)) { return $matches['version']; } else { return false; } } public function getInstallInstructions() { - return pht('Install Cppcheck using `apt-get install cppcheck` or similar.'); + return pht( + 'Install Cppcheck using `%s` or similar.', + 'apt-get install cppcheck'); } protected function getMandatoryFlags() { return array( '--quiet', '--inline-suppr', '--xml', '--xml-version=2', ); } protected function getDefaultFlags() { return $this->getDeprecatedConfiguration( 'lint.cppcheck.options', array('-j2', '--enable=performance,style,portability,information')); } public function shouldExpectCommandErrors() { return false; } protected function parseLinterOutput($path, $err, $stdout, $stderr) { $dom = new DOMDocument(); $ok = @$dom->loadXML($stderr); if (!$ok) { return false; } $errors = $dom->getElementsByTagName('error'); $messages = array(); foreach ($errors as $error) { foreach ($error->getElementsByTagName('location') as $location) { $message = new ArcanistLintMessage(); $message->setPath($location->getAttribute('file')); $message->setLine($location->getAttribute('line')); $message->setCode('Cppcheck'); $message->setName($error->getAttribute('id')); $message->setDescription($error->getAttribute('msg')); switch ($error->getAttribute('severity')) { case 'error': $message->setSeverity(ArcanistLintSeverity::SEVERITY_ERROR); break; default: if ($error->getAttribute('inconclusive')) { $message->setSeverity(ArcanistLintSeverity::SEVERITY_ADVICE); } else { $message->setSeverity(ArcanistLintSeverity::SEVERITY_WARNING); } break; } $messages[] = $message; } } return $messages; } } diff --git a/src/lint/linter/ArcanistCpplintLinter.php b/src/lint/linter/ArcanistCpplintLinter.php index 7d8b4bbc..3587131d 100644 --- a/src/lint/linter/ArcanistCpplintLinter.php +++ b/src/lint/linter/ArcanistCpplintLinter.php @@ -1,82 +1,84 @@ getDeprecatedConfiguration('lint.cpplint.prefix'); $bin = $this->getDeprecatedConfiguration('lint.cpplint.bin', 'cpplint.py'); if ($prefix) { return $prefix.'/'.$bin; } else { return $bin; } } public function getInstallInstructions() { - return pht('Install cpplint.py using `wget http://google-styleguide.'. - 'googlecode.com/svn/trunk/cpplint/cpplint.py`.'); + return pht( + 'Install cpplint.py using `%s`.', + 'wget http://google-styleguide.googlecode.com'. + '/svn/trunk/cpplint/cpplint.py'); } protected function getDefaultFlags() { return $this->getDeprecatedConfiguration('lint.cpplint.options', array()); } protected function parseLinterOutput($path, $err, $stdout, $stderr) { $lines = explode("\n", $stderr); $messages = array(); foreach ($lines as $line) { $line = trim($line); $matches = null; $regex = '/^-:(\d+):\s*(.*)\s*\[(.*)\] \[(\d+)\]$/'; if (!preg_match($regex, $line, $matches)) { continue; } foreach ($matches as $key => $match) { $matches[$key] = trim($match); } $message = new ArcanistLintMessage(); $message->setPath($path); $message->setLine($matches[1]); $message->setCode($matches[3]); $message->setName($matches[3]); $message->setDescription($matches[2]); $message->setSeverity(ArcanistLintSeverity::SEVERITY_WARNING); $messages[] = $message; } if ($err && !$messages) { return false; } return $messages; } protected function getLintCodeFromLinterConfigurationKey($code) { if (!preg_match('@^[a-z_]+/[a-z_]+$@', $code)) { throw new Exception( pht( 'Unrecognized lint message code "%s". Expected a valid cpplint '. 'lint code like "%s" or "%s".', $code, 'build/include_order', 'whitespace/braces')); } return $code; } } diff --git a/src/lint/linter/ArcanistFlake8Linter.php b/src/lint/linter/ArcanistFlake8Linter.php index 6c1c6d6f..4cdab661 100644 --- a/src/lint/linter/ArcanistFlake8Linter.php +++ b/src/lint/linter/ArcanistFlake8Linter.php @@ -1,128 +1,129 @@ getDeprecatedConfiguration('lint.flake8.options', array()); } public function getDefaultBinary() { $prefix = $this->getDeprecatedConfiguration('lint.flake8.prefix'); $bin = $this->getDeprecatedConfiguration('lint.flake8.bin', 'flake8'); if ($prefix) { return $prefix.'/'.$bin; } else { return $bin; } } public function getVersion() { list($stdout) = execx('%C --version', $this->getExecutableCommand()); $matches = array(); if (preg_match('/^(?P\d+\.\d+(?:\.\d+)?)\b/', $stdout, $matches)) { return $matches['version']; } else { return false; } } public function getInstallInstructions() { - return pht('Install flake8 using `easy_install flake8`.'); + return pht('Install flake8 using `%s`.', 'easy_install flake8'); } protected function parseLinterOutput($path, $err, $stdout, $stderr) { $lines = phutil_split_lines($stdout, false); $messages = array(); foreach ($lines as $line) { $matches = null; // stdin:2: W802 undefined name 'foo' # pyflakes // stdin:3:1: E302 expected 2 blank lines, found 1 # pep8 $regexp = '/^(.*?):(\d+):(?:(\d+):)? (\S+) (.*)$/'; if (!preg_match($regexp, $line, $matches)) { continue; } foreach ($matches as $key => $match) { $matches[$key] = trim($match); } $message = new ArcanistLintMessage(); $message->setPath($path); $message->setLine($matches[2]); if (!empty($matches[3])) { $message->setChar($matches[3]); } $message->setCode($matches[4]); $message->setName($this->getLinterName().' '.$matches[3]); $message->setDescription($matches[5]); $message->setSeverity($this->getLintMessageSeverity($matches[4])); $messages[] = $message; } if ($err && !$messages) { return false; } return $messages; } protected function getDefaultMessageSeverity($code) { if (preg_match('/^C/', $code)) { // "C": Cyclomatic complexity return ArcanistLintSeverity::SEVERITY_ADVICE; } else if (preg_match('/^W/', $code)) { // "W": PEP8 Warning return ArcanistLintSeverity::SEVERITY_WARNING; } else { // "E": PEP8 Error // "F": PyFlakes Error return ArcanistLintSeverity::SEVERITY_ERROR; } } protected function getLintCodeFromLinterConfigurationKey($code) { if (!preg_match('/^(E|W|C|F)\d+$/', $code)) { throw new Exception( pht( 'Unrecognized lint message code "%s". Expected a valid flake8 '. 'lint code like "%s", or "%s", or "%s", or "%s".', $code, 'E225', 'W291', 'F811', 'C901')); } return $code; } } diff --git a/src/lint/linter/ArcanistGeneratedLinter.php b/src/lint/linter/ArcanistGeneratedLinter.php index 29c1e809..4d01bca7 100644 --- a/src/lint/linter/ArcanistGeneratedLinter.php +++ b/src/lint/linter/ArcanistGeneratedLinter.php @@ -1,42 +1,42 @@ getData($path); if (preg_match('/@'.'generated/', $data)) { $this->stopAllLinters(); } } } diff --git a/src/lint/linter/ArcanistGoLintLinter.php b/src/lint/linter/ArcanistGoLintLinter.php index e886e05c..0252d4c9 100644 --- a/src/lint/linter/ArcanistGoLintLinter.php +++ b/src/lint/linter/ArcanistGoLintLinter.php @@ -1,64 +1,66 @@ setPath($path); $message->setLine($matches[1]); $message->setChar($matches[2]); $message->setCode($this->getLinterName()); $message->setDescription(ucfirst(trim($matches[3]))); $message->setSeverity(ArcanistLintSeverity::SEVERITY_ADVICE); $messages[] = $message; } } return $messages; } } diff --git a/src/lint/linter/ArcanistHLintLinter.php b/src/lint/linter/ArcanistHLintLinter.php index 6274e045..5a197ce4 100644 --- a/src/lint/linter/ArcanistHLintLinter.php +++ b/src/lint/linter/ArcanistHLintLinter.php @@ -1,99 +1,102 @@ getExecutableCommand()); $matches = null; if (preg_match('@HLint v(.*),@', $stdout, $matches)) { return $matches[1]; } return null; } protected function parseLinterOutput($path, $err, $stdout, $stderr) { - $json = phutil_json_decode($stdout); $messages = array(); foreach ($json as $fix) { if ($fix === null) { return; } $message = new ArcanistLintMessage(); $message->setCode($this->getLinterName()); $message->setPath($path); $message->setLine($fix['startLine']); $message->setChar($fix['startColumn']); $message->setName($fix['hint']); $message->setOriginalText($fix['from']); $message->setReplacementText($fix['to']); /* Some improvements may slightly change semantics, so attach all necessary notes too. */ $notes = ''; foreach ($fix['note'] as $note) { - $notes .= ' **NOTE**: '.trim($note, '"').'.'; + $notes .= phutil_console_format( + ' **%s**: %s.', + pht('NOTE'), + trim($note, '"')); } $message->setDescription( pht( - 'In module `%s`, declaration `%s`.%s', - $fix['module'], $fix['decl'], $notes)); + 'In module `%s`, declaration `%s`.', + $fix['module'], + $fix['decl']).$notes); switch ($fix['severity']) { case 'Error': $message->setSeverity(ArcanistLintSeverity::SEVERITY_ERROR); break; case 'Warning': $message->setSeverity(ArcanistLintSeverity::SEVERITY_WARNING); break; default: $message->setSeverity(ArcanistLintSeverity::SEVERITY_ADVICE); break; } $messages[] = $message; } return $messages; } } diff --git a/src/lint/linter/ArcanistJSHintLinter.php b/src/lint/linter/ArcanistJSHintLinter.php index 76efca9d..f175a144 100644 --- a/src/lint/linter/ArcanistJSHintLinter.php +++ b/src/lint/linter/ArcanistJSHintLinter.php @@ -1,176 +1,178 @@ getDeprecatedConfiguration('lint.jshint.prefix'); $bin = $this->getDeprecatedConfiguration('lint.jshint.bin', 'jshint'); if ($prefix) { return $prefix.'/'.$bin; } else { return $bin; } } public function getVersion() { // NOTE: `jshint --version` emits version information on stderr, not stdout. list($stdout, $stderr) = execx( '%C --version', $this->getExecutableCommand()); $matches = array(); $regex = '/^jshint v(?P\d+\.\d+\.\d+)$/'; if (preg_match($regex, $stderr, $matches)) { return $matches['version']; } else { return false; } } public function getInstallInstructions() { - return pht('Install JSHint using `npm install -g jshint`.'); + return pht('Install JSHint using `%s`.', 'npm install -g jshint'); } protected function getMandatoryFlags() { $options = array(); $options[] = '--reporter='.dirname(realpath(__FILE__)).'/reporter.js'; if ($this->jshintrc) { $options[] = '--config='.$this->jshintrc; } if ($this->jshintignore) { $options[] = '--exclude-path='.$this->jshintignore; } return $options; } public function getLinterConfigurationOptions() { $options = array( 'jshint.jshintignore' => array( 'type' => 'optional string', 'help' => pht('Pass in a custom jshintignore file path.'), ), 'jshint.jshintrc' => array( 'type' => 'optional string', 'help' => pht('Custom configuration file.'), ), ); return $options + parent::getLinterConfigurationOptions(); } public function setLinterConfigurationValue($key, $value) { switch ($key) { case 'jshint.jshintignore': $this->jshintignore = $value; return; case 'jshint.jshintrc': $this->jshintrc = $value; return; } return parent::setLinterConfigurationValue($key, $value); } protected function getDefaultFlags() { $options = $this->getDeprecatedConfiguration( 'lint.jshint.options', array()); $config = $this->getDeprecatedConfiguration('lint.jshint.config'); if ($config) { $options[] = '--config='.$config; } return $options; } protected function parseLinterOutput($path, $err, $stdout, $stderr) { $errors = null; try { $errors = phutil_json_decode($stdout); } catch (PhutilJSONParserException $ex) { // Something went wrong and we can't decode the output. Exit abnormally. throw new PhutilProxyException( pht('JSHint returned unparseable output.'), $ex); } $messages = array(); foreach ($errors as $err) { $message = new ArcanistLintMessage(); $message->setPath($path); $message->setLine(idx($err, 'line')); $message->setChar(idx($err, 'col')); $message->setCode(idx($err, 'code')); $message->setName('JSHint'.idx($err, 'code')); $message->setDescription(idx($err, 'reason')); $message->setSeverity($this->getLintMessageSeverity(idx($err, 'code'))); $messages[] = $message; } return $messages; } protected function getLintCodeFromLinterConfigurationKey($code) { if (!preg_match('/^(E|W)\d+$/', $code)) { throw new Exception( pht( 'Unrecognized lint message code "%s". Expected a valid JSHint '. 'lint code like "%s" or "%s".', $code, 'E033', 'W093')); } return $code; } } diff --git a/src/lint/linter/ArcanistJSONLintLinter.php b/src/lint/linter/ArcanistJSONLintLinter.php index b4a65bdf..2ac0dbbe 100644 --- a/src/lint/linter/ArcanistJSONLintLinter.php +++ b/src/lint/linter/ArcanistJSONLintLinter.php @@ -1,89 +1,89 @@ getExecutableCommand()); $matches = array(); if (preg_match('/^(?P\d+\.\d+\.\d+)$/', $stdout, $matches)) { return $matches['version']; } else { return false; } } public function getInstallInstructions() { - return pht('Install jsonlint using `npm install -g jsonlint`.'); + return pht('Install jsonlint using `%s`.', 'npm install -g jsonlint'); } protected function getMandatoryFlags() { return array( '--compact', ); } protected function parseLinterOutput($path, $err, $stdout, $stderr) { $lines = phutil_split_lines($stderr, false); $messages = array(); foreach ($lines as $line) { $matches = null; $match = preg_match( '/^(?:(?.+): )?'. 'line (?\d+), col (?\d+), '. '(?.*)$/', $line, $matches); if ($match) { $message = new ArcanistLintMessage(); $message->setPath($path); $message->setLine($matches['line']); $message->setChar($matches['column']); $message->setCode($this->getLinterName()); $message->setDescription(ucfirst($matches['description'])); $message->setSeverity(ArcanistLintSeverity::SEVERITY_ERROR); $messages[] = $message; } } if ($err && !$messages) { return false; } return $messages; } } diff --git a/src/lint/linter/ArcanistJscsLinter.php b/src/lint/linter/ArcanistJscsLinter.php index cc037214..6d8f2537 100644 --- a/src/lint/linter/ArcanistJscsLinter.php +++ b/src/lint/linter/ArcanistJscsLinter.php @@ -1,150 +1,152 @@ getExecutableCommand()); $matches = array(); $regex = '/^(?P\d+\.\d+\.\d+)$/'; if (preg_match($regex, $stdout, $matches)) { return $matches['version']; } else { return false; } } public function getInstallInstructions() { - return pht('Install JSCS using `npm install -g jscs`.'); + return pht('Install JSCS using `%s`.', 'npm install -g jscs'); } protected function getMandatoryFlags() { $options = array(); $options[] = '--reporter=checkstyle'; $options[] = '--no-colors'; if ($this->config) { $options[] = '--config='.$this->config; } if ($this->preset) { $options[] = '--preset='.$this->preset; } return $options; } public function getLinterConfigurationOptions() { $options = array( 'jscs.config' => array( 'type' => 'optional string', - 'help' => pht('Pass in a custom jscsrc file path.'), + 'help' => pht('Pass in a custom %s file path.', 'jscsrc'), ), 'jscs.preset' => array( 'type' => 'optional string', 'help' => pht('Custom preset.'), ), ); return $options + parent::getLinterConfigurationOptions(); } public function setLinterConfigurationValue($key, $value) { switch ($key) { case 'jscs.config': $this->config = $value; return; case 'jscs.preset': $this->preset = $value; return; } return parent::setLinterConfigurationValue($key, $value); } protected function parseLinterOutput($path, $err, $stdout, $stderr) { $report_dom = new DOMDocument(); $ok = @$report_dom->loadXML($stdout); if (!$ok) { return false; } $messages = array(); foreach ($report_dom->getElementsByTagName('file') as $file) { foreach ($file->getElementsByTagName('error') as $error) { $message = new ArcanistLintMessage(); $message->setPath($path); $message->setLine($error->getAttribute('line')); $message->setChar($error->getAttribute('column')); $message->setCode('JSCS'); $message->setDescription($error->getAttribute('message')); switch ($error->getAttribute('severity')) { case 'error': $message->setSeverity(ArcanistLintSeverity::SEVERITY_ERROR); break; case 'warning': $message->setSeverity(ArcanistLintSeverity::SEVERITY_WARNING); break; default: $message->setSeverity(ArcanistLintSeverity::SEVERITY_ADVICE); break; } $messages[] = $message; } } if ($err && !$messages) { return false; } return $messages; } protected function getLintCodeFromLinterConfigurationKey($code) { // NOTE: We can't figure out which rule generated each message, so we // can not customize severities. // // See https://github.com/mdevils/node-jscs/issues/224 throw new Exception( pht( "JSCS does not currently support custom severity levels, because ". "rules can't be identified from messages in output.")); } } diff --git a/src/lint/linter/ArcanistLesscLinter.php b/src/lint/linter/ArcanistLesscLinter.php index 5516540b..f51660e2 100644 --- a/src/lint/linter/ArcanistLesscLinter.php +++ b/src/lint/linter/ArcanistLesscLinter.php @@ -1,184 +1,187 @@ array( 'type' => 'optional bool', 'help' => pht( 'Enable strict math, which only processes mathematical expressions '. 'inside extraneous parentheses.'), ), 'lessc.strict-units' => array( 'type' => 'optional bool', 'help' => pht('Enable strict handling of units in expressions.'), ), ); } public function setLinterConfigurationValue($key, $value) { switch ($key) { case 'lessc.strict-math': $this->strictMath = $value; return; case 'lessc.strict-units': $this->strictUnits = $value; return; } return parent::setLinterConfigurationValue($key, $value); } public function getLintNameMap() { return array( self::LINT_RUNTIME_ERROR => pht('Runtime Error'), self::LINT_ARGUMENT_ERROR => pht('Argument Error'), self::LINT_FILE_ERROR => pht('File Error'), self::LINT_NAME_ERROR => pht('Name Error'), self::LINT_OPERATION_ERROR => pht('Operation Error'), self::LINT_PARSE_ERROR => pht('Parse Error'), self::LINT_SYNTAX_ERROR => pht('Syntax Error'), ); } public function getDefaultBinary() { return 'lessc'; } public function getVersion() { list($stdout) = execx('%C --version', $this->getExecutableCommand()); $matches = array(); $regex = '/^lessc (?P\d+\.\d+\.\d+)\b/'; if (preg_match($regex, $stdout, $matches)) { $version = $matches['version']; } else { return false; } } public function getInstallInstructions() { - return pht('Install lessc using `npm install -g less`.'); + return pht('Install lessc using `%s`.', 'npm install -g less'); } protected function getMandatoryFlags() { return array( '--lint', '--no-color', '--strict-math='.($this->strictMath ? 'on' : 'off'), '--strict-units='.($this->strictUnits ? 'on' : 'off'), ); } protected function parseLinterOutput($path, $err, $stdout, $stderr) { $lines = phutil_split_lines($stderr, false); $messages = array(); foreach ($lines as $line) { $matches = null; $match = preg_match( '/^(?P\w+): (?P.+) '. 'in (?P.+|-) '. 'on line (?P\d+), column (?P\d+):$/', $line, $matches); if ($match) { switch ($matches['name']) { case 'RuntimeError': $code = self::LINT_RUNTIME_ERROR; break; case 'ArgumentError': $code = self::LINT_ARGUMENT_ERROR; break; case 'FileError': $code = self::LINT_FILE_ERROR; break; case 'NameError': $code = self::LINT_NAME_ERROR; break; case 'OperationError': $code = self::LINT_OPERATION_ERROR; break; case 'ParseError': $code = self::LINT_PARSE_ERROR; break; case 'SyntaxError': $code = self::LINT_SYNTAX_ERROR; break; default: - throw new RuntimeException(pht( - 'Unrecognized lint message code "%s".', - $code)); + throw new RuntimeException( + pht( + 'Unrecognized lint message code "%s".', + $code)); } $code = $this->getLintCodeFromLinterConfigurationKey($matches['name']); $message = new ArcanistLintMessage(); $message->setPath($path); $message->setLine($matches['line']); $message->setChar($matches['column']); $message->setCode($this->getLintMessageFullCode($code)); $message->setSeverity($this->getLintMessageSeverity($code)); $message->setName($this->getLintMessageName($code)); $message->setDescription(ucfirst($matches['description'])); $messages[] = $message; } } if ($err && !$messages) { return false; } return $messages; } } diff --git a/src/lint/linter/ArcanistLinter.php b/src/lint/linter/ArcanistLinter.php index 41e27c1c..aae1fbbd 100644 --- a/src/lint/linter/ArcanistLinter.php +++ b/src/lint/linter/ArcanistLinter.php @@ -1,649 +1,650 @@ getLinterName(), $this->getLinterConfigurationName(), get_class($this)); } /* -( Runtime State )------------------------------------------------------ */ /** * @task state */ final public function getActivePath() { return $this->activePath; } /** * @task state */ final public function setActivePath($path) { $this->stopAllLinters = false; $this->activePath = $path; return $this; } /** * @task state */ final public function setEngine(ArcanistLintEngine $engine) { $this->engine = $engine; return $this; } /** * @task state */ final protected function getEngine() { return $this->engine; } /** * Set the internal ID for this linter. * * This ID is assigned automatically by the @{class:ArcanistLintEngine}. * * @param string Unique linter ID. * @return this * @task state */ final public function setLinterID($id) { $this->id = $id; return $this; } /** * Get the internal ID for this linter. * * Retrieves an internal linter ID managed by the @{class:ArcanistLintEngine}. * This ID is a unique scalar which distinguishes linters in a list. * * @return string Unique linter ID. * @task state */ final public function getLinterID() { return $this->id; } /* -( Executing Linters )-------------------------------------------------- */ /** * Hook called before a list of paths are linted. * * Parallelizable linters can start multiple requests in parallel here, * to improve performance. They can implement @{method:didLintPaths} to * collect results. * * Linters which are not parallelizable should normally ignore this callback * and implement @{method:lintPath} instead. * * @param list A list of paths to be linted * @return void * @task exec */ public function willLintPaths(array $paths) { return; } /** * Hook called for each path to be linted. * * Linters which are not parallelizable can do work here. * * Linters which are parallelizable may want to ignore this callback and * implement @{method:willLintPaths} and @{method:didLintPaths} instead. * * @param string Path to lint. * @return void * @task exec */ public function lintPath($path) { return; } /** * Hook called after a list of paths are linted. * * Parallelizable linters can collect results here. * * Linters which are not paralleizable should normally ignore this callback * and implement @{method:lintPath} instead. * * @param list A list of paths which were linted. * @return void * @task exec */ public function didLintPaths(array $paths) { return; } /** * Obsolete hook which was invoked before a path was linted. * * WARNING: This is an obsolete hook which is not called. If you maintain * a linter which relies on it, update to use @{method:lintPath} instead. * * @task exec */ final public function willLintPath($path) { // TODO: Remove this method after some time. In the meantime, the "final" // will fatal subclasses which implement this hook and point at the API // change so maintainers get fewer surprises. throw new PhutilMethodNotImplementedException(); } /** * Obsolete hook which was invoked after linters ran. * * WARNING: This is an obsolete hook which is not called. If you maintain * a linter which relies on it, update to use @{method:didLintPaths} instead. * * @return void * @task exec */ final public function didRunLinters() { // TODO: Remove this method after some time. In the meantime, the "final" // will fatal subclasses which implement this hook and point at the API // change so maintainers get fewer surprises. throw new PhutilMethodNotImplementedException(); } public function getLinterPriority() { return 1.0; } /** * TODO: This should be `final`. */ public function setCustomSeverityMap(array $map) { $this->customSeverityMap = $map; return $this; } final public function setCustomSeverityRules(array $rules) { $this->customSeverityRules = $rules; return $this; } final public function getOtherLocation($offset, $path = null) { if ($path === null) { $path = $this->getActivePath(); } list($line, $char) = $this->getEngine()->getLineAndCharFromOffset( $path, $offset); return array( 'path' => $path, 'line' => $line + 1, 'char' => $char, ); } final public function stopAllLinters() { $this->stopAllLinters = true; return $this; } final public function didStopAllLinters() { return $this->stopAllLinters; } final public function addPath($path) { $this->paths[$path] = $path; $this->filteredPaths = null; return $this; } final public function setPaths(array $paths) { $this->paths = $paths; $this->filteredPaths = null; return $this; } /** * Filter out paths which this linter doesn't act on (for example, because * they are binaries and the linter doesn't apply to binaries). * * @param list * @return list */ final private function filterPaths(array $paths) { $engine = $this->getEngine(); $keep = array(); foreach ($paths as $path) { if (!$this->shouldLintDeletedFiles() && !$engine->pathExists($path)) { continue; } if (!$this->shouldLintDirectories() && $engine->isDirectory($path)) { continue; } if (!$this->shouldLintBinaryFiles() && $engine->isBinaryFile($path)) { continue; } if (!$this->shouldLintSymbolicLinks() && $engine->isSymbolicLink($path)) { continue; } $keep[] = $path; } return $keep; } final public function getPaths() { if ($this->filteredPaths === null) { $this->filteredPaths = $this->filterPaths(array_values($this->paths)); } return $this->filteredPaths; } final public function addData($path, $data) { $this->data[$path] = $data; return $this; } final protected function getData($path) { if (!array_key_exists($path, $this->data)) { $this->data[$path] = $this->getEngine()->loadData($path); } return $this->data[$path]; } public function getCacheVersion() { return 0; } final public function getLintMessageFullCode($short_code) { return $this->getLinterName().$short_code; } final public function getLintMessageSeverity($code) { $map = $this->customSeverityMap; if (isset($map[$code])) { return $map[$code]; } foreach ($this->customSeverityRules as $rule => $severity) { if (preg_match($rule, $code)) { return $severity; } } $map = $this->getLintSeverityMap(); if (isset($map[$code])) { return $map[$code]; } return $this->getDefaultMessageSeverity($code); } protected function getDefaultMessageSeverity($code) { return ArcanistLintSeverity::SEVERITY_ERROR; } final public function isMessageEnabled($code) { return ($this->getLintMessageSeverity($code) !== ArcanistLintSeverity::SEVERITY_DISABLED); } final public function getLintMessageName($code) { $map = $this->getLintNameMap(); if (isset($map[$code])) { return $map[$code]; } - return 'Unknown lint message!'; + return pht('Unknown lint message!'); } final protected function addLintMessage(ArcanistLintMessage $message) { $root = $this->getEngine()->getWorkingCopy()->getProjectRoot(); $path = Filesystem::resolvePath($message->getPath(), $root); $message->setPath(Filesystem::readablePath($path, $root)); $this->messages[] = $message; return $message; } final public function getLintMessages() { return $this->messages; } final protected function raiseLintAtLine( $line, $char, $code, $desc, $original = null, $replacement = null) { $message = id(new ArcanistLintMessage()) ->setPath($this->getActivePath()) ->setLine($line) ->setChar($char) ->setCode($this->getLintMessageFullCode($code)) ->setSeverity($this->getLintMessageSeverity($code)) ->setName($this->getLintMessageName($code)) ->setDescription($desc) ->setOriginalText($original) ->setReplacementText($replacement); return $this->addLintMessage($message); } final protected function raiseLintAtPath($code, $desc) { return $this->raiseLintAtLine(null, null, $code, $desc, null, null); } final protected function raiseLintAtOffset( $offset, $code, $desc, $original = null, $replacement = null) { $path = $this->getActivePath(); $engine = $this->getEngine(); if ($offset === null) { $line = null; $char = null; } else { list($line, $char) = $engine->getLineAndCharFromOffset($path, $offset); } return $this->raiseLintAtLine( $line + 1, $char + 1, $code, $desc, $original, $replacement); } public function canRun() { return true; } abstract public function getLinterName(); public function getVersion() { return null; } final protected function isCodeEnabled($code) { $severity = $this->getLintMessageSeverity($code); return $this->getEngine()->isSeverityEnabled($severity); } public function getLintSeverityMap() { return array(); } public function getLintNameMap() { return array(); } public function getCacheGranularity() { return self::GRANULARITY_FILE; } /** * If this linter is selectable via `.arclint` configuration files, return * a short, human-readable name to identify it. For example, `"jshint"` or * `"pep8"`. * * If you do not implement this method, the linter will not be selectable * through `.arclint` files. */ public function getLinterConfigurationName() { return null; } public function getLinterConfigurationOptions() { if (!$this->canCustomizeLintSeverities()) { return array(); } return array( 'severity' => array( 'type' => 'optional map', 'help' => pht( 'Provide a map from lint codes to adjusted severity levels: error, '. 'warning, advice, autofix or disabled.'), ), 'severity.rules' => array( 'type' => 'optional map', 'help' => pht( 'Provide a map of regular expressions to severity levels. All '. 'matching codes have their severity adjusted.'), ), ); } public function setLinterConfigurationValue($key, $value) { $sev_map = array( - 'error' => ArcanistLintSeverity::SEVERITY_ERROR, - 'warning' => ArcanistLintSeverity::SEVERITY_WARNING, - 'autofix' => ArcanistLintSeverity::SEVERITY_AUTOFIX, - 'advice' => ArcanistLintSeverity::SEVERITY_ADVICE, + 'error' => ArcanistLintSeverity::SEVERITY_ERROR, + 'warning' => ArcanistLintSeverity::SEVERITY_WARNING, + 'autofix' => ArcanistLintSeverity::SEVERITY_AUTOFIX, + 'advice' => ArcanistLintSeverity::SEVERITY_ADVICE, 'disabled' => ArcanistLintSeverity::SEVERITY_DISABLED, ); switch ($key) { case 'severity': if (!$this->canCustomizeLintSeverities()) { break; } $custom = array(); foreach ($value as $code => $severity) { if (empty($sev_map[$severity])) { $valid = implode(', ', array_keys($sev_map)); throw new Exception( pht( 'Unknown lint severity "%s". Valid severities are: %s.', $severity, $valid)); } $code = $this->getLintCodeFromLinterConfigurationKey($code); $custom[$code] = $severity; } $this->setCustomSeverityMap($custom); return; case 'severity.rules': if (!$this->canCustomizeLintSeverities()) { break; } foreach ($value as $rule => $severity) { if (@preg_match($rule, '') === false) { throw new Exception( pht( 'Severity rule "%s" is not a valid regular expression.', $rule)); } if (empty($sev_map[$severity])) { $valid = implode(', ', array_keys($sev_map)); throw new Exception( pht( 'Unknown lint severity "%s". Valid severities are: %s.', $severity, $valid)); } } $this->setCustomSeverityRules($value); return; } - throw new Exception("Incomplete implementation: {$key}!"); + throw new Exception(pht('Incomplete implementation: %s!', $key)); } protected function canCustomizeLintSeverities() { return true; } protected function shouldLintBinaryFiles() { return false; } protected function shouldLintDeletedFiles() { return false; } protected function shouldLintDirectories() { return false; } protected function shouldLintSymbolicLinks() { return false; } /** * Map a configuration lint code to an `arc` lint code. Primarily, this is * intended for validation, but can also be used to normalize case or * otherwise be more permissive in accepted inputs. * * If the code is not recognized, you should throw an exception. * * @param string Code specified in configuration. * @return string Normalized code to use in severity map. */ protected function getLintCodeFromLinterConfigurationKey($code) { return $code; } /** * Retrieve an old lint configuration value from `.arcconfig` or a similar * source. * * Modern linters should use @{method:getConfig} to read configuration from * `.arclint`. * * @param string Configuration key to retrieve. * @param wild Default value to return if key is not present in config. * @return wild Configured value, or default if no configuration exists. */ final protected function getDeprecatedConfiguration($key, $default = null) { // If we're being called in a context without an engine (probably from // `arc linters`), just return the default value. if (!$this->engine) { return $default; } $config = $this->getEngine()->getConfigurationManager(); // Construct a sentinel object so we can tell if we're reading config // or not. $sentinel = (object)array(); $result = $config->getConfigFromAnySource($key, $sentinel); // If we read config, warn the user that this mechanism is deprecated and // discouraged. if ($result !== $sentinel) { $console = PhutilConsole::getConsole(); $console->writeErr( "**%s**: %s\n", pht('Deprecation Warning'), pht( 'Configuration option "%s" is deprecated. Generally, linters should '. - 'now be configured using an `.arclint` file. See "Arcanist User '. + 'now be configured using an `%s` file. See "Arcanist User '. 'Guide: Lint" in the documentation for more information.', - $key)); + $key, + '.arclint')); return $result; } return $default; } } diff --git a/src/lint/linter/ArcanistPEP8Linter.php b/src/lint/linter/ArcanistPEP8Linter.php index 03811358..984e64bf 100644 --- a/src/lint/linter/ArcanistPEP8Linter.php +++ b/src/lint/linter/ArcanistPEP8Linter.php @@ -1,126 +1,126 @@ getDeprecatedConfiguration('lint.pep8.options', array()); } public function shouldUseInterpreter() { return ($this->getDefaultBinary() !== 'pep8'); } public function getDefaultInterpreter() { return 'python2.6'; } public function getDefaultBinary() { if (Filesystem::binaryExists('pep8')) { return 'pep8'; } $old_prefix = $this->getDeprecatedConfiguration('lint.pep8.prefix'); $old_bin = $this->getDeprecatedConfiguration('lint.pep8.bin'); if ($old_prefix || $old_bin) { $old_bin = nonempty($old_bin, 'pep8'); return $old_prefix.'/'.$old_bin; } $arc_root = dirname(phutil_get_library_root('arcanist')); return $arc_root.'/externals/pep8/pep8.py'; } public function getVersion() { list($stdout) = execx('%C --version', $this->getExecutableCommand()); $matches = array(); if (preg_match('/^(?P\d+\.\d+(?:\.\d+)?)\b/', $stdout, $matches)) { return $matches['version']; } else { return false; } } public function getInstallInstructions() { - return pht('Install PEP8 using `easy_install pep8`.'); + return pht('Install PEP8 using `%s`.', 'easy_install pep8'); } protected function parseLinterOutput($path, $err, $stdout, $stderr) { $lines = phutil_split_lines($stdout, false); $messages = array(); foreach ($lines as $line) { $matches = null; if (!preg_match('/^(.*?):(\d+):(\d+): (\S+) (.*)$/', $line, $matches)) { continue; } foreach ($matches as $key => $match) { $matches[$key] = trim($match); } $message = new ArcanistLintMessage(); $message->setPath($path); $message->setLine($matches[2]); $message->setChar($matches[3]); $message->setCode($matches[4]); $message->setName('PEP8 '.$matches[4]); $message->setDescription($matches[5]); $message->setSeverity($this->getLintMessageSeverity($matches[4])); $messages[] = $message; } if ($err && !$messages) { return false; } return $messages; } protected function getDefaultMessageSeverity($code) { if (preg_match('/^W/', $code)) { return ArcanistLintSeverity::SEVERITY_WARNING; } else { return ArcanistLintSeverity::SEVERITY_ERROR; } } protected function getLintCodeFromLinterConfigurationKey($code) { if (!preg_match('/^(E|W)\d+$/', $code)) { throw new Exception( pht( 'Unrecognized lint message code "%s". Expected a valid PEP8 '. 'lint code like "%s" or "%s".', $code, 'E101', 'W291')); } return $code; } } diff --git a/src/lint/linter/ArcanistPhpLinter.php b/src/lint/linter/ArcanistPhpLinter.php index bd6b8222..c272305e 100644 --- a/src/lint/linter/ArcanistPhpLinter.php +++ b/src/lint/linter/ArcanistPhpLinter.php @@ -1,101 +1,101 @@ pht('Parse Error'), self::LINT_FATAL_ERROR => pht('Fatal Error'), ); } protected function getMandatoryFlags() { return array('-l'); } public function getInstallInstructions() { return pht('Install PHP.'); } public function getDefaultBinary() { return 'php'; } public function getVersion() { list($stdout) = execx( '%C --run %s', $this->getExecutableCommand(), 'echo phpversion();'); return $stdout; } protected function canCustomizeLintSeverities() { return false; } protected function parseLinterOutput($path, $err, $stdout, $stderr) { // Older versions of PHP had both on stdout, newer ones split it. // Combine stdout and stderr for consistency. $stdout = $stderr."\n".$stdout; $matches = array(); $regex = '/^(PHP )?(?.+) error: +(?.+) in (?.+) '. 'on line (?\d+)$/m'; if (preg_match($regex, $stdout, $matches)) { $code = $this->getLintCodeFromLinterConfigurationKey($matches['type']); $message = id(new ArcanistLintMessage()) ->setPath($path) ->setLine($matches['line']) ->setCode($this->getLinterName().$code) ->setName($this->getLintMessageName($code)) ->setSeverity(ArcanistLintSeverity::SEVERITY_ERROR) ->setDescription($matches['error']); // `php -l` only returns the first error. return array($message); } return array(); } protected function getLintCodeFromLinterConfigurationKey($code) { switch (phutil_utf8_strtolower($code)) { case 'parse': return self::LINT_PARSE_ERROR; case 'fatal': return self::LINT_FATAL_ERROR; default: - throw new Exception(pht('Unrecognized lint message code "%s"', $code)); + throw new Exception(pht('Unrecognized lint message code: "%s"', $code)); } } } diff --git a/src/lint/linter/ArcanistPhpcsLinter.php b/src/lint/linter/ArcanistPhpcsLinter.php index 68d4d7f3..f05df5c5 100644 --- a/src/lint/linter/ArcanistPhpcsLinter.php +++ b/src/lint/linter/ArcanistPhpcsLinter.php @@ -1,160 +1,163 @@ array( 'type' => 'optional string', 'help' => pht('The name or path of the coding standard to use.'), ), ); return $options + parent::getLinterConfigurationOptions(); } public function setLinterConfigurationValue($key, $value) { switch ($key) { case 'phpcs.standard': $this->standard = $value; return; default: return parent::setLinterConfigurationValue($key, $value); } } protected function getMandatoryFlags() { $options = array('--report=xml'); if ($this->standard) { $options[] = '--standard='.$this->standard; } return $options; } protected function getDefaultFlags() { $options = $this->getDeprecatedConfiguration('lint.phpcs.options', array()); $standard = $this->getDeprecatedConfiguration('lint.phpcs.standard'); if (!empty($standard)) { if (is_array($options)) { $options[] = '--standard='.$standard; } else { $options .= ' --standard='.$standard; } } return $options; } public function getDefaultBinary() { return $this->getDeprecatedConfiguration('lint.phpcs.bin', 'phpcs'); } public function getVersion() { list($stdout) = execx('%C --version', $this->getExecutableCommand()); $matches = array(); $regex = '/^PHP_CodeSniffer version (?P\d+\.\d+\.\d+)\b/'; if (preg_match($regex, $stdout, $matches)) { return $matches['version']; } else { return false; } } protected function parseLinterOutput($path, $err, $stdout, $stderr) { // NOTE: Some version of PHPCS after 1.4.6 stopped printing a valid, empty // XML document to stdout in the case of no errors. If PHPCS exits with // error 0, just ignore output. if (!$err) { return array(); } $report_dom = new DOMDocument(); $ok = @$report_dom->loadXML($stdout); if (!$ok) { return false; } $files = $report_dom->getElementsByTagName('file'); $messages = array(); foreach ($files as $file) { foreach ($file->childNodes as $child) { if (!($child instanceof DOMElement)) { continue; } if ($child->tagName == 'error') { $prefix = 'E'; } else { $prefix = 'W'; } $code = 'PHPCS.'.$prefix.'.'.$child->getAttribute('source'); $message = new ArcanistLintMessage(); $message->setPath($path); $message->setLine($child->getAttribute('line')); $message->setChar($child->getAttribute('column')); $message->setCode($code); $message->setDescription($child->nodeValue); $message->setSeverity($this->getLintMessageSeverity($code)); $messages[] = $message; } } return $messages; } protected function getDefaultMessageSeverity($code) { if (preg_match('/^PHPCS\\.W\\./', $code)) { return ArcanistLintSeverity::SEVERITY_WARNING; } else { return ArcanistLintSeverity::SEVERITY_ERROR; } } protected function getLintCodeFromLinterConfigurationKey($code) { if (!preg_match('/^PHPCS\\.(E|W)\\./', $code)) { throw new Exception( - "Invalid severity code '{$code}', should begin with 'PHPCS.'."); + pht( + "Invalid severity code '%s', should begin with '%s.'.", + $code, + 'PHPCS')); } return $code; } } diff --git a/src/lint/linter/ArcanistPhutilXHPASTLinter.php b/src/lint/linter/ArcanistPhutilXHPASTLinter.php index 50897d53..69d1ba86 100644 --- a/src/lint/linter/ArcanistPhutilXHPASTLinter.php +++ b/src/lint/linter/ArcanistPhutilXHPASTLinter.php @@ -1,298 +1,295 @@ deprecatedFunctions = $map; return $this; } public function setDynamicStringFunctions(array $map) { $this->dynamicStringFunctions = $map; return $this; } public function setDynamicStringClasses(array $map) { $this->dynamicStringClasses = $map; return $this; } public function getLintNameMap() { return array( self::LINT_ARRAY_COMBINE => pht( - '%s Unreliable', - 'array_combine()'), + '%s Unreliable', 'array_combine()'), self::LINT_DEPRECATED_FUNCTION => pht( 'Use of Deprecated Function'), self::LINT_UNSAFE_DYNAMIC_STRING => pht( 'Unsafe Usage of Dynamic String'), self::LINT_RAGGED_CLASSTREE_EDGE => pht( - 'Class Not %s Or %s', - 'abstract', - 'final'), + 'Class Not %s Or %s', 'abstract', 'final'), ); } public function getLintSeverityMap() { $warning = ArcanistLintSeverity::SEVERITY_WARNING; return array( self::LINT_ARRAY_COMBINE => $warning, self::LINT_DEPRECATED_FUNCTION => $warning, self::LINT_UNSAFE_DYNAMIC_STRING => $warning, self::LINT_RAGGED_CLASSTREE_EDGE => $warning, ); } public function getLinterName() { return 'PHLXHP'; } public function getLinterConfigurationName() { return 'phutil-xhpast'; } public function getVersion() { // The version number should be incremented whenever a new rule is added. return '3'; } public function getLinterConfigurationOptions() { $options = array( 'phutil-xhpast.deprecated.functions' => array( 'type' => 'optional map', 'help' => pht( 'Functions which should should be considered deprecated.'), ), 'phutil-xhpast.dynamic-string.functions' => array( 'type' => 'optional map', 'help' => pht( 'Functions which should should not be used because they represent '. 'the unsafe usage of dynamic strings.'), ), 'phutil-xhpast.dynamic-string.classes' => array( 'type' => 'optional map', 'help' => pht( 'Classes which should should not be used because they represent the '. 'unsafe usage of dynamic strings.'), ), ); return $options + parent::getLinterConfigurationOptions(); } public function setLinterConfigurationValue($key, $value) { switch ($key) { case 'phutil-xhpast.deprecated.functions': $this->setDeprecatedFunctions($value); return; case 'phutil-xhpast.dynamic-string.functions': $this->setDynamicStringFunctions($value); return; case 'phutil-xhpast.dynamic-string.classes': $this->setDynamicStringClasses($value); return; } return parent::setLinterConfigurationValue($key, $value); } protected function resolveFuture($path, Future $future) { $tree = $this->getXHPASTLinter()->getXHPASTTreeForPath($path); if (!$tree) { return; } $root = $tree->getRootNode(); $method_codes = array( 'lintArrayCombine' => self::LINT_ARRAY_COMBINE, 'lintUnsafeDynamicString' => self::LINT_UNSAFE_DYNAMIC_STRING, 'lintDeprecatedFunctions' => self::LINT_DEPRECATED_FUNCTION, 'lintRaggedClasstreeEdges' => self::LINT_RAGGED_CLASSTREE_EDGE, ); foreach ($method_codes as $method => $codes) { foreach ((array)$codes as $code) { if ($this->isCodeEnabled($code)) { call_user_func(array($this, $method), $root); break; } } } } private function lintUnsafeDynamicString(XHPASTNode $root) { $safe = $this->dynamicStringFunctions + array( 'pht' => 0, 'hsprintf' => 0, 'jsprintf' => 0, 'hgsprintf' => 0, 'csprintf' => 0, 'vcsprintf' => 0, 'execx' => 0, 'exec_manual' => 0, 'phutil_passthru' => 0, 'qsprintf' => 1, 'vqsprintf' => 1, 'queryfx' => 1, 'queryfx_all' => 1, 'queryfx_one' => 1, ); $calls = $root->selectDescendantsOfType('n_FUNCTION_CALL'); $this->lintUnsafeDynamicStringCall($calls, $safe); $safe = $this->dynamicStringClasses + array( 'ExecFuture' => 0, ); $news = $root->selectDescendantsOfType('n_NEW'); $this->lintUnsafeDynamicStringCall($news, $safe); } private function lintUnsafeDynamicStringCall( AASTNodeList $calls, array $safe) { $safe = array_combine( array_map('strtolower', array_keys($safe)), $safe); foreach ($calls as $call) { $name = $call->getChildByIndex(0)->getConcreteString(); $param = idx($safe, strtolower($name)); if ($param === null) { continue; } $parameters = $call->getChildByIndex(1); if (count($parameters->getChildren()) <= $param) { continue; } $identifier = $parameters->getChildByIndex($param); if (!$identifier->isConstantString()) { $this->raiseLintAtNode( $call, self::LINT_UNSAFE_DYNAMIC_STRING, pht( "Parameter %d of %s should be a scalar string, ". "otherwise it's not safe.", $param + 1, $name.'()')); } } } private function lintArrayCombine(XHPASTNode $root) { $function_calls = $root->selectDescendantsOfType('n_FUNCTION_CALL'); foreach ($function_calls as $call) { $name = $call->getChildByIndex(0)->getConcreteString(); if (strcasecmp($name, 'array_combine') == 0) { $parameter_list = $call->getChildOfType(1, 'n_CALL_PARAMETER_LIST'); if (count($parameter_list->getChildren()) !== 2) { // Wrong number of parameters, but raise that elsewhere if we want. continue; } $first = $parameter_list->getChildByIndex(0); $second = $parameter_list->getChildByIndex(1); if ($first->getConcreteString() == $second->getConcreteString()) { $this->raiseLintAtNode( $call, self::LINT_ARRAY_COMBINE, pht( 'Prior to PHP 5.4, `%s` fails when given empty arrays. '. 'Prefer to write `%s` as `%s`.', 'array_combine()', 'array_combine(x, x)', 'array_fuse(x)')); } } } } private function lintDeprecatedFunctions(XHPASTNode $root) { $map = $this->deprecatedFunctions; $function_calls = $root->selectDescendantsOfType('n_FUNCTION_CALL'); foreach ($function_calls as $call) { $name = $call->getChildByIndex(0)->getConcreteString(); $name = strtolower($name); if (empty($map[$name])) { continue; } $this->raiseLintAtNode( $call, self::LINT_DEPRECATED_FUNCTION, $map[$name]); } } private function lintRaggedClasstreeEdges(XHPASTNode $root) { $parser = new PhutilDocblockParser(); $classes = $root->selectDescendantsOfType('n_CLASS_DECLARATION'); foreach ($classes as $class) { $is_final = false; $is_abstract = false; $is_concrete_extensible = false; $attributes = $class->getChildOfType(0, 'n_CLASS_ATTRIBUTES'); foreach ($attributes->getChildren() as $child) { if ($child->getConcreteString() == 'final') { $is_final = true; } if ($child->getConcreteString() == 'abstract') { $is_abstract = true; } } $docblock = $class->getDocblockToken(); if ($docblock) { list($text, $specials) = $parser->parse($docblock->getValue()); $is_concrete_extensible = idx($specials, 'concrete-extensible'); } if (!$is_final && !$is_abstract && !$is_concrete_extensible) { $this->raiseLintAtNode( $class->getChildOfType(1, 'n_CLASS_NAME'), self::LINT_RAGGED_CLASSTREE_EDGE, pht( "This class is neither '%s' nor '%s', and does not have ". "a docblock marking it '%s'.", 'final', 'abstract', '@concrete-extensible')); } } } } diff --git a/src/lint/linter/ArcanistPyFlakesLinter.php b/src/lint/linter/ArcanistPyFlakesLinter.php index 59d91995..b88a3322 100644 --- a/src/lint/linter/ArcanistPyFlakesLinter.php +++ b/src/lint/linter/ArcanistPyFlakesLinter.php @@ -1,98 +1,98 @@ getDeprecatedConfiguration('lint.pyflakes.prefix'); $bin = $this->getDeprecatedConfiguration('lint.pyflakes.bin', 'pyflakes'); if ($prefix) { return $prefix.'/'.$bin; } else { return $bin; } } public function getVersion() { list($stdout) = execx('%C --version', $this->getExecutableCommand()); $matches = array(); if (preg_match('/^(?P\d+\.\d+\.\d+)$/', $stdout, $matches)) { return $matches['version']; } else { return false; } } public function getInstallInstructions() { - return pht('Install pyflakes with `pip install pyflakes`.'); + return pht('Install pyflakes with `%s`.', 'pip install pyflakes'); } protected function parseLinterOutput($path, $err, $stdout, $stderr) { $lines = phutil_split_lines($stdout, false); $messages = array(); foreach ($lines as $line) { $matches = null; if (!preg_match('/^(.*?):(\d+): (.*)$/', $line, $matches)) { continue; } foreach ($matches as $key => $match) { $matches[$key] = trim($match); } $severity = ArcanistLintSeverity::SEVERITY_WARNING; $description = $matches[3]; $error_regexp = '/(^undefined|^duplicate|before assignment$)/'; if (preg_match($error_regexp, $description)) { $severity = ArcanistLintSeverity::SEVERITY_ERROR; } $message = new ArcanistLintMessage(); $message->setPath($path); $message->setLine($matches[2]); $message->setCode($this->getLinterName()); $message->setDescription($description); $message->setSeverity($severity); $messages[] = $message; } if ($err && !$messages) { return false; } return $messages; } protected function canCustomizeLintSeverities() { return false; } } diff --git a/src/lint/linter/ArcanistPyLintLinter.php b/src/lint/linter/ArcanistPyLintLinter.php index 45ebf37f..ce1c60de 100644 --- a/src/lint/linter/ArcanistPyLintLinter.php +++ b/src/lint/linter/ArcanistPyLintLinter.php @@ -1,266 +1,272 @@ getEngine()->getConfigurationManager(); $error_regexp = $config->getConfigFromAnySource( 'lint.pylint.codes.error'); $warning_regexp = $config->getConfigFromAnySource( 'lint.pylint.codes.warning'); $advice_regexp = $config->getConfigFromAnySource( 'lint.pylint.codes.advice'); if (!$error_regexp && !$warning_regexp && !$advice_regexp) { throw new ArcanistUsageException( - "You are invoking the PyLint linter but have not configured any of ". - "'lint.pylint.codes.error', 'lint.pylint.codes.warning', or ". - "'lint.pylint.codes.advice'. Consult the documentation for ". - "ArcanistPyLintLinter."); + pht( + "You are invoking the PyLint linter but have not configured any of ". + "'%s', '%s', or '%s'. Consult the documentation for %s.", + 'lint.pylint.codes.error', + 'lint.pylint.codes.warning', + 'lint.pylint.codes.advice', + __CLASS__)); } $code_map = array( ArcanistLintSeverity::SEVERITY_ERROR => $error_regexp, ArcanistLintSeverity::SEVERITY_WARNING => $warning_regexp, ArcanistLintSeverity::SEVERITY_ADVICE => $advice_regexp, ); foreach ($code_map as $sev => $codes) { if ($codes === null) { continue; } if (!is_array($codes)) { $codes = array($codes); } foreach ($codes as $code_re) { if (preg_match("/{$code_re}/", $code)) { return $sev; } } } // If the message code doesn't match any of the provided regex's, // then just disable it. return ArcanistLintSeverity::SEVERITY_DISABLED; } private function getPyLintPath() { $pylint_bin = 'pylint'; // Use the PyLint prefix specified in the config file $config = $this->getEngine()->getConfigurationManager(); $prefix = $config->getConfigFromAnySource('lint.pylint.prefix'); if ($prefix !== null) { $pylint_bin = $prefix.'/bin/'.$pylint_bin; } if (!Filesystem::pathExists($pylint_bin)) { list($err) = exec_manual('which %s', $pylint_bin); if ($err) { throw new ArcanistMissingLinterException( - "PyLint does not appear to be installed on this system. Install it ". - "(e.g., with 'sudo easy_install pylint') or configure ". - "'lint.pylint.prefix' in your .arcconfig to point to the directory ". - "where it resides."); + pht( + "PyLint does not appear to be installed on this system. Install ". + "it (e.g., with '%s') or configure '%s' in your %s to point to ". + "the directory where it resides.", + 'sudo easy_install pylint', + 'lint.pylint.prefix', + '.arcconfig')); } } return $pylint_bin; } private function getPyLintPythonPath() { // Get non-default install locations for pylint and its dependencies // libraries. $config = $this->getEngine()->getConfigurationManager(); $prefixes = array( $config->getConfigFromAnySource('lint.pylint.prefix'), $config->getConfigFromAnySource('lint.pylint.logilab_astng.prefix'), $config->getConfigFromAnySource('lint.pylint.logilab_common.prefix'), ); // Add the libraries to the python search path $python_path = array(); foreach ($prefixes as $prefix) { if ($prefix !== null) { $python_path[] = $prefix.'/lib/python2.7/site-packages'; $python_path[] = $prefix.'/lib/python2.7/dist-packages'; $python_path[] = $prefix.'/lib/python2.6/site-packages'; $python_path[] = $prefix.'/lib/python2.6/dist-packages'; } } $working_copy = $this->getEngine()->getWorkingCopy(); $config_paths = $config->getConfigFromAnySource('lint.pylint.pythonpath'); if ($config_paths !== null) { foreach ($config_paths as $config_path) { if ($config_path !== null) { $python_path[] = Filesystem::resolvePath( $config_path, $working_copy->getProjectRoot()); } } } $python_path[] = ''; return implode(':', $python_path); } private function getPyLintOptions() { // '-rn': don't print lint report/summary at end $options = array('-rn'); // Version 0.x.x include the pylint message ids in the output if (version_compare($this->getLinterVersion(), '1', 'lt')) { array_push($options, '-iy', '--output-format=text'); } // Version 1.x.x set the output specifically to the 0.x.x format else { array_push($options, "--msg-template='{msg_id}:{line:3d}: {obj}: {msg}'"); } $working_copy = $this->getEngine()->getWorkingCopy(); $config = $this->getEngine()->getConfigurationManager(); // Specify an --rcfile, either absolute or relative to the project root. // Stupidly, the command line args above are overridden by rcfile, so be // careful. $rcfile = $config->getConfigFromAnySource('lint.pylint.rcfile'); if ($rcfile !== null) { $rcfile = Filesystem::resolvePath( $rcfile, $working_copy->getProjectRoot()); $options[] = csprintf('--rcfile=%s', $rcfile); } // Add any options defined in the config file for PyLint $config_options = $config->getConfigFromAnySource('lint.pylint.options'); if ($config_options !== null) { $options = array_merge($options, $config_options); } return implode(' ', $options); } public function getLinterName() { return 'PyLint'; } private function getLinterVersion() { $pylint_bin = $this->getPyLintPath(); $options = '--version'; list($stdout) = execx('%s %s', $pylint_bin, $options); $lines = phutil_split_lines($stdout, false); $matches = null; // If the version command didn't return anything or the regex didn't match // Assume a future version that at least is compatible with 1.x.x if (count($lines) == 0 || !preg_match('/pylint\s((?:\d+\.?)+)/', $lines[0], $matches)) { return '999'; } return $matches[1]; } public function lintPath($path) { $pylint_bin = $this->getPyLintPath(); $python_path = $this->getPyLintPythonPath(); $options = $this->getPyLintOptions(); $path_on_disk = $this->getEngine()->getFilePathOnDisk($path); try { list($stdout, $_) = execx( '/usr/bin/env PYTHONPATH=%s$PYTHONPATH %s %C %s', $python_path, $pylint_bin, $options, $path_on_disk); } catch (CommandException $e) { if ($e->getError() == 32) { // According to ##man pylint## the exit status of 32 means there was a // usage error. That's bad, so actually exit abnormally. throw $e; } else { // The other non-zero exit codes mean there were messages issued, // which is expected, so don't exit. $stdout = $e->getStdout(); } } $lines = phutil_split_lines($stdout, false); $messages = array(); foreach ($lines as $line) { $matches = null; $regex = '/([A-Z]\d+): *(\d+)(?:|,\d*): *(.*)$/'; if (!preg_match($regex, $line, $matches)) { continue; } foreach ($matches as $key => $match) { $matches[$key] = trim($match); } $message = new ArcanistLintMessage(); $message->setPath($path); $message->setLine($matches[2]); $message->setCode($matches[1]); $message->setName($this->getLinterName().' '.$matches[1]); $message->setDescription($matches[3]); $message->setSeverity($this->getMessageCodeSeverity($matches[1])); $this->addLintMessage($message); } } } diff --git a/src/lint/linter/ArcanistRuboCopLinter.php b/src/lint/linter/ArcanistRuboCopLinter.php index 0647a7bc..c7585ca6 100644 --- a/src/lint/linter/ArcanistRuboCopLinter.php +++ b/src/lint/linter/ArcanistRuboCopLinter.php @@ -1,120 +1,120 @@ getExecutableCommand()); $matches = array(); if (preg_match('/^(?P\d+\.\d+\.\d+)$/', $stdout, $matches)) { return $matches['version']; } else { return false; } } public function getInstallInstructions() { return pht('Install RuboCop using `%s`.', 'gem install rubocop'); } protected function getMandatoryFlags() { $options = array( '--format=json', ); if ($this->config) { $options[] = '--config='.$this->config; } return $options; } public function getLinterConfigurationOptions() { $options = array( 'rubocop.config' => array( 'type' => 'optional string', 'help' => pht('A custom configuration file.'), ), ); return $options + parent::getLinterConfigurationOptions(); } public function setLinterConfigurationValue($key, $value) { switch ($key) { case 'rubocop.config': $this->config = $value; return; } return parent::setLinterConfigurationValue($key, $value); } protected function parseLinterOutput($path, $err, $stdout, $stderr) { $results = phutil_json_decode($stdout); $messages = array(); foreach ($results['files'] as $file) { foreach ($file['offenses'] as $offense) { $message = id(new ArcanistLintMessage()) ->setPath($file['path']) ->setDescription($offense['message']) ->setLine($offense['location']['line']) ->setChar($offense['location']['column']) ->setSeverity($this->getLintMessageSeverity($offense['severity'])) ->setName($this->getLinterName()) ->setCode($offense['cop_name']); $messages[] = $message; } } return $messages; } - /* - Take the string from RuboCop's severity terminology and return an - ArcanistLintSeverity + /** + * Take the string from RuboCop's severity terminology and return an + * @{class:ArcanistLintSeverity}. */ protected function getDefaultMessageSeverity($code) { switch ($code) { case 'convention': case 'refactor': case 'warning': return ArcanistLintSeverity::SEVERITY_WARNING; case 'error': case 'fatal': return ArcanistLintSeverity::SEVERITY_ERROR; default: return ArcanistLintSeverity::SEVERITY_ADVICE; } } } diff --git a/src/lint/linter/ArcanistRubyLinter.php b/src/lint/linter/ArcanistRubyLinter.php index b2d76567..fb8ed25e 100644 --- a/src/lint/linter/ArcanistRubyLinter.php +++ b/src/lint/linter/ArcanistRubyLinter.php @@ -1,94 +1,96 @@ getDeprecatedConfiguration('lint.ruby.prefix'); if ($prefix !== null) { $ruby_bin = $prefix.'ruby'; } return 'ruby'; } public function getVersion() { list($stdout) = execx('%C --version', $this->getExecutableCommand()); $matches = array(); $regex = '/^ruby (?P\d+\.\d+\.\d+)p\d+/'; if (preg_match($regex, $stdout, $matches)) { return $matches['version']; } else { return false; } } public function getInstallInstructions() { - return pht('Install `ruby` from .'); + return pht('Install `%s` from <%s>.', 'ruby', 'http://www.ruby-lang.org/'); } protected function getMandatoryFlags() { // -w: turn on warnings // -c: check syntax return array('-w', '-c'); } protected function parseLinterOutput($path, $err, $stdout, $stderr) { $lines = phutil_split_lines($stderr, false); $messages = array(); foreach ($lines as $line) { $matches = null; if (!preg_match('/(.*?):(\d+): (.*?)$/', $line, $matches)) { continue; } foreach ($matches as $key => $match) { $matches[$key] = trim($match); } $code = head(explode(',', $matches[3])); $message = new ArcanistLintMessage(); $message->setPath($path); $message->setLine($matches[2]); $message->setCode($this->getLinterName()); $message->setName(pht('Syntax Error')); $message->setDescription($matches[3]); $message->setSeverity($this->getLintMessageSeverity($code)); $messages[] = $message; } if ($err && !$messages) { return false; } return $messages; } } diff --git a/src/lint/linter/ArcanistScriptAndRegexLinter.php b/src/lint/linter/ArcanistScriptAndRegexLinter.php index c2422f47..8e8a1f5c 100644 --- a/src/lint/linter/ArcanistScriptAndRegexLinter.php +++ b/src/lint/linter/ArcanistScriptAndRegexLinter.php @@ -1,450 +1,450 @@ &1' * * The return code of the script must be 0, or an exception will be raised * reporting that the linter failed. If you have a script which exits nonzero * under normal circumstances, you can force it to always exit 0 by using a * configuration like this: * * sh -c '/opt/lint/lint.sh "$0" || true' * * Multiple instances of the script will be run in parallel if there are * multiple files to be linted, so they should not use any unique resources. * For instance, this configuration would not work properly, because several * processes may attempt to write to the file at the same time: * * COUNTEREXAMPLE * sh -c '/opt/lint/lint.sh --output /tmp/lint.out "$0" && cat /tmp/lint.out' * * There are necessary limits to how gracefully this linter can deal with * edge cases, because it is just a script and a regex. If you need to do * things that this linter can't handle, you can write a phutil linter and move * the logic to handle those cases into PHP. PHP is a better general-purpose * programming language than regular expressions are, if only by a small margin. * * == ...and Regex == * * The regex must be a valid PHP PCRE regex, including delimiters and flags. * * The regex will be matched against the entire output of the script, so it * should generally be in this form if messages are one-per-line: * * /^...$/m * * The regex should capture these named patterns with `(?P...)`: * * - `message` (required) Text describing the lint message. For example, * "This is a syntax error.". * - `name` (optional) Text summarizing the lint message. For example, * "Syntax Error". * - `severity` (optional) The word "error", "warning", "autofix", "advice", * or "disabled", in any combination of upper and lower case. Instead, you * may match groups called `error`, `warning`, `advice`, `autofix`, or * `disabled`. These allow you to match output formats like "E123" and * "W123" to indicate errors and warnings, even though the word "error" is * not present in the output. If no severity capturing group is present, * messages are raised with "error" severity. If multiple severity capturing * groups are present, messages are raised with the highest captured * severity. Capturing groups like `error` supersede the `severity` * capturing group. * - `error` (optional) Match some nonempty substring to indicate that this * message has "error" severity. * - `warning` (optional) Match some nonempty substring to indicate that this * message has "warning" severity. * - `advice` (optional) Match some nonempty substring to indicate that this * message has "advice" severity. * - `autofix` (optional) Match some nonempty substring to indicate that this * message has "autofix" severity. * - `disabled` (optional) Match some nonempty substring to indicate that this * message has "disabled" severity. * - `file` (optional) The name of the file to raise the lint message in. If * not specified, defaults to the linted file. It is generally not necessary * to capture this unless the linter can raise messages in files other than * the one it is linting. * - `line` (optional) The line number of the message. * - `char` (optional) The character offset of the message. * - `offset` (optional) The byte offset of the message. If captured, this * supersedes `line` and `char`. * - `original` (optional) The text the message affects. * - `replacement` (optional) The text that the range captured by `original` * should be automatically replaced by to resolve the message. * - `code` (optional) A short error type identifier which can be used * elsewhere to configure handling of specific types of messages. For * example, "EXAMPLE1", "EXAMPLE2", etc., where each code identifies a * class of message like "syntax error", "missing whitespace", etc. This * allows configuration to later change the severity of all whitespace * messages, for example. * - `ignore` (optional) Match some nonempty substring to ignore the match. * You can use this if your linter sometimes emits text like "No lint * errors". * - `stop` (optional) Match some nonempty substring to stop processing input. * Remaining matches for this file will be discarded, but linting will * continue with other linters and other files. * - `halt` (optional) Match some nonempty substring to halt all linting of * this file by any linter. Linting will continue with other files. * - `throw` (optional) Match some nonempty substring to throw an error, which * will stop `arc` completely. You can use this to fail abruptly if you * encounter unexpected output. All processing will abort. * * Numbered capturing groups are ignored. * * For example, if your lint script's output looks like this: * * error:13 Too many goats! * warning:22 Not enough boats. * * ...you could use this regex to parse it: * * /^(?Pwarning|error):(?P\d+) (?P.*)$/m * * The simplest valid regex for line-oriented output is something like this: * * /^(?P.*)$/m * * @task lint Linting * @task linterinfo Linter Information * @task parse Parsing Output * @task config Validating Configuration */ final class ArcanistScriptAndRegexLinter extends ArcanistLinter { private $script = null; private $regex = null; private $output = array(); public function getInfoName() { return pht('Script and Regex'); } public function getInfoDescription() { return pht( 'Run an external script, then parse its output with a regular '. 'expression. This is a generic binding that can be used to '. 'run custom lint scripts.'); } /* -( Linting )------------------------------------------------------------ */ /** * Run the script on each file to be linted. * * @task lint */ public function willLintPaths(array $paths) { $script = $this->getConfiguredScript(); $root = $this->getEngine()->getWorkingCopy()->getProjectRoot(); $futures = array(); foreach ($paths as $path) { $future = new ExecFuture('%C %s', $script, $path); $future->setCWD($root); $futures[$path] = $future; } $futures = id(new FutureIterator($futures)) ->limit(4); foreach ($futures as $path => $future) { list($stdout) = $future->resolvex(); $this->output[$path] = $stdout; } } /** * Run the regex on the output of the script. * * @task lint */ public function lintPath($path) { $regex = $this->getConfiguredRegex(); $output = idx($this->output, $path); if (!strlen($output)) { // No output, but it exited 0, so just move on. return; } $matches = null; if (!preg_match_all($regex, $output, $matches, PREG_SET_ORDER)) { // Output with no matches. This might be a configuration error, but more // likely it's something like "No lint errors." and the user just hasn't // written a sufficiently powerful/ridiculous regexp to capture it into an // 'ignore' group. Don't make them figure this out; advanced users can // capture 'throw' to handle this case. return; } foreach ($matches as $match) { if (!empty($match['throw'])) { $throw = $match['throw']; throw new ArcanistUsageException( pht( "%s: configuration captured a '%s' named capturing group, ". "'%s'. Script output:\n%s", __CLASS__, 'throw', $throw, $output)); } if (!empty($match['halt'])) { $this->stopAllLinters(); break; } if (!empty($match['stop'])) { break; } if (!empty($match['ignore'])) { continue; } list($line, $char) = $this->getMatchLineAndChar($match, $path); $dict = array( 'path' => idx($match, 'file', $path), 'line' => $line, 'char' => $char, 'code' => idx($match, 'code', $this->getLinterName()), 'severity' => $this->getMatchSeverity($match), 'name' => idx($match, 'name', 'Lint'), - 'description' => idx($match, 'message', 'Undefined Lint Message'), + 'description' => idx($match, 'message', pht('Undefined Lint Message')), ); $original = idx($match, 'original'); if ($original !== null) { $dict['original'] = $original; } $replacement = idx($match, 'replacement'); if ($replacement !== null) { $dict['replacement'] = $replacement; } $lint = ArcanistLintMessage::newFromDictionary($dict); $this->addLintMessage($lint); } } /* -( Linter Information )------------------------------------------------- */ /** * Return the short name of the linter. * * @return string Short linter identifier. * * @task linterinfo */ public function getLinterName() { return 'S&RX'; } public function getLinterConfigurationName() { return 'script-and-regex'; } public function getLinterConfigurationOptions() { // These fields are optional only to avoid breaking things. $options = array( 'script-and-regex.script' => array( 'type' => 'optional string', 'help' => pht('Script to execute.'), ), 'script-and-regex.regex' => array( 'type' => 'optional regex', 'help' => pht('The regex to process output with.'), ), ); return $options + parent::getLinterConfigurationOptions(); } public function setLinterConfigurationValue($key, $value) { switch ($key) { case 'script-and-regex.script': $this->script = $value; return; case 'script-and-regex.regex': $this->regex = $value; return; } return parent::setLinterConfigurationValue($key, $value); } /* -( Parsing Output )----------------------------------------------------- */ /** * Get the line and character of the message from the regex match. * * @param dict Captured groups from regex. * @return pair Line and character of the message. * * @task parse */ private function getMatchLineAndChar(array $match, $path) { if (!empty($match['offset'])) { list($line, $char) = $this->getEngine()->getLineAndCharFromOffset( idx($match, 'file', $path), $match['offset']); return array($line + 1, $char + 1); } $line = idx($match, 'line', 1); $char = idx($match, 'char'); return array($line, $char); } /** * Map the regex matching groups to a message severity. We look for either * a nonempty severity name group like 'error', or a group called 'severity' * with a valid name. * * @param dict Captured groups from regex. * @return const @{class:ArcanistLintSeverity} constant. * * @task parse */ private function getMatchSeverity(array $match) { $map = array( 'error' => ArcanistLintSeverity::SEVERITY_ERROR, 'warning' => ArcanistLintSeverity::SEVERITY_WARNING, 'autofix' => ArcanistLintSeverity::SEVERITY_AUTOFIX, 'advice' => ArcanistLintSeverity::SEVERITY_ADVICE, 'disabled' => ArcanistLintSeverity::SEVERITY_DISABLED, ); $severity_name = strtolower(idx($match, 'severity')); foreach ($map as $name => $severity) { if (!empty($match[$name])) { return $severity; } else if ($severity_name == $name) { return $severity; } } return ArcanistLintSeverity::SEVERITY_ERROR; } /* -( Validating Configuration )------------------------------------------- */ /** * Load, validate, and return the "script" configuration. * * @return string The shell command fragment to use to run the linter. * * @task config */ private function getConfiguredScript() { if (strlen($this->script)) { return $this->script; } $config = $this->getDeprecatedConfiguration('linter.scriptandregex.script'); if (!$config) { throw new ArcanistUsageException( pht( 'No "script" configured for script-and-regex linter, which '. 'requires a script. Use "%s" to configure one.', 'script-and-regex.script')); } // NOTE: No additional validation since the "script" can be some random // shell command and/or include flags, so it does not need to point to some // file on disk. return $config; } /** * Load, validate, and return the "regex" configuration. * * @return string A valid PHP PCRE regular expression. * * @task config */ private function getConfiguredRegex() { if (strlen($this->regex)) { return $this->regex; } $key = 'linter.scriptandregex.regex'; $config = $this->getDeprecatedConfiguration($key); if (!$config) { throw new ArcanistUsageException( pht( 'No "regex" configured for script-and-regex linter, which '. 'requires a regex. Use "%s" to configure one.', 'script-and-regex.regex')); } // NOTE: preg_match() returns 0 for no matches and false for compile error; // this won't match, but will validate the syntax of the regex. $ok = @preg_match($config, 'syntax-check'); if ($ok === false) { throw new ArcanistUsageException( pht( 'Regular expression passed to script-and-regex linter ("%s") is '. 'not a valid regular expression.', $config)); } return $config; } } diff --git a/src/lint/linter/ArcanistXMLLinter.php b/src/lint/linter/ArcanistXMLLinter.php index a1274861..b48b19bc 100644 --- a/src/lint/linter/ArcanistXMLLinter.php +++ b/src/lint/linter/ArcanistXMLLinter.php @@ -1,74 +1,74 @@ getData($path))) { // XML appears to be valid. return; } foreach (libxml_get_errors() as $error) { $message = id(new ArcanistLintMessage()) ->setPath($path) ->setLine($error->line) ->setChar($error->column ? $error->column : null) ->setCode($this->getLintMessageFullCode($error->code)) - ->setName('LibXML Error') + ->setName(pht('LibXML Error')) ->setDescription(trim($error->message)); switch ($error->level) { case LIBXML_ERR_NONE: $message->setSeverity(ArcanistLintSeverity::SEVERITY_DISABLED); break; case LIBXML_ERR_WARNING: $message->setSeverity(ArcanistLintSeverity::SEVERITY_WARNING); break; case LIBXML_ERR_ERROR: case LIBXML_ERR_FATAL: $message->setSeverity(ArcanistLintSeverity::SEVERITY_ERROR); break; default: $message->setSeverity(ArcanistLintSeverity::SEVERITY_ADVICE); break; } $this->addLintMessage($message); } } } diff --git a/src/lint/linter/__tests__/ArcanistCppcheckLinterTestCase.php b/src/lint/linter/__tests__/ArcanistCppcheckLinterTestCase.php index 75310761..8bf305b7 100644 --- a/src/lint/linter/__tests__/ArcanistCppcheckLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistCppcheckLinterTestCase.php @@ -1,10 +1,10 @@ executeTestsInDirectory(dirname(__FILE__).'/cppcheck/'); + $this->executeTestsInDirectory(dirname(__FILE__).'/cppcheck/'); } } diff --git a/src/lint/linter/__tests__/ArcanistCpplintLinterTestCase.php b/src/lint/linter/__tests__/ArcanistCpplintLinterTestCase.php index f178bb9f..7aef6db1 100644 --- a/src/lint/linter/__tests__/ArcanistCpplintLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistCpplintLinterTestCase.php @@ -1,10 +1,10 @@ executeTestsInDirectory(dirname(__FILE__).'/cpplint/'); + $this->executeTestsInDirectory(dirname(__FILE__).'/cpplint/'); } } diff --git a/src/lint/linter/__tests__/ArcanistLinterTestCase.php b/src/lint/linter/__tests__/ArcanistLinterTestCase.php index 322dca7f..738269ae 100644 --- a/src/lint/linter/__tests__/ArcanistLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistLinterTestCase.php @@ -1,263 +1,266 @@ getLinter(); } $files = id(new FileFinder($root)) ->withType('f') ->withSuffix('lint-test') ->find(); $test_count = 0; foreach ($files as $file) { $this->lintFile($root.$file, $linter); $test_count++; } $this->assertTrue( ($test_count > 0), - pht('Expected to find some .lint-test tests in directory %s!', $root)); + pht( + 'Expected to find some %s tests in directory %s!', + '.lint-test', + $root)); } private function lintFile($file, ArcanistLinter $linter) { $linter = clone $linter; $contents = Filesystem::readFile($file); $contents = preg_split('/^~{4,}\n/m', $contents); if (count($contents) < 2) { throw new Exception( pht( "Expected '%s' separating test case and results.", '~~~~~~~~~~')); } list ($data, $expect, $xform, $config) = array_merge( $contents, array(null, null)); $basename = basename($file); if ($config) { $config = phutil_json_decode($config); } else { $config = array(); } PhutilTypeSpec::checkMap( $config, array( 'config' => 'optional map', 'path' => 'optional string', 'mode' => 'optional string', 'stopped' => 'optional bool', )); $exception = null; $after_lint = null; $messages = null; $exception_message = false; $caught_exception = false; try { $tmp = new TempFile($basename); Filesystem::writeFile($tmp, $data); $full_path = (string)$tmp; $mode = idx($config, 'mode'); if ($mode) { Filesystem::changePermissions($tmp, octdec($mode)); } $dir = dirname($full_path); $path = basename($full_path); $working_copy = ArcanistWorkingCopyIdentity::newFromRootAndConfigFile( $dir, null, 'Unit Test'); $configuration_manager = new ArcanistConfigurationManager(); $configuration_manager->setWorkingCopyIdentity($working_copy); $engine = new ArcanistUnitTestableLintEngine(); $engine->setWorkingCopy($working_copy); $engine->setConfigurationManager($configuration_manager); $path_name = idx($config, 'path', $path); $engine->setPaths(array($path_name)); $linter->addPath($path_name); $linter->addData($path_name, $data); foreach (idx($config, 'config', array()) as $key => $value) { $linter->setLinterConfigurationValue($key, $value); } $engine->addLinter($linter); $engine->addFileData($path_name, $data); $results = $engine->run(); $this->assertEqual( 1, count($results), pht('Expect one result returned by linter.')); $assert_stopped = idx($config, 'stopped'); if ($assert_stopped !== null) { $this->assertEqual( $assert_stopped, $linter->didStopAllLinters(), $assert_stopped ? pht('Expect linter to be stopped.') : pht('Expect linter to not be stopped.')); } $result = reset($results); $patcher = ArcanistLintPatcher::newFromArcanistLintResult($result); $after_lint = $patcher->getModifiedFileContent(); } catch (ArcanistPhutilTestTerminatedException $ex) { throw $ex; } catch (Exception $exception) { $caught_exception = true; if ($exception instanceof PhutilAggregateException) { $caught_exception = false; foreach ($exception->getExceptions() as $ex) { if ($ex instanceof ArcanistUsageException || $ex instanceof ArcanistMissingLinterException) { $this->assertSkipped($ex->getMessage()); } else { $caught_exception = true; } } } else if ($exception instanceof ArcanistUsageException || $exception instanceof ArcanistMissingLinterException) { $this->assertSkipped($exception->getMessage()); } $exception_message = $exception->getMessage()."\n\n". $exception->getTraceAsString(); } $this->assertEqual(false, $caught_exception, $exception_message); $this->compareLint($basename, $expect, $result); $this->compareTransform($xform, $after_lint); } private function compareLint($file, $expect, ArcanistLintResult $result) { $seen = array(); $raised = array(); $message_map = array(); foreach ($result->getMessages() as $message) { $sev = $message->getSeverity(); $line = $message->getLine(); $char = $message->getChar(); $code = $message->getCode(); $name = $message->getName(); $message_key = $sev.':'.$line.':'.$char; $message_map[$message_key] = $message; $seen[] = $message_key; $raised[] = sprintf( ' %s: %s %s', pht('%s at line %d, char %d', $sev, $line, $char), $code, $name); } $expect = trim($expect); if ($expect) { $expect = explode("\n", $expect); } else { $expect = array(); } foreach ($expect as $key => $expected) { $expect[$key] = head(explode(' ', $expected)); } $expect = array_fill_keys($expect, true); $seen = array_fill_keys($seen, true); if (!$raised) { $raised = array(pht('No messages.')); } $raised = sprintf( "%s:\n%s", pht('Actually raised'), implode("\n", $raised)); foreach (array_diff_key($expect, $seen) as $missing => $ignored) { $missing = explode(':', $missing); $sev = array_shift($missing); $pos = $missing; $this->assertFailure( pht( "In '%s', expected lint to raise %s on line %d at char %d, ". "but no %s was raised. %s", $file, $sev, idx($pos, 0), idx($pos, 1), $sev, $raised)); } foreach (array_diff_key($seen, $expect) as $surprising => $ignored) { $message = $message_map[$surprising]; $message_info = $message->getDescription(); list($sev, $line, $char) = explode(':', $surprising); $this->assertFailure( sprintf( "%s:\n\n%s\n\n%s", pht( "In '%s', lint raised %s on line %d at char %d, ". "but nothing was expected", $file, $sev, $line, $char), $message_info, $raised)); } } private function compareTransform($expected, $actual) { if (!strlen($expected)) { return; } $this->assertEqual( $expected, $actual, pht('File as patched by lint did not match the expected patched file.')); } } diff --git a/src/lint/linter/__tests__/ArcanistNoLintLinterTestCase.php b/src/lint/linter/__tests__/ArcanistNoLintLinterTestCase.php index dab24b2d..0c4cbd8d 100644 --- a/src/lint/linter/__tests__/ArcanistNoLintLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistNoLintLinterTestCase.php @@ -1,9 +1,9 @@ executeTestsInDirectory(dirname(__FILE__).'/nolint/'); + $this->executeTestsInDirectory(dirname(__FILE__).'/nolint/'); } } diff --git a/src/lint/linter/__tests__/ArcanistPhutilXHPASTLinterTestCase.php b/src/lint/linter/__tests__/ArcanistPhutilXHPASTLinterTestCase.php index 824b1b74..d773e1f5 100644 --- a/src/lint/linter/__tests__/ArcanistPhutilXHPASTLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistPhutilXHPASTLinterTestCase.php @@ -1,14 +1,14 @@ setDeprecatedFunctions(array( - 'deprecated_function' => 'This function is most likely deprecated.', + 'deprecated_function' => pht('This function is most likely deprecated.'), )); $this->executeTestsInDirectory(dirname(__FILE__).'/phlxhp/', $linter); } } diff --git a/src/lint/linter/__tests__/ArcanistPyLintLinterTestCase.php b/src/lint/linter/__tests__/ArcanistPyLintLinterTestCase.php index 4d93f648..bbff142b 100644 --- a/src/lint/linter/__tests__/ArcanistPyLintLinterTestCase.php +++ b/src/lint/linter/__tests__/ArcanistPyLintLinterTestCase.php @@ -1,10 +1,10 @@ executeTestsInDirectory(dirname(__FILE__).'/pylint/'); + $this->executeTestsInDirectory(dirname(__FILE__).'/pylint/'); } } diff --git a/src/lint/renderer/ArcanistConsoleLintRenderer.php b/src/lint/renderer/ArcanistConsoleLintRenderer.php index b1c32a51..6f3476b0 100644 --- a/src/lint/renderer/ArcanistConsoleLintRenderer.php +++ b/src/lint/renderer/ArcanistConsoleLintRenderer.php @@ -1,240 +1,248 @@ showAutofixPatches = $show_autofix_patches; return $this; } public function renderLintResult(ArcanistLintResult $result) { $messages = $result->getMessages(); $path = $result->getPath(); $lines = explode("\n", $result->getData()); $text = array(); foreach ($messages as $message) { if (!$this->showAutofixPatches && $message->isAutofix()) { continue; } if ($message->isError()) { $color = 'red'; } else { $color = 'yellow'; } $severity = ArcanistLintSeverity::getStringForSeverity( $message->getSeverity()); $code = $message->getCode(); $name = $message->getName(); $description = $message->getDescription(); if ($message->getOtherLocations()) { $locations = array(); foreach ($message->getOtherLocations() as $location) { $locations[] = idx($location, 'path', $path). (!empty($location['line']) ? ":{$location['line']}" : ''); } - $description .= "\nOther locations: ".implode(', ', $locations); + $description .= "\n".pht( + 'Other locations: %s', + implode(', ', $locations)); } $text[] = phutil_console_format( " ** %s ** (%s) __%s__\n%s\n", $severity, $code, $name, phutil_console_wrap($description, 4)); if ($message->hasFileContext()) { $text[] = $this->renderContext($message, $lines); } } if ($text) { - $prefix = phutil_console_format("**>>>** Lint for __%s__:\n\n\n", $path); + $prefix = phutil_console_format( + "**>>>** %s\n\n\n", + pht( + 'Lint for %s:', + phutil_console_format('__%s__', $path))); return $prefix.implode("\n", $text); } else { return null; } } protected function renderContext( ArcanistLintMessage $message, array $line_data) { $lines_of_context = 3; $out = array(); $num_lines = count($line_data); // make line numbers line up with array indexes array_unshift($line_data, ''); $line_num = min($message->getLine(), $num_lines); $line_num = max(1, $line_num); // Print out preceding context before the impacted region. $cursor = max(1, $line_num - $lines_of_context); for (; $cursor < $line_num; $cursor++) { $out[] = $this->renderLine($cursor, $line_data[$cursor]); } $text = $message->getOriginalText(); $start = $message->getChar() - 1; $patch = ''; // Refine original and replacement text to eliminate start and end in common if ($message->isPatchable()) { $patch = $message->getReplacementText(); $text_strlen = strlen($text); $patch_strlen = strlen($patch); $min_length = min($text_strlen, $patch_strlen); $same_at_front = 0; for ($ii = 0; $ii < $min_length; $ii++) { if ($text[$ii] !== $patch[$ii]) { break; } $same_at_front++; $start++; if ($text[$ii] == "\n") { $out[] = $this->renderLine($cursor, $line_data[$cursor]); $cursor++; $start = 0; $line_num++; } } // deal with shorter string ' ' longer string ' a ' $min_length -= $same_at_front; // And check the end of the string $same_at_end = 0; for ($ii = 1; $ii <= $min_length; $ii++) { if ($text[$text_strlen - $ii] !== $patch[$patch_strlen - $ii]) { break; } $same_at_end++; } $text = substr( $text, $same_at_front, $text_strlen - $same_at_end - $same_at_front); $patch = substr( $patch, $same_at_front, $patch_strlen - $same_at_end - $same_at_front); } // Print out the impacted region itself. $diff = $message->isPatchable() ? '-' : null; $text_lines = explode("\n", $text); $text_length = count($text_lines); $intraline = ($text != '' || $start || !preg_match('/\n$/', $patch)); if ($intraline) { for (; $cursor < $line_num + $text_length; $cursor++) { $chevron = ($cursor == $line_num); // We may not have any data if, e.g., the old file does not exist. $data = idx($line_data, $cursor, null); // Highlight the problem substring. $text_line = $text_lines[$cursor - $line_num]; if (strlen($text_line)) { $data = substr_replace( $data, phutil_console_format('##%s##', $text_line), ($cursor == $line_num ? ($start > 0 ? $start : null) : 0), strlen($text_line)); } $out[] = $this->renderLine($cursor, $data, $chevron, $diff); } } // Print out replacement text. if ($message->isPatchable()) { // Strip trailing newlines, since "explode" will create an extra patch // line for these. if (strlen($patch) && ($patch[strlen($patch) - 1] === "\n")) { $patch = substr($patch, 0, -1); } $patch_lines = explode("\n", $patch); $patch_length = count($patch_lines); $patch_line = $patch_lines[0]; $len = isset($text_lines[0]) ? strlen($text_lines[0]) : 0; $patched = phutil_console_format('##%s##', $patch_line); if ($intraline) { $patched = substr_replace( $line_data[$line_num], $patched, $start, $len); } $out[] = $this->renderLine(null, $patched, false, '+'); foreach (array_slice($patch_lines, 1) as $patch_line) { $out[] = $this->renderLine( null, phutil_console_format('##%s##', $patch_line), false, '+'); } } $end = min($num_lines, $cursor + $lines_of_context); for (; $cursor < $end; $cursor++) { // If there is no original text, we didn't print out a chevron or any // highlighted text above, so print it out here. This allows messages // which don't have any original/replacement information to still // render with indicator chevrons. if ($text || $message->isPatchable()) { $chevron = false; } else { $chevron = ($cursor == $line_num); } $out[] = $this->renderLine($cursor, $line_data[$cursor], $chevron); // With original text, we'll render the text highlighted above. If the // lint message only has a line/char offset there's nothing to // highlight, so print out a caret on the next line instead. if ($chevron && $message->getChar()) { $out[] = $this->renderCaret($message->getChar()); } } $out[] = null; return implode("\n", $out); } private function renderCaret($pos) { return str_repeat(' ', 16 + $pos).'^'; } protected function renderLine($line, $data, $chevron = false, $diff = null) { $chevron = $chevron ? '>>>' : ''; return sprintf( ' %3s %1s %6s %s', $chevron, $diff, $line, $data); } public function renderOkayResult() { return phutil_console_format( - "** OKAY ** No lint warnings.\n"); + "** %s ** %s\n", + pht('OKAY'), + pht('No lint warnings.')); } } diff --git a/src/lint/renderer/ArcanistSummaryLintRenderer.php b/src/lint/renderer/ArcanistSummaryLintRenderer.php index c7837494..92eed3a4 100644 --- a/src/lint/renderer/ArcanistSummaryLintRenderer.php +++ b/src/lint/renderer/ArcanistSummaryLintRenderer.php @@ -1,30 +1,32 @@ getMessages(); $path = $result->getPath(); $text = array(); foreach ($messages as $message) { $name = $message->getName(); $severity = ArcanistLintSeverity::getStringForSeverity( $message->getSeverity()); $line = $message->getLine(); $text[] = "{$path}:{$line}:{$severity}: {$name}\n"; } return implode('', $text); } public function renderOkayResult() { return phutil_console_format( - "** OKAY ** No lint warnings.\n"); + "** %s ** %s\n", + pht('OKAY'), + pht('No lint warnings.')); } } diff --git a/src/parser/ArcanistBaseCommitParser.php b/src/parser/ArcanistBaseCommitParser.php index 7927f6b6..a546b702 100644 --- a/src/parser/ArcanistBaseCommitParser.php +++ b/src/parser/ArcanistBaseCommitParser.php @@ -1,192 +1,203 @@ api = $api; return $this; } private function tokenizeBaseCommitSpecification($raw_spec) { if (!$raw_spec) { return array(); } $spec = preg_split('/\s*,\s*/', $raw_spec); $spec = array_filter($spec); foreach ($spec as $rule) { if (strpos($rule, ':') === false) { throw new ArcanistUsageException( - "Rule '{$rule}' is invalid, it must have a type and name like ". - "'arc:upstream'."); + pht( + "Rule '%s' is invalid, it must have a type and name like '%s'.", + $rule, + 'arc:upstream')); } } return $spec; } private function log($message) { if ($this->verbose) { fwrite(STDERR, $message."\n"); } } public function resolveBaseCommit(array $specs) { $specs += array( 'runtime' => '', 'local' => '', 'project' => '', 'user' => '', 'system' => '', ); foreach ($specs as $source => $spec) { $specs[$source] = self::tokenizeBaseCommitSpecification($spec); } $this->try = array( 'runtime', 'local', 'project', 'user', 'system', ); while ($this->try) { $source = head($this->try); if (!idx($specs, $source)) { - $this->log("No rules left from source '{$source}'."); + $this->log(pht("No rules left from source '%s'.", $source)); array_shift($this->try); continue; } - $this->log("Trying rules from source '{$source}'."); + $this->log(pht("Trying rules from source '%s'.", $source)); $rules = &$specs[$source]; while ($rule = array_shift($rules)) { - $this->log("Trying rule '{$rule}'."); + $this->log(pht("Trying rule '%s'.", $rule)); $commit = $this->resolveRule($rule, $source); if ($commit === false) { // If a rule returns false, it means to go to the next ruleset. break; } else if ($commit !== null) { - $this->log("Resolved commit '{$commit}' from rule '{$rule}'."); + $this->log(pht( + "Resolved commit '%s' from rule '%s'.", + $commit, + $rule)); return $commit; } } } return null; } /** * Handle resolving individual rules. */ private function resolveRule($rule, $source) { // NOTE: Returning `null` from this method means "no match". // Returning `false` from this method means "stop current ruleset". list($type, $name) = explode(':', $rule, 2); switch ($type) { case 'literal': return $name; case 'git': case 'hg': return $this->api->resolveBaseCommitRule($rule, $source); case 'arc': return $this->resolveArcRule($rule, $name, $source); default: throw new ArcanistUsageException( - "Base commit rule '{$rule}' (from source '{$source}') ". - "is not a recognized rule."); + pht( + "Base commit rule '%s' (from source '%s') ". + "is not a recognized rule.", + $rule, + $source)); } } /** * Handle resolving "arc:*" rules. */ private function resolveArcRule($rule, $name, $source) { $name = $this->updateLegacyRuleName($name); switch ($name) { case 'verbose': $this->verbose = true; - $this->log('Enabled verbose mode.'); + $this->log(pht('Enabled verbose mode.')); break; case 'prompt': - $reason = 'it is what you typed when prompted.'; + $reason = pht('it is what you typed when prompted.'); $this->api->setBaseCommitExplanation($reason); - return phutil_console_prompt('Against which commit?'); + return phutil_console_prompt(pht('Against which commit?')); case 'local': case 'user': case 'project': case 'runtime': case 'system': // Push the other source on top of the list. array_unshift($this->try, $name); - $this->log("Switching to source '{$name}'."); + $this->log(pht("Switching to source '%s'.", $name)); return false; case 'yield': // Cycle this source to the end of the list. $this->try[] = array_shift($this->try); - $this->log("Yielding processing of rules from '{$source}'."); + $this->log(pht("Yielding processing of rules from '%s'.", $source)); return false; case 'halt': // Dump the whole stack. $this->try = array(); - $this->log('Halting all rule processing.'); + $this->log(pht('Halting all rule processing.')); return false; case 'skip': return null; case 'empty': case 'upstream': case 'outgoing': case 'bookmark': case 'amended': case 'this': return $this->api->resolveBaseCommitRule($rule, $source); default: $matches = null; if (preg_match('/^exec\((.*)\)$/', $name, $matches)) { $root = $this->api->getWorkingCopyIdentity()->getProjectRoot(); $future = new ExecFuture('%C', $matches[1]); $future->setCWD($root); list($err, $stdout) = $future->resolve(); if (!$err) { return trim($stdout); } else { return null; } } else if (preg_match('/^nodiff\((.*)\)$/', $name, $matches)) { return $this->api->resolveBaseCommitRule($rule, $source); } throw new ArcanistUsageException( - "Base commit rule '{$rule}' (from source '{$source}') ". - "is not a recognized rule."); + pht( + "Base commit rule '%s' (from source '%s') ". + "is not a recognized rule.", + $rule, + $source)); } } private function updateLegacyRuleName($name) { $updated = array( 'global' => 'user', 'args' => 'runtime', ); $new_name = idx($updated, $name); if ($new_name) { - $this->log("translating legacy name '$name' to '$new_name'"); + $this->log(pht("Translating legacy name '%s' to '%s'", $name, $new_name)); return $new_name; } return $name; } } diff --git a/src/parser/ArcanistBundle.php b/src/parser/ArcanistBundle.php index 79bb574c..7324b8ea 100644 --- a/src/parser/ArcanistBundle.php +++ b/src/parser/ArcanistBundle.php @@ -1,869 +1,874 @@ authorEmail = $author_email; return $this; } public function getAuthorEmail() { return $this->authorEmail; } public function setAuthorName($author_name) { $this->authorName = $author_name; return $this; } public function getAuthorName() { return $this->authorName; } public function getFullAuthor() { $author_name = $this->getAuthorName(); if ($author_name === null) { return null; } $author_email = $this->getAuthorEmail(); if ($author_email === null) { return null; } $full_author = sprintf('%s <%s>', $author_name, $author_email); // Because git is very picky about the author being in a valid format, // verify that we can parse it. $address = new PhutilEmailAddress($full_author); if (!$address->getDisplayName() || !$address->getAddress()) { return null; } return $full_author; } public function setConduit(ConduitClient $conduit) { $this->conduit = $conduit; return $this; } public function setProjectID($project_id) { $this->projectID = $project_id; return $this; } public function getProjectID() { return $this->projectID; } public function setBaseRevision($base_revision) { $this->baseRevision = $base_revision; return $this; } public function setEncoding($encoding) { $this->encoding = $encoding; return $this; } public function getEncoding() { return $this->encoding; } public function getBaseRevision() { return $this->baseRevision; } public function setRevisionID($revision_id) { $this->revisionID = $revision_id; return $this; } public function getRevisionID() { return $this->revisionID; } public static function newFromChanges(array $changes) { $obj = new ArcanistBundle(); $obj->changes = $changes; return $obj; } private function getEOL($patch_type) { // NOTE: Git always generates "\n" line endings, even under Windows, and // can not parse certain patches with "\r\n" line endings. SVN generates // patches with "\n" line endings on Mac or Linux and "\r\n" line endings // on Windows. (This EOL style is used only for patch metadata lines, not // for the actual patch content.) // (On Windows, Mercurial generates \n newlines for `--git` diffs, as it // must, but also \n newlines for unified diffs. We never need to deal with // these as we use Git format for Mercurial, so this case is currently // ignored.) switch ($patch_type) { case 'git': return "\n"; case 'unified': return phutil_is_windows() ? "\r\n" : "\n"; default: throw new Exception( - "Unknown patch type '{$patch_type}'!"); + pht("Unknown patch type '%s'!", $patch_type)); } } public static function newFromArcBundle($path) { $path = Filesystem::resolvePath($path); $future = new ExecFuture( 'tar tfO %s', $path); list($stdout, $file_list) = $future->resolvex(); $file_list = explode("\n", trim($file_list)); if (in_array('meta.json', $file_list)) { $future = new ExecFuture( 'tar xfO %s meta.json', $path); $meta_info = $future->resolveJSON(); $version = idx($meta_info, 'version', 0); $project_name = idx($meta_info, 'projectName'); $base_revision = idx($meta_info, 'baseRevision'); $revision_id = idx($meta_info, 'revisionID'); $encoding = idx($meta_info, 'encoding'); $author_name = idx($meta_info, 'authorName'); $author_email = idx($meta_info, 'authorEmail'); } else { // this arc bundle was probably made before we started storing meta info $version = 0; $project_name = null; $base_revision = null; $revision_id = null; $encoding = null; $author = null; } $future = new ExecFuture( 'tar xfO %s changes.json', $path); $changes = $future->resolveJSON(); foreach ($changes as $change_key => $change) { foreach ($change['hunks'] as $key => $hunk) { list($hunk_data) = execx('tar xfO %s hunks/%s', $path, $hunk['corpus']); $changes[$change_key]['hunks'][$key]['corpus'] = $hunk_data; } } foreach ($changes as $change_key => $change) { $changes[$change_key] = ArcanistDiffChange::newFromDictionary($change); } $obj = new ArcanistBundle(); $obj->changes = $changes; $obj->diskPath = $path; $obj->setProjectID($project_name); $obj->setBaseRevision($base_revision); $obj->setRevisionID($revision_id); $obj->setEncoding($encoding); return $obj; } public static function newFromDiff($data) { $obj = new ArcanistBundle(); $parser = new ArcanistDiffParser(); $obj->changes = $parser->parseDiff($data); return $obj; } private function __construct() {} public function writeToDisk($path) { $changes = $this->getChanges(); $change_list = array(); foreach ($changes as $change) { $change_list[] = $change->toDictionary(); } $hunks = array(); foreach ($change_list as $change_key => $change) { foreach ($change['hunks'] as $key => $hunk) { $hunks[] = $hunk['corpus']; $change_list[$change_key]['hunks'][$key]['corpus'] = count($hunks) - 1; } } $blobs = array(); foreach ($change_list as $change) { if (!empty($change['metadata']['old:binary-phid'])) { $blobs[$change['metadata']['old:binary-phid']] = null; } if (!empty($change['metadata']['new:binary-phid'])) { $blobs[$change['metadata']['new:binary-phid']] = null; } } foreach ($blobs as $phid => $null) { $blobs[$phid] = $this->getBlob($phid); } $meta_info = array( 'version' => 5, 'projectName' => $this->getProjectID(), 'baseRevision' => $this->getBaseRevision(), 'revisionID' => $this->getRevisionID(), 'encoding' => $this->getEncoding(), 'authorName' => $this->getAuthorName(), 'authorEmail' => $this->getAuthorEmail(), ); $dir = Filesystem::createTemporaryDirectory(); Filesystem::createDirectory($dir.'/hunks'); Filesystem::createDirectory($dir.'/blobs'); Filesystem::writeFile($dir.'/changes.json', json_encode($change_list)); Filesystem::writeFile($dir.'/meta.json', json_encode($meta_info)); foreach ($hunks as $key => $hunk) { Filesystem::writeFile($dir.'/hunks/'.$key, $hunk); } foreach ($blobs as $key => $blob) { Filesystem::writeFile($dir.'/blobs/'.$key, $blob); } execx( '(cd %s; tar -czf %s *)', $dir, Filesystem::resolvePath($path)); Filesystem::remove($dir); } public function toUnifiedDiff() { $eol = $this->getEOL('unified'); $result = array(); $changes = $this->getChanges(); foreach ($changes as $change) { $hunk_changes = $this->buildHunkChanges($change->getHunks(), $eol); if (!$hunk_changes) { continue; } $old_path = $this->getOldPath($change); $cur_path = $this->getCurrentPath($change); $index_path = $cur_path; if ($index_path === null) { $index_path = $old_path; } $result[] = 'Index: '.$index_path; $result[] = $eol; $result[] = str_repeat('=', 67); $result[] = $eol; if ($old_path === null) { $old_path = '/dev/null'; } if ($cur_path === null) { $cur_path = '/dev/null'; } // When the diff is used by `patch`, `patch` ignores what is listed as the // current path and just makes changes to the file at the old path (unless // the current path is '/dev/null'. // If the old path and the current path aren't the same (and neither is // /dev/null), this indicates the file was moved or copied. By listing // both paths as the new file, `patch` will apply the diff to the new // file. if ($cur_path !== '/dev/null' && $old_path !== '/dev/null') { $old_path = $cur_path; } $result[] = '--- '.$old_path.$eol; $result[] = '+++ '.$cur_path.$eol; $result[] = $hunk_changes; } if (!$result) { return ''; } $diff = implode('', $result); return $this->convertNonUTF8Diff($diff); } public function toGitPatch() { $eol = $this->getEOL('git'); $result = array(); $changes = $this->getChanges(); $binary_sources = array(); foreach ($changes as $change) { if (!$this->isGitBinaryChange($change)) { continue; } $type = $change->getType(); if ($type == ArcanistDiffChangeType::TYPE_MOVE_AWAY || $type == ArcanistDiffChangeType::TYPE_COPY_AWAY || $type == ArcanistDiffChangeType::TYPE_MULTICOPY) { foreach ($change->getAwayPaths() as $path) { $binary_sources[$path] = $change; } } } foreach (array_keys($changes) as $multicopy_key) { $multicopy_change = $changes[$multicopy_key]; $type = $multicopy_change->getType(); if ($type != ArcanistDiffChangeType::TYPE_MULTICOPY) { continue; } // Decompose MULTICOPY into one MOVE_HERE and several COPY_HERE because // we need more information than we have in order to build a delete patch // and represent it as a bunch of COPY_HERE plus a delete. For details, // see T419. // Basically, MULTICOPY means there are 2 or more corresponding COPY_HERE // changes, so find one of them arbitrarily and turn it into a MOVE_HERE. // TODO: We might be able to do this more cleanly after T230 is resolved. $decompose_okay = false; foreach ($changes as $change_key => $change) { if ($change->getType() != ArcanistDiffChangeType::TYPE_COPY_HERE) { continue; } if ($change->getOldPath() != $multicopy_change->getCurrentPath()) { continue; } $decompose_okay = true; $change = clone $change; $change->setType(ArcanistDiffChangeType::TYPE_MOVE_HERE); $changes[$change_key] = $change; // The multicopy is now fully represented by MOVE_HERE plus one or more // COPY_HERE, so throw it away. unset($changes[$multicopy_key]); break; } if (!$decompose_okay) { throw new Exception( - 'Failed to decompose multicopy changeset in order to generate diff.'); + pht( + 'Failed to decompose multicopy changeset in '. + 'order to generate diff.')); } } foreach ($changes as $change) { $type = $change->getType(); $file_type = $change->getFileType(); if ($file_type == ArcanistDiffChangeType::FILE_DIRECTORY) { // TODO: We should raise a FYI about this, so the user is aware // that we omitted it, if the directory is empty or has permissions // which git can't represent. // Git doesn't support empty directories, so we simply ignore them. If // the directory is nonempty, 'git apply' will create it when processing // the changesets for files inside it. continue; } if ($type == ArcanistDiffChangeType::TYPE_MOVE_AWAY) { // Git will apply this in the corresponding MOVE_HERE. continue; } $old_mode = idx($change->getOldProperties(), 'unix:filemode', '100644'); $new_mode = idx($change->getNewProperties(), 'unix:filemode', '100644'); $is_binary = $this->isGitBinaryChange($change); if ($is_binary) { $old_binary = idx($binary_sources, $this->getCurrentPath($change)); $change_body = $this->buildBinaryChange($change, $old_binary); } else { $change_body = $this->buildHunkChanges($change->getHunks(), $eol); } if ($type == ArcanistDiffChangeType::TYPE_COPY_AWAY) { // TODO: This is only relevant when patching old Differential diffs // which were created prior to arc pruning TYPE_COPY_AWAY for files // with no modifications. if (!strlen($change_body) && ($old_mode == $new_mode)) { continue; } } $old_path = $this->getOldPath($change); $cur_path = $this->getCurrentPath($change); if ($old_path === null) { $old_index = 'a/'.$cur_path; $old_target = '/dev/null'; } else { $old_index = 'a/'.$old_path; $old_target = 'a/'.$old_path; } if ($cur_path === null) { $cur_index = 'b/'.$old_path; $cur_target = '/dev/null'; } else { $cur_index = 'b/'.$cur_path; $cur_target = 'b/'.$cur_path; } $result[] = "diff --git {$old_index} {$cur_index}".$eol; if ($type == ArcanistDiffChangeType::TYPE_ADD) { $result[] = "new file mode {$new_mode}".$eol; } if ($type == ArcanistDiffChangeType::TYPE_COPY_HERE || $type == ArcanistDiffChangeType::TYPE_MOVE_HERE || $type == ArcanistDiffChangeType::TYPE_COPY_AWAY || $type == ArcanistDiffChangeType::TYPE_CHANGE) { if ($old_mode !== $new_mode) { $result[] = "old mode {$old_mode}".$eol; $result[] = "new mode {$new_mode}".$eol; } } if ($type == ArcanistDiffChangeType::TYPE_COPY_HERE) { $result[] = "copy from {$old_path}".$eol; $result[] = "copy to {$cur_path}".$eol; } else if ($type == ArcanistDiffChangeType::TYPE_MOVE_HERE) { $result[] = "rename from {$old_path}".$eol; $result[] = "rename to {$cur_path}".$eol; } else if ($type == ArcanistDiffChangeType::TYPE_DELETE || $type == ArcanistDiffChangeType::TYPE_MULTICOPY) { $old_mode = idx($change->getOldProperties(), 'unix:filemode'); if ($old_mode) { $result[] = "deleted file mode {$old_mode}".$eol; } } if ($change_body) { if (!$is_binary) { $result[] = "--- {$old_target}".$eol; $result[] = "+++ {$cur_target}".$eol; } $result[] = $change_body; } } $diff = implode('', $result).$eol; return $this->convertNonUTF8Diff($diff); } private function isGitBinaryChange(ArcanistDiffChange $change) { $file_type = $change->getFileType(); return ($file_type == ArcanistDiffChangeType::FILE_BINARY || $file_type == ArcanistDiffChangeType::FILE_IMAGE); } private function convertNonUTF8Diff($diff) { if ($this->encoding) { $diff = phutil_utf8_convert($diff, $this->encoding, 'UTF-8'); } return $diff; } public function getChanges() { return $this->changes; } private function breakHunkIntoSmallHunks(ArcanistDiffHunk $base_hunk) { $context = 3; $results = array(); $lines = phutil_split_lines($base_hunk->getCorpus()); $n = count($lines); $old_offset = $base_hunk->getOldOffset(); $new_offset = $base_hunk->getNewOffset(); $ii = 0; $jj = 0; while ($ii < $n) { // Skip lines until we find the next line with changes. Note: this skips // both ' ' (no changes) and '\' (no newline at end of file) lines. If we // don't skip the latter, we may incorrectly generate a terminal hunk // that has no actual change information when a file doesn't have a // terminal newline and not changed near the end of the file. 'patch' will // fail to apply the diff if we generate a hunk that does not actually // contain changes. for ($jj = $ii; $jj < $n; ++$jj) { $char = $lines[$jj][0]; if ($char == '-' || $char == '+') { break; } } if ($jj >= $n) { break; } $hunk_start = max($jj - $context, 0); // NOTE: There are two tricky considerations here. // We can not generate a patch with overlapping hunks, or 'git apply' // rejects it after 1.7.3.4. // We can not generate a patch with too much trailing context, or // 'patch' rejects it. // So we need to ensure that we generate disjoint hunks, but don't // generate any hunks with too much context. $old_lines = 0; $new_lines = 0; $hunk_adjust = 0; $last_change = $jj; $break_here = null; for (; $jj < $n; ++$jj) { if ($lines[$jj][0] == ' ') { if ($jj - $last_change > $context) { if ($break_here === null) { // We haven't seen a change in $context lines, so this is a // potential place to break the hunk. However, we need to keep // looking in case there is another change fewer than $context // lines away, in which case we have to merge the hunks. $break_here = $jj; } } if ($jj - $last_change > (($context + 1) * 2)) { // We definitely aren't going to merge this with the next hunk, so // break out of the loop. We'll end the hunk at $break_here. break; } } else { $break_here = null; $last_change = $jj; if ($lines[$jj][0] == '\\') { // When we have a "\ No newline at end of file" line, it does not // contribute to either hunk length. ++$hunk_adjust; } else if ($lines[$jj][0] == '-') { ++$old_lines; } else if ($lines[$jj][0] == '+') { ++$new_lines; } } } if ($break_here !== null) { $jj = $break_here; } $hunk_length = min($jj, $n) - $hunk_start; $count_length = ($hunk_length - $hunk_adjust); $hunk = new ArcanistDiffHunk(); $hunk->setOldOffset($old_offset + $hunk_start - $ii); $hunk->setNewOffset($new_offset + $hunk_start - $ii); $hunk->setOldLength($count_length - $new_lines); $hunk->setNewLength($count_length - $old_lines); $corpus = array_slice($lines, $hunk_start, $hunk_length); $corpus = implode('', $corpus); $hunk->setCorpus($corpus); $results[] = $hunk; $old_offset += ($jj - $ii) - $new_lines; $new_offset += ($jj - $ii) - $old_lines; $ii = $jj; } return $results; } private function getOldPath(ArcanistDiffChange $change) { $old_path = $change->getOldPath(); $type = $change->getType(); if (!strlen($old_path) || $type == ArcanistDiffChangeType::TYPE_ADD) { $old_path = null; } return $old_path; } private function getCurrentPath(ArcanistDiffChange $change) { $cur_path = $change->getCurrentPath(); $type = $change->getType(); if (!strlen($cur_path) || $type == ArcanistDiffChangeType::TYPE_DELETE || $type == ArcanistDiffChangeType::TYPE_MULTICOPY) { $cur_path = null; } return $cur_path; } private function buildHunkChanges(array $hunks, $eol) { assert_instances_of($hunks, 'ArcanistDiffHunk'); $result = array(); foreach ($hunks as $hunk) { $small_hunks = $this->breakHunkIntoSmallHunks($hunk); foreach ($small_hunks as $small_hunk) { $o_off = $small_hunk->getOldOffset(); $o_len = $small_hunk->getOldLength(); $n_off = $small_hunk->getNewOffset(); $n_len = $small_hunk->getNewLength(); $corpus = $small_hunk->getCorpus(); // NOTE: If the length is 1 it can be omitted. Since git does this, // we also do it so that "arc export --git" diffs are as similar to // real git diffs as possible, which helps debug issues. if ($o_len == 1) { $o_head = "{$o_off}"; } else { $o_head = "{$o_off},{$o_len}"; } if ($n_len == 1) { $n_head = "{$n_off}"; } else { $n_head = "{$n_off},{$n_len}"; } $result[] = "@@ -{$o_head} +{$n_head} @@".$eol; $result[] = $corpus; $last = substr($corpus, -1); if ($last !== false && $last != "\r" && $last != "\n") { $result[] = $eol; } } } return implode('', $result); } public function setLoadFileDataCallback($callback) { $this->loadFileDataCallback = $callback; return $this; } private function getBlob($phid, $name = null) { if ($this->loadFileDataCallback) { return call_user_func($this->loadFileDataCallback, $phid); } if ($this->diskPath) { list($blob_data) = execx('tar xfO %s blobs/%s', $this->diskPath, $phid); return $blob_data; } $console = PhutilConsole::getConsole(); if ($this->conduit) { if ($name) { - $console->writeErr("Downloading binary data for '%s'...\n", $name); + $console->writeErr( + "%s\n", + pht("Downloading binary data for '%s'...", $name)); } else { - $console->writeErr("Downloading binary data...\n"); + $console->writeErr("%s\n", pht('Downloading binary data...')); } $data_base64 = $this->conduit->callMethodSynchronous( 'file.download', array( 'phid' => $phid, )); return base64_decode($data_base64); } - throw new Exception("Nowhere to load blob '{$phid}' from!"); + throw new Exception(pht("Nowhere to load blob '%s' from!", $phid)); } private function buildBinaryChange(ArcanistDiffChange $change, $old_binary) { $eol = $this->getEOL('git'); // In Git, when we write out a binary file move or copy, we need the // original binary for the source and the current binary for the // destination. if ($old_binary) { if ($old_binary->getOriginalFileData() !== null) { $old_data = $old_binary->getOriginalFileData(); $old_phid = null; } else { $old_data = null; $old_phid = $old_binary->getMetadata('old:binary-phid'); } } else { $old_data = $change->getOriginalFileData(); $old_phid = $change->getMetadata('old:binary-phid'); } if ($old_data === null && $old_phid) { $name = basename($change->getOldPath()); $old_data = $this->getBlob($old_phid, $name); } $old_length = strlen($old_data); if ($old_data === null) { $old_data = ''; $old_sha1 = str_repeat('0', 40); } else { $old_sha1 = sha1("blob {$old_length}\0{$old_data}"); } $new_phid = $change->getMetadata('new:binary-phid'); $new_data = null; if ($change->getCurrentFileData() !== null) { $new_data = $change->getCurrentFileData(); } else if ($new_phid) { $name = basename($change->getCurrentPath()); $new_data = $this->getBlob($new_phid, $name); } $new_length = strlen($new_data); if ($new_data === null) { $new_data = ''; $new_sha1 = str_repeat('0', 40); } else { $new_sha1 = sha1("blob {$new_length}\0{$new_data}"); } $content = array(); $content[] = "index {$old_sha1}..{$new_sha1}".$eol; $content[] = 'GIT binary patch'.$eol; $content[] = "literal {$new_length}".$eol; $content[] = $this->emitBinaryDiffBody($new_data).$eol; $content[] = "literal {$old_length}".$eol; $content[] = $this->emitBinaryDiffBody($old_data).$eol; return implode('', $content); } private function emitBinaryDiffBody($data) { $eol = $this->getEOL('git'); if (!function_exists('gzcompress')) { throw new Exception( - 'This patch has binary data. The PHP zlib extension is required to '. - 'apply patches with binary data to git. Install the PHP zlib '. - 'extension to continue.'); + pht( + 'This patch has binary data. The PHP zlib extension is required to '. + 'apply patches with binary data to git. Install the PHP zlib '. + 'extension to continue.')); } // See emit_binary_diff_body() in diff.c for git's implementation. $buf = ''; $deflated = gzcompress($data); $lines = str_split($deflated, 52); foreach ($lines as $line) { $len = strlen($line); // The first character encodes the line length. if ($len <= 26) { $buf .= chr($len + ord('A') - 1); } else { $buf .= chr($len - 26 + ord('a') - 1); } $buf .= self::encodeBase85($line); $buf .= $eol; } return $buf; } public static function encodeBase85($data) { // This is implemented awkwardly in order to closely mirror git's // implementation in base85.c // It is also implemented awkwardly to work correctly on 32-bit machines. // Broadly, this algorithm converts the binary input to printable output // by transforming each 4 binary bytes of input to 5 printable bytes of // output, one piece at a time. // // To do this, we convert the 4 bytes into a 32-bit integer, then use // modulus and division by 85 to pick out printable bytes (85^5 is slightly // larger than 2^32). In C, this algorithm is fairly easy to implement // because the accumulator can be made unsigned. // // In PHP, there are no unsigned integers, so values larger than 2^31 break // on 32-bit systems under modulus: // // $ php -r 'print (1 << 31) % 13;' # On a 32-bit machine. // -11 // // However, PHP's float type is an IEEE 754 64-bit double precision float, // so we can safely store integers up to around 2^53 without loss of // precision. To work around the lack of an unsigned type, we just use a // double and perform the modulus with fmod(). // // (Since PHP overflows integer operations into floats, we don't need much // additional casting.) static $map = array( '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '!', '#', '$', '%', '&', '(', ')', '*', '+', '-', ';', '<', '=', '>', '?', '@', '^', '_', '`', '{', '|', '}', '~', ); $buf = ''; $pos = 0; $bytes = strlen($data); while ($bytes) { $accum = 0; for ($count = 24; $count >= 0; $count -= 8) { $val = ord($data[$pos++]); $val = $val * (1 << $count); $accum = $accum + $val; if (--$bytes == 0) { break; } } $slice = ''; for ($count = 4; $count >= 0; $count--) { $val = (int)fmod($accum, 85.0); $accum = floor($accum / 85.0); $slice .= $map[$val]; } $buf .= strrev($slice); } return $buf; } } diff --git a/src/parser/ArcanistDiffParser.php b/src/parser/ArcanistDiffParser.php index bbe01d0f..0e9627b7 100644 --- a/src/parser/ArcanistDiffParser.php +++ b/src/parser/ArcanistDiffParser.php @@ -1,1420 +1,1443 @@ repositoryAPI = $repository_api; return $this; } public function setDetectBinaryFiles($detect) { $this->detectBinaryFiles = $detect; return $this; } public function setTryEncoding($encoding) { $this->tryEncoding = $encoding; return $this; } public function forcePath($path) { $this->forcePath = $path; return $this; } public function setChanges(array $changes) { assert_instances_of($changes, 'ArcanistDiffChange'); $this->changes = mpull($changes, null, 'getCurrentPath'); return $this; } public function parseSubversionDiff(ArcanistSubversionAPI $api, $paths) { $this->setRepositoryAPI($api); $diffs = array(); foreach ($paths as $path => $status) { if ($status & ArcanistRepositoryAPI::FLAG_UNTRACKED || $status & ArcanistRepositoryAPI::FLAG_CONFLICT || $status & ArcanistRepositoryAPI::FLAG_MISSING) { unset($paths[$path]); } } $root = null; $from = array(); foreach ($paths as $path => $status) { $change = $this->buildChange($path); if ($status & ArcanistRepositoryAPI::FLAG_ADDED) { $change->setType(ArcanistDiffChangeType::TYPE_ADD); } else if ($status & ArcanistRepositoryAPI::FLAG_DELETED) { $change->setType(ArcanistDiffChangeType::TYPE_DELETE); } else { $change->setType(ArcanistDiffChangeType::TYPE_CHANGE); } $is_dir = is_dir($api->getPath($path)); if ($is_dir) { $change->setFileType(ArcanistDiffChangeType::FILE_DIRECTORY); // We have to go hit the diff even for directories because they may // have property changes or moves, etc. } $is_link = is_link($api->getPath($path)); if ($is_link) { $change->setFileType(ArcanistDiffChangeType::FILE_SYMLINK); } $diff = $api->getRawDiffText($path); if ($diff) { $this->parseDiff($diff); } $info = $api->getSVNInfo($path); if (idx($info, 'Copied From URL')) { if (!$root) { $rinfo = $api->getSVNInfo('.'); $root = $rinfo['URL'].'/'; } $cpath = $info['Copied From URL']; $root_len = strlen($root); if (!strncmp($cpath, $root, $root_len)) { $cpath = substr($cpath, $root_len); // The user can "svn cp /path/to/file@12345 x", which pulls a file out // of version history at a specific revision. If we just use the path, // we'll collide with possible changes to that path in the working // copy below. In particular, "svn cp"-ing a path which no longer // exists somewhere in the working copy and then adding that path // gets us to the "origin change type" branches below with a // TYPE_ADD state on the path. To avoid this, append the origin // revision to the path so we'll necessarily generate a new change. // TODO: In theory, you could have an '@' in your path and this could // cause a collision, e.g. two files named 'f' and 'f@12345'. This is // at least somewhat the user's fault, though. if ($info['Copied From Rev']) { if ($info['Copied From Rev'] != $info['Revision']) { $cpath .= '@'.$info['Copied From Rev']; } } $change->setOldPath($cpath); $from[$path] = $cpath; } } $type = $change->getType(); if (($type === ArcanistDiffChangeType::TYPE_MOVE_AWAY || $type === ArcanistDiffChangeType::TYPE_DELETE) && idx($info, 'Node Kind') === 'directory') { $change->setFileType(ArcanistDiffChangeType::FILE_DIRECTORY); } } foreach ($paths as $path => $status) { $change = $this->buildChange($path); if (empty($from[$path])) { continue; } if (empty($this->changes[$from[$path]])) { if ($change->getType() == ArcanistDiffChangeType::TYPE_COPY_HERE) { // If the origin path wasn't changed (or isn't included in this diff) // and we only copied it, don't generate a changeset for it. This // keeps us out of trouble when we go to 'arc commit' and need to // figure out which files should be included in the commit list. continue; } } $origin = $this->buildChange($from[$path]); $origin->addAwayPath($change->getCurrentPath()); $type = $origin->getType(); switch ($type) { case ArcanistDiffChangeType::TYPE_MULTICOPY: case ArcanistDiffChangeType::TYPE_COPY_AWAY: // "Add" is possible if you do some bizarre tricks with svn:ignore and // "svn copy"'ing URLs straight from the repository; you can end up with // a file that is a copy of itself. See T271. case ArcanistDiffChangeType::TYPE_ADD: break; case ArcanistDiffChangeType::TYPE_DELETE: $origin->setType(ArcanistDiffChangeType::TYPE_MOVE_AWAY); break; case ArcanistDiffChangeType::TYPE_MOVE_AWAY: $origin->setType(ArcanistDiffChangeType::TYPE_MULTICOPY); break; case ArcanistDiffChangeType::TYPE_CHANGE: $origin->setType(ArcanistDiffChangeType::TYPE_COPY_AWAY); break; default: - throw new Exception("Bad origin state {$type}."); + throw new Exception(pht('Bad origin state %s.', $type)); } $type = $origin->getType(); switch ($type) { case ArcanistDiffChangeType::TYPE_MULTICOPY: case ArcanistDiffChangeType::TYPE_MOVE_AWAY: $change->setType(ArcanistDiffChangeType::TYPE_MOVE_HERE); break; case ArcanistDiffChangeType::TYPE_ADD: case ArcanistDiffChangeType::TYPE_COPY_AWAY: $change->setType(ArcanistDiffChangeType::TYPE_COPY_HERE); break; default: - throw new Exception("Bad origin state {$type}."); + throw new Exception(pht('Bad origin state %s.', $type)); } } return $this->changes; } public function parseDiff($diff) { if (!strlen(trim($diff))) { - throw new Exception("Can't parse an empty diff!"); + throw new Exception(pht("Can't parse an empty diff!")); } // Detect `git-format-patch`, by looking for a "---" line somewhere in // the file and then a footer with Git version number, which looks like // this: // // -- // 1.8.4.2 // // Note that `git-format-patch` adds a space after the "--", but we don't // require it when detecting patches, as trailing whitespace can easily be // lost in transit. $detect_patch = '/^---$.*^-- ?[\s\d.]+\z/ms'; $message = null; if (preg_match($detect_patch, $diff)) { list($message, $diff) = $this->stripGitFormatPatch($diff); } $this->didStartParse($diff); // Strip off header comments. While `patch` allows comments anywhere in the // file, `git apply` is more strict. We get these comments in `hg export` // diffs, and Eclipse can also produce them. $line = $this->getLineTrimmed(); while (preg_match('/^#/', $line)) { $line = $this->nextLine(); } if (strlen($message)) { // If we found a message during pre-parse steps, add it to the resulting // changes here. $change = $this->buildChange(null) ->setType(ArcanistDiffChangeType::TYPE_MESSAGE) ->setMetadata('message', $message); } do { $patterns = array( // This is a normal SVN text change, probably from "svn diff". '(?PIndex): (?P.+)', // This is an SVN text change, probably from "svnlook diff". '(?PModified|Added|Deleted|Copied): (?P.+)', // This is an SVN property change, probably from "svn diff". '(?PProperty changes on): (?P.+)', // This is a git commit message, probably from "git show". '(?Pcommit) (?P[a-f0-9]+)(?: \(.*\))?', // This is a git diff, probably from "git show" or "git diff". // Note that the filenames may appear quoted. '(?Pdiff --git) (?P.*)', // RCS Diff '(?Prcsdiff -u) (?P.*)', // This is a unified diff, probably from "diff -u" or synthetic diffing. '(?P---) (?P.+)\s+\d{4}-\d{2}-\d{2}.*', '(?PBinary files|Files) '. '(?P.+)\s+\d{4}-\d{2}-\d{2} and '. '(?P.+)\s+\d{4}-\d{2}-\d{2} differ.*', // This is a normal Mercurial text change, probably from "hg diff". It // may have two "-r" blocks if it came from "hg diff -r x:y". '(?Pdiff -r) (?P[a-f0-9]+) (?:-r [a-f0-9]+ )?(?P.+)', ); $line = $this->getLineTrimmed(); $match = null; $ok = $this->tryMatchHeader($patterns, $line, $match); $failed_parse = false; if (!$ok && $this->isFirstNonEmptyLine()) { // 'hg export' command creates so called "extended diff" that // contains some meta information and comment at the beginning // (isFirstNonEmptyLine() to check for beginning). Actual mercurial // code detects where comment ends and unified diff starts by // searching for "diff -r" or "diff --git" in the text. $this->saveLine(); $line = $this->nextLineThatLooksLikeDiffStart(); if (!$this->tryMatchHeader($patterns, $line, $match)) { // Restore line before guessing to display correct error. $this->restoreLine(); $failed_parse = true; } } else if (!$ok) { $failed_parse = true; } if ($failed_parse) { $this->didFailParse( - "Expected a hunk header, like 'Index: /path/to/file.ext' (svn), ". - "'Property changes on: /path/to/file.ext' (svn properties), ". - "'commit 59bcc3ad6775562f845953cf01624225' (git show), ". - "'diff --git' (git diff), '--- filename' (unified diff), or ". - "'diff -r' (hg diff or patch)."); + pht( + "Expected a hunk header, like '%s' (svn), '%s' (svn properties), ". + "'%s' (git show), '%s' (git diff), '%s' (unified diff), or ". + "'%s' (hg diff or patch).", + 'Index: /path/to/file.ext', + 'Property changes on: /path/to/file.ext', + 'commit 59bcc3ad6775562f845953cf01624225', + 'diff --git', + '--- filename', + 'diff -r')); } if (isset($match['type'])) { if ($match['type'] == 'diff --git') { list($old, $new) = self::splitGitDiffPaths($match['oldnew']); $match['old'] = $old; $match['cur'] = $new; } } $change = $this->buildChange(idx($match, 'cur')); if (isset($match['old'])) { $change->setOldPath($match['old']); } if (isset($match['hash'])) { $change->setCommitHash($match['hash']); } if (isset($match['binary'])) { $change->setFileType(ArcanistDiffChangeType::FILE_BINARY); $line = $this->nextNonemptyLine(); continue; } $line = $this->nextLine(); switch ($match['type']) { case 'Index': case 'Modified': case 'Added': case 'Deleted': case 'Copied': $this->parseIndexHunk($change); break; case 'Property changes on': $this->parsePropertyHunk($change); break; case 'diff --git': $this->setIsGit(true); $this->parseIndexHunk($change); break; case 'commit': $this->setIsGit(true); $this->parseCommitMessage($change); break; case '---': $ok = preg_match( '@^(?:\+\+\+) (.*)\s+\d{4}-\d{2}-\d{2}.*$@', $line, $match); if (!$ok) { - $this->didFailParse("Expected '+++ filename' in unified diff."); + $this->didFailParse(pht( + "Expected '%s' in unified diff.", + '+++ filename')); } $change->setCurrentPath($match[1]); $line = $this->nextLine(); $this->parseChangeset($change); break; case 'diff -r': $this->setIsMercurial(true); $this->parseIndexHunk($change); break; case 'rcsdiff -u': $this->isRCS = true; $this->parseIndexHunk($change); break; default: - $this->didFailParse('Unknown diff type.'); + $this->didFailParse(pht('Unknown diff type.')); break; } } while ($this->getLine() !== null); $this->didFinishParse(); $this->loadSyntheticData(); return $this->changes; } protected function tryMatchHeader($patterns, $line, &$match) { foreach ($patterns as $pattern) { if (preg_match('@^'.$pattern.'$@', $line, $match)) { return true; } } return false; } protected function parseCommitMessage(ArcanistDiffChange $change) { $change->setType(ArcanistDiffChangeType::TYPE_MESSAGE); $message = array(); $line = $this->getLine(); if (preg_match('/^Merge: /', $line)) { $this->nextLine(); } $line = $this->getLine(); if (!preg_match('/^Author: /', $line)) { - $this->didFailParse("Expected 'Author:'."); + $this->didFailParse(pht("Expected 'Author:'.")); } $line = $this->nextLine(); if (!preg_match('/^Date: /', $line)) { - $this->didFailParse("Expected 'Date:'."); + $this->didFailParse(pht("Expected 'Date:'.")); } while (($line = $this->nextLineTrimmed()) !== null) { if (strlen($line) && $line[0] != ' ') { break; } // Strip leading spaces from Git commit messages. Note that empty lines // are represented as just "\n"; don't touch those. $message[] = preg_replace('/^ /', '', $this->getLine()); } $message = rtrim(implode('', $message), "\r\n"); $change->setMetadata('message', $message); } /** * Parse an SVN property change hunk. These hunks are ambiguous so just sort * of try to get it mostly right. It's entirely possible to foil this parser * (or any other parser) with a carefully constructed property change. */ protected function parsePropertyHunk(ArcanistDiffChange $change) { $line = $this->getLineTrimmed(); if (!preg_match('/^_+$/', $line)) { - $this->didFailParse("Expected '______________________'."); + $this->didFailParse(pht("Expected '%s'.", '______________________')); } $line = $this->nextLine(); while ($line !== null) { $done = preg_match('/^(Index|Property changes on):/', $line); if ($done) { break; } // NOTE: Before 1.5, SVN uses "Name". At 1.5 and later, SVN uses // "Modified", "Added" and "Deleted". $matches = null; $ok = preg_match( '/^(Name|Modified|Added|Deleted): (.*)$/', $line, $matches); if (!$ok) { $this->didFailParse( - "Expected 'Name', 'Added', 'Deleted', or 'Modified'."); + pht("Expected 'Name', 'Added', 'Deleted', or 'Modified'.")); } $op = $matches[1]; $prop = $matches[2]; list($old, $new) = $this->parseSVNPropertyChange($op, $prop); if ($old !== null) { $change->setOldProperty($prop, $old); } if ($new !== null) { $change->setNewProperty($prop, $new); } $line = $this->getLine(); } } private function parseSVNPropertyChange($op, $prop) { $old = array(); $new = array(); $target = null; $line = $this->nextLine(); $prop_index = 2; while ($line !== null) { $done = preg_match( '/^(Modified|Added|Deleted|Index|Property changes on):/', $line); if ($done) { break; } $trimline = ltrim($line); if ($trimline && $trimline[0] == '#') { // in svn1.7, a line like ## -0,0 +1 ## is put between the Added: line // and the line with the property change. If we have such a line, we'll // just ignore it (: $line = $this->nextLine(); $prop_index = 1; $trimline = ltrim($line); } if ($trimline && $trimline[0] == '+') { if ($op == 'Deleted') { - $this->didFailParse('Unexpected "+" section in property deletion.'); + $this->didFailParse(pht( + 'Unexpected "%s" section in property deletion.', + '+')); } $target = 'new'; $line = substr($trimline, $prop_index); } else if ($trimline && $trimline[0] == '-') { if ($op == 'Added') { - $this->didFailParse('Unexpected "-" section in property addition.'); + $this->didFailParse(pht( + 'Unexpected "%s" section in property addition.', + '-')); } $target = 'old'; $line = substr($trimline, $prop_index); } else if (!strncmp($trimline, 'Merged', 6)) { if ($op == 'Added') { $target = 'new'; } else { // These can appear on merges. No idea how to interpret this (unclear // what the old / new values are) and it's of dubious usefulness so // just throw it away until someone complains. $target = null; } $line = $trimline; } if ($target == 'new') { $new[] = $line; } else if ($target == 'old') { $old[] = $line; } $line = $this->nextLine(); } $old = rtrim(implode('', $old)); $new = rtrim(implode('', $new)); if (!strlen($old)) { $old = null; } if (!strlen($new)) { $new = null; } return array($old, $new); } protected function setIsGit($git) { if ($this->isGit !== null && $this->isGit != $git) { - throw new Exception('Git status has changed!'); + throw new Exception(pht('Git status has changed!')); } $this->isGit = $git; return $this; } protected function getIsGit() { return $this->isGit; } public function setIsMercurial($is_mercurial) { $this->isMercurial = $is_mercurial; return $this; } public function getIsMercurial() { return $this->isMercurial; } protected function parseIndexHunk(ArcanistDiffChange $change) { $is_git = $this->getIsGit(); $is_mercurial = $this->getIsMercurial(); $is_svn = (!$is_git && !$is_mercurial); $move_source = null; $line = $this->getLine(); if ($is_git) { do { $patterns = array( '(?Pnew) file mode (?P\d+)', '(?Pdeleted) file mode (?P\d+)', // These occur when someone uses `chmod` on a file. 'old mode (?P\d+)', 'new mode (?P\d+)', // These occur when you `mv` a file and git figures it out. 'similarity index ', 'rename from (?P.*)', '(?Prename) to (?P.*)', 'copy from (?P.*)', '(?Pcopy) to (?P.*)', ); $ok = false; $match = null; foreach ($patterns as $pattern) { $ok = preg_match('@^'.$pattern.'@', $line, $match); if ($ok) { break; } } if (!$ok) { if ($line === null || preg_match('/^(diff --git|commit) /', $line)) { // In this case, there are ONLY file mode changes, or this is a // pure move. If it's a move, flag these changesets so we can build // synthetic changes later, enabling us to show file contents in // Differential -- git only gives us a block like this: // // diff --git a/README b/READYOU // similarity index 100% // rename from README // rename to READYOU // // ...i.e., there is no associated diff. // This allows us to distinguish between property changes only // and actual moves. For property changes only, we can't currently // build a synthetic diff correctly, so just skip it. // TODO: Build synthetic diffs for property changes, too. if ($change->getType() != ArcanistDiffChangeType::TYPE_CHANGE) { $change->setNeedsSyntheticGitHunks(true); if ($move_source) { $move_source->setNeedsSyntheticGitHunks(true); } } return; } break; } if (!empty($match['oldmode'])) { $change->setOldProperty('unix:filemode', $match['oldmode']); } if (!empty($match['newmode'])) { $change->setNewProperty('unix:filemode', $match['newmode']); } if (!empty($match['deleted'])) { $change->setType(ArcanistDiffChangeType::TYPE_DELETE); } if (!empty($match['new'])) { // If you replace a symlink with a normal file, git renders the change // as a "delete" of the symlink plus an "add" of the new file. We // prefer to represent this as a change. if ($change->getType() == ArcanistDiffChangeType::TYPE_DELETE) { $change->setType(ArcanistDiffChangeType::TYPE_CHANGE); } else { $change->setType(ArcanistDiffChangeType::TYPE_ADD); } } if (!empty($match['old'])) { $match['old'] = self::unescapeFilename($match['old']); $change->setOldPath($match['old']); } if (!empty($match['cur'])) { $match['cur'] = self::unescapeFilename($match['cur']); $change->setCurrentPath($match['cur']); } if (!empty($match['copy'])) { $change->setType(ArcanistDiffChangeType::TYPE_COPY_HERE); $old = $this->buildChange($change->getOldPath()); $type = $old->getType(); if ($type == ArcanistDiffChangeType::TYPE_MOVE_AWAY) { $old->setType(ArcanistDiffChangeType::TYPE_MULTICOPY); } else { $old->setType(ArcanistDiffChangeType::TYPE_COPY_AWAY); } $old->addAwayPath($change->getCurrentPath()); } if (!empty($match['move'])) { $change->setType(ArcanistDiffChangeType::TYPE_MOVE_HERE); $old = $this->buildChange($change->getOldPath()); $type = $old->getType(); if ($type == ArcanistDiffChangeType::TYPE_MULTICOPY) { // Great, no change. } else if ($type == ArcanistDiffChangeType::TYPE_MOVE_AWAY) { $old->setType(ArcanistDiffChangeType::TYPE_MULTICOPY); } else if ($type == ArcanistDiffChangeType::TYPE_COPY_AWAY) { $old->setType(ArcanistDiffChangeType::TYPE_MULTICOPY); } else { $old->setType(ArcanistDiffChangeType::TYPE_MOVE_AWAY); } // We'll reference this above. $move_source = $old; $old->addAwayPath($change->getCurrentPath()); } $line = $this->nextNonemptyLine(); } while (true); } $line = $this->getLine(); if ($is_svn) { $ok = preg_match('/^=+\s*$/', $line); if (!$ok) { - $this->didFailParse("Expected '=======================' divider line."); + $this->didFailParse(pht( + "Expected '%s' divider line.", + '=======================')); } else { // Adding an empty file in SVN can produce an empty line here. $line = $this->nextNonemptyLine(); } } else if ($is_git) { $ok = preg_match('/^index .*$/', $line); if (!$ok) { // TODO: "hg diff -g" diffs ("mercurial git-style diffs") do not include // this line, so we can't parse them if we fail on it. Maybe introduce // a flag saying "parse this diff using relaxed git-style diff rules"? // $this->didFailParse("Expected 'index af23f...a98bc' header line."); } else { // NOTE: In the git case, where this patch is the last change in the // file, we may have a final terminal newline. Skip over it so that // we'll hit the '$line === null' block below. This is covered by the // 'git-empty-file.gitdiff' test case. $line = $this->nextNonemptyLine(); } } // If there are files with only whitespace changes and -b or -w are // supplied as command-line flags to `diff', svn and git both produce // changes without any body. if ($line === null || preg_match( '/^(Index:|Property changes on:|diff --git|commit) /', $line)) { return; } $is_binary_add = preg_match( '/^Cannot display: file marked as a binary type\.$/', rtrim($line)); if ($is_binary_add) { $this->nextLine(); // Cannot display: file marked as a binary type. $this->nextNonemptyLine(); // svn:mime-type = application/octet-stream $this->markBinary($change); return; } // We can get this in git, or in SVN when a file exists in the repository // WITHOUT a binary mime-type and is changed and given a binary mime-type. $is_binary_diff = preg_match( '/^(Binary files|Files) .* and .* differ$/', rtrim($line)); if ($is_binary_diff) { $this->nextNonemptyLine(); // Binary files x and y differ $this->markBinary($change); return; } // This occurs under "hg diff --git" when a binary file is removed. See // test case "hg-binary-delete.hgdiff". (I believe it never occurs under // git, which reports the "files X and /dev/null differ" string above. Git // can not apply these patches.) $is_hg_binary_delete = preg_match( '/^Binary file .* has changed$/', rtrim($line)); if ($is_hg_binary_delete) { $this->nextNonemptyLine(); $this->markBinary($change); return; } // With "git diff --binary" (not a normal mode, but one users may explicitly // invoke and then, e.g., copy-paste into the web console) or "hg diff // --git" (normal under hg workflows), we may encounter a literal binary // patch. $is_git_binary_patch = preg_match( '/^GIT binary patch$/', rtrim($line)); if ($is_git_binary_patch) { $this->nextLine(); $this->parseGitBinaryPatch(); $line = $this->getLine(); if (preg_match('/^literal/', $line)) { // We may have old/new binaries (change) or just a new binary (hg add). // If there are two blocks, parse both. $this->parseGitBinaryPatch(); } $this->markBinary($change); return; } if ($is_git) { // "git diff -b" ignores whitespace, but has an empty hunk target if (preg_match('@^diff --git .*$@', $line)) { $this->nextLine(); return null; } } if ($this->isRCS) { // Skip the RCS headers. $this->nextLine(); $this->nextLine(); $this->nextLine(); } $old_file = $this->parseHunkTarget(); $new_file = $this->parseHunkTarget(); if ($this->isRCS) { $change->setCurrentPath($new_file); } $change->setOldPath($old_file); $this->parseChangeset($change); } private function parseGitBinaryPatch() { // TODO: We could decode the patches, but it's a giant mess so don't bother // for now. We'll pick up the data from the working copy in the common // case ("arc diff"). $line = $this->getLine(); if (!preg_match('/^literal /', $line)) { - $this->didFailParse("Expected 'literal NNNN' to start git binary patch."); + $this->didFailParse( + pht("Expected '%s' to start git binary patch.", 'literal NNNN')); } do { $line = $this->nextLineTrimmed(); if ($line === '' || $line === null) { // Some versions of Mercurial apparently omit the terminal newline, // although it's unclear if Git will ever do this. In either case, // rely on the base85 check for sanity. $this->nextNonemptyLine(); return; } else if (!preg_match('/^[a-zA-Z]/', $line)) { - $this->didFailParse('Expected base85 line length character (a-zA-Z).'); + $this->didFailParse( + pht('Expected base85 line length character (a-zA-Z).')); } } while (true); } protected function parseHunkTarget() { $line = $this->getLine(); $matches = null; $remainder = '(?:\s*\(.*\))?'; if ($this->getIsMercurial()) { // Something like "Fri Aug 26 01:20:50 2005 -0700", don't bother trying // to parse it. $remainder = '\t.*'; } else if ($this->isRCS) { $remainder = '\s.*'; } else if ($this->getIsGit()) { // When filenames contain spaces, Git terminates this line with a tab. // Normally, the tab is not present. If there's a tab, ignore it. $remainder = '(?:\t.*)?'; } $ok = preg_match( '@^[-+]{3} (?:[ab]/)?(?P.*?)'.$remainder.'$@', $line, $matches); if (!$ok) { $this->didFailParse( - "Expected hunk target '+++ path/to/file.ext (revision N)'."); + pht( + "Expected hunk target '%s'.", + '+++ path/to/file.ext (revision N)')); } $this->nextLine(); return $matches['path']; } protected function markBinary(ArcanistDiffChange $change) { $change->setFileType(ArcanistDiffChangeType::FILE_BINARY); return $this; } protected function parseChangeset(ArcanistDiffChange $change) { // If a diff includes two sets of changes to the same file, let the // second one win. In particular, this occurs when adding subdirectories // in Subversion that contain files: the file text will be present in // both the directory diff and the file diff. See T5555. Dropping the // hunks lets whichever one shows up later win instead of showing changes // twice. $change->dropHunks(); $all_changes = array(); do { $hunk = new ArcanistDiffHunk(); $line = $this->getLineTrimmed(); $real = array(); // In the case where only one line is changed, the length is omitted. // The final group is for git, which appends a guess at the function // context to the diff. $matches = null; $ok = preg_match( '/^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@(?: .*?)?$/U', $line, $matches); if (!$ok) { // It's possible we hit the style of an svn1.7 property change. // This is a 4-line Index block, followed by an empty line, followed // by a "Property changes on:" section similar to svn1.6. if ($line == '') { $line = $this->nextNonemptyLine(); $ok = preg_match('/^Property changes on:/', $line); if (!$ok) { - $this->didFailParse('Confused by empty line'); + $this->didFailParse(pht('Confused by empty line')); } $line = $this->nextLine(); return $this->parsePropertyHunk($change); } - $this->didFailParse("Expected hunk header '@@ -NN,NN +NN,NN @@'."); + $this->didFailParse(pht( + "Expected hunk header '%s'.", + '@@ -NN,NN +NN,NN @@')); } $hunk->setOldOffset($matches[1]); $hunk->setNewOffset($matches[3]); // Cover for the cases where length wasn't present (implying one line). $old_len = idx($matches, 2); if (!strlen($old_len)) { $old_len = 1; } $new_len = idx($matches, 4); if (!strlen($new_len)) { $new_len = 1; } $hunk->setOldLength($old_len); $hunk->setNewLength($new_len); $add = 0; $del = 0; $hit_next_hunk = false; while ((($line = $this->nextLine()) !== null)) { if (strlen(rtrim($line, "\r\n"))) { $char = $line[0]; } else { // Normally, we do not encouter empty lines in diffs, because // unchanged lines have an initial space. However, in Git, with // the option `diff.suppress-blank-empty` set, unchanged blank lines // emit as completely empty. If we encounter a completely empty line, // treat it as a ' ' (i.e., unchanged empty line) line. $char = ' '; } switch ($char) { case '\\': if (!preg_match('@\\ No newline at end of file@', $line)) { $this->didFailParse( - "Expected '\ No newline at end of file'."); + pht("Expected '\ No newline at end of file'.")); } if ($new_len) { $real[] = $line; $hunk->setIsMissingOldNewline(true); } else { $real[] = $line; $hunk->setIsMissingNewNewline(true); } if (!$new_len) { break 2; } break; case '+': ++$add; --$new_len; $real[] = $line; break; case '-': if (!$old_len) { // In this case, we've hit "---" from a new file. So don't // advance the line cursor. $hit_next_hunk = true; break 2; } ++$del; --$old_len; $real[] = $line; break; case ' ': if (!$old_len && !$new_len) { break 2; } --$old_len; --$new_len; $real[] = $line; break; default: // We hit something, likely another hunk. $hit_next_hunk = true; break 2; } } if ($old_len || $new_len) { - $this->didFailParse('Found the wrong number of hunk lines.'); + $this->didFailParse(pht('Found the wrong number of hunk lines.')); } $corpus = implode('', $real); $is_binary = false; if ($this->detectBinaryFiles) { $is_binary = !phutil_is_utf8($corpus); $try_encoding = $this->tryEncoding; if ($is_binary && $try_encoding) { $is_binary = ArcanistDiffUtils::isHeuristicBinaryFile($corpus); if (!$is_binary) { $corpus = phutil_utf8_convert($corpus, 'UTF-8', $try_encoding); if (!phutil_is_utf8($corpus)) { throw new Exception( - "Failed to convert a hunk from '{$try_encoding}' to UTF-8. ". - "Check that the specified encoding is correct."); + pht( + "Failed to convert a hunk from '%s' to UTF-8. ". + "Check that the specified encoding is correct.", + $try_encoding)); } } } } if ($is_binary) { // SVN happily treats binary files which aren't marked with the right // mime type as text files. Detect that junk here and mark the file // binary. We'll catch stuff with unicode too, but that's verboten // anyway. If there are too many false positives with this we might // need to make it threshold-triggered instead of triggering on any // unprintable byte. $change->setFileType(ArcanistDiffChangeType::FILE_BINARY); } else { $hunk->setCorpus($corpus); $hunk->setAddLines($add); $hunk->setDelLines($del); $change->addHunk($hunk); } if (!$hit_next_hunk) { $line = $this->nextNonemptyLine(); } } while (preg_match('/^@@ /', $line)); } protected function buildChange($path = null) { $change = null; if ($path !== null) { if (!empty($this->changes[$path])) { return $this->changes[$path]; } } if ($this->forcePath) { return $this->changes[$this->forcePath]; } $change = new ArcanistDiffChange(); if ($path !== null) { $change->setCurrentPath($path); $this->changes[$path] = $change; } else { $this->changes[] = $change; } return $change; } protected function didStartParse($text) { $this->rawDiff = $text; // Eat leading whitespace. This may happen if the first change in the diff // is an SVN property change. $text = ltrim($text); // Try to strip ANSI color codes from colorized diffs. ANSI color codes // might be present in two cases: // // - You piped a colorized diff into 'arc --raw' or similar (normally // we're able to disable colorization on diffs we control the generation // of). // - You're diffing a file which actually contains ANSI color codes. // // The former is vastly more likely, but we try to distinguish between the // two cases by testing for a color code at the beginning of a line. If // we find one, we know it's a colorized diff (since the beginning of the // line should be "+", "-" or " " if the code is in the diff text). // // While it's possible a diff might be colorized and fail this test, it's // unlikely, and it covers hg's color extension which seems to be the most // stubborn about colorizing text despite stdout not being a TTY. // // We might incorrectly strip color codes from a colorized diff of a text // file with color codes inside it, but this case is stupid and pathological // and you've dug your own grave. $ansi_color_pattern = '\x1B\[[\d;]*m'; if (preg_match('/^'.$ansi_color_pattern.'/m', $text)) { $text = preg_replace('/'.$ansi_color_pattern.'/', '', $text); } $this->text = phutil_split_lines($text); $this->line = 0; } protected function getLine() { if ($this->text === null) { throw new Exception('Not parsing!'); } if (isset($this->text[$this->line])) { return $this->text[$this->line]; } return null; } protected function getLineTrimmed() { $line = $this->getLine(); if ($line !== null) { $line = trim($line, "\r\n"); } return $line; } protected function nextLine() { $this->line++; return $this->getLine(); } protected function nextLineTrimmed() { $line = $this->nextLine(); if ($line !== null) { $line = trim($line, "\r\n"); } return $line; } protected function nextNonemptyLine() { while (($line = $this->nextLine()) !== null) { if (strlen(trim($line)) !== 0) { break; } } return $this->getLine(); } protected function nextLineThatLooksLikeDiffStart() { while (($line = $this->nextLine()) !== null) { if (preg_match('/^\s*diff\s+-(?:r|-git)/', $line)) { break; } } return $this->getLine(); } protected function saveLine() { $this->lineSaved = $this->line; } protected function restoreLine() { $this->line = $this->lineSaved; } protected function isFirstNonEmptyLine() { $len = count($this->text); for ($ii = 0; $ii < $len; $ii++) { $line = $this->text[$ii]; if (!strlen(trim($line))) { // This line is empty, skip it. continue; } if (preg_match('/^#/', $line)) { // This line is a comment, skip it. continue; } return ($ii == $this->line); } // Entire file is empty. return false; } protected function didFinishParse() { $this->text = null; } public function setWriteDiffOnFailure($write) { $this->writeDiffOnFailure = $write; return $this; } protected function didFailParse($message) { $context = 5; $min = max(0, $this->line - $context); $max = min($this->line + $context, count($this->text) - 1); $context = ''; for ($ii = $min; $ii <= $max; $ii++) { $context .= sprintf( '%8.8s %6.6s %s', ($ii == $this->line) ? '>>> ' : '', $ii + 1, $this->text[$ii]); } $out = array(); $out[] = "Diff Parse Exception: {$message}"; if ($this->writeDiffOnFailure) { $temp = new TempFile(); $temp->setPreserveFile(true); Filesystem::writeFile($temp, $this->rawDiff); $out[] = 'Raw input file was written to: '.(string)$temp; } $out[] = $context; $out = implode("\n\n", $out); throw new Exception($out); } /** * Unescape escaped filenames, e.g. from "git diff". */ private static function unescapeFilename($name) { if (preg_match('/^".+"$/', $name)) { return stripcslashes(substr($name, 1, -1)); } else { return $name; } } private function loadSyntheticData() { if (!$this->changes) { return; } $repository_api = $this->repositoryAPI; if (!$repository_api) { return; } $imagechanges = array(); $changes = $this->changes; foreach ($changes as $change) { $path = $change->getCurrentPath(); // Certain types of changes (moves and copies) don't contain change data // when expressed in raw "git diff" form. Augment any such diffs with // textual data. if ($change->getNeedsSyntheticGitHunks() && ($repository_api instanceof ArcanistGitAPI)) { $diff = $repository_api->getRawDiffText($path, $moves = false); // NOTE: We're reusing the parser and it doesn't reset change state // between parses because there's an oddball SVN workflow in Phabricator // which relies on being able to inject changes. // TODO: Fix this. $parser = clone $this; $parser->setChanges(array()); $raw_changes = $parser->parseDiff($diff); foreach ($raw_changes as $raw_change) { if ($raw_change->getCurrentPath() == $path) { $change->setFileType($raw_change->getFileType()); foreach ($raw_change->getHunks() as $hunk) { // Git thinks that this file has been added. But we know that it // has been moved or copied without a change. $hunk->setCorpus( preg_replace('/^\+/m', ' ', $hunk->getCorpus())); $change->addHunk($hunk); } break; } } $change->setNeedsSyntheticGitHunks(false); } if ($change->getFileType() != ArcanistDiffChangeType::FILE_BINARY && $change->getFileType() != ArcanistDiffChangeType::FILE_IMAGE) { continue; } $imagechanges[$path] = $change; } // Fetch the actual file contents in batches so repositories // that have slow random file accesses (i.e. mercurial) can // optimize the retrieval. $paths = array_keys($imagechanges); $filedata = $repository_api->getBulkOriginalFileData($paths); foreach ($filedata as $path => $data) { $imagechanges[$path]->setOriginalFileData($data); } $filedata = $repository_api->getBulkCurrentFileData($paths); foreach ($filedata as $path => $data) { $imagechanges[$path]->setCurrentFileData($data); } $this->changes = $changes; } /** * Strip prefixes off paths from `git diff`. By default git uses a/ and b/, * but you can set `diff.mnemonicprefix` to get a different set of prefixes, * or use `--no-prefix`, `--src-prefix` or `--dst-prefix` to set these to * other arbitrary values. * * We strip the default and mnemonic prefixes, and trust the user knows what * they're doing in the other cases. * * @param string Path to strip. * @return string Stripped path. */ public static function stripGitPathPrefix($path) { static $regex; if ($regex === null) { $prefixes = array( // These are the defaults. 'a/', 'b/', // These show up when you set "diff.mnemonicprefix". 'i/', 'c/', 'w/', 'o/', '1/', '2/', ); foreach ($prefixes as $key => $prefix) { $prefixes[$key] = preg_quote($prefix, '@'); } $regex = '@^('.implode('|', $prefixes).')@S'; } return preg_replace($regex, '', $path); } /** * Split the paths on a "diff --git" line into old and new paths. This * is difficult because they may be ambiguous if the files contain spaces. * * @param string Text from a diff line after "diff --git ". * @return pair Old and new paths. */ public static function splitGitDiffPaths($paths) { $matches = null; $paths = rtrim($paths, "\r\n"); $patterns = array( // Try quoted paths, used for unicode filenames or filenames with quotes. '@^(?P"(?:\\\\.|[^"\\\\]+)+") (?P"(?:\\\\.|[^"\\\\]+)+")$@', // Try paths without spaces. '@^(?P[^ ]+) (?P[^ ]+)$@', // Try paths with well-known prefixes. '@^(?P[abicwo12]/.*) (?P[abicwo12]/.*)$@', // Try the exact same string twice in a row separated by a space. // This can hit a false positive for moves from files like "old file old" // to "file", but such a case combined with custom diff prefixes is // incredibly obscure. '@^(?P.*) (?P\\1)$@', ); foreach ($patterns as $pattern) { if (preg_match($pattern, $paths, $matches)) { break; } } if (!$matches) { throw new Exception( - "Input diff contains ambiguous line 'diff --git {$paths}'. This line ". - "is ambiguous because there are spaces in the file names, so the ". - "parser can not determine where the file names begin and end. To ". - "resolve this ambiguity, use standard prefixes ('a/' and 'b/') when ". - "generating diffs."); + pht( + "Input diff contains ambiguous line '%s'. This line is ambiguous ". + "because there are spaces in the file names, so the parser can not ". + "determine where the file names begin and end. To resolve this ". + "ambiguity, use standard prefixes ('a/' and 'b/') when ". + "generating diffs.", + "diff --git {$paths}")); } $old = $matches['old']; $old = self::unescapeFilename($old); $old = self::stripGitPathPrefix($old); $new = $matches['new']; $new = self::unescapeFilename($new); $new = self::stripGitPathPrefix($new); return array($old, $new); } /** * Strip the header and footer off a `git-format-patch` diff. * * Returns a parseable normal diff and a textual commit message. */ private function stripGitFormatPatch($diff) { // We can parse this by splitting it into two pieces over and over again // along different section dividers: // // 1. Mail headers. // 2. ("\n\n") // 3. Mail body. // 4. ("---") // 5. Diff stat section. // 6. ("\n\n") // 7. Actual diff body. // 8. ("--") // 9. Patch footer. list($head, $tail) = preg_split('/^---$/m', $diff, 2); list($mail_headers, $mail_body) = explode("\n\n", $head, 2); list($body, $foot) = preg_split('/^-- ?$/m', $tail, 2); list($stat, $diff) = explode("\n\n", $body, 2); // Rebuild the commit message by putting the subject line back on top of it, // if we can find one. $matches = null; $pattern = '/^Subject: (?:\[PATCH\] )?(.*)$/mi'; if (preg_match($pattern, $mail_headers, $matches)) { $mail_body = $matches[1]."\n\n".$mail_body; $mail_body = rtrim($mail_body); } return array($mail_body, $diff); } } diff --git a/src/parser/__tests__/ArcanistBundleTestCase.php b/src/parser/__tests__/ArcanistBundleTestCase.php index 19f251d9..cbd6b41e 100644 --- a/src/parser/__tests__/ArcanistBundleTestCase.php +++ b/src/parser/__tests__/ArcanistBundleTestCase.php @@ -1,912 +1,919 @@ getResourcePath($name)); } private function getResourcePath($name) { return dirname(__FILE__).'/bundle/'.$name; } private function loadDiff($old, $new) { list($err, $stdout) = exec_manual( 'diff --unified=65535 --label %s --label %s -- %s %s', 'file 9999-99-99', 'file 9999-99-99', $this->getResourcePath($old), $this->getResourcePath($new)); $this->assertEqual( 1, $err, - "Expect `diff` to find changes between '{$old}' and '{$new}'."); + pht( + "Expect `%s` to find changes between '%s' and '%s'.", + 'diff', + $old, + $new)); return $stdout; } private function loadOneChangeBundle($old, $new) { $diff = $this->loadDiff($old, $new); return ArcanistBundle::newFromDiff($diff); } /** * Unarchive a saved git repository and apply each commit as though via * "arc patch", verifying that the resulting tree hash is identical to the * tree hash produced by the real commit. */ public function testGitRepository() { if (phutil_is_windows()) { - $this->assertSkipped('This test is not supported under Windows.'); + $this->assertSkipped(pht('This test is not supported under Windows.')); } $archive = dirname(__FILE__).'/bundle.git.tgz'; $fixture = PhutilDirectoryFixture::newFromArchive($archive); $old_dir = getcwd(); chdir($fixture->getPath()); $caught = null; try { $this->runGitRepositoryTests($fixture); } catch (Exception $ex) { $caught = $ex; } chdir($old_dir); if ($caught) { throw $ex; } } private function runGitRepositoryTests(PhutilDirectoryFixture $fixture) { $patches = dirname(__FILE__).'/patches/'; list($commits) = execx( 'git log --format=%s', '%H %T %s'); $commits = explode("\n", trim($commits)); // The very first commit doesn't have a meaningful parent, so don't examine // it. array_pop($commits); foreach ($commits as $commit) { list($commit_hash, $tree_hash, $subject) = explode(' ', $commit, 3); execx('git reset --hard %s --', $commit_hash); $fixture_path = $fixture->getPath(); $working_copy = ArcanistWorkingCopyIdentity::newFromPath($fixture_path); $configuration_manager = new ArcanistConfigurationManager(); $configuration_manager->setWorkingCopyIdentity($working_copy); $repository_api = ArcanistRepositoryAPI::newAPIFromConfigurationManager( $configuration_manager); $repository_api->setBaseCommitArgumentRules('arc:this'); $diff = $repository_api->getFullGitDiff( $repository_api->getBaseCommit(), $repository_api->getHeadCommit()); $parser = new ArcanistDiffParser(); $parser->setRepositoryAPI($repository_api); $changes = $parser->parseDiff($diff); $this->makeChangeAssertions($commit_hash, $changes); $bundle = ArcanistBundle::newFromChanges($changes); execx('git reset --hard %s^ --', $commit_hash); $patch = $bundle->toGitPatch(); $expect_path = $patches.'/'.$commit_hash.'.gitpatch'; $expect = null; if (Filesystem::pathExists($expect_path)) { $expect = Filesystem::readFile($expect_path); } if ($patch === $expect) { $this->assertEqual($expect, $patch); } else { Filesystem::writeFile($expect_path.'.real', $patch); throw new Exception( - "Expected patch and actual patch for {$commit_hash} differ. ". - "Wrote actual patch to '{$expect_path}.real'."); + pht( + "Expected patch and actual patch for %s differ. ". + "Wrote actual patch to '%s.real'.", + $commit_hash, + $expect_path)); } try { id(new ExecFuture('git apply --index --reject')) ->write($patch) ->resolvex(); } catch (CommandException $ex) { $temp = new TempFile(substr($commit_hash, 0, 8).'.patch'); $temp->setPreserveFile(true); Filesystem::writeFile($temp, $patch); PhutilConsole::getConsole()->writeErr( - "Wrote failing patch to '%s'.\n", - $temp); + "%s\n", + pht("Wrote failing patch to '%s'.", $temp)); throw $ex; } execx('git commit -m %s', $subject); list($result_hash) = execx('git log -n1 --format=%s', '%T'); $result_hash = trim($result_hash); $this->assertEqual( $tree_hash, $result_hash, - "Commit {$commit_hash}: {$subject}"); + pht('Commit %s: %s', $commit_hash, $subject)); } } private function makeChangeAssertions($commit, array $raw_changes) { $changes = array(); // Verify that there are no duplicate changes, and rekey the changes on // affected path because we don't care about the order in which the // changes appear. foreach ($raw_changes as $change) { $this->assertTrue( empty($changes[$change->getCurrentPath()]), 'Unique Path: '.$change->getCurrentPath()); $changes[$change->getCurrentPath()] = $change; } switch ($commit) { case '1830a13adf764b55743f7edc6066451898d8ffa4': // "Mark koan2 as +x and edit it." $this->assertEqual(1, count($changes)); $c = $changes['koan2']; $this->assertEqual( ArcanistDiffChangeType::TYPE_CHANGE, $c->getType()); $this->assertEqual( '100644', idx($c->getOldProperties(), 'unix:filemode')); $this->assertEqual( '100755', idx($c->getNewProperties(), 'unix:filemode')); break; case '8ecc728bcc9b482a9a91527ea471b04fc1a025cf': // "Move 'text' to 'executable' and mark it +x." $this->assertEqual(2, count($changes)); $c = $changes['executable']; $this->assertEqual( ArcanistDiffChangeType::TYPE_MOVE_HERE, $c->getType()); $this->assertEqual( '100644', idx($c->getOldProperties(), 'unix:filemode')); $this->assertEqual( '100755', idx($c->getNewProperties(), 'unix:filemode')); break; case '39c8e7dd3914edff087a6214f0cd996ad08e5b3d': // "Mark koan as +x." // Primarily a test against a recusive synthetic hunk construction bug. $this->assertEqual(1, count($changes)); $c = $changes['koan']; $this->assertEqual( ArcanistDiffChangeType::TYPE_CHANGE, $c->getType()); $this->assertEqual( '100644', idx($c->getOldProperties(), 'unix:filemode')); $this->assertEqual( '100755', idx($c->getNewProperties(), 'unix:filemode')); break; case 'c573c25d1a767d270fed504cd993e78aba936338': // "Copy a koan over text, editing the original koan." // Git doesn't really do anything meaningful with this. $this->assertEqual(2, count($changes)); $c = $changes['koan']; $this->assertEqual( ArcanistDiffChangeType::TYPE_CHANGE, $c->getType()); $c = $changes['text']; $this->assertEqual( ArcanistDiffChangeType::TYPE_CHANGE, $c->getType()); break; case 'd26628e588cf7d16368845b121c6ac6c781e81d0': // "Copy a koan, modifying both the source and destination." $this->assertEqual(2, count($changes)); $c = $changes['koan']; $this->assertEqual( ArcanistDiffChangeType::TYPE_COPY_AWAY, $c->getType()); $c = $changes['koan2']; $this->assertEqual( ArcanistDiffChangeType::TYPE_COPY_HERE, $c->getType()); break; case 'b0c9663ecda5f666f62dad245a3a7549aac5e636': // "Remove a koan copy." $this->assertEqual(1, count($changes)); $c = $changes['koan2']; $this->assertEqual( ArcanistDiffChangeType::TYPE_DELETE, $c->getType()); break; case 'b6ecdb3b4801f3028d88ba49940a558360847dbf': // "Copy a koan and edit the destination." // Git does not detect this as a copy without --find-copies-harder. $this->assertEqual(1, count($changes)); $c = $changes['koan2']; $this->assertEqual( ArcanistDiffChangeType::TYPE_ADD, $c->getType()); break; case '30d23787e1ecd254c884afbe37afa612f61e3904': // "Move and edit a koan." $this->assertEqual(2, count($changes)); $c = $changes['koan2']; $this->assertEqual( ArcanistDiffChangeType::TYPE_MOVE_AWAY, $c->getType()); $c = $changes['koan']; $this->assertEqual( ArcanistDiffChangeType::TYPE_MOVE_HERE, $c->getType()); break; case 'c0ba9bfe3695f95c3f558bc5797eeba421d32483': // "Remove two koans." $this->assertEqual(2, count($changes)); $c = $changes['koan3']; $this->assertEqual( ArcanistDiffChangeType::TYPE_DELETE, $c->getType()); $c = $changes['koan4']; $this->assertEqual( ArcanistDiffChangeType::TYPE_DELETE, $c->getType()); break; case '2658fd01d5355abe5d4c7ead3a0e7b4b3449fe77': // "Multicopy a koan." $this->assertEqual(3, count($changes)); $c = $changes['koan']; $this->assertEqual( ArcanistDiffChangeType::TYPE_MULTICOPY, $c->getType()); $c = $changes['koan3']; $this->assertEqual( ArcanistDiffChangeType::TYPE_COPY_HERE, $c->getType()); $c = $changes['koan4']; $this->assertEqual( ArcanistDiffChangeType::TYPE_MOVE_HERE, $c->getType()); break; case '1c5fe4e2243bb19d6b3bf15896177b13768e6eb6': // "Copy a koan." // Git does not detect this as a copy without --find-copies-harder. $this->assertEqual(1, count($changes)); $c = $changes['koan']; $this->assertEqual( ArcanistDiffChangeType::TYPE_ADD, $c->getType()); break; case '6d9eb65a2c2b56dee64d72f59554c1cca748dd34': // "Move a koan." $this->assertEqual(2, count($changes)); $c = $changes['koan']; $this->assertEqual( ArcanistDiffChangeType::TYPE_MOVE_AWAY, $c->getType()); $c = $changes['koan2']; $this->assertEqual( ArcanistDiffChangeType::TYPE_MOVE_HERE, $c->getType()); break; case '141452e2a775ee86409e8779dd2eda767b4fe8ab': // "Add a koan." $this->assertEqual(1, count($changes)); $c = $changes['koan']; $this->assertEqual( ArcanistDiffChangeType::TYPE_ADD, $c->getType()); break; case '5dec8bf28557f078d1987c4e8cfb53d08310f522': // "Copy an image, and replace the original." // `image_2.png` is copied to `image.png` and then replaced. $this->assertEqual(2, count($changes)); $c = $changes['image.png']; $this->assertEqual( ArcanistDiffChangeType::TYPE_COPY_HERE, $c->getType()); $this->assertEqual( ArcanistDiffChangeType::FILE_BINARY, $c->getFileType()); $this->assertEqual( null, $c->getOriginalFileData()); $this->assertEqual( '8645053452b2cc2f955ef3944ac0831a', md5($c->getCurrentFileData())); $c = $changes['image_2.png']; $this->assertEqual( ArcanistDiffChangeType::TYPE_COPY_AWAY, $c->getType()); $this->assertEqual( ArcanistDiffChangeType::FILE_BINARY, $c->getFileType()); $this->assertEqual( '8645053452b2cc2f955ef3944ac0831a', md5($c->getOriginalFileData())); $this->assertEqual( 'c9ec1b952480da09b393ba672d9b13da', md5($c->getCurrentFileData())); break; case 'fb28468d25a5fdd063aca4ca559454c998a0af51': // "Multicopy image." // `image.png` is copied to `image_2.png` and `image_3.png` and then // deleted. Git detects this as a move and an add. $this->assertEqual(3, count($changes)); $c = $changes['image.png']; $this->assertEqual( ArcanistDiffChangeType::TYPE_MULTICOPY, $c->getType()); $this->assertEqual( ArcanistDiffChangeType::FILE_BINARY, $c->getFileType()); $this->assertEqual( '8645053452b2cc2f955ef3944ac0831a', md5($c->getOriginalFileData())); $this->assertEqual( null, $c->getCurrentFileData()); $c = $changes['image_2.png']; $this->assertEqual( ArcanistDiffChangeType::TYPE_COPY_HERE, $c->getType()); $this->assertEqual( ArcanistDiffChangeType::FILE_BINARY, $c->getFileType()); $this->assertEqual( null, $c->getOriginalFileData()); $this->assertEqual( '8645053452b2cc2f955ef3944ac0831a', md5($c->getCurrentFileData())); $c = $changes['image_3.png']; $this->assertEqual( ArcanistDiffChangeType::TYPE_MOVE_HERE, $c->getType()); $this->assertEqual( ArcanistDiffChangeType::FILE_BINARY, $c->getFileType()); $this->assertEqual( null, $c->getOriginalFileData()); $this->assertEqual( '8645053452b2cc2f955ef3944ac0831a', md5($c->getCurrentFileData())); break; case 'df340e88d8aba12e8f2b8827f01f0cd9f35eb758': // "Remove binary image." // `image_2.png` is deleted. $this->assertEqual(1, count($changes)); $c = $changes['image_2.png']; $this->assertEqual( ArcanistDiffChangeType::TYPE_DELETE, $c->getType()); $this->assertEqual( ArcanistDiffChangeType::FILE_BINARY, $c->getFileType()); $this->assertEqual( '8645053452b2cc2f955ef3944ac0831a', md5($c->getOriginalFileData())); $this->assertEqual( null, $c->getCurrentFileData()); break; case '3f5c6d735e64c25a04f83be48ef184b25b5282f0': // "Copy binary image." // `image_2.png` is copied to `image.png`. Git does not detect this as // a copy without --find-copies-harder. $this->assertEqual(1, count($changes)); $c = $changes['image.png']; $this->assertEqual( ArcanistDiffChangeType::TYPE_ADD, $c->getType()); $this->assertEqual( ArcanistDiffChangeType::FILE_BINARY, $c->getFileType()); $this->assertEqual( null, $c->getOriginalFileData()); $this->assertEqual( '8645053452b2cc2f955ef3944ac0831a', md5($c->getCurrentFileData())); break; case 'b454edb3bb29890ee5b3af5ef66ce6a24d15d882': // "Move binary image." // `image.png` is moved to `image_2.png`. $this->assertEqual(2, count($changes)); $c = $changes['image.png']; $this->assertEqual( ArcanistDiffChangeType::TYPE_MOVE_AWAY, $c->getType()); $this->assertEqual( ArcanistDiffChangeType::FILE_BINARY, $c->getFileType()); $this->assertEqual( '8645053452b2cc2f955ef3944ac0831a', md5($c->getOriginalFileData())); $this->assertEqual( null, $c->getCurrentFileData()); $c = $changes['image_2.png']; $this->assertEqual( ArcanistDiffChangeType::TYPE_MOVE_HERE, $c->getType()); $this->assertEqual( ArcanistDiffChangeType::FILE_BINARY, $c->getFileType()); $this->assertEqual( null, $c->getOriginalFileData()); $this->assertEqual( '8645053452b2cc2f955ef3944ac0831a', md5($c->getCurrentFileData())); break; case '5de5f3dfda1b7db2eb054e57699f05aaf1f4483e': // "Add a binary image." // `image.png` is added. $c = $changes['image.png']; $this->assertEqual( ArcanistDiffChangeType::TYPE_ADD, $c->getType()); $this->assertEqual( ArcanistDiffChangeType::FILE_BINARY, $c->getFileType()); $this->assertEqual( null, $c->getOriginalFileData()); $this->assertEqual( '8645053452b2cc2f955ef3944ac0831a', md5($c->getCurrentFileData())); break; case '176a4c2c3fd88b2d598ce41a55d9c3958be9fd2d': // "Convert \r\n newlines to \n newlines." case 'a73b28e139296d23ade768f2346038318b331f94': // "Add text with \r\n newlines." case '337ccec314075a2bdb4a912ef467d35d04a713e4': // "Convert \n newlines to \r\n newlines."; case '6d5e64a4a7a6a036c53b1d087184cb2c70099f2c': // "Remove tabs." case '49395994a1a8a06287e40a3b318be4349e8e0288': // "Add tabs." case 'a5a53c424f3c2a7e85f6aee35e834c8ec5b3dbe3': // "Add trailing newline." case 'd53dc614090c6c7d6d023e170877d7f611f18f5a': // "Remove trailing newline." case 'f19fb9fa1385c01b53bdb6d8842dd154e47151ec': // "Edit a text file." $this->assertEqual(1, count($changes)); $c = $changes['text']; $this->assertEqual( ArcanistDiffChangeType::TYPE_CHANGE, $c->getType()); $this->assertEqual( ArcanistDiffChangeType::FILE_TEXT, $c->getFileType()); break; case '228d7be4840313ed805c25c15bba0f7b188af3e6': // "Add a text file." // This commit is never reached because we skip the 0th commit junk. - $this->assertTrue(true, 'This is never reached.'); + $this->assertTrue(true, pht('This is never reached.')); break; default: throw new Exception( - "Commit {$commit} has no change assertions!"); + pht('Commit %s has no change assertions!', $commit)); } } public function testTrailingContext() { // Diffs need to generate without extra trailing context, or 'patch' will // choke on them. $this->assertEqual( $this->loadResource('trailing-context.diff'), $this->loadOneChangeBundle( 'trailing-context.old', 'trailing-context.new')->toUnifiedDiff()); } public function testDisjointHunks() { // Diffs need to generate without overlapping hunks. $this->assertEqual( $this->loadResource('disjoint-hunks.diff'), $this->loadOneChangeBundle( 'disjoint-hunks.old', 'disjoint-hunks.new')->toUnifiedDiff()); } public function testNonlocalTrailingNewline() { // Diffs without changes near the end of the file should not generate a // bogus, change-free hunk if the file has no trailing newline. $this->assertEqual( $this->loadResource('trailing-newline.diff'), $this->loadOneChangeBundle( 'trailing-newline.old', 'trailing-newline.new')->toUnifiedDiff()); } public function testEncodeBase85() { $data = ''; for ($ii = 0; $ii <= 255; $ii++) { $data .= chr($ii); } for ($ii = 255; $ii >= 0; $ii--) { $data .= chr($ii); } $expect = Filesystem::readFile(dirname(__FILE__).'/base85/expect1.txt'); $expect = trim($expect); $this->assertEqual( $expect, ArcanistBundle::encodeBase85($data)); // This is just a large block of random binary data, it has no special // significance. $data = "\x56\x4c\xb3\x63\xe5\x4a\x9f\x03\xa3\x4c\xdd\x5d\x85\x86\x10". "\x30\x3f\xc1\x28\x51\xd8\xb2\x1a\xc3\x79\x15\x85\x31\x66\xf9". "\x8e\xe1\x20\x8f\x12\xa1\x94\x0e\xbf\xb6\x9c\xb5\xc0\x15\x43". "\x3d\xad\xed\x00\x3c\x16\xfa\x76\x2f\xed\x99\x3a\x78\x3e\xd1". "\x91\xf8\xb0\xca\xb9\x29\xfe\xd4\x0f\x16\x70\x19\xad\xd9\x42". "\x15\xb4\x8f\xd6\x8f\x80\x62\xe9\x48\x77\x9f\x38\x6d\x3f\xd6". "\x0e\x40\x68\x68\x93\xae\x75\x6d\x7f\x75\x9c\x80\x69\x94\x22". "\x87\xb6\xc0\x62\x6b\xab\x49\xb8\x91\xe9\x96\xbf\x04\xc2\x50". "\x30\xae\xea\xc1\x70\x8e\x91\xd0\xb6\xec\x56\x14\x78\xd5\x8a". "\x8c\x52\xd1\x3c\xde\x65\x21\xec\x93\xab\xcf\x7e\xf5\xfd\x6d". "\x2d\x69\xb9\x2e\xa3\x42\x7b\x4d\xa5\xfb\x28\x6d\x74\xa3\x7b". "\x3a\xc5\x34\x7c\x63\xa9\xf9\x8e\x34\x14\x42\xb0\xf1\x0e\xe2". "\xd0\xd2\x04\x81\xff\x62\xd5\xd9\x46\x3b\x36\x88\x8a\x93\x55". "\x02\x2c\xff\x9f\x48\xd6\x7a\xcb\xbf\x6a\x33\xaa\x6b\x08\x4c". "\x96\x98\x89\x53\x56\xb4\xb3\x9b\x06\xb1\xa0\x13\x69\xfa\x6a". "\xa8\x0d\x6a\xda\xb2\x6f\x62\x0b\xa8\xf6\x59\x29\x46\x7d\x04". "\x44\xeb\x90\x6f\xd7\xc7\xb6\xca\xc5\xeb\xde\x10\x9b\xbd\xf2". "\x66\x8e\xd0\x0b\xda\x8c\xeb\x90\x73\x73\x33\xe7\x6f\x26\x57". "\x4e\xfc\x95\xe0\xfc\x62\x93\xa7\x28\xe6\x0c\x46\x73\xdd\x01". "\xce\x43\x9b\x4e\x16\x74\x5b\x36\x92\x5a\x66\x4c\xe3\x9e\x90". "\x2d\x9a\x1a\x3d\x69\x39\x67\x04\xd6\xf8\x5f\x45\xee\xbb\xd4". "\x63\xcf\x8c\x9b\x31\x69\x98\x1a\x98\x57\x4b\xa9\x49\xf6\x1b". "\x76\x28\xd7\xe3\x8f\x63\x95\x5b\x06\xe2\xa8\x66\x60\xf9\x49". "\x4e\x40\x53\x32\x9b\x74\x36\xc0\x56\xf4\x33\xec\x83\xd2\x2c". "\x69\x60\x55\x11\x3b\x4f\xd6\x0a\xf6\x04\x38\x75\xb6\xc2\x82". "\x4d\xfa\x83\x56\xba\x35\x42\xc3\xcb\xdc\x28\xf4\x69\x48\xa9". "\xe0\x51\x41\x79\x66\xfe\x61\xd1\xf2\x9f\x7b\xde\xc4\x3e\x8f". "\x8f\xb6\x9c\x0a\x74\xf8\x71\x03\x37\x37\x30\x8d\x2a\x6a\xc9". "\x51\xa1\xe2\x34\xe5\x42\xdb\x4f\x61\x4e\x16\xfc\x23\x72\x12". "\x46\x53\x12\x82\x3e\x44\x63\x23\x82\xaa\xab\x7e\x8d\x70\x66". "\xf1\x94\x86\x02\xc5\x3e\x9c\x79\x17\x1e\x9f\x13\x89\x3d\x25". "\x45\xc9\x3b\x1e\xa0\x1a\x03\x20\x1c\x81\x6b\xfc\xb5\xc9\xe2". "\xda\xb1\x87\x34\xa0\xb2\x72\x36\x68\x12\x05\x53\x7c\x68\x6b". "\x1e\x2a\x56\x2a\x7e\x7f\xd0\x9c\x13\xa9\xb2\x4c\xe6\x8a\x65". "\xd7\x67\xad\xf3\xf3\x2b\x9c\xe8\x10\x07\x8a\xe2\x20\x67\xe4". "\x51\x47\xc1\x22\x91\x05\x22\x39\x1a\xef\x54\xd2\x8a\x88\x55". "\x3f\x83\xba\x73\xd4\x95\xc7\xb8\xa2\xfd\x4d\x4e\x5d\xff\xdd". "\xaf\x1a\xc2\x7e\xb5\xfa\x86\x5f\x93\x38\x5d\xca\x9a\x5a\x7e". "\xb7\x47\xd5\x5c\x6b\xf3\x32\x03\x11\x44\xe9\x49\x12\x40\x82". "\x67\x7d\x2a\x5a\x61\x81\xbd\x24\xaa\xd7\x7c\xc9\xcf\xaf\xb0". "\x3e\xb0\x43\xcd\xce\x21\xe4\x1b\x5a\xd6\x40\xf5\x0e\x23\xef". "\x70\xf4\xc6\xd2\xd7\x36\xd7\x20\xda\x8d\x39\x46\xea\xfc\x78". "\x55\xa2\x02\xd6\x77\x21\xc8\x97\x1e\xdf\x45\xde\x93\xa7\x74". "\xd8\x59\x10\x24\x8a\xe8\xcd\xe9\x00\xb5\x4e\xe6\x49\xb0\xde". "\x14\x1a\x5d\xdd\x38\x47\xb0\xc7\x1e\xec\x7c\x76\xc9\x21\x3c". "\x3a\x85\x4f\x71\x97\xed\x4a\x94\x2c\x51\x48\x9c\x43\x90\x70". "\xe9\x0e\x84\x55\xd2\xa4\x48\xfa\xfd\x54\x12\x11\xb9\x32\xfc". "\x1d\x66\xe7\x42\xe3\x5e\x65\xf4\x3d\xea\x1a\x53\xe3\x7b\x4b". "\xee\xdb\x74\xce\x30\xd3\x04\xcb\xda\xa4\xdd\xad\x98\x3a\x76". "\xe8\xba\x1b\x03\x53\xed\x46\x5d\xef\xd4\x34\xc2\x8d\xef\xae". "\x51\x35\x0f\x4d\x40\xaa\x3a\xdb\x50\x1a\xbe\x5f\x8b\xb8\x24". "\x40\x19\x8f\x8a\x6b\x44\x4f\x9b\xe0\xf4\x9c\x4b\xc4\x23\x37". "\xf0\xb3\xe1\x58\x9d\x0e\xd9\xa9\xf7\x3e\x86\x43\x9b\x5b\x90". "\x3c\xc0\x20\xa0\xc5\x86\x4f\xc6\xcb\xb5\xcb\xd4\x88\xc6\x72". "\x57\xa7\x57\x2c\x34\x26\x91\x44\x15\xa8\xf4\x88\xca\x74\x56". "\x9e\x12\x6c\xdf\x52\xef\xc0\xb4\x5c\x16\xe8\xaa\xf7\xb6\xf3". "\x7c\xda\xcd\x42\xf9\x1c\x40\x88\x44\x68\x4f\x1b\x5a\x7b\x8f". "\xc3\x47\x48\xd3\xf3\xe5\xf5\x66\x35\x48\xbe\x64\xdf\xfe\x35". "\xf1\xc3\xe4\xa8\xfc\x86\xfb\x69\x20\xc9\xf4\x16\x96\xc1\x7a". "\x51\x14\x77\xa4\x6e\x13\xe8\x59\x35\x24\xf1\xe5\xfe\xe9\x98". "\x0d\xd1\xe8\xce\x9c\x7f\xf8\x3b\x79\x39\x3a\x1d\xa3\x77\xef". "\x4f\x4b\x59\x73\x03\xb3\xfe\xae\x70\x2a\x3a\xf0\x79\x9d\x7e". "\x9b\xaa\xb1\x18\xf9\x43\x69\xf3\x55\x46\xad\x38\xa2\xf1\xcb". "\xce\x37\xa9\x88\x20\x38\xea\x19\x29\x95\x8c\x75\x06\x9d\x1d". "\x9e\xf2\xb7\x64\x98\x21\x36\x90\x92\xf8\xb8\x89\x1e\x5c\x5d". "\x09\x3b\x52\xc5\x6a\x87\x7e\x46\xca\x8c\xdf\xe7\xca\xa9\x7b". "\x11\x63\x0f\x9e\x42\x9a\x3e\xe0\x8b\x80\x9e\x91\x76\x88\x9a". "\xa1\xe2\x96\xae\xfb\x18\x39\xdc\x92\x99\x34\xfd\x98\x20\xa8". "\x89\x61\x2c\x26\xe0\xb8\x83\xa7\xe7\x50\x42\x8f\xfc\x36\x66". "\x6b\x25\xc5\x6d\xb4\x31\xe1\x4d\x0f\x2e\xf8\x44\xe2\xb6\x6a". "\x6d\xfe\x83\x9e\x2c\x07\x2f\x15\x41\xf3\xe7\xa6\x18\x2b\x84". "\x7e\xeb\x43\xcc\xbb\xdb\xa9\x54\x5c\xbc\x59\x6a\xdc\x26\x2a". "\xf4\x59\xa7\x75\xa4\xac\xed\x73\x8f\x16\x43\x0d\x97\x10\x2c". "\x70\xef\x9e\xb2\xc9\xdf\xe6\xa7\x9b\x08\x79\xa3\xf7\x99\xf5". "\x59\xe4\xd5\x89\x10\xe5\xc9\xf7\xe7\x29\x72\x06\xc6\x54\xc3". "\xcd\xd0\xff\x69\xf8\xdf\x19\xf2\x66\x1c\x69\x40\xbc\x97\xf1". "\x49\x5e\x78\x62\x52\x46\x7f\xcf\x44\x50\x8b\x5f\xe7\xa8\xeb". "\xd5\x84\x24\x81\xc0\x2c\x65\xf7\x95\xbd\xf2\x8e\x43\xfb\x6a". "\x49\x3c\x6a\xe5\x2a\x39\xf0\xfa\x89\x59\x5f\x39\x75\xb4\x6f". "\x04\xf1\xe0\x2c\xcd\x77\x34\xec\x6b\x45\x16\xe3\x18\x24\x05". "\xb9\x68\xc1\x4e\x71\x4b\xff\x88\x18\xea\x0d\x56\x49\x55\xdf". "\xe5\xb0\x59\xdb\x74\x9e\x0b\x38\x03\x9f\x10\x6f\xd9\x34\x07". "\x44\x29\x08\xb1\xd4\x77\xc6\x84\x0d\xbb\xb5\xd5\x09\x05\x19". "\x01\x62\x29\x45\x52\x1d\xc6\x4f\x25\x78\x7e\xbc\xae\x07\xb3". "\xd4\xe0\x19\x91\x03\xd6\x8d\x2f\x00\xc9\xb2\x66\x3b\x4e\x3d". "\x75\xf7\x23\x9a\x3e\xa4\xd5\x7f\x75\x47\xd0\xbc\xc3\xc8\x2a". "\xdc\x85\x09\x6c\x0c\x90\x38\xd8\xef\xcf\xf4\x7a\x1b\xc7\x76". "\xe0\xdb\x81\xa8\x1b\x2b\x8d\xd4\x36\x90\x76\xde\x8a\x90\xc8". "\x5b\x05\x00\xeb\xb3\x20\xce\x6e\x5c\xb9\x35\x3d\x95\x3a\x79". "\x4a\x60\xeb\x23\x11\xfb\x90\x2d\xf6\xb7\x05\x4a\x43\x41\x79". "\x51\xaa\xe6\x90\x0a\x71\x87\x80\xbe\xb0\x89\x0f\xd3\x84\x19". "\xce\x6c\xf9\xbb\x1b\x15\x4d\x0f\x33\x65\xf7\x9e\x3a\xd9\x8c". "\x02\x43\xcf\xdf\xb2\x60\xc1\x4c\xe9\xa5\x3c\xaf\xfa\x41\x2d". "\xb9\x1f\x45\x32\xcb\x39\x2f\x94\xae\x44\x6d\x69\xc1\xc9\x57". "\x8c\xe5\xf4\xa4\x3a\xb6\x70\x61\xf9\xbb\x41\xdc\x78\xf0\xf7". "\xbf\xa8\x8e\xe3\x77\x51\xce\x25\x2f\xdf\x27\x6b\x07\x30\x9f". "\xce\xdb\x59\x58\xaa\xb2\x2e\xdc\x90\x92\x82\x55\xfe\x25\x36". "\x49\x7f\x6d\x2d\x39\x51\xef\x3d\xc8\xa3\x87\x0b\xe7\xf2\xac". "\x90\xa0\x1d\xd8\xc7\xea\x93\x53\x3b\x21\x84\x2e\x52\x6c\xfb". "\x4f\x31\xda\xd1\xea\x45\x3e\xdc\xeb\x52\x81\x8c\x2b\xf4\x2a". "\xbc\x01\xc4\xe7\x68\x36\x9c\xd5\x2d\xc1\x61\xcb\x9a\x5f\x18". "\x00\x6a\xc8\x9a\x4e\xfd\x31\x5b\xce\x90\x4e\x45\xff\x7f\xea". "\xb2\x26\xad\xc1\x3a\x21\xa9\xe8\x7c\x14\xae\x81\x1e\xbe\xa3". "\x6d\xda\x92\x1b\xeb\xf2\x69\x76\x3e\xf1\x2b\xf7\x1a\x45\xd5". "\xb3\x81\xb1\xbe\x80\x7f\x24\xba\x0e\xd5\x68\x34\x3f\x1a\x29". "\x15\x0e\xc2\x26\x62\x0c\xaa\xa9\x20\x4c\x61\x65\x49\x07\xbe". "\x69\xf4\xc9\xec\x2f\x1c\xfa\x59\x2e\x72\xc0\x17\xc5\x4c\xfa". "\xba\x2f\x64\xab\xa9\xb4\xcb\xdc\xcb\x25\x5f\xcf\x0c\x87\xcc". "\xf0\x36\x2b\xce\x81\x5a\x22\x85\xa0\x50\x50\x97\x8e\xda\x36". "\x80\x74\xb5\x1e\x02\x3f\xd7\xc8\x29\x11\xeb\x1d\x3d\x74\x9f". "\x26\x1a\xa4\x3d\xf9\x0e\xf0\x2d\x5c\xa9\x43\xbf\x51\x6c\x8d". "\xe6\x78\xe0\x67\x57\xf0\xc8\x0e\x97\x9c\x57\x23\x30\xac\x63". "\xdf\x46\x98\xa4\xaf\x4e\xa7\xe5\xac\x31\xbd\xeb\x6a\xa0\xb0". "\xe4\x94\x7e\x51\xf6\x89\x81\x3e\xab\x4f\x64\xb7\xc5\x51\x71". "\xcd\x74\x02\xa9\x02\x99\x5c\xab\x0e\x14\x47\x3b\x04\xc1\x9b". "\x59\x1a\x93\x92\x4c\x71\x20\x5f\x6e\xd3\xf3\xa7\x47\x1b\x39". "\x3e\x73\x69\xe2\xec\xcb\x52\xb3\x5c\x7a\x95\x25\x3f\x16\x98". "\x60\xa8\xa2\x5d\xc4\x5a\x67\xe4\x11\x06\x06\xf9\x7a\xb4\x14". "\xe0\xbc\x7b\x13\x1d\x0f\xf2\xca\x0b\xd4\xaa\x71\x35\x3e\xd6". "\x2e\x2e\x5d\x7b\x15\xc9\x23\x1a\xa9\x24\x31\x48\xd4\xcf\x4a". "\xf4\x32\x17\x9b\x1d\x4b\xfe\x49\x69\xd6\xc0\x8f\xb9\xdb\x72". "\x52\x2c\xe8\xf3\xc4\xfc\x46\xf5\xb8\x1b\x05\x06\xcf\xcc\x23". "\x34\xbf\x25\x6a\xea\x3c\xc7\x64\xd4\xd5\xb3\x67\xed\x24\x27". "\xd3\x67\xc1\xbd\x9f\x7b\x7d\x19\x04\x5c\xd1\x96\x7e\xa5\xc7". "\xbb\xb2\x84\x68\x98\x38\x11\x90\xfb\x62\x15\xfd\xe6\xb7\x24". "\x77\xb2\x78\xc7\x73\x91\xc9\x60\x1d\x91\x6d\x04\x2b\x41\xe9". "\xc9\xfa\xe4\x98\x54\x83\x9a\x6e\x76\x8c\x21\xf9\x91\x38\x1f". "\xdc\xfe\x13\x09\x30\xd7\x53\x63\x62\xba\xe3\x2c\x70\xd5\xfc". "\x78\x35\x36\x79\x5d\xb6\x0e\x35\x3d\x46\x87\xfb\xf5\x64\x1f". "\x3e\xfd\x2f\x1c\xbb\xed\x95\x2d\xd6\x63\xdc\xa7\x6a\x39\x8f". "\xbd\xcb\x79\x95\xe9\x45\xbf\xe4\x3e\x05\x55\x00\xdb\x33\x28". "\x3a\x6c\xe2\x35\xbb\xac\x70\x52\x2b\xac\x4e\x11\x44\x58\x16". "\x21\xb4\xae\x0d\x6a\xb9\xdc\x85\x5d\x90\x11\x26\x85\xdb\xc3". "\xf0\x38\x6f\x8a\xff\x12\xf0\xc9\x9e\xf0\xfc\xae\x94\x11\x4d". "\xce\x96\x29\x09\x6c\xf4\x2a\x6c\xda\x1e\x4c\x4a\xa2\x96\x5a". "\xef\xc6\x38\x5c\x60\xa2\x28\x13\x58\x73\x96\xde\x59\x2a\x57". "\x64\x6c\x14\x94\x8a\x2e\x8e\x21\x3f\xa2\x43\xde\xf6\x2d\x23". "\x74\x5c\xbd\x7a\x10\xdb\x17\xa8\x93\xd0\x74\x86\x9d\x33\x07". "\x48\xee\xac\x18\x6d\x64\x61\x7b\x61\x2b\xa4\xa2\xab\x99\x59". "\xbe\x19\xd7\x19\x41\x1e\x61\x87\xad\x40\x5b\x69\x8c\x32\xf5". "\xb6\x49\xbe\x1f\xad\xd8\x0f\x3e\xd9\x62\xac\x3a\x76\xde\x32". "\xa3\xb2\x41\x95\xad\x17\x23\xab\xa1\x37\x9c\xab\x73\x79\x70". "\xd6\x66\x0d\x6e\x4d\x8b\xa0\xac\xe3\x44\x1e\x0a\xee\xf0\x74". "\x64\xd8\x44\xd1\x6c\xa6\xd5\x36\x2e\xd9\x55\x6e\x90\x63\xb7". "\xf7\x8e\xc6\x28\xa3\x40\x00\x60\x9a\x3c\xfe\xff\x03\x30\x11". "\x18\x92\x2f\x5b\x23\xe1\x4e\x99\xe4\x82\xc9\x51\xe2\x15\x6a". "\x76\x5c\x67\xae\xa3\xa2\x9c\x85\x51\xe0\x44\x89\x63\xa5\x71". "\x99\xbc\x2d\x9c\xab\x9a\xfb\x20\x37\x58\xd6\x2d\x8b\x7d\x42". "\x13\x35\x44\x4c\x11\x97\x66\x27\x17\xac\x44\xe8\x6a\x03\x78". "\xa2\x88\xc6\x36\x71\x5a\x5a\x5a\x72\xa3\xe9\x72\x0c\x91\x31". "\xfc\xae\x7b\xa0\x75\x21\x0a\xc1\x4b\x95\xcb\xe3\xc2\xee\x03". "\x0f\xb8\xb2\x51\xc7\xc8\x9c\x8d\x6d\x3a\xe7\x4e\x2c\xaa\xeb". "\x5e\x49\x93\xe0\x8f\xa1\x54\x93\xe7\x7c\x5d\x31\xc7\x05\x00". "\x28\x14\x57\x47\xb3\x05\x2d\x17\x92\x28\x45\xee\x85\x3a\x59". "\xb6\xa6\x04\xc0\x5c\x07\x1f\xe6\x5b\x36\x53\x62\x82\x64\xd5". "\xb6\xf2\xf5\x67\x19\x11\xee\xd2\x70\xc5\x14\x63\xc1\x75\xe1". "\x24\xe5\x01\x59\x52\x7c\x88\x17\xb4\xe0\x15\xe9\x12\x05\xcd". "\x88\x7a\xd5\xea\x45\xc3\xbb\x65\xd4\xdd\x0d\xde\x36\x94\x98". "\x0d\x2c\xfb\x3c\x2f\x69\xd0\x28\xe2\x85\xd9\x27\xf3\x7a\xad". "\x50\x68\x96\x54\x5e\xeb\xbc\x2a\x74\xde\xf3\x4e\x8b\x27\x0a". "\xcf\x4c\x60\x40\xe8\xc5\x72\xab\x8c\xfd\xe9\xab\xff\x51\xe5". "\xd6\xea\x9e\x34\x73\xe1\xe6\xf8\x5b\xb1\x10\xf0\xf9\x2d\x23". "\x0e\xfe\xe5\xf4\x8d\xb6\x6d\x37\x14\xed\x54\x97\x92\x5c\x68". "\x40\x88\xf1\x43\x29\xef\x5e\x96\x77\xa2\xe8\x3c\xae\x7f\xb1". "\x99\x17\xa7\x0c\x6f\xe2\x43\x32\x9b\x14\x43\xf2\x15\x6b\x13". "\x10\x68\x56\x0b\xaa\x06\x2e\xc0\xf8\xde\x9e\x54\x9d\xba\xff". "\x76\x26\x6d\x5e\x9e\x88\x3a\x2b\x9b\x20\x43\xb9\x1a\x0e\x58". "\x65\xec\xdb\x9e\x97\xb8\xfb\x03\x6c\xb0\x7f\xa2\xf1\xf4\x27". "\x24\x21\x47\x51\x21\x40\x45\x28\x71\xf7\xa1\x6b\xbe\x0e\xc8". "\x3f\x9b\xda\x62\x9d\x73\xf7\x5f\x70\x6c\xba\x1e\xeb\x16\x5c". "\x2e\x44\x0a\x22\x02\x6c\xbe\xb9\x69\x93\xfd\xa5\x33\x26\x64". "\x24\x6c\xc2\x3d\x2f\xf3\xd1\x97\xde\x60\x43\x1c\x0d\x1b\x94". "\xb3\x48\x45\x7c\xd5\xd0\x71\x4d\xad\xbf\xa4\x0a\x22\x27\x04". "\x38\x84\x19\x66\x63\xf0\xf3\xfc\xb0\xf3\x1d\xea\xba\xb9\xe4". "\xe5\x80\xed\xe3\xf1\x78\x24\xc3\x25\x27\x71\x81\xc2\xec\x54". "\xed\xcc\x63\xf7\x39\xcd\x83\xdf\x32\x88\xc0\x3b\xd4\x62\xb8". "\xea\x34\xd8\xcf\xbc\x3a\x89\x38\x64\x60\x44\xde\xb6\x76\x59". "\xb1\x95\x6a\x26\x08\xf0\xf4\x71\x25\x8b\xf8\x81\xdd\x0d\x2f". "\x8c\xe2\x70\xc2\x96\xc2\xd8\x9b\xe4\x3f\xec\x8b\xfd\xbd\xc9". "\x36\x33\xb7\xbc\x59\x37\x19\x09\x30\x5e\xef\x67\xae\x67\x48". "\x72\x0b\xf4\x2a\x82\xff\xcb\xd7\xd9\x9d\x6d\x7c\xa6\x20\x42". "\x50\x2b\x0a\x2f\x45\x99\x5b\x76\x6d\x99\x39\xa9\xb6\x32\x06". "\x11\xf8\x19\xd1\x3f\xc0\xd6\x1f\x67\xfa\xd5\xae\x7a\x71\x8c". "\xbc\x3d\xb4\x5f\x5c\x81\x7c\xa1\x39\x70\x0a\x17\x24\xb7\x22". "\x86\x50\xd8\x1f\xc8\x6c\x59\x9a\xdc\xf0\x71\x01\xda\xd8\x53". "\x98\x1c\x73\x36\xf1\x09\x86\xc9\xa7\x26\x25\xc0\x03\x3e\x13". "\x4e\x29\xeb\xf0\x8d\xe3\x38\x03\x54\xee\x37\xfb\x51\x2e\xb4". "\xf6\x12\x1f\xb2\x8c\x66\x75\x00\x30\x5b\xef\x59\xf9\x63\xa9". "\x74\x07\x91\xe4\x9c\xb7\xc9\x89\xd9\xa9\x51\x93\xcb\xb1\xa7". "\x64\x08\x79\x8f\xb4\x6d\x09\xd7\xc5\xbf\x0a\xdb\x50\xe0\x1c". "\x83\xca\xf8\xcf\xa7\x81\xbb\x0b\xe6\xcf\x1b\x0e\x0a\xe0\xcd". "\x68\xe2\xde\xc4\x2d\xba\x55\xc7\xc7\x1e\x6c\x5e\xca\x9b\x20". "\x75\x96\x94\x92\x84\xec\xf5\x22\x25\x78\x67\xcd\xbe\x01\xfe". "\x53\xa5\xcc\x6a\x40\x33\x83\xa4\x7a\x44\x93\x0b\xf9\x4c\xb2". "\x95\xb6\x7e\x4b\xa4\xc8\x86\xfe\x8a\xf1\x77\x40\x56\x13\xc1". "\x31\x2c\x8c\x4a\xa8\x89\x61\x0c\x39\x33\x78\x8c\xd5\x50\x3b". "\x89\xc3\xd3\x80\x1c\xa7\xb6\x36\xc2\x00\x8d\x0a\x7f\xcc\xd3". "\x20\x74\x60\x70\x36\x7d\xda\xdc\xc4\x49\x04\xf0\xe6\x6c\xd1". "\xbe\xcb\xfb\xf1\xa2\xd6\xd4\xe4\x97\x3f\x35\x09\x5b\xda\x06". "\x6b\x6d\x86\x53\x23\x0c\x26\x51\x2a\x15\xaa\xe2\x73\xfb\xc7". "\x41\x54\xdc\x5d\x99\x0b\x0a\x1e\xd4\xdb\x70\xa3\x8e\xfd\x5b". "\xf0\xa8\x3e\x9b\xff\x57\x98\xbc\xd9\x2a\x56\xd3\x19\xf9\x0b". "\xd9\x67\x0f\x10\x9c\x23\xe5\x6b\x12\xc6\xb6\x4b\xd1\x0c\xe9". "\x45\x36\xdf\x54\x6f\xcc\xfe\xb5\xcc\xb9\xfe\xde\xc8\xb5\xc9". "\x04\x59\x61\x75\x1e\x72\x37\x54\xfd\xc6\xc3\x7e\x74\xae\x55". "\x31\x6a\xbc\x8a\xd8\x45\x91\xe2\x8d\x20\x97\x71\xe7\x55\xd6". "\x8a\xb8\x82\x2a\x27\x4f\xdc\x53\x89\x28\xf7\x3a\xfe\x07\xef". "\x60\xb2\x32\x7c\xbc\x13\xc4\x3d\xda\xd7\xfb\xb8\x61\x7d\x69". "\xae\x0e\x9a\x71\xd6\x00\x26\x97\xff\xdb\xe6\xbe\x45\x7a\xb5". "\x00\x31\xfd\x70\xcc\xd7\x34\x88\xe4\x05\x61\xf5\x72\x1d\x14". "\xf0\x7e\x90\xdb\x0e\xc7\xda\xd4\xf3\x99\xd4\x60\xd9\xa7\xc8". "\x5b\x33\x34\xb5\x23\x74\x2c\x5f\x6b\x56\x95\x9c\x1b\x2a\xac". "\xf9\xfe\x46\xc3\xf1\x9b\x24\x7e\x4b\xca\x25\x58\x41\x10\x63". "\xe8\xe7\x68\xda\xcc\xb6\x4d\x5b\x8f\xc9\xa9\x31\xeb\x5c\x2a". "\xcf\x9d\x89\xd5\x51\x93\x80\x30\xf4\xc9\x2c\x8c\xb8\x8c\x62". "\xd6\x33\xbd\x95\x9f\xfa\x19\xf2\x48\x28\x09\x73\xc9\x53\x61". "\x94\x3a\x62\x68\x6c\xc6\xd6\x0a\xb4\xae\x27\x96\xfb\x29\xd7". "\x46\x67\x11\x7a\xe8\x3a\x9a\x3f\xf4\x9a\x75\xed\x24\x67\x45". "\x79\xdc\x8b\x19\xf2\xef\x57\xaa\xc7\x84\xff\x9d\x2d\xc3\xa8". "\x85\x54\xb7\x9d\xe1\xd6\x2b\xe9\x31\x9d\x6c\xb8\x4e\x76\x50". "\x80\x44\x46\x8f\x5e\x7e\x20\xaa\xa0\x8a\x36\x6b\xef\xd1\x75". "\xf8\x3f\x20\xdd\x09\x73\xbf\xa5\xf7\xb4\x87\xb2\x44\xc0\x0f". "\x10\xc0\x95\x2e\x8a\x42\xfa\xc3\x49\x17\xb9\xb5\x1a\xc3\x80". "\x93\x0c\xd8\xe3\xcd\xa4\x38\x61\x7a\x22\x73\x8e\x32\x8f\x55". "\x9c\x91\x08\xd9\x65\xa9\x02\x28\xc6\x59\xc8\x51\x32\x20\x48". "\xea\x2c\xae\x0e\xa6\x35\x5b\xe2\x63\xf9\xf2\x9d\x5f\xe3\x45". "\xdc\x41\xba\xfb\x40\xcc\x8d\xde\x6c\x3d\x50\x97\x9d\x83\xa0". "\xda\x41\x61\xba\xaf\xf8\x74\xd2\x21\x7b\x09\xcc\x83\xe1\x08". "\x01\x04\x42\xce\xcb\xec\x1d\x6b\xb7\x6f\x0f\x4b\xd4\x53\x90". "\x55\x3b\xcf\x9f\x93\xb8\xad\xce\x5f\x13\x83\xb3\x89\x6f\x5a". "\x1b\xa4\xf5\x95\x4b\xb4\x22\x22\x1d\x35\xaa\xfa\xc7\x14\x8c". "\xcd\x50\x66\x14\x47\xff\x67\xb2\xf8\x12\x09\xb3\x8a\xe5\x7d". "\xb8\xc9\xe4\x89\xf7\xa4\xb5\x70\xfa\x2d\xeb\x95\x89\xec\xbb". "\x49\x59\xd2\xc1\x6d\x0e\x06\xe4\x5e\xd5\x13\x13\x0d\x72\x6e". "\xf0\x6d\xa9\xd5\xe7\x54\x68\x35\xcd\xd0\xd5\xa6\xe5\xb2\xe4". "\xb1\x19\xe4\xf1\xe3\x8a\x56\x4c\x3b\x3d\xb8\x03\xfe\x22\x2f". "\xc6\xdc\x88\x7b\xca\x5c\xc6\xdd\x17\x34\x08\x22\xf0\x17\x61". "\x0e\x60\x9c\xb4\x27\x57\x30\x6e\xb8\x4f\xdd\x25\x7b\xef\x9e". "\x8e\x88\x6b\xd8\x10\x23\xc2\x44\x53\x73\x64\x8f\x40\x22\xe1". "\xe8\xa2\xb0\x3f\x8a\x07\x66\xcd\x64\x4f\x9c\x1e\x89\x76\x04". "\x6d\xab\xc2\xbb\x16\x85\x80\x01\xa5\xb1\xe2\x12\x04\x2e\x39". "\x87\x8c\xee\xbc\xfb\x07\x6d\x03\x4c\x3a\xa5\x7b\x95\xd9\xd7". "\xd6\xee\x2b\xe9\xcb\xe6\xec\xa8\x84\x6a\x42\xf9\xb2\x25\xc8". "\xf3\x6a\xaa\x34\x3b\xd9\x72\xd9\x70\x81\x3b\xd4\x5e\x66\x97". "\x1b\xe6\x2b\x88\x71\x82\xa3\x8a\x98\xb0\x16\xd9\xbb\x97\x8b". "\x57\x79\x41\x56\x6e\xc2\x8f\xdf\xfa\x5b\xc7\x68\x5b\xb8\x09". "\x41\x31\x7c\x19\xe1\x95\x2e\x05\x4c\xac\x38\x81\xda\xb3\x8b". "\x3e\x1c\x79\x9a\x31\xac\x3e\x3d\x6d\xab\xf3\x5a\x5e\xc7\x6e". "\x8e\x39\xcd\x7b\x6f\x62\xee\xb9\x73\xdd\x82\x42\x6f\x09\xe4". "\xc3\xae\x92\xe8\x18\x99\xa0\x5e\xa2\x12\xf4\xe2\xe0\xe6\x95". "\x58\x3a\x45\xad\xfe\x23\x79\x5f\x82\xce\x95\x88\x73\xeb\x46". "\xc8\x00\xac\xc3\x2a\xdc\x7e\xab\x9b\xf8\xbb\x46\x5c\xa8\x46". "\xbc\xfd\x99\xae\x4c\xa7\x77\xeb\x7c\x58\xbf\xbb\x52\x68\x62". "\x3d\x0b\x79\x64\x38\x65\xa7\xcb\x7b\xe9\xb2\x33\xb5\x59\x52". "\x7b\x17\xb4\x02\x2b\x07\x0d\x3a\x11\x57\x92\xa5\x22\x2b\xbc". "\xe6\x97\x05\x12\x05\xe7\x91\xe3\xfa\xae\x15\xbe\x20\xe5\x5c". "\x71\x24\x80\x85\xc9\x66\xc1\x53\x5c\x8f\x08\xd4\x52\xe1\x10". "\xb6\xd6\x20\x08\x01\x79\x33\x9f\x1b\xbd\xa0\xab\x7c\xb1\xd9". "\xdc\xca\x44\x22\x49\xb7\xb7\x3d\x84\xac\x92\xf4\xfa\x0a\xc9". "\xc5\xb2\x42\x2b\x9a\x63\xbb\x8a\x82\x04\x2f\xf7\xe9\x30\x05". "\x67\x32\xd1\x41\x1a\x69\x6e\xb9\xf8\x5f\x6d\xb7\xe5\x4e\x85". "\x21\xfa\x16\x8a\x44\xfd\xf6\xd9\xa2\x5f\x68\x2b\xf3\xe2\x3c". "\x8a\x69\xd2\xc1\x38\xed\x83\xef\x0d\x53\x86\x93\x32\x23\xc6". "\x14\x0c\xb0\xb6\x6e\x77\xa4\x20\x0f\xb1\x6e\xe2\xce\xca\x6f". "\x93\x1c\x3a\x8f\xd0\xd2\x5a\x6e\x30\xd6\x8e\x5f\x4b\xa5\xef". "\xa9\x62\xeb\x28\xa0\x5e\x3f\xc1\xbc\x0a\x68\xab\xd7\xfa\xa2". "\xb7\x8f\x12\xb0\x99\xbc\x93\x20\xb8\x95\x8d\xca\xc7\xa7\xd9". "\x2e\x19\xac\x06\xb9\x4e\x56\x8e\x74\xef\x2a\x04\xd8\x75\x04". "\x38\x2a\xc7\xa0\xa4\x89\xf3\xa4\x8a\xd4\x2c\x2c\x58\x6f\x00". "\x03\x23\xb8\xaf\x02\x48\x7d\x50\x46\x6f\x5a\x08\x41\xe3\x56". "\x6d\xcb\xe2\x4f\xea\x8e\xab\x74\xcd\xf9\xef\xcf\xf9\x1e\xf1". "\xf8\xb9\x6c\xaa\x3b\x37\xd1\x21\x42\x67\xec\xd6\x44\x55\x33". "\xe8\x1d\xa4\x18\xf3\x73\x82\xb4\x50\x59\xc2\x34\x36\x05\xeb"; $expect = Filesystem::readFile(dirname(__FILE__).'/base85/expect2.txt'); $expect = trim($expect); $this->assertEqual( $expect, ArcanistBundle::encodeBase85($data)); } } diff --git a/src/parser/__tests__/ArcanistDiffParserTestCase.php b/src/parser/__tests__/ArcanistDiffParserTestCase.php index 37801315..9322063c 100644 --- a/src/parser/__tests__/ArcanistDiffParserTestCase.php +++ b/src/parser/__tests__/ArcanistDiffParserTestCase.php @@ -1,687 +1,687 @@ parseDiff($root.$file); } } private function parseDiff($diff_file) { $contents = Filesystem::readFile($diff_file); $file = basename($diff_file); $parser = new ArcanistDiffParser(); $changes = $parser->parseDiff($contents); switch ($file) { case 'colorized.hggitdiff': $this->assertEqual(1, count($changes)); break; case 'basic-missing-both-newlines-plus.udiff': case 'basic-missing-both-newlines.udiff': case 'basic-missing-new-newline-plus.udiff': case 'basic-missing-new-newline.udiff': case 'basic-missing-old-newline-plus.udiff': case 'basic-missing-old-newline.udiff': $expect_old = strpos($file, '-old-') || strpos($file, '-both-'); $expect_new = strpos($file, '-new-') || strpos($file, '-both-'); $expect_two = strpos($file, '-plus'); $this->assertEqual(count($changes), $expect_two ? 2 : 1); $change = reset($changes); $this->assertTrue($change !== null); $hunks = $change->getHunks(); $this->assertEqual(1, count($hunks)); $hunk = reset($hunks); $this->assertEqual((bool)$expect_old, $hunk->getIsMissingOldNewline()); $this->assertEqual((bool)$expect_new, $hunk->getIsMissingNewNewline()); break; case 'basic-binary.udiff': $this->assertEqual(1, count($changes)); $change = reset($changes); $this->assertEqual( ArcanistDiffChangeType::FILE_BINARY, $change->getFileType()); break; case 'basic-multi-hunk.udiff': $this->assertEqual(1, count($changes)); $change = reset($changes); $hunks = $change->getHunks(); $this->assertEqual(4, count($hunks)); $this->assertEqual('right', $change->getCurrentPath()); $this->assertEqual('left', $change->getOldPath()); break; case 'basic-multi-hunk-content.svndiff': $this->assertEqual(1, count($changes)); $change = reset($changes); $hunks = $change->getHunks(); $this->assertEqual(2, count($hunks)); $there_is_a_literal_trailing_space_here = ' '; $corpus_0 = <<assertEqual( $corpus_0, $hunks[0]->getCorpus()); $this->assertEqual( $corpus_1, $hunks[1]->getCorpus()); break; case 'svn-ignore-whitespace-only.svndiff': $this->assertEqual(2, count($changes)); $hunks = reset($changes)->getHunks(); $this->assertEqual(0, count($hunks)); break; case 'svn-property-add.svndiff': $this->assertEqual(1, count($changes)); $change = reset($changes); $hunks = reset($changes)->getHunks(); $this->assertEqual(1, count($hunks)); $this->assertEqual( array( 'duck' => 'quack', ), $change->getNewProperties()); break; case 'svn-property-modify.svndiff': $this->assertEqual(2, count($changes)); $change = array_shift($changes); $this->assertEqual(0, count($change->getHunks())); $this->assertEqual( array( 'svn:ignore' => '*.phpz', ), $change->getOldProperties()); $this->assertEqual( array( 'svn:ignore' => '*.php', ), $change->getNewProperties()); $change = array_shift($changes); $this->assertEqual(0, count($change->getHunks())); $this->assertEqual( array( 'svn:special' => '*', ), $change->getOldProperties()); $this->assertEqual( array( 'svn:special' => 'moo', ), $change->getNewProperties()); break; case 'svn-property-delete.svndiff': $this->assertEqual(1, count($changes)); $change = reset($changes); $this->assertEqual(0, count($change->getHunks())); $this->assertEqual( $change->getOldProperties(), array( 'svn:special' => '*', )); $this->assertEqual( array( ), $change->getNewProperties()); break; case 'svn-property-merged.svndiff': $this->assertEqual(1, count($changes)); $change = reset($changes); $this->assertEqual(count($change->getHunks()), 0); $this->assertEqual( $change->getOldProperties(), array()); $this->assertEqual( $change->getNewProperties(), array()); break; case 'svn-property-merge.svndiff': $this->assertEqual(1, count($changes)); $change = reset($changes); $this->assertEqual(count($change->getHunks()), 0); $this->assertEqual( $change->getOldProperties(), array( )); $this->assertEqual( $change->getNewProperties(), array( 'svn:mergeinfo' => <<assertEqual(1, count($changes)); $change = reset($changes); $this->assertEqual(count($change->getHunks()), 0); $this->assertEqual( $change->getOldProperties(), array( )); $this->assertEqual( $change->getNewProperties(), array( 'svn:executable' => '*', )); break; case 'svn-binary-add.svndiff': $this->assertEqual(1, count($changes)); $change = reset($changes); $this->assertEqual( ArcanistDiffChangeType::FILE_BINARY, $change->getFileType()); $this->assertEqual(0, count($change->getHunks())); $this->assertEqual( array( 'svn:mime-type' => 'application/octet-stream', ), $change->getNewProperties()); break; case 'svn-binary-diff.svndiff': case 'svn-binary-diff-freebsd.svndiff': $this->assertEqual(1, count($changes)); $change = reset($changes); $this->assertEqual( ArcanistDiffChangeType::FILE_BINARY, $change->getFileType()); $this->assertEqual(count($change->getHunks()), 0); break; case 'git-delete-file.gitdiff': $this->assertEqual(1, count($changes)); $change = reset($changes); $this->assertEqual( ArcanistDiffChangeType::TYPE_DELETE, $change->getType()); $this->assertEqual( 'scripts/intern/test/testfile2', $change->getCurrentPath()); $this->assertEqual(1, count($change->getHunks())); break; case 'git-binary-change.gitdiff': $this->assertEqual(1, count($changes)); $change = reset($changes); $this->assertEqual( ArcanistDiffChangeType::FILE_BINARY, $change->getFileType()); $this->assertEqual(0, count($change->getHunks())); break; case 'git-filemode-change.gitdiff': $this->assertEqual(1, count($changes)); $change = reset($changes); $this->assertEqual(1, count($change->getHunks())); $this->assertEqual( array( 'unix:filemode' => '100644', ), $change->getOldProperties()); $this->assertEqual( array( 'unix:filemode' => '100755', ), $change->getNewProperties()); break; case 'git-filemode-change-only.gitdiff': $this->assertEqual(count($changes), 2); $change = reset($changes); $this->assertEqual(count($change->getHunks()), 0); $this->assertEqual( array( 'unix:filemode' => '100644', ), $change->getOldProperties()); $this->assertEqual( array( 'unix:filemode' => '100755', ), $change->getNewProperties()); break; case 'svn-empty-file.svndiff': $this->assertEqual(2, count($changes)); $change = array_shift($changes); $this->assertEqual(0, count($change->getHunks())); break; case 'git-ignore-whitespace-only.gitdiff': $this->assertEqual(count($changes), 2); $change = array_shift($changes); $this->assertEqual(count($change->getHunks()), 0); $this->assertEqual( $change->getOldPath(), 'scripts/intern/test/testfile2'); $this->assertEqual( $change->getCurrentPath(), 'scripts/intern/test/testfile2'); $change = array_shift($changes); $this->assertEqual(count($change->getHunks()), 1); $this->assertEqual( $change->getOldPath(), 'scripts/intern/test/testfile3'); $this->assertEqual( $change->getCurrentPath(), 'scripts/intern/test/testfile3'); break; case 'git-move.gitdiff': case 'git-move-edit.gitdiff': case 'git-move-plus.gitdiff': $extra_changeset = (bool)strpos($file, '-plus'); $has_hunk = (bool)strpos($file, '-edit'); $this->assertEqual($extra_changeset ? 3 : 2, count($changes)); $change = array_shift($changes); $this->assertEqual($has_hunk ? 1 : 0, count($change->getHunks())); $this->assertEqual( $change->getType(), ArcanistDiffChangeType::TYPE_MOVE_HERE); $target = $change; $change = array_shift($changes); $this->assertEqual(0, count($change->getHunks())); $this->assertEqual( ArcanistDiffChangeType::TYPE_MOVE_AWAY, $change->getType()); $this->assertEqual( $change->getCurrentPath(), $target->getOldPath()); $this->assertTrue( in_array($target->getCurrentPath(), $change->getAwayPaths())); break; case 'git-merge-header.gitdiff': $this->assertEqual(1, count($changes)); $change = reset($changes); $this->assertEqual( ArcanistDiffChangeType::TYPE_MESSAGE, $change->getType()); $this->assertEqual( '501f6d519703458471dbea6284ec5f49d1408598', $change->getCommitHash()); break; case 'git-new-file.gitdiff': $this->assertEqual(1, count($changes)); $change = reset($changes); $this->assertEqual( ArcanistDiffChangeType::TYPE_ADD, $change->getType()); break; case 'git-copy.gitdiff': $this->assertEqual(2, count($changes)); $change = array_shift($changes); $this->assertEqual(0, count($change->getHunks())); $this->assertEqual( ArcanistDiffChangeType::TYPE_COPY_HERE, $change->getType()); $this->assertEqual( 'flib/intern/widgets/ui/UIWidgetRSSBox.php', $change->getCurrentPath()); $change = array_shift($changes); $this->assertEqual(0, count($change->getHunks())); $this->assertEqual( ArcanistDiffChangeType::TYPE_COPY_AWAY, $change->getType()); $this->assertEqual( 'lib/display/intern/ui/widget/UIWidgetRSSBox.php', $change->getCurrentPath()); break; case 'git-copy-plus.gitdiff': $this->assertEqual(2, count($changes)); $change = array_shift($changes); $this->assertEqual(3, count($change->getHunks())); $this->assertEqual( ArcanistDiffChangeType::TYPE_COPY_HERE, $change->getType()); $this->assertEqual( 'flib/intern/widgets/ui/UIWidgetGraphConnect.php', $change->getCurrentPath()); $change = array_shift($changes); $this->assertEqual(0, count($change->getHunks())); $this->assertEqual( ArcanistDiffChangeType::TYPE_COPY_AWAY, $change->getType()); $this->assertEqual( 'lib/display/intern/ui/widget/UIWidgetLunchtime.php', $change->getCurrentPath()); break; case 'svn-property-multiline.svndiff': $this->assertEqual(1, count($changes)); $change = array_shift($changes); $this->assertEqual(0, count($change->getHunks())); $this->assertEqual( array( 'svn:ignore' => 'tags', ), $change->getOldProperties()); $this->assertEqual( array( 'svn:ignore' => "tags\nasdf\nlol\nwhat", ), $change->getNewProperties()); break; case 'git-empty-files.gitdiff': $this->assertEqual(2, count($changes)); while ($change = array_shift($changes)) { $this->assertEqual(0, count($change->getHunks())); } break; case 'git-mnemonicprefix.gitdiff': // Check parsing of diffs created with `diff.mnemonicprefix` // configuration option set to `true`. $this->assertEqual(1, count($changes)); $this->assertEqual(1, count(reset($changes)->getHunks())); break; case 'git-commit.gitdiff': case 'git-commit-logdecorate.gitdiff': $this->assertEqual(1, count($changes)); $change = reset($changes); $this->assertEqual( ArcanistDiffChangeType::TYPE_MESSAGE, $change->getType()); $this->assertEqual( '76e2f1339c298c748aa0b52030799ed202a6537b', $change->getCommitHash()); $this->assertEqual( <<. I tested most of these calls, but there were some that I didn't know how to reach, so if you are one of the owners of this code, please test your feature in my sandbox: www.ngao.devrs013.facebook.com @brosenthal, I removed some logic that was setting a disabled state on a UIActionButton, which is actually a no-op. Reviewed By: brosenthal Other Commenters: sparker, egiovanola Test Plan: www.ngao.devrs013.facebook.com Explicitly tested: * ads creation flow (add keyword) * ads manager (conversion tracking) * help center (create a discussion) * new user wizard (next step button) Revert: OK DiffCamp Revision: 94064 git-svn-id: svn+ssh://tubbs/svnroot/tfb/trunk/www@223593 2c7ba8d8 EOTEXT , $change->getMetadata('message')); break; case 'git-binary.gitdiff': $this->assertEqual(1, count($changes)); $change = reset($changes); $this->assertEqual( ArcanistDiffChangeType::TYPE_CHANGE, $change->getType()); $this->assertEqual( ArcanistDiffChangeType::FILE_BINARY, $change->getFileType()); break; case 'git-odd-filename.gitdiff': $this->assertEqual(2, count($changes)); $change = reset($changes); $this->assertEqual( 'old/'."\342\210\206".'.jpg', $change->getOldPath()); $this->assertEqual( 'new/'."\342\210\206".'.jpg', $change->getCurrentPath()); break; case 'hg-binary-change.hgdiff': case 'hg-solo-binary-change.hgdiff': $this->assertEqual(1, count($changes)); $change = reset($changes); $this->assertEqual( ArcanistDiffChangeType::TYPE_ADD, $change->getType()); $this->assertEqual( ArcanistDiffChangeType::FILE_BINARY, $change->getFileType()); break; case 'hg-binary-delete.hgdiff': $this->assertEqual(1, count($changes)); $change = reset($changes); $this->assertEqual( ArcanistDiffChangeType::TYPE_DELETE, $change->getType()); $this->assertEqual( ArcanistDiffChangeType::FILE_BINARY, $change->getFileType()); break; case 'git-replace-symlink.gitdiff': $this->assertEqual(1, count($changes)); $change = array_shift($changes); $this->assertEqual( ArcanistDiffChangeType::TYPE_CHANGE, $change->getType()); break; case 'svn-1.7-property-added.svndiff': $this->assertEqual(1, count($changes)); $change = head($changes); $new_properties = $change->getNewProperties(); $this->assertEqual(2, count($new_properties)); $this->assertEqual('*', idx($new_properties, 'svn:executable')); $this->assertEqual('text/html', idx($new_properties, 'svn:mime-type')); break; case 'hg-diff-range.hgdiff': $this->assertEqual(1, count($changes)); $change = array_shift($changes); $this->assertEqual( 'Test.java', $change->getOldPath()); $this->assertEqual( 'Test.java', $change->getCurrentPath()); break; case 'hg-patch.hgdiff': $this->assertEqual(1, count($changes)); break; case 'hg-patch-git.hgdiff': $this->assertEqual(1, count($changes)); break; case 'custom-prefixes.gitdiff': $this->assertEqual(1, count($changes)); $change = head($changes); $this->assertEqual( 'dst/file', $change->getCurrentPath()); break; case 'more-newlines.svndiff': $this->assertEqual(1, count($changes)); break; case 'suppress-blank-empty.gitdiff': $this->assertEqual(1, count($changes)); break; case 'svn-property-windows.svndiff': $this->assertEqual(1, count($changes)); break; case 'rcs-addline.rcsdiff': $this->assertEqual(1, count($changes)); $change = array_shift($changes); $this->assertEqual( ArcanistDiffChangeType::TYPE_CHANGE, $change->getType()); break; case 'rcs-deleteline.rcsdiff': $this->assertEqual(1, count($changes)); $change = array_shift($changes); $this->assertEqual( ArcanistDiffChangeType::TYPE_CHANGE, $change->getType()); break; case 'comment.svndiff': $this->assertEqual(1, count($changes)); $change = array_shift($changes); $this->assertEqual( ArcanistDiffChangeType::TYPE_CHANGE, $change->getType()); break; case 'svnlook-basics.svndiff': case 'svnlook-add.svndiff': case 'svnlook-delete.svndiff': case 'svnlook-copied.svndiff': $this->assertEqual(1, count($changes)); break; case 'git-format-patch.gitdiff': $this->assertEqual(2, count($changes)); $change = array_shift($changes); $this->assertEqual( ArcanistDiffChangeType::TYPE_MESSAGE, $change->getType()); $this->assertEqual('WIP', $change->getMetadata('message')); $change = array_shift($changes); $this->assertEqual( ArcanistDiffChangeType::TYPE_CHANGE, $change->getType()); break; case 'svn-double-diff.svndiff': $this->assertEqual(1, count($changes)); $change = array_shift($changes); $hunks = $change->getHunks(); $this->assertEqual(1, count($hunks)); break; case 'git-remove-spaces.gitdiff': $this->assertEqual(1, count($changes)); $change = array_shift($changes); $this->assertEqual('file with spaces.txt', $change->getOldPath()); break; default: - throw new Exception("No test block for diff file {$diff_file}."); + throw new Exception(pht('No test block for diff file %s.', $diff_file)); break; } } public function testGitPrefixStripping() { static $tests = array( 'a/file.c' => 'file.c', 'b/file.c' => 'file.c', 'i/file.c' => 'file.c', 'c/file.c' => 'file.c', 'w/file.c' => 'file.c', 'o/file.c' => 'file.c', '1/file.c' => 'file.c', '2/file.c' => 'file.c', 'src/file.c' => 'src/file.c', 'file.c' => 'file.c', ); foreach ($tests as $input => $expect) { $this->assertEqual( $expect, ArcanistDiffParser::stripGitPathPrefix($input), - "Strip git prefix from '{$input}'."); + pht("Strip git prefix from '%s'.", $input)); } } public function testGitPathSplitting() { static $tests = array( 'a/old.c b/new.c' => array('old.c', 'new.c'), "a/old.c b/new.c\n" => array('old.c', 'new.c'), "a/old.c b/new.c\r\n" => array('old.c', 'new.c'), 'old.c new.c' => array('old.c', 'new.c'), '1/old.c 2/new.c' => array('old.c', 'new.c'), '"a/\\"quotes1\\"" "b/\\"quotes2\\""' => array( '"quotes1"', '"quotes2"', ), '"a/\\"quotes and spaces1\\"" "b/\\"quotes and spaces2\\""' => array( '"quotes and spaces1"', '"quotes and spaces2"', ), '"a/\\342\\230\\2031" "b/\\342\\230\\2032"' => array( "\xE2\x98\x831", "\xE2\x98\x832", ), 'a/Core Data/old.c b/Core Data/new.c' => array( 'Core Data/old.c', 'Core Data/new.c', ), 'some file with spaces.c some file with spaces.c' => array( 'some file with spaces.c', 'some file with spaces.c', ), ); foreach ($tests as $input => $expect) { $result = ArcanistDiffParser::splitGitDiffPaths($input); $this->assertEqual( $expect, $result, - "Split: {$input}"); + pht('Split: %s', $input)); } static $ambiguous = array( 'old file with spaces.c new file with spaces.c', ); foreach ($ambiguous as $input) { $caught = null; try { ArcanistDiffParser::splitGitDiffPaths($input); } catch (Exception $ex) { $caught = $ex; } $this->assertTrue( ($caught instanceof Exception), - "Ambiguous: {$input}"); + pht('Ambiguous: %s', $input)); } } } diff --git a/src/parser/diff/ArcanistDiffChange.php b/src/parser/diff/ArcanistDiffChange.php index c5e8de7e..06f68b6c 100644 --- a/src/parser/diff/ArcanistDiffChange.php +++ b/src/parser/diff/ArcanistDiffChange.php @@ -1,316 +1,316 @@ originalFileData = $original_file_data; return $this; } public function getOriginalFileData() { return $this->originalFileData; } public function setCurrentFileData($current_file_data) { $this->currentFileData = $current_file_data; return $this; } public function getCurrentFileData() { return $this->currentFileData; } public function toDictionary() { $hunks = array(); foreach ($this->hunks as $hunk) { $hunks[] = $hunk->toDictionary(); } return array( 'metadata' => $this->metadata, 'oldPath' => $this->oldPath, 'currentPath' => $this->currentPath, 'awayPaths' => $this->awayPaths, 'oldProperties' => $this->oldProperties, 'newProperties' => $this->newProperties, 'type' => $this->type, 'fileType' => $this->fileType, 'commitHash' => $this->commitHash, 'hunks' => $hunks, ); } public static function newFromDictionary(array $dict) { $hunks = array(); foreach ($dict['hunks'] as $hunk) { $hunks[] = ArcanistDiffHunk::newFromDictionary($hunk); } $obj = new ArcanistDiffChange(); $obj->metadata = $dict['metadata']; $obj->oldPath = $dict['oldPath']; $obj->currentPath = $dict['currentPath']; // TODO: The backend is shipping down some bogus data, e.g. diff 199453. // Should probably clean this up. $obj->awayPaths = nonempty($dict['awayPaths'], array()); $obj->oldProperties = nonempty($dict['oldProperties'], array()); $obj->newProperties = nonempty($dict['newProperties'], array()); $obj->type = $dict['type']; $obj->fileType = $dict['fileType']; $obj->commitHash = $dict['commitHash']; $obj->hunks = $hunks; return $obj; } public static function newFromConduit(array $dicts) { $changes = array(); foreach ($dicts as $dict) { $changes[] = self::newFromDictionary($dict); } return $changes; } public function getChangedLines($type) { $lines = array(); foreach ($this->hunks as $hunk) { $lines += $hunk->getChangedLines($type); } return $lines; } public function getAllMetadata() { return $this->metadata; } public function setMetadata($key, $value) { $this->metadata[$key] = $value; return $this; } public function getMetadata($key) { return idx($this->metadata, $key); } public function setCommitHash($hash) { $this->commitHash = $hash; return $this; } public function getCommitHash() { return $this->commitHash; } public function addAwayPath($path) { $this->awayPaths[] = $path; return $this; } public function getAwayPaths() { return $this->awayPaths; } public function setFileType($type) { $this->fileType = $type; return $this; } public function getFileType() { return $this->fileType; } public function setType($type) { $this->type = $type; return $this; } public function getType() { return $this->type; } public function setOldProperty($key, $value) { $this->oldProperties[$key] = $value; return $this; } public function setNewProperty($key, $value) { $this->newProperties[$key] = $value; return $this; } public function getOldProperties() { return $this->oldProperties; } public function getNewProperties() { return $this->newProperties; } public function setCurrentPath($path) { $this->currentPath = $this->filterPath($path); return $this; } public function getCurrentPath() { return $this->currentPath; } public function setOldPath($path) { $this->oldPath = $this->filterPath($path); return $this; } public function getOldPath() { return $this->oldPath; } public function addHunk(ArcanistDiffHunk $hunk) { $this->hunks[] = $hunk; return $this; } public function dropHunks() { $this->hunks = array(); return $this; } public function getHunks() { return $this->hunks; } /** * @return array $old => array($new, ) */ public function buildLineMap() { $line_map = array(); $old = 1; $new = 1; foreach ($this->getHunks() as $hunk) { for ($n = $old; $n < $hunk->getOldOffset(); $n++) { $line_map[$n] = array($n + $new - $old); } $old = $hunk->getOldOffset(); $new = $hunk->getNewOffset(); $olds = array(); $news = array(); $lines = explode("\n", $hunk->getCorpus()); foreach ($lines as $line) { $type = substr($line, 0, 1); if ($type == '-' || $type == ' ') { $olds[] = $old; $old++; } if ($type == '+' || $type == ' ') { $news[] = $new; $new++; } if ($type == ' ' || $type == '') { $line_map += array_fill_keys($olds, $news); $olds = array(); $news = array(); } } } return $line_map; } public function convertToBinaryChange(ArcanistRepositoryAPI $api) { // Fill in the binary data from the working copy. $this->setOriginalFileData( $api->getOriginalFileData( $this->getOldPath())); $this->setCurrentFileData( $api->getCurrentFileData( $this->getCurrentPath())); $this->hunks = array(); $this->setFileType(ArcanistDiffChangeType::FILE_BINARY); return $this; } protected function filterPath($path) { if ($path == '/dev/null') { return null; } return $path; } public function renderTextSummary() { $type = $this->getType(); $file = $this->getFileType(); $char = ArcanistDiffChangeType::getSummaryCharacterForChangeType($type); $attr = ArcanistDiffChangeType::getShortNameForFileType($file); if ($attr) { $attr = '('.$attr.')'; } $summary = array(); $summary[] = sprintf( '%s %5.5s %s', $char, $attr, $this->getCurrentPath()); if (ArcanistDiffChangeType::isOldLocationChangeType($type)) { foreach ($this->getAwayPaths() as $path) { $summary[] = ' to: '.$path; } } if (ArcanistDiffChangeType::isNewLocationChangeType($type)) { $summary[] = ' from: '.$this->getOldPath(); } return implode("\n", $summary); } public function getSymlinkTarget() { if ($this->getFileType() != ArcanistDiffChangeType::FILE_SYMLINK) { - throw new Exception('Not a symlink!'); + throw new Exception(pht('Not a symlink!')); } $hunks = $this->getHunks(); $hunk = reset($hunks); $corpus = $hunk->getCorpus(); $match = null; if (!preg_match('/^\+(?:link )?(.*)$/m', $corpus, $match)) { - throw new Exception('Failed to extract link target!'); + throw new Exception(pht('Failed to extract link target!')); } return trim($match[1]); } public function setNeedsSyntheticGitHunks($needs_synthetic_git_hunks) { $this->needsSyntheticGitHunks = $needs_synthetic_git_hunks; return $this; } public function getNeedsSyntheticGitHunks() { return $this->needsSyntheticGitHunks; } } diff --git a/src/parser/diff/ArcanistDiffChangeType.php b/src/parser/diff/ArcanistDiffChangeType.php index ef84985a..36ea5a5c 100644 --- a/src/parser/diff/ArcanistDiffChangeType.php +++ b/src/parser/diff/ArcanistDiffChangeType.php @@ -1,112 +1,117 @@ 'A', self::TYPE_CHANGE => 'M', self::TYPE_DELETE => 'D', self::TYPE_MOVE_AWAY => 'V', self::TYPE_COPY_AWAY => 'P', self::TYPE_MOVE_HERE => 'V', self::TYPE_COPY_HERE => 'P', self::TYPE_MULTICOPY => 'P', self::TYPE_MESSAGE => 'Q', self::TYPE_CHILD => '@', ); return idx($types, coalesce($type, '?'), '~'); } public static function getShortNameForFileType($type) { static $names = array( self::FILE_TEXT => null, self::FILE_DIRECTORY => 'dir', self::FILE_IMAGE => 'img', self::FILE_BINARY => 'bin', self::FILE_SYMLINK => 'sym', ); return idx($names, coalesce($type, '?'), '???'); } public static function isOldLocationChangeType($type) { static $types = array( self::TYPE_MOVE_AWAY => true, self::TYPE_COPY_AWAY => true, self::TYPE_MULTICOPY => true, ); return isset($types[$type]); } public static function isNewLocationChangeType($type) { static $types = array( self::TYPE_MOVE_HERE => true, self::TYPE_COPY_HERE => true, ); return isset($types[$type]); } public static function isDeleteChangeType($type) { static $types = array( self::TYPE_DELETE => true, self::TYPE_MOVE_AWAY => true, self::TYPE_MULTICOPY => true, ); return isset($types[$type]); } public static function isCreateChangeType($type) { static $types = array( self::TYPE_ADD => true, self::TYPE_COPY_HERE => true, self::TYPE_MOVE_HERE => true, ); return isset($types[$type]); } public static function isModifyChangeType($type) { static $types = array( self::TYPE_CHANGE => true, ); return isset($types[$type]); } public static function getFullNameForChangeType($type) { - static $types = array( - self::TYPE_ADD => 'Added', - self::TYPE_CHANGE => 'Modified', - self::TYPE_DELETE => 'Deleted', - self::TYPE_MOVE_AWAY => 'Moved Away', - self::TYPE_COPY_AWAY => 'Copied Away', - self::TYPE_MOVE_HERE => 'Moved Here', - self::TYPE_COPY_HERE => 'Copied Here', - self::TYPE_MULTICOPY => 'Deleted After Multiple Copy', - self::TYPE_MESSAGE => 'Commit Message', - self::TYPE_CHILD => 'Contents Modified', - ); - return idx($types, coalesce($type, '?'), 'Unknown'); + static $types = null; + + if ($types === null) { + $types = array( + self::TYPE_ADD => pht('Added'), + self::TYPE_CHANGE => pht('Modified'), + self::TYPE_DELETE => pht('Deleted'), + self::TYPE_MOVE_AWAY => pht('Moved Away'), + self::TYPE_COPY_AWAY => pht('Copied Away'), + self::TYPE_MOVE_HERE => pht('Moved Here'), + self::TYPE_COPY_HERE => pht('Copied Here'), + self::TYPE_MULTICOPY => pht('Deleted After Multiple Copy'), + self::TYPE_MESSAGE => pht('Commit Message'), + self::TYPE_CHILD => pht('Contents Modified'), + ); + } + + return idx($types, coalesce($type, '?'), pht('Unknown')); } } diff --git a/src/parser/diff/ArcanistDiffHunk.php b/src/parser/diff/ArcanistDiffHunk.php index f47dd242..cfd6d998 100644 --- a/src/parser/diff/ArcanistDiffHunk.php +++ b/src/parser/diff/ArcanistDiffHunk.php @@ -1,171 +1,171 @@ $this->oldOffset, 'newOffset' => $this->newOffset, 'oldLength' => $this->oldLength, 'newLength' => $this->newLength, 'addLines' => $this->addLines, 'delLines' => $this->delLines, 'isMissingOldNewline' => $this->isMissingOldNewline, 'isMissingNewNewline' => $this->isMissingNewNewline, 'corpus' => (string)$this->corpus, ); } public static function newFromDictionary(array $dict) { $obj = new ArcanistDiffHunk(); $obj->oldOffset = $dict['oldOffset']; $obj->newOffset = $dict['newOffset']; $obj->oldLength = $dict['oldLength']; $obj->newLength = $dict['newLength']; $obj->addLines = $dict['addLines']; $obj->delLines = $dict['delLines']; $obj->isMissingOldNewline = $dict['isMissingOldNewline']; $obj->isMissingNewNewline = $dict['isMissingNewNewline']; $obj->corpus = $dict['corpus']; return $obj; } public function getChangedLines($type) { $old_map = array(); $new_map = array(); $cover_map = array(); $oline = $this->getOldOffset(); $nline = $this->getNewOffset(); foreach (explode("\n", $this->getCorpus()) as $line) { $char = strlen($line) ? $line[0] : '~'; switch ($char) { case '-': $old_map[$oline] = true; $cover_map[$oline] = true; ++$oline; break; case '+': $new_map[$nline] = true; if ($oline > 1) { $cover_map[$oline - 1] = true; } $cover_map[$oline] = true; ++$nline; break; default: ++$oline; ++$nline; break; } } switch ($type) { case 'new': return $new_map; case 'old': return $old_map; case 'cover': return $cover_map; default: - throw new Exception("Unknown line change type '{$type}'."); + throw new Exception(pht("Unknown line change type '%s'.", $type)); } } public function setOldOffset($old_offset) { $this->oldOffset = $old_offset; return $this; } public function getOldOffset() { return $this->oldOffset; } public function setNewOffset($new_offset) { $this->newOffset = $new_offset; return $this; } public function getNewOffset() { return $this->newOffset; } public function setOldLength($old_length) { $this->oldLength = $old_length; return $this; } public function getOldLength() { return $this->oldLength; } public function setNewLength($new_length) { $this->newLength = $new_length; return $this; } public function getNewLength() { return $this->newLength; } public function setAddLines($add_lines) { $this->addLines = $add_lines; return $this; } public function getAddLines() { return $this->addLines; } public function setDelLines($del_lines) { $this->delLines = $del_lines; return $this; } public function getDelLines() { return $this->delLines; } public function setCorpus($corpus) { $this->corpus = $corpus; return $this; } public function getCorpus() { return $this->corpus; } public function setIsMissingOldNewline($missing) { $this->isMissingOldNewline = (bool)$missing; return $this; } public function getIsMissingOldNewline() { return $this->isMissingOldNewline; } public function setIsMissingNewNewline($missing) { $this->isMissingNewNewline = (bool)$missing; return $this; } public function getIsMissingNewNewline() { return $this->isMissingNewNewline; } } diff --git a/src/repository/api/ArcanistGitAPI.php b/src/repository/api/ArcanistGitAPI.php index 5377ebaf..7b0bd64b 100644 --- a/src/repository/api/ArcanistGitAPI.php +++ b/src/repository/api/ArcanistGitAPI.php @@ -1,1206 +1,1254 @@ setCWD($this->getPath()); return $future; } public function execPassthru($pattern /* , ... */) { $args = func_get_args(); static $git = null; if ($git === null) { if (phutil_is_windows()) { // NOTE: On Windows, phutil_passthru() uses 'bypass_shell' because // everything goes to hell if we don't. We must provide an absolute // path to Git for this to work properly. $git = Filesystem::resolveBinary('git'); $git = csprintf('%s', $git); } else { $git = 'git'; } } $args[0] = $git.' '.$args[0]; return call_user_func_array('phutil_passthru', $args); } public function getSourceControlSystemName() { return 'git'; } public function getMetadataPath() { static $path = null; if ($path === null) { list($stdout) = $this->execxLocal('rev-parse --git-dir'); $path = rtrim($stdout, "\n"); // the output of git rev-parse --git-dir is an absolute path, unless // the cwd is the root of the repository, in which case it uses the // relative path of .git. If we get this relative path, turn it into // an absolute path. if ($path === '.git') { $path = $this->getPath('.git'); } } return $path; } public function getHasCommits() { return !$this->repositoryHasNoCommits; } /** * Tests if a child commit is descendant of a parent commit. * If child and parent are the same, it returns false. * @param Child commit SHA. * @param Parent commit SHA. * @return bool True if the child is a descendant of the parent. */ private function isDescendant($child, $parent) { list($common_ancestor) = $this->execxLocal( 'merge-base %s %s', $child, $parent); $common_ancestor = trim($common_ancestor); return ($common_ancestor == $parent) && ($common_ancestor != $child); } public function getLocalCommitInformation() { if ($this->repositoryHasNoCommits) { // Zero commits. throw new Exception( - "You can't get local commit information for a repository with no ". - "commits."); + pht( + "You can't get local commit information for a repository with no ". + "commits.")); } else if ($this->getBaseCommit() == self::GIT_MAGIC_ROOT_COMMIT) { // One commit. $against = 'HEAD'; } else { // 2..N commits. We include commits reachable from HEAD which are // not reachable from the base commit; this is consistent with user // expectations even though it is not actually the diff range. // Particularly: // // | // D <----- master branch // | // C Y <- feature branch // | /| // B X // | / // A // | // // If "A, B, C, D" are master, and the user is at Y, when they run // "arc diff B" they want (and get) a diff of B vs Y, but they think about // this as being the commits X and Y. If we log "B..Y", we only show // Y. With "Y --not B", we show X and Y. if ($this->symbolicHeadCommit !== null) { $base_commit = $this->getBaseCommit(); $resolved_base = $this->resolveCommit($base_commit); $head_commit = $this->symbolicHeadCommit; $resolved_head = $this->getHeadCommit(); if (!$this->isDescendant($resolved_head, $resolved_base)) { // NOTE: Since the base commit will have been resolved as the // merge-base of the specified base and the specified HEAD, we can't // easily tell exactly what's wrong with the range. // For example, `arc diff HEAD --head HEAD^^^` is invalid because it // is reversed, but resolving the commit "HEAD" will compute its // merge-base with "HEAD^^^", which is "HEAD^^^", so the range will // appear empty. throw new ArcanistUsageException( pht( 'The specified commit range is empty, backward or invalid: the '. 'base (%s) is not an ancestor of the head (%s). You can not '. 'diff an empty or reversed commit range.', $base_commit, $head_commit)); } } $against = csprintf( '%s --not %s', $this->getHeadCommit(), $this->getBaseCommit()); } // NOTE: Windows escaping of "%" symbols apparently is inherently broken; // when passed through escapeshellarg() they are replaced with spaces. // TODO: Learn how cmd.exe works and find some clever workaround? // NOTE: If we use "%x00", output is truncated in Windows. list($info) = $this->execxLocal( phutil_is_windows() ? 'log %C --format=%C --' : 'log %C --format=%s --', $against, // NOTE: "%B" is somewhat new, use "%s%n%n%b" instead. '%H%x01%T%x01%P%x01%at%x01%an%x01%aE%x01%s%x01%s%n%n%b%x02'); $commits = array(); $info = trim($info, " \n\2"); if (!strlen($info)) { return array(); } $info = explode("\2", $info); foreach ($info as $line) { list($commit, $tree, $parents, $time, $author, $author_email, $title, $message) = explode("\1", trim($line), 8); $message = rtrim($message); $commits[$commit] = array( 'commit' => $commit, 'tree' => $tree, 'parents' => array_filter(explode(' ', $parents)), 'time' => $time, 'author' => $author, 'summary' => $title, 'message' => $message, 'authorEmail' => $author_email, ); } return $commits; } protected function buildBaseCommit($symbolic_commit) { if ($symbolic_commit !== null) { if ($symbolic_commit == self::GIT_MAGIC_ROOT_COMMIT) { $this->setBaseCommitExplanation( - 'you explicitly specified the empty tree.'); + pht('you explicitly specified the empty tree.')); return $symbolic_commit; } list($err, $merge_base) = $this->execManualLocal( 'merge-base %s %s', $symbolic_commit, $this->getHeadCommit()); if ($err) { throw new ArcanistUsageException( - "Unable to find any git commit named '{$symbolic_commit}' in ". - "this repository."); + pht( + "Unable to find any git commit named '%s' in this repository.", + $symbolic_commit)); } if ($this->symbolicHeadCommit === null) { $this->setBaseCommitExplanation( - "it is the merge-base of the explicitly specified base commit ". - "'{$symbolic_commit}' and HEAD."); + pht( + "it is the merge-base of the explicitly specified base commit ". + "'%s' and HEAD.", + $symbolic_commit)); } else { $this->setBaseCommitExplanation( - "it is the merge-base of the explicitly specified base commit ". - "'{$symbolic_commit}' and the explicitly specified head ". - "commit '{$this->symbolicHeadCommit}'."); + pht( + "it is the merge-base of the explicitly specified base commit ". + "'%s' and the explicitly specified head commit '%s'.", + $symbolic_commit, + $this->symbolicHeadCommit)); } return trim($merge_base); } // Detect zero-commit or one-commit repositories. There is only one // relative-commit value that makes any sense in these repositories: the // empty tree. list($err) = $this->execManualLocal('rev-parse --verify HEAD^'); if ($err) { list($err) = $this->execManualLocal('rev-parse --verify HEAD'); if ($err) { $this->repositoryHasNoCommits = true; } if ($this->repositoryHasNoCommits) { - $this->setBaseCommitExplanation( - 'the repository has no commits.'); + $this->setBaseCommitExplanation(pht('the repository has no commits.')); } else { $this->setBaseCommitExplanation( - 'the repository has only one commit.'); + pht('the repository has only one commit.')); } return self::GIT_MAGIC_ROOT_COMMIT; } if ($this->getBaseCommitArgumentRules() || $this->getConfigurationManager()->getConfigFromAnySource('base')) { $base = $this->resolveBaseCommit(); if (!$base) { throw new ArcanistUsageException( - "None of the rules in your 'base' configuration matched a valid ". - "commit. Adjust rules or specify which commit you want to use ". - "explicitly."); + pht( + "None of the rules in your 'base' configuration matched a valid ". + "commit. Adjust rules or specify which commit you want to use ". + "explicitly.")); } return $base; } $do_write = false; $default_relative = null; $working_copy = $this->getWorkingCopyIdentity(); if ($working_copy) { $default_relative = $working_copy->getProjectConfig( 'git.default-relative-commit'); $this->setBaseCommitExplanation( - "it is the merge-base of '{$default_relative}' and HEAD, as ". - "specified in 'git.default-relative-commit' in '.arcconfig'. This ". - "setting overrides other settings."); + pht( + "it is the merge-base of '%s' and HEAD, as specified in '%s' in ". + "'%s'. This setting overrides other settings.", + $default_relative, + 'git.default-relative-commit', + '.arcconfig')); } if (!$default_relative) { list($err, $upstream) = $this->execManualLocal( 'rev-parse --abbrev-ref --symbolic-full-name %s', '@{upstream}'); if (!$err) { $default_relative = trim($upstream); $this->setBaseCommitExplanation( - "it is the merge-base of '{$default_relative}' (the Git upstream ". - "of the current branch) HEAD."); + pht( + "it is the merge-base of '%s' (the Git upstream ". + "of the current branch) HEAD.", + $default_relative)); } } if (!$default_relative) { $default_relative = $this->readScratchFile('default-relative-commit'); $default_relative = trim($default_relative); if ($default_relative) { $this->setBaseCommitExplanation( - "it is the merge-base of '{$default_relative}' and HEAD, as ". - "specified in '.git/arc/default-relative-commit'."); + pht( + "it is the merge-base of '%s' and HEAD, as specified in '%s'.", + $default_relative, + '.git/arc/default-relative-commit')); } } if (!$default_relative) { // TODO: Remove the history lesson soon. echo phutil_console_format( "** Select a Default Commit Range **\n\n"); echo phutil_console_wrap( - "You're running a command which operates on a range of revisions ". - "(usually, from some revision to HEAD) but have not specified the ". - "revision that should determine the start of the range.\n\n". - "Previously, arc assumed you meant 'HEAD^' when you did not specify ". - "a start revision, but this behavior does not make much sense in ". - "most workflows outside of Facebook's historic git-svn workflow.\n\n". - "arc no longer assumes 'HEAD^'. You must specify a relative commit ". - "explicitly when you invoke a command (e.g., `arc diff HEAD^`, not ". - "just `arc diff`) or select a default for this working copy.\n\n". - "In most cases, the best default is 'origin/master'. You can also ". - "select 'HEAD^' to preserve the old behavior, or some other remote ". - "or branch. But you almost certainly want to select ". - "'origin/master'.\n\n". - "(Technically: the merge-base of the selected revision and HEAD is ". - "used to determine the start of the commit range.)"); - - $prompt = 'What default do you want to use? [origin/master]'; + pht( + "You're running a command which operates on a range of revisions ". + "(usually, from some revision to HEAD) but have not specified the ". + "revision that should determine the start of the range.\n\n". + "Previously, arc assumed you meant '%s' when you did not specify ". + "a start revision, but this behavior does not make much sense in ". + "most workflows outside of Facebook's historic %s workflow.\n\n". + "arc no longer assumes '%s'. You must specify a relative commit ". + "explicitly when you invoke a command (e.g., `%s`, not just `%s`) ". + "or select a default for this working copy.\n\nIn most cases, the ". + "best default is '%s'. You can also select '%s' to preserve the ". + "old behavior, or some other remote or branch. But you almost ". + "certainly want to select 'origin/master'.\n\n". + "(Technically: the merge-base of the selected revision and HEAD is ". + "used to determine the start of the commit range.)", + 'HEAD^', + 'git-svn', + 'HEAD^', + 'arc diff HEAD^', + 'arc diff', + 'origin/master', + 'HEAD^')); + + $prompt = pht('What default do you want to use? [origin/master]'); $default = phutil_console_prompt($prompt); if (!strlen(trim($default))) { $default = 'origin/master'; } $default_relative = $default; $do_write = true; } list($object_type) = $this->execxLocal( 'cat-file -t %s', $default_relative); if (trim($object_type) !== 'commit') { throw new Exception( - "Relative commit '{$default_relative}' is not the name of a commit!"); + pht( + "Relative commit '%s' is not the name of a commit!", + $default_relative)); } if ($do_write) { // Don't perform this write until we've verified that the object is a // valid commit name. $this->writeScratchFile('default-relative-commit', $default_relative); $this->setBaseCommitExplanation( - "it is the merge-base of '{$default_relative}' and HEAD, as you ". - "just specified."); + pht( + "it is the merge-base of '%s' and HEAD, as you just specified.", + $default_relative)); } list($merge_base) = $this->execxLocal( 'merge-base %s HEAD', $default_relative); return trim($merge_base); } public function getHeadCommit() { if ($this->resolvedHeadCommit === null) { $this->resolvedHeadCommit = $this->resolveCommit( coalesce($this->symbolicHeadCommit, 'HEAD')); } return $this->resolvedHeadCommit; } public function setHeadCommit($symbolic_commit) { $this->symbolicHeadCommit = $symbolic_commit; $this->reloadCommitRange(); return $this; } /** * Translates a symbolic commit (like "HEAD^") to a commit identifier. * @param string_symbol commit. * @return string the commit SHA. */ private function resolveCommit($symbolic_commit) { list($err, $commit_hash) = $this->execManualLocal( 'rev-parse %s', $symbolic_commit); if ($err) { throw new ArcanistUsageException( - "Unable to find any git commit named '{$symbolic_commit}' in ". - "this repository."); + pht( + "Unable to find any git commit named '%s' in this repository.", + $symbolic_commit)); } return trim($commit_hash); } private function getDiffFullOptions($detect_moves_and_renames = true) { $options = array( self::getDiffBaseOptions(), '--no-color', '--src-prefix=a/', '--dst-prefix=b/', '-U'.$this->getDiffLinesOfContext(), ); if ($detect_moves_and_renames) { $options[] = '-M'; $options[] = '-C'; } return implode(' ', $options); } private function getDiffBaseOptions() { $options = array( // Disable external diff drivers, like graphical differs, since Arcanist // needs to capture the diff text. '--no-ext-diff', // Disable textconv so we treat binary files as binary, even if they have // an alternative textual representation. TODO: Ideally, Differential // would ship up the binaries for 'arc patch' but display the textconv // output in the visual diff. '--no-textconv', ); return implode(' ', $options); } /** * @param the base revision * @param head revision. If this is null, the generated diff will include the * working copy */ public function getFullGitDiff($base, $head = null) { $options = $this->getDiffFullOptions(); if ($head !== null) { list($stdout) = $this->execxLocal( "diff {$options} %s %s --", $base, $head); } else { list($stdout) = $this->execxLocal( "diff {$options} %s --", $base); } return $stdout; } /** * @param string Path to generate a diff for. * @param bool If true, detect moves and renames. Otherwise, ignore * moves/renames; this is useful because it prompts git to * generate real diff text. */ public function getRawDiffText($path, $detect_moves_and_renames = true) { $options = $this->getDiffFullOptions($detect_moves_and_renames); list($stdout) = $this->execxLocal( "diff {$options} %s -- %s", $this->getBaseCommit(), $path); return $stdout; } public function getBranchName() { // TODO: consider: // // $ git rev-parse --abbrev-ref `git symbolic-ref HEAD` // // But that may fail if you're not on a branch. list($stdout) = $this->execxLocal('branch --no-color'); // Assume that any branch beginning with '(' means 'no branch', or whatever // 'no branch' is in the current locale. $matches = null; if (preg_match('/^\* ([^\(].*)$/m', $stdout, $matches)) { return $matches[1]; } return null; } public function getRemoteURI() { list($stdout) = $this->execxLocal('remote show -n origin'); $matches = null; if (preg_match('/^\s*Fetch URL: (.*)$/m', $stdout, $matches)) { return trim($matches[1]); } return null; } public function getSourceControlPath() { // TODO: Try to get something useful here. return null; } public function getGitCommitLog() { $relative = $this->getBaseCommit(); if ($this->repositoryHasNoCommits) { // No commits yet. return ''; } else if ($relative == self::GIT_MAGIC_ROOT_COMMIT) { // First commit. list($stdout) = $this->execxLocal( 'log --format=medium HEAD'); } else { // 2..N commits. list($stdout) = $this->execxLocal( 'log --first-parent --format=medium %s..%s', $this->getBaseCommit(), $this->getHeadCommit()); } return $stdout; } public function getGitHistoryLog() { list($stdout) = $this->execxLocal( 'log --format=medium -n%d %s', self::SEARCH_LENGTH_FOR_PARENT_REVISIONS, $this->getBaseCommit()); return $stdout; } public function getSourceControlBaseRevision() { list($stdout) = $this->execxLocal( 'rev-parse %s', $this->getBaseCommit()); return rtrim($stdout, "\n"); } public function getCanonicalRevisionName($string) { $match = null; if (preg_match('/@([0-9]+)$/', $string, $match)) { $stdout = $this->getHashFromFromSVNRevisionNumber($match[1]); } else { list($stdout) = $this->execxLocal( phutil_is_windows() ? 'show -s --format=%C %s --' : 'show -s --format=%s %s --', '%H', $string); } return rtrim($stdout); } private function executeSVNFindRev($input, $vcs) { $match = array(); list($stdout) = $this->execxLocal( 'svn find-rev %s', $input); if (!$stdout) { throw new ArcanistUsageException( - "Cannot find the {$vcs} equivalent of {$input}."); + pht( + 'Cannot find the %s equivalent of %s.', + $vcs, + $input)); } // When git performs a partial-rebuild during svn // look-up, we need to parse the final line $lines = explode("\n", $stdout); $stdout = $lines[count($lines) - 2]; return rtrim($stdout); } // Convert svn revision number to git hash public function getHashFromFromSVNRevisionNumber($revision_id) { return $this->executeSVNFindRev('r'.$revision_id, 'Git'); } // Convert a git hash to svn revision number public function getSVNRevisionNumberFromHash($hash) { return $this->executeSVNFindRev($hash, 'SVN'); } protected function buildUncommittedStatus() { $diff_options = $this->getDiffBaseOptions(); if ($this->repositoryHasNoCommits) { $diff_base = self::GIT_MAGIC_ROOT_COMMIT; } else { $diff_base = 'HEAD'; } // Find uncommitted changes. $uncommitted_future = $this->buildLocalFuture( array( 'diff %C --raw %s --', $diff_options, $diff_base, )); $untracked_future = $this->buildLocalFuture( array( 'ls-files --others --exclude-standard', )); // Unstaged changes $unstaged_future = $this->buildLocalFuture( array( 'diff-files --name-only', )); $futures = array( $uncommitted_future, $untracked_future, // NOTE: `git diff-files` races with each of these other commands // internally, and resolves with inconsistent results if executed // in parallel. To work around this, DO NOT run it at the same time. // After the other commands exit, we can start the `diff-files` command. ); id(new FutureIterator($futures))->resolveAll(); // We're clear to start the `git diff-files` now. $unstaged_future->start(); $result = new PhutilArrayWithDefaultValue(); list($stdout) = $uncommitted_future->resolvex(); $uncommitted_files = $this->parseGitStatus($stdout); foreach ($uncommitted_files as $path => $mask) { $result[$path] |= ($mask | self::FLAG_UNCOMMITTED); } list($stdout) = $untracked_future->resolvex(); $stdout = rtrim($stdout, "\n"); if (strlen($stdout)) { $stdout = explode("\n", $stdout); foreach ($stdout as $path) { $result[$path] |= self::FLAG_UNTRACKED; } } list($stdout, $stderr) = $unstaged_future->resolvex(); $stdout = rtrim($stdout, "\n"); if (strlen($stdout)) { $stdout = explode("\n", $stdout); foreach ($stdout as $path) { $result[$path] |= self::FLAG_UNSTAGED; } } return $result->toArray(); } protected function buildCommitRangeStatus() { list($stdout, $stderr) = $this->execxLocal( 'diff %C --raw %s --', $this->getDiffBaseOptions(), $this->getBaseCommit()); return $this->parseGitStatus($stdout); } public function getGitConfig($key, $default = null) { list($err, $stdout) = $this->execManualLocal('config %s', $key); if ($err) { return $default; } return rtrim($stdout); } public function getAuthor() { list($stdout) = $this->execxLocal('var GIT_AUTHOR_IDENT'); return preg_replace('/\s+<.*/', '', rtrim($stdout, "\n")); } public function addToCommit(array $paths) { $this->execxLocal( 'add -A -- %Ls', $paths); $this->reloadWorkingCopy(); return $this; } public function doCommit($message) { $tmp_file = new TempFile(); Filesystem::writeFile($tmp_file, $message); // NOTE: "--allow-empty-message" was introduced some time after 1.7.0.4, // so we do not provide it and thus require a message. $this->execxLocal( 'commit -F %s', $tmp_file); $this->reloadWorkingCopy(); return $this; } public function amendCommit($message = null) { if ($message === null) { $this->execxLocal('commit --amend --allow-empty -C HEAD'); } else { $tmp_file = new TempFile(); Filesystem::writeFile($tmp_file, $message); $this->execxLocal( 'commit --amend --allow-empty -F %s', $tmp_file); } $this->reloadWorkingCopy(); return $this; } private function parseGitStatus($status, $full = false) { static $flags = array( 'A' => self::FLAG_ADDED, 'M' => self::FLAG_MODIFIED, 'D' => self::FLAG_DELETED, ); $status = trim($status); $lines = array(); foreach (explode("\n", $status) as $line) { if ($line) { $lines[] = preg_split("/[ \t]/", $line, 6); } } $files = array(); foreach ($lines as $line) { $mask = 0; $flag = $line[4]; $file = $line[5]; foreach ($flags as $key => $bits) { if ($flag == $key) { $mask |= $bits; } } if ($full) { $files[$file] = array( 'mask' => $mask, 'ref' => rtrim($line[3], '.'), ); } else { $files[$file] = $mask; } } return $files; } public function getAllFiles() { $future = $this->buildLocalFuture(array('ls-files -z')); return id(new LinesOfALargeExecFuture($future)) ->setDelimiter("\0"); } public function getChangedFiles($since_commit) { list($stdout) = $this->execxLocal( 'diff --raw %s', $since_commit); return $this->parseGitStatus($stdout); } public function getBlame($path) { // TODO: 'git blame' supports --porcelain and we should probably use it. list($stdout) = $this->execxLocal( 'blame --date=iso -w -M %s -- %s', $this->getBaseCommit(), $path); $blame = array(); foreach (explode("\n", trim($stdout)) as $line) { if (!strlen($line)) { continue; } // lines predating a git repo's history are blamed to the oldest revision, // with the commit hash prepended by a ^. we shouldn't count these lines // as blaming to the oldest diff's unfortunate author if ($line[0] == '^') { continue; } $matches = null; $ok = preg_match( '/^([0-9a-f]+)[^(]+?[(](.*?) +\d\d\d\d-\d\d-\d\d/', $line, $matches); if (!$ok) { - throw new Exception("Bad blame? `{$line}'"); + throw new Exception(pht("Bad blame? `%s'", $line)); } $revision = $matches[1]; $author = $matches[2]; $blame[] = array($author, $revision); } return $blame; } public function getOriginalFileData($path) { return $this->getFileDataAtRevision($path, $this->getBaseCommit()); } public function getCurrentFileData($path) { return $this->getFileDataAtRevision($path, 'HEAD'); } private function parseGitTree($stdout) { $result = array(); $stdout = trim($stdout); if (!strlen($stdout)) { return $result; } $lines = explode("\n", $stdout); foreach ($lines as $line) { $matches = array(); $ok = preg_match( '/^(\d{6}) (blob|tree|commit) ([a-z0-9]{40})[\t](.*)$/', $line, $matches); if (!$ok) { - throw new Exception('Failed to parse git ls-tree output!'); + throw new Exception(pht('Failed to parse %s output!', 'git ls-tree')); } $result[$matches[4]] = array( 'mode' => $matches[1], 'type' => $matches[2], 'ref' => $matches[3], ); } return $result; } private function getFileDataAtRevision($path, $revision) { // NOTE: We don't want to just "git show {$revision}:{$path}" since if the // path was a directory at the given revision we'll get a list of its files // and treat it as though it as a file containing a list of other files, // which is silly. list($stdout) = $this->execxLocal( 'ls-tree %s -- %s', $revision, $path); $info = $this->parseGitTree($stdout); if (empty($info[$path])) { // No such path, or the path is a directory and we executed 'ls-tree dir/' // and got a list of its contents back. return null; } if ($info[$path]['type'] != 'blob') { // Path is or was a directory, not a file. return null; } list($stdout) = $this->execxLocal( 'cat-file blob %s', $info[$path]['ref']); return $stdout; } /** * Returns names of all the branches in the current repository. * * @return list> Dictionary of branch information. */ public function getAllBranches() { list($branch_info) = $this->execxLocal( 'branch --no-color'); $lines = explode("\n", rtrim($branch_info)); $result = array(); foreach ($lines as $line) { if (preg_match('@^[* ]+\(no branch|detached from \w+/\w+\)@', $line)) { // This is indicating that the working copy is in a detached state; // just ignore it. continue; } list($current, $name) = preg_split('/\s+/', $line, 2); $result[] = array( 'current' => !empty($current), 'name' => $name, ); } return $result; } public function getWorkingCopyRevision() { list($stdout) = $this->execxLocal('rev-parse HEAD'); return rtrim($stdout, "\n"); } public function getUnderlyingWorkingCopyRevision() { list($err, $stdout) = $this->execManualLocal('svn find-rev HEAD'); if (!$err && $stdout) { return rtrim($stdout, "\n"); } return $this->getWorkingCopyRevision(); } public function isHistoryDefaultImmutable() { return false; } public function supportsAmend() { return true; } public function supportsCommitRanges() { return true; } public function supportsLocalCommits() { return true; } public function hasLocalCommit($commit) { try { if (!$this->getCanonicalRevisionName($commit)) { return false; } } catch (CommandException $exception) { return false; } return true; } public function getAllLocalChanges() { $diff = $this->getFullGitDiff($this->getBaseCommit()); if (!strlen(trim($diff))) { return array(); } $parser = new ArcanistDiffParser(); return $parser->parseDiff($diff); } public function supportsLocalBranchMerge() { return true; } public function performLocalBranchMerge($branch, $message) { if (!$branch) { throw new ArcanistUsageException( - 'Under git, you must specify the branch you want to merge.'); + pht('Under git, you must specify the branch you want to merge.')); } $err = phutil_passthru( '(cd %s && git merge --no-ff -m %s %s)', $this->getPath(), $message, $branch); if ($err) { throw new ArcanistUsageException('Merge failed!'); } } public function getFinalizedRevisionMessage() { - return "You may now push this commit upstream, as appropriate (e.g. with ". - "'git push', or 'git svn dcommit', or by printing and faxing it)."; + return pht( + "You may now push this commit upstream, as appropriate (e.g. with ". + "'%s', or '%s', or by printing and faxing it).", + 'git push', + 'git svn dcommit'); } public function getCommitMessage($commit) { list($message) = $this->execxLocal( 'log -n1 --format=%C %s --', '%s%n%n%b', $commit); return $message; } public function loadWorkingCopyDifferentialRevisions( ConduitClient $conduit, array $query) { $messages = $this->getGitCommitLog(); if (!strlen($messages)) { return array(); } $parser = new ArcanistDiffParser(); $messages = $parser->parseDiff($messages); // First, try to find revisions by explicit revision IDs in commit messages. $reason_map = array(); $revision_ids = array(); foreach ($messages as $message) { $object = ArcanistDifferentialCommitMessage::newFromRawCorpus( $message->getMetadata('message')); if ($object->getRevisionID()) { $revision_ids[] = $object->getRevisionID(); $reason_map[$object->getRevisionID()] = $message->getCommitHash(); } } if ($revision_ids) { $results = $conduit->callMethodSynchronous( 'differential.query', $query + array( 'ids' => $revision_ids, )); foreach ($results as $key => $result) { $hash = substr($reason_map[$result['id']], 0, 16); - $results[$key]['why'] = - "Commit message for '{$hash}' has explicit 'Differential Revision'."; + $results[$key]['why'] = pht( + "Commit message for '%s' has explicit 'Differential Revision'.", + $hash); } return $results; } // If we didn't succeed, try to find revisions by hash. $hashes = array(); foreach ($this->getLocalCommitInformation() as $commit) { $hashes[] = array('gtcm', $commit['commit']); $hashes[] = array('gttr', $commit['tree']); } $results = $conduit->callMethodSynchronous( 'differential.query', $query + array( 'commitHashes' => $hashes, )); foreach ($results as $key => $result) { - $results[$key]['why'] = + $results[$key]['why'] = pht( 'A git commit or tree hash in the commit range is already attached '. - 'to the Differential revision.'; + 'to the Differential revision.'); } return $results; } public function updateWorkingCopy() { $this->execxLocal('pull'); $this->reloadWorkingCopy(); } public function getCommitSummary($commit) { if ($commit == self::GIT_MAGIC_ROOT_COMMIT) { - return '(The Empty Tree)'; + return pht('(The Empty Tree)'); } list($summary) = $this->execxLocal( 'log -n 1 --format=%C %s', '%s', $commit); return trim($summary); } public function backoutCommit($commit_hash) { - $this->execxLocal( - 'revert %s -n --no-edit', $commit_hash); + $this->execxLocal('revert %s -n --no-edit', $commit_hash); $this->reloadWorkingCopy(); if (!$this->getUncommittedStatus()) { throw new ArcanistUsageException( - "{$commit_hash} has already been reverted."); + pht('%s has already been reverted.', $commit_hash)); } } public function getBackoutMessage($commit_hash) { - return 'This reverts commit '.$commit_hash.'.'; + return pht('This reverts commit %s.', $commit_hash); } public function isGitSubversionRepo() { return Filesystem::pathExists($this->getPath('.git/svn')); } public function resolveBaseCommitRule($rule, $source) { list($type, $name) = explode(':', $rule, 2); switch ($type) { case 'git': $matches = null; if (preg_match('/^merge-base\((.+)\)$/', $name, $matches)) { list($err, $merge_base) = $this->execManualLocal( 'merge-base %s HEAD', $matches[1]); if (!$err) { $this->setBaseCommitExplanation( - "it is the merge-base of '{$matches[1]}' and HEAD, as ". - "specified by '{$rule}' in your {$source} 'base' ". - "configuration."); + pht( + "it is the merge-base of '%s' and HEAD, as specified by ". + "'%s' in your %s 'base' configuration.", + $matches[1], + $rule, + $source)); return trim($merge_base); } } else if (preg_match('/^branch-unique\((.+)\)$/', $name, $matches)) { list($err, $merge_base) = $this->execManualLocal( 'merge-base %s HEAD', $matches[1]); if ($err) { return null; } $merge_base = trim($merge_base); list($commits) = $this->execxLocal( 'log --format=%C %s..HEAD --', '%H', $merge_base); $commits = array_filter(explode("\n", $commits)); if (!$commits) { return null; } $commits[] = $merge_base; $head_branch_count = null; foreach ($commits as $commit) { list($branches) = $this->execxLocal( 'branch --contains %s', $commit); $branches = array_filter(explode("\n", $branches)); if ($head_branch_count === null) { // If this is the first commit, it's HEAD. Count how many // branches it is on; we want to include commits on the same // number of branches. This covers a case where this branch // has sub-branches and we're running "arc diff" here again // for whatever reason. $head_branch_count = count($branches); } else if (count($branches) > $head_branch_count) { foreach ($branches as $key => $branch) { $branches[$key] = trim($branch, ' *'); } $branches = implode(', ', $branches); $this->setBaseCommitExplanation( - "it is the first commit between '{$merge_base}' (the ". - "merge-base of '{$matches[1]}' and HEAD) which is also ". - "contained by another branch ({$branches})."); + pht( + "it is the first commit between '%s' (the merge-base of ". + "'%s' and HEAD) which is also contained by another branch ". + "(%s).", + $merge_base, + $matches[1], + $branches)); return $commit; } } } else { list($err) = $this->execManualLocal( 'cat-file -t %s', $name); if (!$err) { $this->setBaseCommitExplanation( - "it is specified by '{$rule}' in your {$source} 'base' ". - "configuration."); + pht( + "it is specified by '%s' in your %s 'base' configuration.", + $rule, + $source)); return $name; } } break; case 'arc': switch ($name) { case 'empty': $this->setBaseCommitExplanation( - "you specified '{$rule}' in your {$source} 'base' ". - "configuration."); + pht( + "you specified '%s' in your %s 'base' configuration.", + $rule, + $source)); return self::GIT_MAGIC_ROOT_COMMIT; case 'amended': $text = $this->getCommitMessage('HEAD'); $message = ArcanistDifferentialCommitMessage::newFromRawCorpus( $text); if ($message->getRevisionID()) { $this->setBaseCommitExplanation( - "HEAD has been amended with 'Differential Revision:', ". - "as specified by '{$rule}' in your {$source} 'base' ". - "configuration."); + pht( + "HEAD has been amended with 'Differential Revision:', ". + "as specified by '%s' in your %s 'base' configuration.", + $rule, + $source)); return 'HEAD^'; } break; case 'upstream': list($err, $upstream) = $this->execManualLocal( 'rev-parse --abbrev-ref --symbolic-full-name %s', '@{upstream}'); if (!$err) { $upstream = rtrim($upstream); list($upstream_merge_base) = $this->execxLocal( 'merge-base %s HEAD', $upstream); $upstream_merge_base = rtrim($upstream_merge_base); $this->setBaseCommitExplanation( - "it is the merge-base of the upstream of the current branch ". - "and HEAD, and matched the rule '{$rule}' in your {$source} ". - "'base' configuration."); + pht( + "it is the merge-base of the upstream of the current branch ". + "and HEAD, and matched the rule '%s' in your %s ". + "'base' configuration.", + $rule, + $source)); return $upstream_merge_base; } break; case 'this': $this->setBaseCommitExplanation( - "you specified '{$rule}' in your {$source} 'base' ". - "configuration."); + pht( + "you specified '%s' in your %s 'base' configuration.", + $rule, + $source)); return 'HEAD^'; } default: return null; } return null; } public function canStashChanges() { return true; } public function stashChanges() { $this->execxLocal('stash'); $this->reloadWorkingCopy(); } public function unstashChanges() { $this->execxLocal('stash pop'); } protected function didReloadCommitRange() { // After an amend, the symbolic head may resolve to a different commit. $this->resolvedHeadCommit = null; } } diff --git a/src/repository/api/ArcanistMercurialAPI.php b/src/repository/api/ArcanistMercurialAPI.php index efe51198..4abcea04 100644 --- a/src/repository/api/ArcanistMercurialAPI.php +++ b/src/repository/api/ArcanistMercurialAPI.php @@ -1,1093 +1,1128 @@ setCWD($this->getPath()); return $future; } public function execPassthru($pattern /* , ... */) { $args = func_get_args(); if (phutil_is_windows()) { $args[0] = 'hg '.$args[0]; } else { $args[0] = 'HGPLAIN=1 hg '.$args[0]; } return call_user_func_array('phutil_passthru', $args); } public function getSourceControlSystemName() { return 'hg'; } public function getMetadataPath() { return $this->getPath('.hg'); } public function getSourceControlBaseRevision() { return $this->getCanonicalRevisionName($this->getBaseCommit()); } public function getCanonicalRevisionName($string) { $match = null; if ($this->isHgSubversionRepo() && preg_match('/@([0-9]+)$/', $string, $match)) { $string = hgsprintf('svnrev(%s)', $match[1]); } list($stdout) = $this->execxLocal( 'log -l 1 --template %s -r %s --', '{node}', $string); return $stdout; } public function getHashFromFromSVNRevisionNumber($revision_id) { $matches = array(); $string = hgsprintf('svnrev(%s)', $revision_id); list($stdout) = $this->execxLocal( 'log -l 1 --template %s -r %s --', '{node}', $string); if (!$stdout) { throw new ArcanistUsageException( - "Cannot find the HG equivalent of {$revision_id} given."); + pht('Cannot find the HG equivalent of %s given.', $revision_id)); } return $stdout; } public function getSVNRevisionNumberFromHash($hash) { $matches = array(); list($stdout) = $this->execxLocal( 'log -r %s --template {svnrev}', $hash); if (!$stdout) { throw new ArcanistUsageException( - "Cannot find the SVN equivalent of {$hash} given."); + pht('Cannot find the SVN equivalent of %s given.', $hash)); } return $stdout; } public function getSourceControlPath() { return '/'; } public function getBranchName() { if (!$this->branch) { list($stdout) = $this->execxLocal('branch'); $this->branch = trim($stdout); } return $this->branch; } protected function didReloadCommitRange() { $this->localCommitInfo = null; } protected function buildBaseCommit($symbolic_commit) { if ($symbolic_commit !== null) { try { $commit = $this->getCanonicalRevisionName( hgsprintf('ancestor(%s,.)', $symbolic_commit)); } catch (Exception $ex) { // Try it as a revset instead of a commit id try { $commit = $this->getCanonicalRevisionName( hgsprintf('ancestor(%R,.)', $symbolic_commit)); } catch (Exception $ex) { throw new ArcanistUsageException( - "Commit '{$symbolic_commit}' is not a valid Mercurial commit ". - "identifier."); + pht( + "Commit '%s' is not a valid Mercurial commit identifier.", + $symbolic_commit)); } } $this->setBaseCommitExplanation( - 'it is the greatest common ancestor of the working directory '. - 'and the commit you specified explicitly.'); + pht( + 'it is the greatest common ancestor of the working directory '. + 'and the commit you specified explicitly.')); return $commit; } if ($this->getBaseCommitArgumentRules() || $this->getConfigurationManager()->getConfigFromAnySource('base')) { $base = $this->resolveBaseCommit(); if (!$base) { throw new ArcanistUsageException( - "None of the rules in your 'base' configuration matched a valid ". - "commit. Adjust rules or specify which commit you want to use ". - "explicitly."); + pht( + "None of the rules in your 'base' configuration matched a valid ". + "commit. Adjust rules or specify which commit you want to use ". + "explicitly.")); } return $base; } // Mercurial 2.1 and up have phases which indicate if something is // published or not. To find which revs are outgoing, it's much // faster to check the phase instead of actually checking the server. if ($this->supportsPhases()) { list($err, $stdout) = $this->execManualLocal( 'log --branch %s -r %s --style default', $this->getBranchName(), 'draft()'); } else { list($err, $stdout) = $this->execManualLocal( 'outgoing --branch %s --style default', $this->getBranchName()); } if (!$err) { $logs = ArcanistMercurialParser::parseMercurialLog($stdout); } else { // Mercurial (in some versions?) raises an error when there's nothing // outgoing. $logs = array(); } if (!$logs) { $this->setBaseCommitExplanation( - 'you have no outgoing commits, so arc assumes you intend to submit '. - 'uncommitted changes in the working copy.'); + pht( + 'you have no outgoing commits, so arc assumes you intend to submit '. + 'uncommitted changes in the working copy.')); return $this->getWorkingCopyRevision(); } $outgoing_revs = ipull($logs, 'rev'); // This is essentially an implementation of a theoretical `hg merge-base` // command. $against = $this->getWorkingCopyRevision(); while (true) { // NOTE: The "^" and "~" syntaxes were only added in hg 1.9, which is // new as of July 2011, so do this in a compatible way. Also, "hg log" // and "hg outgoing" don't necessarily show parents (even if given an // explicit template consisting of just the parents token) so we need // to separately execute "hg parents". list($stdout) = $this->execxLocal( 'parents --style default --rev %s', $against); $parents_logs = ArcanistMercurialParser::parseMercurialLog($stdout); list($p1, $p2) = array_merge($parents_logs, array(null, null)); if ($p1 && !in_array($p1['rev'], $outgoing_revs)) { $against = $p1['rev']; break; } else if ($p2 && !in_array($p2['rev'], $outgoing_revs)) { $against = $p2['rev']; break; } else if ($p1) { $against = $p1['rev']; } else { // This is the case where you have a new repository and the entire // thing is outgoing; Mercurial literally accepts "--rev null" as // meaning "diff against the empty state". $against = 'null'; break; } } if ($against == 'null') { $this->setBaseCommitExplanation( - 'this is a new repository (all changes are outgoing).'); + pht('this is a new repository (all changes are outgoing).')); } else { $this->setBaseCommitExplanation( - 'it is the first commit reachable from the working copy state '. - 'which is not outgoing.'); + pht( + 'it is the first commit reachable from the working copy state '. + 'which is not outgoing.')); } return $against; } public function getLocalCommitInformation() { if ($this->localCommitInfo === null) { $base_commit = $this->getBaseCommit(); list($info) = $this->execxLocal( 'log --template %s --rev %s --branch %s --', "{node}\1{rev}\1{author}\1". "{date|rfc822date}\1{branch}\1{tag}\1{parents}\1{desc}\2", hgsprintf('(%s::. - %s)', $base_commit, $base_commit), $this->getBranchName()); $logs = array_filter(explode("\2", $info)); $last_node = null; $futures = array(); $commits = array(); foreach ($logs as $log) { list($node, $rev, $full_author, $date, $branch, $tag, $parents, $desc) = explode("\1", $log, 9); list ($author, $author_email) = $this->parseFullAuthor($full_author); // NOTE: If a commit has only one parent, {parents} returns empty. // If it has two parents, {parents} returns revs and short hashes, not // full hashes. Try to avoid making calls to "hg parents" because it's // relatively expensive. $commit_parents = null; if (!$parents) { if ($last_node) { $commit_parents = array($last_node); } } if (!$commit_parents) { // We didn't get a cheap hit on previous commit, so do the full-cost // "hg parents" call. We can run these in parallel, at least. $futures[$node] = $this->execFutureLocal( 'parents --template %s --rev %s', '{node}\n', $node); } $commits[$node] = array( 'author' => $author, 'time' => strtotime($date), 'branch' => $branch, 'tag' => $tag, 'commit' => $node, 'rev' => $node, // TODO: Remove eventually. 'local' => $rev, 'parents' => $commit_parents, 'summary' => head(explode("\n", $desc)), 'message' => $desc, 'authorEmail' => $author_email, ); $last_node = $node; } $futures = id(new FutureIterator($futures)) ->limit(4); foreach ($futures as $node => $future) { list($parents) = $future->resolvex(); $parents = array_filter(explode("\n", $parents)); $commits[$node]['parents'] = $parents; } // Put commits in newest-first order, to be consistent with Git and the // expected order of "hg log" and "git log" under normal circumstances. // The order of ancestors() is oldest-first. $commits = array_reverse($commits); $this->localCommitInfo = $commits; } return $this->localCommitInfo; } public function getAllFiles() { // TODO: Handle paths with newlines. $future = $this->buildLocalFuture(array('manifest')); return new LinesOfALargeExecFuture($future); } public function getChangedFiles($since_commit) { list($stdout) = $this->execxLocal( 'status --rev %s', $since_commit); return ArcanistMercurialParser::parseMercurialStatus($stdout); } public function getBlame($path) { list($stdout) = $this->execxLocal( 'annotate -u -v -c --rev %s -- %s', $this->getBaseCommit(), $path); $lines = phutil_split_lines($stdout, $retain_line_endings = true); $blame = array(); foreach ($lines as $line) { if (!strlen($line)) { continue; } $matches = null; $ok = preg_match('/^\s*([^:]+?) ([a-f0-9]{12}):/', $line, $matches); if (!$ok) { - throw new Exception("Unable to parse Mercurial blame line: {$line}"); + throw new Exception( + pht( + 'Unable to parse Mercurial blame line: %s', + $line)); } $revision = $matches[2]; $author = trim($matches[1]); $blame[] = array($author, $revision); } return $blame; } protected function buildUncommittedStatus() { list($stdout) = $this->execxLocal('status'); $results = new PhutilArrayWithDefaultValue(); $working_status = ArcanistMercurialParser::parseMercurialStatus($stdout); foreach ($working_status as $path => $mask) { if (!($mask & ArcanistRepositoryAPI::FLAG_UNTRACKED)) { // Mark tracked files as uncommitted. $mask |= self::FLAG_UNCOMMITTED; } $results[$path] |= $mask; } return $results->toArray(); } protected function buildCommitRangeStatus() { // TODO: Possibly we should use "hg status --rev X --rev ." for this // instead, but we must run "hg diff" later anyway in most cases, so // building and caching it shouldn't hurt us. $diff = $this->getFullMercurialDiff(); if (!$diff) { return array(); } $parser = new ArcanistDiffParser(); $changes = $parser->parseDiff($diff); $status_map = array(); foreach ($changes as $change) { $flags = 0; switch ($change->getType()) { case ArcanistDiffChangeType::TYPE_ADD: case ArcanistDiffChangeType::TYPE_MOVE_HERE: case ArcanistDiffChangeType::TYPE_COPY_HERE: $flags |= self::FLAG_ADDED; break; case ArcanistDiffChangeType::TYPE_CHANGE: case ArcanistDiffChangeType::TYPE_COPY_AWAY: // Check for changes? $flags |= self::FLAG_MODIFIED; break; case ArcanistDiffChangeType::TYPE_DELETE: case ArcanistDiffChangeType::TYPE_MOVE_AWAY: case ArcanistDiffChangeType::TYPE_MULTICOPY: $flags |= self::FLAG_DELETED; break; } $status_map[$change->getCurrentPath()] = $flags; } return $status_map; } protected function didReloadWorkingCopy() { // Diffs are against ".", so we need to drop the cache if we change the // working copy. $this->rawDiffCache = array(); $this->branch = null; } private function getDiffOptions() { $options = array( '--git', '-U'.$this->getDiffLinesOfContext(), ); return implode(' ', $options); } public function getRawDiffText($path) { $options = $this->getDiffOptions(); $range = $this->getBaseCommit(); $raw_diff_cache_key = $options.' '.$range.' '.$path; if (idx($this->rawDiffCache, $raw_diff_cache_key)) { return idx($this->rawDiffCache, $raw_diff_cache_key); } list($stdout) = $this->execxLocal( 'diff %C --rev %s -- %s', $options, $range, $path); $this->rawDiffCache[$raw_diff_cache_key] = $stdout; return $stdout; } public function getFullMercurialDiff() { return $this->getRawDiffText(''); } public function getOriginalFileData($path) { return $this->getFileDataAtRevision($path, $this->getBaseCommit()); } public function getCurrentFileData($path) { return $this->getFileDataAtRevision( $path, $this->getWorkingCopyRevision()); } public function getBulkOriginalFileData($paths) { return $this->getBulkFileDataAtRevision($paths, $this->getBaseCommit()); } public function getBulkCurrentFileData($paths) { return $this->getBulkFileDataAtRevision( $paths, $this->getWorkingCopyRevision()); } private function getBulkFileDataAtRevision($paths, $revision) { // Calling 'hg cat' on each file individually is slow (1 second per file // on a large repo) because mercurial has to decompress and parse the // entire manifest every time. Do it in one large batch instead. // hg cat will write the file data to files in a temp directory $tmpdir = Filesystem::createTemporaryDirectory(); // Mercurial doesn't create the directories for us :( foreach ($paths as $path) { $tmppath = $tmpdir.'/'.$path; Filesystem::createDirectory(dirname($tmppath), 0755, true); } list($err, $stdout) = $this->execManualLocal( 'cat --rev %s --output %s -- %C', $revision, // %p is the formatter for the repo-relative filepath $tmpdir.'/%p', implode(' ', $paths)); $filedata = array(); foreach ($paths as $path) { $tmppath = $tmpdir.'/'.$path; if (Filesystem::pathExists($tmppath)) { $filedata[$path] = Filesystem::readFile($tmppath); } } Filesystem::remove($tmpdir); return $filedata; } private function getFileDataAtRevision($path, $revision) { list($err, $stdout) = $this->execManualLocal( 'cat --rev %s -- %s', $revision, $path); if ($err) { // Assume this is "no file at revision", i.e. a deleted or added file. return null; } else { return $stdout; } } public function getWorkingCopyRevision() { return '.'; } public function isHistoryDefaultImmutable() { return true; } public function supportsAmend() { list($err, $stdout) = $this->execManualLocal('help commit'); if ($err) { return false; } else { return (strpos($stdout, 'amend') !== false); } } public function supportsRebase() { if ($this->supportsRebase === null) { list ($err) = $this->execManualLocal('help rebase'); $this->supportsRebase = $err === 0; } return $this->supportsRebase; } public function supportsPhases() { if ($this->supportsPhases === null) { list ($err) = $this->execManualLocal('help phase'); $this->supportsPhases = $err === 0; } return $this->supportsPhases; } public function supportsCommitRanges() { return true; } public function supportsLocalCommits() { return true; } public function getAllBranches() { list($branch_info) = $this->execxLocal('bookmarks'); if (trim($branch_info) == 'no bookmarks set') { return array(); } $matches = null; preg_match_all( '/^\s*(\*?)\s*(.+)\s(\S+)$/m', $branch_info, $matches, PREG_SET_ORDER); $return = array(); foreach ($matches as $match) { list(, $current, $name) = $match; $return[] = array( 'current' => (bool)$current, 'name' => rtrim($name), ); } return $return; } public function hasLocalCommit($commit) { try { $this->getCanonicalRevisionName($commit); return true; } catch (Exception $ex) { return false; } } public function getCommitMessage($commit) { list($message) = $this->execxLocal( 'log --template={desc} --rev %s', $commit); return $message; } public function getAllLocalChanges() { $diff = $this->getFullMercurialDiff(); if (!strlen(trim($diff))) { return array(); } $parser = new ArcanistDiffParser(); return $parser->parseDiff($diff); } public function supportsLocalBranchMerge() { return true; } public function performLocalBranchMerge($branch, $message) { if ($branch) { $err = phutil_passthru( '(cd %s && HGPLAIN=1 hg merge --rev %s && hg commit -m %s)', $this->getPath(), $branch, $message); } else { $err = phutil_passthru( '(cd %s && HGPLAIN=1 hg merge && hg commit -m %s)', $this->getPath(), $message); } if ($err) { - throw new ArcanistUsageException('Merge failed!'); + throw new ArcanistUsageException(pht('Merge failed!')); } } public function getFinalizedRevisionMessage() { - return "You may now push this commit upstream, as appropriate (e.g. with ". - "'hg push' or by printing and faxing it)."; + return pht( + "You may now push this commit upstream, as appropriate (e.g. with ". + "'%s' or by printing and faxing it).", + 'hg push'); } public function getCommitMessageLog() { $base_commit = $this->getBaseCommit(); list($stdout) = $this->execxLocal( 'log --template %s --rev %s --branch %s --', "{node}\1{desc}\2", hgsprintf('(%s::. - %s)', $base_commit, $base_commit), $this->getBranchName()); $map = array(); $logs = explode("\2", trim($stdout)); foreach (array_filter($logs) as $log) { list($node, $desc) = explode("\1", $log); $map[$node] = $desc; } return array_reverse($map); } public function loadWorkingCopyDifferentialRevisions( ConduitClient $conduit, array $query) { $messages = $this->getCommitMessageLog(); $parser = new ArcanistDiffParser(); // First, try to find revisions by explicit revision IDs in commit messages. $reason_map = array(); $revision_ids = array(); foreach ($messages as $node_id => $message) { $object = ArcanistDifferentialCommitMessage::newFromRawCorpus($message); if ($object->getRevisionID()) { $revision_ids[] = $object->getRevisionID(); $reason_map[$object->getRevisionID()] = $node_id; } } if ($revision_ids) { $results = $conduit->callMethodSynchronous( 'differential.query', $query + array( 'ids' => $revision_ids, )); foreach ($results as $key => $result) { $hash = substr($reason_map[$result['id']], 0, 16); $results[$key]['why'] = - "Commit message for '{$hash}' has explicit 'Differential Revision'."; + pht( + "Commit message for '%s' has explicit 'Differential Revision'.", + $hash); } return $results; } // Try to find revisions by hash. $hashes = array(); foreach ($this->getLocalCommitInformation() as $commit) { $hashes[] = array('hgcm', $commit['commit']); } if ($hashes) { // NOTE: In the case of "arc diff . --uncommitted" in a Mercurial working // copy with dirty changes, there may be no local commits. $results = $conduit->callMethodSynchronous( 'differential.query', $query + array( 'commitHashes' => $hashes, )); foreach ($results as $key => $hash) { - $results[$key]['why'] = + $results[$key]['why'] = pht( 'A mercurial commit hash in the commit range is already attached '. - 'to the Differential revision.'; + 'to the Differential revision.'); } return $results; } return array(); } public function updateWorkingCopy() { $this->execxLocal('up'); $this->reloadWorkingCopy(); } private function getMercurialConfig($key, $default = null) { list($stdout) = $this->execxLocal('showconfig %s', $key); if ($stdout == '') { return $default; } return rtrim($stdout); } public function getAuthor() { $full_author = $this->getMercurialConfig('ui.username'); list($author, $author_email) = $this->parseFullAuthor($full_author); return $author; } /** * Parse the Mercurial author field. * * Not everyone enters their email address as a part of the username * field. Try to make it work when it's obvious. * * @param string $full_author * @return array */ protected function parseFullAuthor($full_author) { if (strpos($full_author, '@') === false) { $author = $full_author; $author_email = null; } else { $email = new PhutilEmailAddress($full_author); $author = $email->getDisplayName(); $author_email = $email->getAddress(); } return array($author, $author_email); } public function addToCommit(array $paths) { $this->execxLocal( 'addremove -- %Ls', $paths); $this->reloadWorkingCopy(); } public function doCommit($message) { $tmp_file = new TempFile(); Filesystem::writeFile($tmp_file, $message); $this->execxLocal('commit -l %s', $tmp_file); $this->reloadWorkingCopy(); } public function amendCommit($message = null) { if ($message === null) { $message = $this->getCommitMessage('.'); } $tmp_file = new TempFile(); Filesystem::writeFile($tmp_file, $message); try { $this->execxLocal( 'commit --amend -l %s', $tmp_file); } catch (CommandException $ex) { if (preg_match('/nothing changed/', $ex->getStdOut())) { // NOTE: Mercurial considers it an error to make a no-op amend. Although // we generally defer to the underlying VCS to dictate behavior, this // one seems a little goofy, and we use amend as part of various // workflows under the assumption that no-op amends are fine. If this // amend failed because it's a no-op, just continue. } else { throw $ex; } } $this->reloadWorkingCopy(); } public function getCommitSummary($commit) { if ($commit == 'null') { - return '(The Empty Void)'; + return pht('(The Empty Void)'); } list($summary) = $this->execxLocal( 'log --template {desc} --limit 1 --rev %s', $commit); $summary = head(explode("\n", $summary)); return trim($summary); } public function backoutCommit($commit_hash) { - $this->execxLocal( - 'backout -r %s', $commit_hash); + $this->execxLocal('backout -r %s', $commit_hash); $this->reloadWorkingCopy(); if (!$this->getUncommittedStatus()) { throw new ArcanistUsageException( - "{$commit_hash} has already been reverted."); + pht('%s has already been reverted.', $commit_hash)); } } public function getBackoutMessage($commit_hash) { - return 'Backed out changeset '.$commit_hash.'.'; + return pht('Backed out changeset %s,', $commit_hash); } public function resolveBaseCommitRule($rule, $source) { list($type, $name) = explode(':', $rule, 2); // NOTE: This function MUST return node hashes or symbolic commits (like // branch names or the word "tip"), not revsets. This includes ".^" and // similar, which a revset, not a symbolic commit identifier. If you return // a revset it will be escaped later and looked up literally. switch ($type) { case 'hg': $matches = null; if (preg_match('/^gca\((.+)\)$/', $name, $matches)) { list($err, $merge_base) = $this->execManualLocal( 'log --template={node} --rev %s', sprintf('ancestor(., %s)', $matches[1])); if (!$err) { $this->setBaseCommitExplanation( - "it is the greatest common ancestor of '{$matches[1]}' and ., as". - " specified by '{$rule}' in your {$source} 'base' ". - "configuration."); + pht( + "it is the greatest common ancestor of '%s' and %s, as ". + "specified by '%s' in your %s 'base' configuration.", + $matches[1], + '.', + $rule, + $source)); return trim($merge_base); } } else { list($err, $commit) = $this->execManualLocal( 'log --template {node} --rev %s', hgsprintf('%s', $name)); if ($err) { list($err, $commit) = $this->execManualLocal( 'log --template {node} --rev %s', $name); } if (!$err) { $this->setBaseCommitExplanation( - "it is specified by '{$rule}' in your {$source} 'base' ". - "configuration."); + pht( + "it is specified by '%s' in your %s 'base' configuration.", + $rule, + $source)); return trim($commit); } } break; case 'arc': switch ($name) { case 'empty': $this->setBaseCommitExplanation( - "you specified '{$rule}' in your {$source} 'base' ". - "configuration."); + pht( + "you specified '%s' in your %s 'base' configuration.", + $rule, + $source)); return 'null'; case 'outgoing': list($err, $outgoing_base) = $this->execManualLocal( 'log --template={node} --rev %s', 'limit(reverse(ancestors(.) - outgoing()), 1)'); if (!$err) { $this->setBaseCommitExplanation( - "it is the first ancestor of the working copy that is not ". - "outgoing, and it matched the rule {$rule} in your {$source} ". - "'base' configuration."); + pht( + "it is the first ancestor of the working copy that is not ". + "outgoing, and it matched the rule %s in your %s ". + "'base' configuration.", + $rule, + $source)); return trim($outgoing_base); } case 'amended': $text = $this->getCommitMessage('.'); $message = ArcanistDifferentialCommitMessage::newFromRawCorpus( $text); if ($message->getRevisionID()) { $this->setBaseCommitExplanation( - "'.' has been amended with 'Differential Revision:', ". - "as specified by '{$rule}' in your {$source} 'base' ". - "configuration."); + pht( + "'%s' has been amended with 'Differential Revision:', ". + "as specified by '%s' in your %s 'base' configuration.", + '.'. + $rule, + $source)); // NOTE: This should be safe because Mercurial doesn't support // amend until 2.2. return $this->getCanonicalRevisionName('.^'); } break; case 'bookmark': $revset = 'limit('. ' sort('. ' (ancestors(.) and bookmark() - .) or'. ' (ancestors(.) - outgoing()), '. ' -rev),'. '1)'; list($err, $bookmark_base) = $this->execManualLocal( 'log --template={node} --rev %s', $revset); if (!$err) { $this->setBaseCommitExplanation( - "it is the first ancestor of . that either has a bookmark, or ". - "is already in the remote and it matched the rule {$rule} in ". - "your {$source} 'base' configuration"); + pht( + "it is the first ancestor of %s that either has a bookmark, ". + "or is already in the remote and it matched the rule %s in ". + "your %s 'base' configuration", + '.', + $rule, + $source)); return trim($bookmark_base); } break; case 'this': $this->setBaseCommitExplanation( - "you specified '{$rule}' in your {$source} 'base' ". - "configuration."); + pht( + "you specified '%s' in your %s 'base' configuration.", + $rule, + $source)); return $this->getCanonicalRevisionName('.^'); default: if (preg_match('/^nodiff\((.+)\)$/', $name, $matches)) { list($results) = $this->execxLocal( 'log --template %s --rev %s', "{node}\1{desc}\2", sprintf('ancestor(.,%s)::.^', $matches[1])); $results = array_reverse(explode("\2", trim($results))); foreach ($results as $result) { if (empty($result)) { continue; } list($node, $desc) = explode("\1", $result, 2); $message = ArcanistDifferentialCommitMessage::newFromRawCorpus( $desc); if ($message->getRevisionID()) { $this->setBaseCommitExplanation( - "it is the first ancestor of . that has a diff ". - "and is the gca or a descendant of the gca with ". - "'{$matches[1]}', specified by '{$rule}' in your ". - "{$source} 'base' configuration."); + pht( + "it is the first ancestor of %s that has a diff and is ". + "the gca or a descendant of the gca with '%s', ". + "specified by '%s' in your %s 'base' configuration.", + '.', + $matches[1], + $rule, + $source)); return $node; } } } break; } break; default: return null; } return null; } public function isHgSubversionRepo() { return file_exists($this->getPath('.hg/svn/rev_map')); } public function getSubversionInfo() { $info = array(); $base_path = null; $revision = null; list($err, $raw_info) = $this->execManualLocal('svn info'); if (!$err) { foreach (explode("\n", trim($raw_info)) as $line) { list($key, $value) = explode(': ', $line, 2); switch ($key) { case 'URL': $info['base_path'] = $value; $base_path = $value; break; case 'Repository UUID': $info['uuid'] = $value; break; case 'Revision': $revision = $value; break; default: break; } } if ($base_path && $revision) { $info['base_revision'] = $base_path.'@'.$revision; } } return $info; } public function getActiveBookmark() { $bookmarks = $this->getBookmarks(); foreach ($bookmarks as $bookmark) { if ($bookmark['is_active']) { return $bookmark['name']; } } return null; } public function isBookmark($name) { $bookmarks = $this->getBookmarks(); foreach ($bookmarks as $bookmark) { if ($bookmark['name'] === $name) { return true; } } return false; } public function isBranch($name) { $branches = $this->getBranches(); foreach ($branches as $branch) { if ($branch['name'] === $name) { return true; } } return false; } public function getBranches() { list($stdout) = $this->execxLocal('--debug branches'); $lines = ArcanistMercurialParser::parseMercurialBranches($stdout); $branches = array(); foreach ($lines as $name => $spec) { $branches[] = array( 'name' => $name, 'revision' => $spec['rev'], ); } return $branches; } public function getBookmarks() { $bookmarks = array(); list($raw_output) = $this->execxLocal('bookmarks'); $raw_output = trim($raw_output); if ($raw_output !== 'no bookmarks set') { foreach (explode("\n", $raw_output) as $line) { // example line: * mybook 2:6b274d49be97 list($name, $revision) = $this->splitBranchOrBookmarkLine($line); $is_active = false; if ('*' === $name[0]) { $is_active = true; $name = substr($name, 2); } $bookmarks[] = array( 'is_active' => $is_active, 'name' => $name, 'revision' => $revision, ); } } return $bookmarks; } private function splitBranchOrBookmarkLine($line) { // branches and bookmarks are printed in the format: // default 0:a5ead76cdf85 (inactive) // * mybook 2:6b274d49be97 // this code divides the name half from the revision half // it does not parse the * and (inactive) bits $colon_index = strrpos($line, ':'); $before_colon = substr($line, 0, $colon_index); $start_rev_index = strrpos($before_colon, ' '); $name = substr($line, 0, $start_rev_index); $rev = substr($line, $start_rev_index); return array(trim($name), trim($rev)); } public function getRemoteURI() { list($stdout) = $this->execxLocal('paths default'); $stdout = trim($stdout); if (strlen($stdout)) { return $stdout; } return null; } } diff --git a/src/repository/api/ArcanistRepositoryAPI.php b/src/repository/api/ArcanistRepositoryAPI.php index d5a3b540..56c7a931 100644 --- a/src/repository/api/ArcanistRepositoryAPI.php +++ b/src/repository/api/ArcanistRepositoryAPI.php @@ -1,659 +1,660 @@ diffLinesOfContext; } public function setDiffLinesOfContext($lines) { $this->diffLinesOfContext = $lines; return $this; } public function getWorkingCopyIdentity() { return $this->configurationManager->getWorkingCopyIdentity(); } public function getConfigurationManager() { return $this->configurationManager; } public static function newAPIFromConfigurationManager( ArcanistConfigurationManager $configuration_manager) { $working_copy = $configuration_manager->getWorkingCopyIdentity(); if (!$working_copy) { throw new Exception( pht( - 'Trying to create a RepositoryAPI without a working copy!')); + 'Trying to create a %s without a working copy!', + __CLASS__)); } $root = $working_copy->getProjectRoot(); switch ($working_copy->getVCSType()) { case 'svn': $api = new ArcanistSubversionAPI($root); break; case 'hg': $api = new ArcanistMercurialAPI($root); break; case 'git': $api = new ArcanistGitAPI($root); break; default: throw new Exception( pht( 'The current working directory is not part of a working copy for '. 'a supported version control system (Git, Subversion or '. 'Mercurial).')); } $api->configurationManager = $configuration_manager; return $api; } public function __construct($path) { $this->path = $path; } public function getPath($to_file = null) { if ($to_file !== null) { return $this->path.DIRECTORY_SEPARATOR. ltrim($to_file, DIRECTORY_SEPARATOR); } else { return $this->path.DIRECTORY_SEPARATOR; } } /* -( Path Status )-------------------------------------------------------- */ abstract protected function buildUncommittedStatus(); abstract protected function buildCommitRangeStatus(); /** * Get a list of uncommitted paths in the working copy that have been changed * or are affected by other status effects, like conflicts or untracked * files. * * Convenience methods @{method:getUntrackedChanges}, * @{method:getUnstagedChanges}, @{method:getUncommittedChanges}, * @{method:getMergeConflicts}, and @{method:getIncompleteChanges} allow * simpler selection of paths in a specific state. * * This method returns a map of paths to bitmasks with status, using * `FLAG_` constants. For example: * * array( * 'some/uncommitted/file.txt' => ArcanistRepositoryAPI::FLAG_UNSTAGED, * ); * * A file may be in several states. Not all states are possible with all * version control systems. * * @return map Map of paths, see above. * @task status */ final public function getUncommittedStatus() { if ($this->uncommittedStatusCache === null) { $status = $this->buildUncommittedStatus(); ksort($status); $this->uncommittedStatusCache = $status; } return $this->uncommittedStatusCache; } /** * @task status */ final public function getUntrackedChanges() { return $this->getUncommittedPathsWithMask(self::FLAG_UNTRACKED); } /** * @task status */ final public function getUnstagedChanges() { return $this->getUncommittedPathsWithMask(self::FLAG_UNSTAGED); } /** * @task status */ final public function getUncommittedChanges() { return $this->getUncommittedPathsWithMask(self::FLAG_UNCOMMITTED); } /** * @task status */ final public function getMergeConflicts() { return $this->getUncommittedPathsWithMask(self::FLAG_CONFLICT); } /** * @task status */ final public function getIncompleteChanges() { return $this->getUncommittedPathsWithMask(self::FLAG_INCOMPLETE); } /** * @task status */ final public function getMissingChanges() { return $this->getUncommittedPathsWithMask(self::FLAG_MISSING); } /** * @task status */ private function getUncommittedPathsWithMask($mask) { $match = array(); foreach ($this->getUncommittedStatus() as $path => $flags) { if ($flags & $mask) { $match[] = $path; } } return $match; } /** * Get a list of paths affected by the commits in the current commit range. * * See @{method:getUncommittedStatus} for a description of the return value. * * @return map Map from paths to status. * @task status */ final public function getCommitRangeStatus() { if ($this->commitRangeStatusCache === null) { $status = $this->buildCommitRangeStatus(); ksort($status); $this->commitRangeStatusCache = $status; } return $this->commitRangeStatusCache; } /** * Get a list of paths affected by commits in the current commit range, or * uncommitted changes in the working copy. See @{method:getUncommittedStatus} * or @{method:getCommitRangeStatus} to retrieve smaller parts of the status. * * See @{method:getUncommittedStatus} for a description of the return value. * * @return map Map from paths to status. * @task status */ final public function getWorkingCopyStatus() { $range_status = $this->getCommitRangeStatus(); $uncommitted_status = $this->getUncommittedStatus(); $result = new PhutilArrayWithDefaultValue($range_status); foreach ($uncommitted_status as $path => $mask) { $result[$path] |= $mask; } $result = $result->toArray(); ksort($result); return $result; } /** * Drops caches after changes to the working copy. By default, some queries * against the working copy are cached. They * * @return this * @task status */ final public function reloadWorkingCopy() { $this->uncommittedStatusCache = null; $this->commitRangeStatusCache = null; $this->didReloadWorkingCopy(); $this->reloadCommitRange(); return $this; } /** * Hook for implementations to dirty working copy caches after the working * copy has been updated. * * @return this * @task status */ protected function didReloadWorkingCopy() { return; } /** * Fetches the original file data for each path provided. * * @return map Map from path to file data. */ public function getBulkOriginalFileData($paths) { $filedata = array(); foreach ($paths as $path) { $filedata[$path] = $this->getOriginalFileData($path); } return $filedata; } /** * Fetches the current file data for each path provided. * * @return map Map from path to file data. */ public function getBulkCurrentFileData($paths) { $filedata = array(); foreach ($paths as $path) { $filedata[$path] = $this->getCurrentFileData($path); } return $filedata; } /** * @return Traversable */ abstract public function getAllFiles(); abstract public function getBlame($path); abstract public function getRawDiffText($path); abstract public function getOriginalFileData($path); abstract public function getCurrentFileData($path); abstract public function getLocalCommitInformation(); abstract public function getSourceControlBaseRevision(); abstract public function getCanonicalRevisionName($string); abstract public function getBranchName(); abstract public function getSourceControlPath(); abstract public function isHistoryDefaultImmutable(); abstract public function supportsAmend(); abstract public function getWorkingCopyRevision(); abstract public function updateWorkingCopy(); abstract public function getMetadataPath(); abstract public function loadWorkingCopyDifferentialRevisions( ConduitClient $conduit, array $query); abstract public function getRemoteURI(); public function getUnderlyingWorkingCopyRevision() { return $this->getWorkingCopyRevision(); } public function getChangedFiles($since_commit) { throw new ArcanistCapabilityNotSupportedException($this); } public function getAuthor() { throw new ArcanistCapabilityNotSupportedException($this); } public function addToCommit(array $paths) { throw new ArcanistCapabilityNotSupportedException($this); } abstract public function supportsLocalCommits(); public function doCommit($message) { throw new ArcanistCapabilityNotSupportedException($this); } public function amendCommit($message = null) { throw new ArcanistCapabilityNotSupportedException($this); } public function getAllBranches() { // TODO: Implement for Mercurial/SVN and make abstract. return array(); } public function hasLocalCommit($commit) { throw new ArcanistCapabilityNotSupportedException($this); } public function getCommitMessage($commit) { throw new ArcanistCapabilityNotSupportedException($this); } public function getCommitSummary($commit) { throw new ArcanistCapabilityNotSupportedException($this); } public function getAllLocalChanges() { throw new ArcanistCapabilityNotSupportedException($this); } abstract public function supportsLocalBranchMerge(); public function performLocalBranchMerge($branch, $message) { throw new ArcanistCapabilityNotSupportedException($this); } public function getFinalizedRevisionMessage() { throw new ArcanistCapabilityNotSupportedException($this); } public function execxLocal($pattern /* , ... */) { $args = func_get_args(); return $this->buildLocalFuture($args)->resolvex(); } public function execManualLocal($pattern /* , ... */) { $args = func_get_args(); return $this->buildLocalFuture($args)->resolve(); } public function execFutureLocal($pattern /* , ... */) { $args = func_get_args(); return $this->buildLocalFuture($args); } abstract protected function buildLocalFuture(array $argv); public function canStashChanges() { return false; } public function stashChanges() { throw new ArcanistCapabilityNotSupportedException($this); } public function unstashChanges() { throw new ArcanistCapabilityNotSupportedException($this); } /* -( Scratch Files )------------------------------------------------------ */ /** * Try to read a scratch file, if it exists and is readable. * * @param string Scratch file name. * @return mixed String for file contents, or false for failure. * @task scratch */ public function readScratchFile($path) { $full_path = $this->getScratchFilePath($path); if (!$full_path) { return false; } if (!Filesystem::pathExists($full_path)) { return false; } try { $result = Filesystem::readFile($full_path); } catch (FilesystemException $ex) { return false; } return $result; } /** * Try to write a scratch file, if there's somewhere to put it and we can * write there. * * @param string Scratch file name to write. * @param string Data to write. * @return bool True on success, false on failure. * @task scratch */ public function writeScratchFile($path, $data) { $dir = $this->getScratchFilePath(''); if (!$dir) { return false; } if (!Filesystem::pathExists($dir)) { try { Filesystem::createDirectory($dir); } catch (Exception $ex) { return false; } } try { Filesystem::writeFile($this->getScratchFilePath($path), $data); } catch (FilesystemException $ex) { return false; } return true; } /** * Try to remove a scratch file. * * @param string Scratch file name to remove. * @return bool True if the file was removed successfully. * @task scratch */ public function removeScratchFile($path) { $full_path = $this->getScratchFilePath($path); if (!$full_path) { return false; } try { Filesystem::remove($full_path); } catch (FilesystemException $ex) { return false; } return true; } /** * Get a human-readable description of the scratch file location. * * @param string Scratch file name. * @return mixed String, or false on failure. * @task scratch */ public function getReadableScratchFilePath($path) { $full_path = $this->getScratchFilePath($path); if ($full_path) { return Filesystem::readablePath( $full_path, $this->getPath()); } else { return false; } } /** * Get the path to a scratch file, if possible. * * @param string Scratch file name. * @return mixed File path, or false on failure. * @task scratch */ public function getScratchFilePath($path) { $new_scratch_path = Filesystem::resolvePath( 'arc', $this->getMetadataPath()); static $checked = false; if (!$checked) { $checked = true; $old_scratch_path = $this->getPath('.arc'); // we only want to do the migration once // unfortunately, people have checked in .arc directories which // means that the old one may get recreated after we delete it if (Filesystem::pathExists($old_scratch_path) && !Filesystem::pathExists($new_scratch_path)) { Filesystem::createDirectory($new_scratch_path); $existing_files = Filesystem::listDirectory($old_scratch_path, true); foreach ($existing_files as $file) { $new_path = Filesystem::resolvePath($file, $new_scratch_path); $old_path = Filesystem::resolvePath($file, $old_scratch_path); Filesystem::writeFile( $new_path, Filesystem::readFile($old_path)); } Filesystem::remove($old_scratch_path); } } return Filesystem::resolvePath($path, $new_scratch_path); } /* -( Base Commits )------------------------------------------------------- */ abstract public function supportsCommitRanges(); final public function setBaseCommit($symbolic_commit) { if (!$this->supportsCommitRanges()) { throw new ArcanistCapabilityNotSupportedException($this); } $this->symbolicBaseCommit = $symbolic_commit; $this->reloadCommitRange(); return $this; } public function setHeadCommit($symbolic_commit) { throw new ArcanistCapabilityNotSupportedException($this); } final public function getBaseCommit() { if (!$this->supportsCommitRanges()) { throw new ArcanistCapabilityNotSupportedException($this); } if ($this->resolvedBaseCommit === null) { $commit = $this->buildBaseCommit($this->symbolicBaseCommit); $this->resolvedBaseCommit = $commit; } return $this->resolvedBaseCommit; } public function getHeadCommit() { throw new ArcanistCapabilityNotSupportedException($this); } final public function reloadCommitRange() { $this->resolvedBaseCommit = null; $this->baseCommitExplanation = null; $this->didReloadCommitRange(); return $this; } protected function didReloadCommitRange() { return; } protected function buildBaseCommit($symbolic_commit) { throw new ArcanistCapabilityNotSupportedException($this); } public function getBaseCommitExplanation() { return $this->baseCommitExplanation; } public function setBaseCommitExplanation($explanation) { $this->baseCommitExplanation = $explanation; return $this; } public function resolveBaseCommitRule($rule, $source) { return null; } public function setBaseCommitArgumentRules($base_commit_argument_rules) { $this->baseCommitArgumentRules = $base_commit_argument_rules; return $this; } public function getBaseCommitArgumentRules() { return $this->baseCommitArgumentRules; } public function resolveBaseCommit() { $base_commit_rules = array( 'runtime' => $this->getBaseCommitArgumentRules(), 'local' => '', 'project' => '', 'user' => '', 'system' => '', ); $all_sources = $this->configurationManager->getConfigFromAllSources('base'); $base_commit_rules = $all_sources + $base_commit_rules; $parser = new ArcanistBaseCommitParser($this); $commit = $parser->resolveBaseCommit($base_commit_rules); return $commit; } public function getRepositoryUUID() { return null; } } diff --git a/src/repository/api/ArcanistSubversionAPI.php b/src/repository/api/ArcanistSubversionAPI.php index 9b529c08..e9536111 100644 --- a/src/repository/api/ArcanistSubversionAPI.php +++ b/src/repository/api/ArcanistSubversionAPI.php @@ -1,701 +1,705 @@ getPath()) as $parent) { $possible_svn_dir = Filesystem::resolvePath('.svn', $parent); if (Filesystem::pathExists($possible_svn_dir)) { $svn_dir = $possible_svn_dir; break; } } } return $svn_dir; } protected function buildLocalFuture(array $argv) { - $argv[0] = 'svn '.$argv[0]; $future = newv('ExecFuture', $argv); $future->setCWD($this->getPath()); return $future; } protected function buildCommitRangeStatus() { // In SVN, the commit range is always "uncommitted changes", so these // statuses are equivalent. return $this->getUncommittedStatus(); } protected function buildUncommittedStatus() { return $this->getSVNStatus(); } public function getSVNBaseRevisions() { if ($this->svnBaseRevisions === null) { $this->getSVNStatus(); } return $this->svnBaseRevisions; } public function limitStatusToPaths(array $paths) { $this->statusPaths = $paths; return $this; } public function getSVNStatus($with_externals = false) { if ($this->svnStatus === null) { if ($this->statusPaths) { list($status) = $this->execxLocal( '--xml status %Ls', $this->statusPaths); } else { list($status) = $this->execxLocal('--xml status'); } $xml = new SimpleXMLElement($status); $externals = array(); $files = array(); foreach ($xml->target as $target) { $this->svnBaseRevisions = array(); foreach ($target->entry as $entry) { $path = (string)$entry['path']; // On Windows, we get paths with backslash directory separators here. // Normalize them to the format everything else expects and generates. if (phutil_is_windows()) { $path = str_replace(DIRECTORY_SEPARATOR, '/', $path); } $mask = 0; $props = (string)($entry->{'wc-status'}[0]['props']); $item = (string)($entry->{'wc-status'}[0]['item']); $base = (string)($entry->{'wc-status'}[0]['revision']); $this->svnBaseRevisions[$path] = $base; switch ($props) { case 'none': case 'normal': break; case 'modified': $mask |= self::FLAG_MODIFIED; break; default: - throw new Exception("Unrecognized property status '{$props}'."); + throw new Exception(pht( + "Unrecognized property status '%s'.", + $props)); } $mask |= $this->parseSVNStatus($item); if ($item == 'external') { $externals[] = $path; } // This is new in or around Subversion 1.6. $tree_conflicts = ($entry->{'wc-status'}[0]['tree-conflicted']); if ((string)$tree_conflicts) { $mask |= self::FLAG_CONFLICT; } $files[$path] = $mask; } } foreach ($files as $path => $mask) { foreach ($externals as $external) { if (!strncmp($path.'/', $external.'/', strlen($external) + 1)) { $files[$path] |= self::FLAG_EXTERNALS; } } } $this->svnStatus = $files; } $status = $this->svnStatus; if (!$with_externals) { foreach ($status as $path => $mask) { if ($mask & ArcanistRepositoryAPI::FLAG_EXTERNALS) { unset($status[$path]); } } } return $status; } private function parseSVNStatus($item) { switch ($item) { case 'none': // We can get 'none' for property changes on a directory. case 'normal': return 0; case 'external': return self::FLAG_EXTERNALS; case 'unversioned': return self::FLAG_UNTRACKED; case 'obstructed': return self::FLAG_OBSTRUCTED; case 'missing': return self::FLAG_MISSING; case 'added': return self::FLAG_ADDED; case 'replaced': // This is the result of "svn rm"-ing a file, putting another one // in place of it, and then "svn add"-ing the new file. Just treat // this as equivalent to "modified". return self::FLAG_MODIFIED; case 'modified': return self::FLAG_MODIFIED; case 'deleted': return self::FLAG_DELETED; case 'conflicted': return self::FLAG_CONFLICT; case 'incomplete': return self::FLAG_INCOMPLETE; default: - throw new Exception("Unrecognized item status '{$item}'."); + throw new Exception(pht("Unrecognized item status '%s'.", $item)); } } public function addToCommit(array $paths) { $add = array_filter($paths, 'Filesystem::pathExists'); if ($add) { $this->execxLocal( 'add -- %Ls', $add); } if ($add != $paths) { $this->execxLocal( 'delete -- %Ls', array_diff($paths, $add)); } $this->svnStatus = null; } public function getSVNProperty($path, $property) { list($stdout) = execx( 'svn propget %s %s@', $property, $this->getPath($path)); return trim($stdout); } public function getSourceControlPath() { return idx($this->getSVNInfo('/'), 'URL'); } public function getSourceControlBaseRevision() { $info = $this->getSVNInfo('/'); return $info['URL'].'@'.$this->getSVNBaseRevisionNumber(); } public function getCanonicalRevisionName($string) { // TODO: This could be more accurate, but is only used by `arc browse` // for now. if (is_numeric($string)) { return $string; } return null; } public function getSVNBaseRevisionNumber() { if ($this->svnBaseRevisionNumber) { return $this->svnBaseRevisionNumber; } $info = $this->getSVNInfo('/'); return $info['Revision']; } public function overrideSVNBaseRevisionNumber($effective_base_revision) { $this->svnBaseRevisionNumber = $effective_base_revision; return $this; } public function getBranchName() { $info = $this->getSVNInfo('/'); $repo_root = idx($info, 'Repository Root'); $repo_root_length = strlen($repo_root); $url = idx($info, 'URL'); if (substr($url, 0, $repo_root_length) == $repo_root) { return substr($url, $repo_root_length); } return 'svn'; } public function getRemoteURI() { return idx($this->getSVNInfo('/'), 'Repository Root'); } public function buildInfoFuture($path) { if ($path == '/') { // When the root of a working copy is referenced by a symlink and you // execute 'svn info' on that symlink, svn fails. This is a longstanding // bug in svn: // // See http://subversion.tigris.org/issues/show_bug.cgi?id=2305 // // To reproduce, do: // // $ ln -s working_copy working_link // $ svn info working_copy # ok // $ svn info working_link # fails // // Work around this by cd-ing into the directory before executing // 'svn info'. return $this->buildLocalFuture(array('info .')); } else { // Note: here and elsewhere we need to append "@" to the path because if // a file has a literal "@" in it, everything after that will be // interpreted as a revision. By appending "@" with no argument, SVN // parses it properly. return $this->buildLocalFuture(array('info %s@', $this->getPath($path))); } } public function buildDiffFuture($path) { $root = phutil_get_library_root('arcanist'); // The "--depth empty" flag prevents us from picking up changes in // children when we run 'diff' against a directory. Specifically, when a // user has added or modified some directory "example/", we want to return // ONLY changes to that directory when given it as a path. If we run // without "--depth empty", svn will give us changes to the directory // itself (such as property changes) and also give us changes to any // files within the directory (basically, implicit recursion). We don't // want that, so prevent recursive diffing. This flag does not work if the // directory is newly added (see T5555) so we need to filter the results // out later as well. if (phutil_is_windows()) { // TODO: Provide a binary_safe_diff script for Windows. // TODO: Provide a diff command which can take lines of context somehow. return $this->buildLocalFuture( array( 'diff --depth empty %s', $path, )); } else { $diff_bin = $root.'/../scripts/repository/binary_safe_diff.sh'; $diff_cmd = Filesystem::resolvePath($diff_bin); return $this->buildLocalFuture( array( 'diff --depth empty --diff-cmd %s -x -U%d %s', $diff_cmd, $this->getDiffLinesOfContext(), $path, )); } } public function primeSVNInfoResult($path, $result) { $this->svnInfoRaw[$path] = $result; return $this; } public function primeSVNDiffResult($path, $result) { $this->svnDiffRaw[$path] = $result; return $this; } public function getSVNInfo($path) { if (empty($this->svnInfo[$path])) { if (empty($this->svnInfoRaw[$path])) { $this->svnInfoRaw[$path] = $this->buildInfoFuture($path)->resolve(); } list($err, $stdout) = $this->svnInfoRaw[$path]; if ($err) { throw new Exception( - "Error #{$err} executing svn info against '{$path}'."); + pht("Error #%d executing svn info against '%s'.", $err, $path)); } // TODO: Hack for Windows. $stdout = str_replace("\r\n", "\n", $stdout); $patterns = array( '/^(URL): (\S+)$/m', '/^(Revision): (\d+)$/m', '/^(Last Changed Author): (\S+)$/m', '/^(Last Changed Rev): (\d+)$/m', '/^(Last Changed Date): (.+) \(.+\)$/m', '/^(Copied From URL): (\S+)$/m', '/^(Copied From Rev): (\d+)$/m', '/^(Repository Root): (\S+)$/m', '/^(Repository UUID): (\S+)$/m', '/^(Node Kind): (\S+)$/m', ); $result = array(); foreach ($patterns as $pattern) { $matches = null; if (preg_match($pattern, $stdout, $matches)) { $result[$matches[1]] = $matches[2]; } } if (isset($result['Last Changed Date'])) { $result['Last Changed Date'] = strtotime($result['Last Changed Date']); } if (empty($result)) { - throw new Exception('Unable to parse SVN info.'); + throw new Exception(pht('Unable to parse SVN info.')); } $this->svnInfo[$path] = $result; } return $this->svnInfo[$path]; } public function getRawDiffText($path) { $status = $this->getSVNStatus(); if (!isset($status[$path])) { return null; } $status = $status[$path]; // Build meaningful diff text for "svn copy" operations. if ($status & ArcanistRepositoryAPI::FLAG_ADDED) { $info = $this->getSVNInfo($path); if (!empty($info['Copied From URL'])) { return $this->buildSyntheticAdditionDiff( $path, $info['Copied From URL'], $info['Copied From Rev']); } } // If we run "diff" on a binary file which doesn't have the "svn:mime-type" // of "application/octet-stream", `diff' will explode in a rain of // unhelpful hellfire as it tries to build a textual diff of the two // files. We just fix this inline since it's pretty unambiguous. // TODO: Move this to configuration? $matches = null; if (preg_match('/\.(gif|png|jpe?g|swf|pdf|ico)$/i', $path, $matches)) { // Check if the file is deleted first; SVN will complain if we try to // get properties of a deleted file. if ($status & ArcanistRepositoryAPI::FLAG_DELETED) { return <<getSVNProperty($path, 'svn:mime-type'); if ($mime != 'application/octet-stream') { execx( 'svn propset svn:mime-type application/octet-stream %s', self::escapeFileNameForSVN($this->getPath($path))); } } if (empty($this->svnDiffRaw[$path])) { $this->svnDiffRaw[$path] = $this->buildDiffFuture($path)->resolve(); } list($err, $stdout, $stderr) = $this->svnDiffRaw[$path]; // Note: GNU Diff returns 2 when SVN hands it binary files to diff and they // differ. This is not an error; it is documented behavior. But SVN isn't // happy about it. SVN will exit with code 1 and return the string below. if ($err != 0 && $stderr !== "svn: 'diff' returned 2\n") { throw new Exception( - "svn diff returned unexpected error code: $err\n". - "stdout: $stdout\n". - "stderr: $stderr"); + pht( + "%s returned unexpected error code: %d\nstdout: %s\nstderr: %s", + 'svn diff', + $err, + $stdout, + $stderr)); } if ($err == 0 && empty($stdout)) { // If there are no changes, 'diff' exits with no output, but that means // we can not distinguish between empty and unmodified files. Build a // synthetic "diff" without any changes in it. return $this->buildSyntheticUnchangedDiff($path); } return $stdout; } protected function buildSyntheticAdditionDiff($path, $source, $rev) { $type = $this->getSVNProperty($path, 'svn:mime-type'); if ($type == 'application/octet-stream') { return <<getPath($path))) { return null; } $data = Filesystem::readFile($this->getPath($path)); list($orig) = execx('svn cat %s@%s', $source, $rev); $src = new TempFile(); $dst = new TempFile(); Filesystem::writeFile($src, $orig); Filesystem::writeFile($dst, $data); list($err, $diff) = exec_manual( 'diff -L a/%s -L b/%s -U%d %s %s', str_replace($this->getSourceControlPath().'/', '', $source), $path, $this->getDiffLinesOfContext(), $src, $dst); if ($err == 1) { // 1 means there are differences. return <<buildSyntheticUnchangedDiff($path); } } protected function buildSyntheticUnchangedDiff($path) { $full_path = $this->getPath($path); if (is_dir($full_path)) { return null; } if (!file_exists($full_path)) { return null; } $data = Filesystem::readFile($full_path); $lines = explode("\n", $data); $len = count($lines); foreach ($lines as $key => $line) { $lines[$key] = ' '.$line; } $lines = implode("\n", $lines); return <<buildLocalFuture(array('list -R')); return new PhutilCallbackFilterIterator( new LinesOfALargeExecFuture($future), array($this, 'filterFiles')); } public function getChangedFiles($since_commit) { $url = ''; $match = null; if (preg_match('/(.*)@(.*)/', $since_commit, $match)) { list(, $url, $since_commit) = $match; } // TODO: Handle paths with newlines. list($stdout) = $this->execxLocal( '--xml diff --revision %s:HEAD --summarize %s', $since_commit, $url); $xml = new SimpleXMLElement($stdout); $return = array(); foreach ($xml->paths[0]->path as $path) { $return[(string)$path] = $this->parseSVNStatus($path['item']); } return $return; } public function filterFiles($path) { // NOTE: SVN uses '/' also on Windows. if ($path == '' || substr($path, -1) == '/') { return null; } return $path; } public function getBlame($path) { $blame = array(); list($stdout) = $this->execxLocal('blame %s', $path); $stdout = trim($stdout); if (!strlen($stdout)) { // Empty file. return $blame; } foreach (explode("\n", $stdout) as $line) { $m = array(); if (!preg_match('/^\s*(\d+)\s+(\S+)/', $line, $m)) { - throw new Exception("Bad blame? `{$line}'"); + throw new Exception(pht("Bad blame? `%s'", $line)); } $revision = $m[1]; $author = $m[2]; $blame[] = array($author, $revision); } return $blame; } public function getOriginalFileData($path) { // SVN issues warnings for nonexistent paths, directories, etc., but still // returns no error code. However, for new paths in the working copy it // fails. Assume that failure means the original file does not exist. list($err, $stdout) = $this->execManualLocal('cat %s@', $path); if ($err) { return null; } return $stdout; } public function getCurrentFileData($path) { $full_path = $this->getPath($path); if (Filesystem::pathExists($full_path)) { return Filesystem::readFile($full_path); } return null; } public function getRepositoryUUID() { $info = $this->getSVNInfo('/'); return $info['Repository UUID']; } public function getLocalCommitInformation() { return null; } public function isHistoryDefaultImmutable() { return true; } public function supportsAmend() { return false; } public function supportsCommitRanges() { return false; } public function supportsLocalCommits() { return false; } public function hasLocalCommit($commit) { return false; } public function getWorkingCopyRevision() { return $this->getSourceControlBaseRevision(); } public function supportsLocalBranchMerge() { return false; } public function getFinalizedRevisionMessage() { // In other VCSes we give push instructions here, but it never makes sense // in SVN. return 'Done.'; } public function loadWorkingCopyDifferentialRevisions( ConduitClient $conduit, array $query) { // We don't have much to go on in SVN, look for revisions that came from // this directory and belong to the same project. $project = $this->getWorkingCopyIdentity()->getProjectID(); if (!$project) { return array(); } $results = $conduit->callMethodSynchronous( 'differential.query', $query + array( 'arcanistProjects' => array($project), )); foreach ($results as $key => $result) { if ($result['sourcePath'] != $this->getPath()) { unset($results[$key]); } } foreach ($results as $key => $result) { - $results[$key]['why'] = - 'Matching arcanist project name and working copy directory path.'; + $results[$key]['why'] = pht( + 'Matching arcanist project name and working copy directory path.'); } return $results; } public function updateWorkingCopy() { $this->execxLocal('up'); } public static function escapeFileNamesForSVN(array $files) { foreach ($files as $k => $file) { $files[$k] = self::escapeFileNameForSVN($file); } return $files; } public static function escapeFileNameForSVN($file) { // SVN interprets "x@1" as meaning "file x at revision 1", which is not // intended for files named "sprite@2x.png" or similar. For files with an // "@" in their names, escape them by adding "@" at the end, which SVN // interprets as "at the working copy revision". There is a special case // where ".@" means "fail with an error" instead of ". at the working copy // revision", so avoid escaping "." into ".@". if (strpos($file, '@') !== false) { $file = $file.'@'; } return $file; } } diff --git a/src/repository/api/__tests__/ArcanistRepositoryAPIStateTestCase.php b/src/repository/api/__tests__/ArcanistRepositoryAPIStateTestCase.php index ed13af09..236f46c7 100644 --- a/src/repository/api/__tests__/ArcanistRepositoryAPIStateTestCase.php +++ b/src/repository/api/__tests__/ArcanistRepositoryAPIStateTestCase.php @@ -1,123 +1,123 @@ parseState('git_basic.git.tgz'); } else { - $this->assertSkipped('Git is not installed'); + $this->assertSkipped(pht('Git is not installed')); } } public function testHgStateParsing() { if (Filesystem::binaryExists('hg')) { $this->parseState('hg_basic.hg.tgz'); } else { - $this->assertSkipped('Mercurial is not installed'); + $this->assertSkipped(pht('Mercurial is not installed')); } } public function testSvnStateParsing() { if (Filesystem::binaryExists('svn')) { $this->parseState('svn_basic.svn.tgz'); } else { - $this->assertSkipped('Subversion is not installed'); + $this->assertSkipped(pht('Subversion is not installed')); } } private function parseState($test) { $dir = dirname(__FILE__).'/state/'; $fixture = PhutilDirectoryFixture::newFromArchive($dir.'/'.$test); $fixture_path = $fixture->getPath(); $working_copy = ArcanistWorkingCopyIdentity::newFromPath($fixture_path); $configuration_manager = new ArcanistConfigurationManager(); $configuration_manager->setWorkingCopyIdentity($working_copy); $api = ArcanistRepositoryAPI::newAPIFromConfigurationManager( $configuration_manager); $api->setBaseCommitArgumentRules('arc:this'); if ($api instanceof ArcanistSubversionAPI) { // Upgrade the repository so that the test will still pass if the local // `svn` is newer than the `svn` which created the repository. // NOTE: Some versions of Subversion (1.7.x?) exit with an error code on // a no-op upgrade, although newer versions do not. We just ignore the // error here; if it's because of an actual problem we'll hit an error // shortly anyway. $api->execManualLocal('upgrade'); } $this->assertCorrectState($test, $api); } private function assertCorrectState($test, ArcanistRepositoryAPI $api) { $f_mod = ArcanistRepositoryAPI::FLAG_MODIFIED; $f_add = ArcanistRepositoryAPI::FLAG_ADDED; $f_del = ArcanistRepositoryAPI::FLAG_DELETED; $f_unt = ArcanistRepositoryAPI::FLAG_UNTRACKED; $f_con = ArcanistRepositoryAPI::FLAG_CONFLICT; $f_mis = ArcanistRepositoryAPI::FLAG_MISSING; $f_uns = ArcanistRepositoryAPI::FLAG_UNSTAGED; $f_unc = ArcanistRepositoryAPI::FLAG_UNCOMMITTED; $f_ext = ArcanistRepositoryAPI::FLAG_EXTERNALS; $f_obs = ArcanistRepositoryAPI::FLAG_OBSTRUCTED; $f_inc = ArcanistRepositoryAPI::FLAG_INCOMPLETE; switch ($test) { case 'svn_basic.svn.tgz': $expect = array( 'ADDED' => $f_add, 'COPIED_TO' => $f_add, 'DELETED' => $f_del, 'MODIFIED' => $f_mod, 'MOVED' => $f_del, 'MOVED_TO' => $f_add, 'PROPCHANGE' => $f_mod, 'UNTRACKED' => $f_unt, ); $this->assertEqual($expect, $api->getUncommittedStatus()); $this->assertEqual($expect, $api->getCommitRangeStatus()); break; case 'git_basic.git.tgz': $expect_uncommitted = array( 'UNCOMMITTED' => $f_add | $f_unc, 'UNSTAGED' => $f_mod | $f_uns | $f_unc, 'UNTRACKED' => $f_unt, ); $this->assertEqual($expect_uncommitted, $api->getUncommittedStatus()); $expect_range = array( 'ADDED' => $f_add, 'DELETED' => $f_del, 'MODIFIED' => $f_mod, 'UNCOMMITTED' => $f_add, 'UNSTAGED' => $f_add, ); $this->assertEqual($expect_range, $api->getCommitRangeStatus()); break; case 'hg_basic.hg.tgz': $expect_uncommitted = array( 'UNCOMMITTED' => $f_mod | $f_unc, 'UNTRACKED' => $f_unt, ); $this->assertEqual($expect_uncommitted, $api->getUncommittedStatus()); $expect_range = array( 'ADDED' => $f_add, 'DELETED' => $f_del, 'MODIFIED' => $f_mod, 'UNCOMMITTED' => $f_add, ); $this->assertEqual($expect_range, $api->getCommitRangeStatus()); break; default: throw new Exception( - "No test cases for working copy '{$test}'!"); + pht("No test cases for working copy '%s'!", $test)); } } } diff --git a/src/repository/parser/ArcanistMercurialParser.php b/src/repository/parser/ArcanistMercurialParser.php index d5d2bf13..b9044500 100644 --- a/src/repository/parser/ArcanistMercurialParser.php +++ b/src/repository/parser/ArcanistMercurialParser.php @@ -1,225 +1,236 @@ $flags, 'from' => null, ); $last_path = $path; } return $result; } /** * Parse the output of "hg status". This provides only basic information, you * can get more detailed information by invoking * @{method:parseMercurialStatusDetails}. * * @param string The stdout from running an "hg status" command. * @return dict Map of paths to ArcanistRepositoryAPI status flags. * @task parse */ public static function parseMercurialStatus($stdout) { $result = self::parseMercurialStatusDetails($stdout); return ipull($result, 'flags'); } /** * Parse the output of "hg log". This also parses "hg outgoing", "hg parents", * and other similar commands. This assumes "--style default". * * @param string The stdout from running an "hg log" command. * @return list List of dictionaries with commit information. * @task parse */ public static function parseMercurialLog($stdout) { $result = array(); $stdout = trim($stdout); if (!strlen($stdout)) { return $result; } $chunks = explode("\n\n", $stdout); foreach ($chunks as $chunk) { $commit = array(); $lines = explode("\n", $chunk); foreach ($lines as $line) { if (preg_match('/^(comparing with|searching for changes)/', $line)) { // These are sent to stdout when you run "hg outgoing" although the // format is otherwise identical to "hg log". continue; } if (preg_match('/^remote:/', $line)) { // This indicates remote error in "hg outgoing". continue; } list($name, $value) = explode(':', $line, 2); $value = trim($value); switch ($name) { case 'user': $commit['user'] = $value; break; case 'date': $commit['date'] = strtotime($value); break; case 'summary': $commit['summary'] = $value; break; case 'changeset': list($local, $rev) = explode(':', $value, 2); $commit['local'] = $local; $commit['rev'] = $rev; break; case 'parent': if (empty($commit['parents'])) { $commit['parents'] = array(); } list($local, $rev) = explode(':', $value, 2); $commit['parents'][] = array( 'local' => $local, 'rev' => $rev, ); break; case 'branch': $commit['branch'] = $value; break; case 'tag': $commit['tag'] = $value; break; case 'bookmark': $commit['bookmark'] = $value; break; default: - throw new Exception("Unknown Mercurial log field '{$name}'!"); + throw new Exception( + pht("Unknown Mercurial log field '%s'!", $name)); } } $result[] = $commit; } return $result; } /** * Parse the output of "hg branches". * * @param string The stdout from running an "hg branches" command. * @return list A list of dictionaries with branch information. * @task parse */ public static function parseMercurialBranches($stdout) { $stdout = rtrim($stdout, "\n"); if (!strlen($stdout)) { // No branches; commonly, this occurs in a newly initialized repository. return array(); } $lines = explode("\n", $stdout); $branches = array(); foreach ($lines as $line) { $matches = null; // Output of "hg branches" normally looks like: // // default 15101:a21ccf4412d5 // // ...but may also have human-readable cues like: // // stable 15095:ec222a29bdf0 (inactive) // // See the unit tests for more examples. $regexp = '/^(\S+(?:\s+\S+)*)\s+(\d+):([a-f0-9]+)(\s+\\(inactive\\))?$/'; if (!preg_match($regexp, $line, $matches)) { - throw new Exception("Failed to parse 'hg branches' output: {$line}"); + throw new Exception( + pht( + "Failed to parse '%s' output: %s", + 'hg branches', + $line)); } $branches[$matches[1]] = array( 'local' => $matches[2], 'rev' => $matches[3], ); } return $branches; } } diff --git a/src/repository/parser/__tests__/ArcanistMercurialParserTestCase.php b/src/repository/parser/__tests__/ArcanistMercurialParserTestCase.php index 2129448d..6ff5f426 100644 --- a/src/repository/parser/__tests__/ArcanistMercurialParserTestCase.php +++ b/src/repository/parser/__tests__/ArcanistMercurialParserTestCase.php @@ -1,89 +1,90 @@ parseData( basename($file), Filesystem::readFile($root.'/'.$file)); } } private function parseData($name, $data) { switch ($name) { case 'branches-basic.txt': $output = ArcanistMercurialParser::parseMercurialBranches($data); $this->assertEqual( array('default', 'stable'), array_keys($output)); $this->assertEqual( array('a21ccf4412d5', 'ec222a29bdf0'), array_values(ipull($output, 'rev'))); break; case 'branches-with-spaces.txt': $output = ArcanistMercurialParser::parseMercurialBranches($data); $this->assertEqual( array( 'm m m m m 2:ffffffffffff (inactive)', 'xxx yyy zzz', 'default', "'", ), array_keys($output)); $this->assertEqual( array('0b9d8290c4e0', '78963faacfc7', '5db03c5500c6', 'ffffffffffff'), array_values(ipull($output, 'rev'))); break; case 'branches-empty.txt': $output = ArcanistMercurialParser::parseMercurialBranches($data); $this->assertEqual(array(), $output); break; case 'log-basic.txt': $output = ArcanistMercurialParser::parseMercurialLog($data); $this->assertEqual( 3, count($output)); $this->assertEqual( array('a21ccf4412d5', 'a051f8a6a7cc', 'b1f49efeab65'), array_values(ipull($output, 'rev'))); break; case 'log-empty.txt': // Empty logs (e.g., "hg parents" for a root revision) should parse // correctly. $output = ArcanistMercurialParser::parseMercurialLog($data); $this->assertEqual( array(), $output); break; case 'status-basic.txt': $output = ArcanistMercurialParser::parseMercurialStatus($data); $this->assertEqual( 4, count($output)); $this->assertEqual( array('changed', 'added', 'removed', 'untracked'), array_keys($output)); break; case 'status-moves.txt': $output = ArcanistMercurialParser::parseMercurialStatusDetails($data); $this->assertEqual( 'move_source', $output['moved_file']['from']); $this->assertEqual( null, $output['changed_file']['from']); $this->assertEqual( 'copy_source', $output['copied_file']['from']); $this->assertEqual( null, idx($output, 'copy_source')); break; default: - throw new Exception("No test information for test data '{$name}'!"); + throw new Exception( + pht("No test information for test data '%s'!", $name)); } } } diff --git a/src/unit/engine/ArcanistUnitTestEngine.php b/src/unit/engine/ArcanistUnitTestEngine.php index c449ed6f..8dd6e672 100644 --- a/src/unit/engine/ArcanistUnitTestEngine.php +++ b/src/unit/engine/ArcanistUnitTestEngine.php @@ -1,120 +1,122 @@ supportsRunAllTests() && $run_all_tests) { - $class = get_class($this); throw new Exception( - "Engine '{$class}' does not support --everything."); + pht( + "Engine '%s' does not support %s.", + get_class($this), + '--everything')); } $this->runAllTests = $run_all_tests; return $this; } public function getRunAllTests() { return $this->runAllTests; } protected function supportsRunAllTests() { return false; } final public function __construct() {} public function setConfigurationManager( ArcanistConfigurationManager $configuration_manager) { $this->configurationManager = $configuration_manager; return $this; } public function getConfigurationManager() { return $this->configurationManager; } final public function setWorkingCopy( ArcanistWorkingCopyIdentity $working_copy) { // TODO: Remove this once ArcanistBaseUnitTestEngine is gone. if ($this instanceof ArcanistBaseUnitTestEngine) { phutil_deprecated( 'ArcanistBaseUnitTestEngine', - 'You should extend from `ArcanistUnitTestEngine` instead.'); + pht('You should extend from `%s` instead.', __CLASS__)); } $this->workingCopy = $working_copy; return $this; } final public function getWorkingCopy() { return $this->workingCopy; } final public function setPaths(array $paths) { $this->paths = $paths; return $this; } final public function getPaths() { return $this->paths; } final public function setArguments(array $arguments) { $this->arguments = $arguments; return $this; } final public function getArgument($key, $default = null) { return idx($this->arguments, $key, $default); } final public function setEnableAsyncTests($enable_async_tests) { $this->enableAsyncTests = $enable_async_tests; return $this; } final public function getEnableAsyncTests() { return $this->enableAsyncTests; } final public function setEnableCoverage($enable_coverage) { $this->enableCoverage = $enable_coverage; return $this; } final public function getEnableCoverage() { return $this->enableCoverage; } public function setRenderer(ArcanistUnitRenderer $renderer) { $this->renderer = $renderer; return $this; } abstract public function run(); /** * Modify the return value of this function in the child class, if you do * not need to echo the test results after all the tests have been run. This * is the case for example when the child class prints the tests results * while the tests are running. */ public function shouldEchoTestResults() { return true; } } diff --git a/src/unit/engine/CSharpToolsTestEngine.php b/src/unit/engine/CSharpToolsTestEngine.php index 03ad75bb..7fd23e0d 100644 --- a/src/unit/engine/CSharpToolsTestEngine.php +++ b/src/unit/engine/CSharpToolsTestEngine.php @@ -1,282 +1,287 @@ getConfigurationManager(); $this->cscoverHintPath = $config->getConfigFromAnySource( 'unit.csharp.cscover.binary'); $this->matchRegex = $config->getConfigFromAnySource( 'unit.csharp.coverage.match'); $this->excludedFiles = $config->getConfigFromAnySource( 'unit.csharp.coverage.excluded'); parent::loadEnvironment(); if ($this->getEnableCoverage() === false) { return; } // Determine coverage path. if ($this->cscoverHintPath === null) { throw new Exception( - "Unable to locate cscover. Configure it with ". - "the `unit.csharp.coverage.binary' option in .arcconfig"); + pht( + "Unable to locate %s. Configure it with the '%s' option in %s.", + 'cscover', + 'unit.csharp.coverage.binary', + '.arcconfig')); } $cscover = $this->projectRoot.DIRECTORY_SEPARATOR.$this->cscoverHintPath; if (file_exists($cscover)) { $this->coverEngine = Filesystem::resolvePath($cscover); } else { throw new Exception( - 'Unable to locate cscover coverage runner (have you built yet?)'); + pht( + 'Unable to locate %s coverage runner (have you built yet?)', + 'cscover')); } } /** * Returns whether the specified assembly should be instrumented for * code coverage reporting. Checks the excluded file list and the * matching regex if they are configured. * * @return boolean Whether the assembly should be instrumented. */ private function assemblyShouldBeInstrumented($file) { if ($this->excludedFiles !== null) { if (array_key_exists((string)$file, $this->excludedFiles)) { return false; } } if ($this->matchRegex !== null) { if (preg_match($this->matchRegex, $file) === 1) { return true; } else { return false; } } return true; } /** * Overridden version of `buildTestFuture` so that the unit test can be run * via `cscover`, which instruments assemblies and reports on code coverage. * * @param string Name of the test assembly. * @return array The future, output filename and coverage filename * stored in an array. */ protected function buildTestFuture($test_assembly) { if ($this->getEnableCoverage() === false) { return parent::buildTestFuture($test_assembly); } // FIXME: Can't use TempFile here as xUnit doesn't like // UNIX-style full paths. It sees the leading / as the // start of an option flag, even when quoted. $xunit_temp = Filesystem::readRandomCharacters(10).'.results.xml'; if (file_exists($xunit_temp)) { unlink($xunit_temp); } $cover_temp = new TempFile(); $cover_temp->setPreserveFile(true); $xunit_cmd = $this->runtimeEngine; $xunit_args = null; if ($xunit_cmd === '') { $xunit_cmd = $this->testEngine; $xunit_args = csprintf( '%s /xml %s', $test_assembly, $xunit_temp); } else { $xunit_args = csprintf( '%s %s /xml %s', $this->testEngine, $test_assembly, $xunit_temp); } $assembly_dir = dirname($test_assembly); $assemblies_to_instrument = array(); foreach (Filesystem::listDirectory($assembly_dir) as $file) { if (substr($file, -4) == '.dll' || substr($file, -4) == '.exe') { if ($this->assemblyShouldBeInstrumented($file)) { $assemblies_to_instrument[] = $assembly_dir.DIRECTORY_SEPARATOR.$file; } } } if (count($assemblies_to_instrument) === 0) { return parent::buildTestFuture($test_assembly); } $future = new ExecFuture( '%C -o %s -c %s -a %s -w %s %Ls', trim($this->runtimeEngine.' '.$this->coverEngine), $cover_temp, $xunit_cmd, $xunit_args, $assembly_dir, $assemblies_to_instrument); $future->setCWD(Filesystem::resolvePath($this->projectRoot)); return array( $future, $assembly_dir.DIRECTORY_SEPARATOR.$xunit_temp, $cover_temp, ); } /** * Returns coverage results for the unit tests. * * @param string The name of the coverage file if one was provided by * `buildTestFuture`. * @return array Code coverage results, or null. */ protected function parseCoverageResult($cover_file) { if ($this->getEnableCoverage() === false) { return parent::parseCoverageResult($cover_file); } return $this->readCoverage($cover_file); } /** * Retrieves the cached results for a coverage result file. The coverage * result file is XML and can be large depending on what has been instrumented * so we cache it in case it's requested again. * * @param string The name of the coverage file. * @return array Code coverage results, or null if not cached. */ private function getCachedResultsIfPossible($cover_file) { if ($this->cachedResults == null) { $this->cachedResults = array(); } if (array_key_exists((string)$cover_file, $this->cachedResults)) { return $this->cachedResults[(string)$cover_file]; } return null; } /** * Stores the code coverage results in the cache. * * @param string The name of the coverage file. * @param array The results to cache. */ private function addCachedResults($cover_file, array $results) { if ($this->cachedResults == null) { $this->cachedResults = array(); } $this->cachedResults[(string)$cover_file] = $results; } /** * Processes a set of XML tags as code coverage results. We parse * the `instrumented` and `executed` tags with this method so that * we can access the data multiple times without a performance hit. * * @param array The array of XML tags to parse. * @return array A PHP array containing the data. */ private function processTags($tags) { $results = array(); foreach ($tags as $tag) { $results[] = array( 'file' => $tag->getAttribute('file'), 'start' => $tag->getAttribute('start'), 'end' => $tag->getAttribute('end'), ); } return $results; } /** * Reads the code coverage results from the cscover results file. * * @param string The path to the code coverage file. * @return array The code coverage results. */ public function readCoverage($cover_file) { $cached = $this->getCachedResultsIfPossible($cover_file); if ($cached !== null) { return $cached; } $coverage_dom = new DOMDocument(); $coverage_dom->loadXML(Filesystem::readFile($cover_file)); $modified = $this->getPaths(); $files = array(); $reports = array(); $instrumented = array(); $executed = array(); $instrumented = $this->processTags( $coverage_dom->getElementsByTagName('instrumented')); $executed = $this->processTags( $coverage_dom->getElementsByTagName('executed')); foreach ($instrumented as $instrument) { $absolute_file = $instrument['file']; $relative_file = substr($absolute_file, strlen($this->projectRoot) + 1); if (!in_array($relative_file, $files)) { $files[] = $relative_file; } } foreach ($files as $file) { $absolute_file = Filesystem::resolvePath( $this->projectRoot.DIRECTORY_SEPARATOR.$file); // get total line count in file $line_count = count(file($absolute_file)); $coverage = array(); for ($i = 0; $i < $line_count; $i++) { $coverage[$i] = 'N'; } foreach ($instrumented as $instrument) { if ($instrument['file'] !== $absolute_file) { continue; } for ( $i = $instrument['start']; $i <= $instrument['end']; $i++) { $coverage[$i - 1] = 'U'; } } foreach ($executed as $execute) { if ($execute['file'] !== $absolute_file) { continue; } for ( $i = $execute['start']; $i <= $execute['end']; $i++) { $coverage[$i - 1] = 'C'; } } $reports[$file] = implode($coverage); } $this->addCachedResults($cover_file, $reports); return $reports; } } diff --git a/src/unit/engine/NoseTestEngine.php b/src/unit/engine/NoseTestEngine.php index c5c95e3e..9af83c56 100644 --- a/src/unit/engine/NoseTestEngine.php +++ b/src/unit/engine/NoseTestEngine.php @@ -1,164 +1,166 @@ getPaths(); $affected_tests = array(); foreach ($paths as $path) { $absolute_path = Filesystem::resolvePath($path); if (is_dir($absolute_path)) { $absolute_test_path = Filesystem::resolvePath('tests/'.$path); if (is_readable($absolute_test_path)) { $affected_tests[] = $absolute_test_path; } } if (is_readable($absolute_path)) { $filename = basename($path); $directory = dirname($path); // assumes directory layout: tests//test_.py $relative_test_path = 'tests/'.$directory.'/test_'.$filename; $absolute_test_path = Filesystem::resolvePath($relative_test_path); if (is_readable($absolute_test_path)) { $affected_tests[] = $absolute_test_path; } } } return $this->runTests($affected_tests, './'); } public function runTests($test_paths, $source_path) { if (empty($test_paths)) { return array(); } $futures = array(); $tmpfiles = array(); foreach ($test_paths as $test_path) { $xunit_tmp = new TempFile(); $cover_tmp = new TempFile(); $future = $this->buildTestFuture($test_path, $xunit_tmp, $cover_tmp); $futures[$test_path] = $future; $tmpfiles[$test_path] = array( 'xunit' => $xunit_tmp, 'cover' => $cover_tmp, ); } $results = array(); $futures = id(new FutureIterator($futures)) ->limit(4); foreach ($futures as $test_path => $future) { try { list($stdout, $stderr) = $future->resolvex(); } catch(CommandException $exc) { if ($exc->getError() > 1) { // 'nose' returns 1 when tests are failing/broken. throw $exc; } } $xunit_tmp = $tmpfiles[$test_path]['xunit']; $cover_tmp = $tmpfiles[$test_path]['cover']; $this->parser = new ArcanistXUnitTestResultParser(); - $results[] = $this->parseTestResults($source_path, - $xunit_tmp, - $cover_tmp); + $results[] = $this->parseTestResults( + $source_path, + $xunit_tmp, + $cover_tmp); } return array_mergev($results); } public function buildTestFuture($path, $xunit_tmp, $cover_tmp) { - $cmd_line = csprintf('nosetests --with-xunit --xunit-file=%s', - $xunit_tmp); + $cmd_line = csprintf( + 'nosetests --with-xunit --xunit-file=%s', + $xunit_tmp); if ($this->getEnableCoverage() !== false) { $cmd_line .= csprintf( ' --with-coverage --cover-xml --cover-xml-file=%s', $cover_tmp); } return new ExecFuture('%C %s', $cmd_line, $path); } public function parseTestResults($source_path, $xunit_tmp, $cover_tmp) { $results = $this->parser->parseTestResults( Filesystem::readFile($xunit_tmp)); // coverage is for all testcases in the executed $path if ($this->getEnableCoverage() !== false) { $coverage = $this->readCoverage($cover_tmp, $source_path); foreach ($results as $result) { $result->setCoverage($coverage); } } return $results; } public function readCoverage($cover_file, $source_path) { $coverage_dom = new DOMDocument(); $coverage_dom->loadXML(Filesystem::readFile($cover_file)); $reports = array(); $classes = $coverage_dom->getElementsByTagName('class'); foreach ($classes as $class) { $path = $class->getAttribute('filename'); $root = $this->getWorkingCopy()->getProjectRoot(); if (!Filesystem::isDescendant($path, $root)) { continue; } // get total line count in file $line_count = count(phutil_split_lines(Filesystem::readFile($path))); $coverage = ''; $start_line = 1; $lines = $class->getElementsByTagName('line'); for ($ii = 0; $ii < $lines->length; $ii++) { $line = $lines->item($ii); $next_line = intval($line->getAttribute('number')); for ($start_line; $start_line < $next_line; $start_line++) { $coverage .= 'N'; } if (intval($line->getAttribute('hits')) == 0) { $coverage .= 'U'; } else if (intval($line->getAttribute('hits')) > 0) { $coverage .= 'C'; } $start_line++; } if ($start_line < $line_count) { foreach (range($start_line, $line_count) as $line_num) { $coverage .= 'N'; } } $reports[$path] = $coverage; } return $reports; } } diff --git a/src/unit/engine/PhpunitTestEngine.php b/src/unit/engine/PhpunitTestEngine.php index eb545fb1..8206b787 100644 --- a/src/unit/engine/PhpunitTestEngine.php +++ b/src/unit/engine/PhpunitTestEngine.php @@ -1,278 +1,280 @@ projectRoot = $this->getWorkingCopy()->getProjectRoot(); $this->affectedTests = array(); foreach ($this->getPaths() as $path) { $path = Filesystem::resolvePath($path, $this->projectRoot); // TODO: add support for directories // Users can call phpunit on the directory themselves if (is_dir($path)) { continue; } // Not sure if it would make sense to go further if // it is not a .php file if (substr($path, -4) != '.php') { continue; } if (substr($path, -8) == 'Test.php') { // Looks like a valid test file name. $this->affectedTests[$path] = $path; continue; } if ($test = $this->findTestFile($path)) { $this->affectedTests[$path] = $test; } } if (empty($this->affectedTests)) { - throw new ArcanistNoEffectException('No tests to run.'); + throw new ArcanistNoEffectException(pht('No tests to run.')); } $this->prepareConfigFile(); $futures = array(); $tmpfiles = array(); foreach ($this->affectedTests as $class_path => $test_path) { if (!Filesystem::pathExists($test_path)) { continue; } $json_tmp = new TempFile(); $clover_tmp = null; $clover = null; if ($this->getEnableCoverage() !== false) { $clover_tmp = new TempFile(); $clover = csprintf('--coverage-clover %s', $clover_tmp); } $config = $this->configFile ? csprintf('-c %s', $this->configFile) : null; $stderr = '-d display_errors=stderr'; $futures[$test_path] = new ExecFuture('%C %C %C --log-json %s %C %s', $this->phpunitBinary, $config, $stderr, $json_tmp, $clover, $test_path); $tmpfiles[$test_path] = array( 'json' => $json_tmp, 'clover' => $clover_tmp, ); } $results = array(); $futures = id(new FutureIterator($futures)) ->limit(4); foreach ($futures as $test => $future) { list($err, $stdout, $stderr) = $future->resolve(); $results[] = $this->parseTestResults( $test, $tmpfiles[$test]['json'], $tmpfiles[$test]['clover'], $stderr); } return array_mergev($results); } /** * Parse test results from phpunit json report. * * @param string $path Path to test * @param string $json_tmp Path to phpunit json report * @param string $clover_tmp Path to phpunit clover report * @param string $stderr Data written to stderr * * @return array */ private function parseTestResults($path, $json_tmp, $clover_tmp, $stderr) { $test_results = Filesystem::readFile($json_tmp); return id(new ArcanistPhpunitTestResultParser()) ->setEnableCoverage($this->getEnableCoverage()) ->setProjectRoot($this->projectRoot) ->setCoverageFile($clover_tmp) ->setAffectedTests($this->affectedTests) ->setStderr($stderr) ->parseTestResults($path, $test_results); } /** * Search for test cases for a given file in a large number of "reasonable" * locations. See @{method:getSearchLocationsForTests} for specifics. * * TODO: Add support for finding tests in testsuite folders from * phpunit.xml configuration. * * @param string PHP file to locate test cases for. * @return string|null Path to test cases, or null. */ private function findTestFile($path) { $root = $this->projectRoot; $path = Filesystem::resolvePath($path, $root); $file = basename($path); $possible_files = array( $file, substr($file, 0, -4).'Test.php', ); $search = self::getSearchLocationsForTests($path); foreach ($search as $search_path) { foreach ($possible_files as $possible_file) { $full_path = $search_path.$possible_file; if (!Filesystem::pathExists($full_path)) { // If the file doesn't exist, it's clearly a miss. continue; } if (!Filesystem::isDescendant($full_path, $root)) { // Don't look above the project root. continue; } if (0 == strcasecmp(Filesystem::resolvePath($full_path), $path)) { // Don't return the original file. continue; } return $full_path; } } return null; } /** * Get places to look for PHP Unit tests that cover a given file. For some * file "/a/b/c/X.php", we look in the same directory: * * /a/b/c/ * * We then look in all parent directories for a directory named "tests/" * (or "Tests/"): * * /a/b/c/tests/ * /a/b/tests/ * /a/tests/ * /tests/ * * We also try to replace each directory component with "tests/": * * /a/b/tests/ * /a/tests/c/ * /tests/b/c/ * * We also try to add "tests/" at each directory level: * * /a/b/c/tests/ * /a/b/tests/c/ * /a/tests/b/c/ * /tests/a/b/c/ * * This finds tests with a layout like: * * docs/ * src/ * tests/ * * ...or similar. This list will be further pruned by the caller; it is * intentionally filesystem-agnostic to be unit testable. * * @param string PHP file to locate test cases for. * @return list List of directories to search for tests in. */ public static function getSearchLocationsForTests($path) { $file = basename($path); $dir = dirname($path); $test_dir_names = array('tests', 'Tests'); $try_directories = array(); // Try in the current directory. $try_directories[] = array($dir); // Try in a tests/ directory anywhere in the ancestry. foreach (Filesystem::walkToRoot($dir) as $parent_dir) { if ($parent_dir == '/') { // We'll restore this later. $parent_dir = ''; } foreach ($test_dir_names as $test_dir_name) { $try_directories[] = array($parent_dir, $test_dir_name); } } // Try replacing each directory component with 'tests/'. $parts = trim($dir, DIRECTORY_SEPARATOR); $parts = explode(DIRECTORY_SEPARATOR, $parts); foreach (array_reverse(array_keys($parts)) as $key) { foreach ($test_dir_names as $test_dir_name) { $try = $parts; $try[$key] = $test_dir_name; array_unshift($try, ''); $try_directories[] = $try; } } // Try adding 'tests/' at each level. foreach (array_reverse(array_keys($parts)) as $key) { foreach ($test_dir_names as $test_dir_name) { $try = $parts; $try[$key] = $test_dir_name.DIRECTORY_SEPARATOR.$try[$key]; array_unshift($try, ''); $try_directories[] = $try; } } $results = array(); foreach ($try_directories as $parts) { $results[implode(DIRECTORY_SEPARATOR, $parts).DIRECTORY_SEPARATOR] = true; } return array_keys($results); } /** * Tries to find and update phpunit configuration file based on * `phpunit_config` option in `.arcconfig`. */ private function prepareConfigFile() { $project_root = $this->projectRoot.DIRECTORY_SEPARATOR; $config = $this->getConfigurationManager()->getConfigFromAnySource( 'phpunit_config'); if ($config) { if (Filesystem::pathExists($project_root.$config)) { $this->configFile = $project_root.$config; } else { - throw new Exception('PHPUnit configuration file was not '. - 'found in '.$project_root.$config); + throw new Exception( + pht( + 'PHPUnit configuration file was not found in %s', + $project_root.$config)); } } $bin = $this->getConfigurationManager()->getConfigFromAnySource( 'unit.phpunit.binary'); if ($bin) { if (Filesystem::binaryExists($bin)) { $this->phpunitBinary = $bin; } else { $this->phpunitBinary = Filesystem::resolvePath($bin, $project_root); } } } } diff --git a/src/unit/engine/PhutilUnitTestEngine.php b/src/unit/engine/PhutilUnitTestEngine.php index c2a255c3..ae790600 100644 --- a/src/unit/engine/PhutilUnitTestEngine.php +++ b/src/unit/engine/PhutilUnitTestEngine.php @@ -1,191 +1,200 @@ getRunAllTests()) { $run_tests = $this->getAllTests(); } else { $run_tests = $this->getTestsForPaths(); } if (!$run_tests) { throw new ArcanistNoEffectException('No tests to run.'); } $enable_coverage = $this->getEnableCoverage(); if ($enable_coverage !== false) { if (!function_exists('xdebug_start_code_coverage')) { if ($enable_coverage === true) { throw new ArcanistUsageException( - 'You specified --coverage but xdebug is not available, so '. - 'coverage can not be enabled for PhutilUnitTestEngine.'); + pht( + 'You specified %s but xdebug is not available, so '. + 'coverage can not be enabled for %s.', + '--coverage', + __CLASS__)); } } else { $enable_coverage = true; } } $project_root = $this->getWorkingCopy()->getProjectRoot(); $test_cases = array(); foreach ($run_tests as $test_class) { $test_case = newv($test_class, array()); $test_case->setEnableCoverage($enable_coverage); $test_case->setProjectRoot($project_root); if ($this->getPaths()) { $test_case->setPaths($this->getPaths()); } if ($this->renderer) { $test_case->setRenderer($this->renderer); } $test_cases[] = $test_case; } foreach ($test_cases as $test_case) { $test_case->willRunTestCases($test_cases); } $results = array(); foreach ($test_cases as $test_case) { $results[] = $test_case->run(); } $results = array_mergev($results); foreach ($test_cases as $test_case) { $test_case->didRunTestCases($test_cases); } return $results; } private function getAllTests() { $project_root = $this->getWorkingCopy()->getProjectRoot(); $symbols = id(new PhutilSymbolLoader()) ->setType('class') ->setAncestorClass('ArcanistPhutilTestCase') ->setConcreteOnly(true) ->selectSymbolsWithoutLoading(); $in_working_copy = array(); $run_tests = array(); foreach ($symbols as $symbol) { if (!preg_match('@(?:^|/)__tests__/@', $symbol['where'])) { continue; } $library = $symbol['library']; if (!isset($in_working_copy[$library])) { $library_root = phutil_get_library_root($library); $in_working_copy[$library] = Filesystem::isDescendant( $library_root, $project_root); } if ($in_working_copy[$library]) { $run_tests[] = $symbol['name']; } } return $run_tests; } private function getTestsForPaths() { $project_root = $this->getWorkingCopy()->getProjectRoot(); $look_here = array(); foreach ($this->getPaths() as $path) { $library_root = phutil_get_library_root_for_path($path); if (!$library_root) { continue; } $library_name = phutil_get_library_name_for_root($library_root); if (!$library_name) { throw new Exception( - "Attempting to run unit tests on a libphutil library which has not ". - "been loaded, at:\n\n". - " {$library_root}\n\n". - "This probably means one of two things:\n\n". - " - You may need to add this library to .arcconfig.\n". - " - You may be running tests on a copy of libphutil or arcanist\n". - " using a different copy of libphutil or arcanist. This\n". - " operation is not supported."); + sprintf( + "%s\n\n %s\n\n%s\n\n - %s\n - %s\n", + pht( + 'Attempting to run unit tests on a libphutil library '. + 'which has not been loaded, at:'), + $library_root, + pht('This probably means one of two things:'), + pht( + 'You may need to add this library to %s.', + '.arcconfig.'), + pht( + 'You may be running tests on a copy of libphutil or '. + 'arcanist using a different copy of libphutil or arcanist. '. + 'This operation is not supported.'))); } $path = Filesystem::resolvePath($path, $project_root); if (!is_dir($path)) { $path = dirname($path); } if ($path == $library_root) { $look_here[$library_name.':.'] = array( 'library' => $library_name, 'path' => '', ); } else if (!Filesystem::isDescendant($path, $library_root)) { // We have encountered some kind of symlink maze -- for instance, $path // is some symlink living outside the library that links into some file // inside the library. Just ignore these cases, since the affected file // does not actually lie within the library. continue; } else { $library_path = Filesystem::readablePath($path, $library_root); do { $look_here[$library_name.':'.$library_path] = array( 'library' => $library_name, 'path' => $library_path, ); $library_path = dirname($library_path); } while ($library_path != '.'); } } // Look for any class that extends ArcanistPhutilTestCase inside a // __tests__ directory in any parent directory of every affected file. // // The idea is that "infrastructure/__tests__/" tests defines general tests // for all of "infrastructure/", and those tests run for any change in // "infrastructure/". However, "infrastructure/concrete/rebar/__tests__/" // defines more specific tests that run only when rebar/ (or some // subdirectory) changes. $run_tests = array(); foreach ($look_here as $path_info) { $library = $path_info['library']; $path = $path_info['path']; $symbols = id(new PhutilSymbolLoader()) ->setType('class') ->setLibrary($library) ->setPathPrefix(($path ? $path.'/' : '').'__tests__/') ->setAncestorClass('ArcanistPhutilTestCase') ->setConcreteOnly(true) ->selectAndLoadSymbols(); foreach ($symbols as $symbol) { $run_tests[$symbol['name']] = true; } } $run_tests = array_keys($run_tests); return $run_tests; } public function shouldEchoTestResults() { return !$this->renderer; } } diff --git a/src/unit/engine/PytestTestEngine.php b/src/unit/engine/PytestTestEngine.php index 11315e3f..e5fa7370 100644 --- a/src/unit/engine/PytestTestEngine.php +++ b/src/unit/engine/PytestTestEngine.php @@ -1,143 +1,143 @@ getWorkingCopy(); $this->project_root = $working_copy->getProjectRoot(); $junit_tmp = new TempFile(); $cover_tmp = new TempFile(); $future = $this->buildTestFuture($junit_tmp, $cover_tmp); list($err, $stdout, $stderr) = $future->resolve(); if (!Filesystem::pathExists($junit_tmp)) { throw new CommandException( - "Command failed with error #{$err}!", + pht('Command failed with error #%s!', $err), $future->getCommand(), $err, $stdout, $stderr); } $future = new ExecFuture('coverage xml -o %s', $cover_tmp); $future->setCWD($this->project_root); $future->resolvex(); return $this->parseTestResults($junit_tmp, $cover_tmp); } public function buildTestFuture($junit_tmp, $cover_tmp) { $paths = $this->getPaths(); $cmd_line = csprintf('py.test --junit-xml=%s', $junit_tmp); if ($this->getEnableCoverage() !== false) { $cmd_line = csprintf( 'coverage run --source %s -m %C', $this->project_root, $cmd_line); } return new ExecFuture('%C', $cmd_line); } public function parseTestResults($junit_tmp, $cover_tmp) { $parser = new ArcanistXUnitTestResultParser(); $results = $parser->parseTestResults( Filesystem::readFile($junit_tmp)); if ($this->getEnableCoverage() !== false) { $coverage_report = $this->readCoverage($cover_tmp); foreach ($results as $result) { $result->setCoverage($coverage_report); } } return $results; } public function readCoverage($path) { $coverage_data = Filesystem::readFile($path); if (empty($coverage_data)) { return array(); } $coverage_dom = new DOMDocument(); $coverage_dom->loadXML($coverage_data); $paths = $this->getPaths(); $reports = array(); $classes = $coverage_dom->getElementsByTagName('class'); foreach ($classes as $class) { // filename is actually python module path with ".py" at the end, // e.g.: tornado.web.py $relative_path = explode('.', $class->getAttribute('filename')); array_pop($relative_path); $relative_path = implode('/', $relative_path); // first we check if the path is a directory (a Python package), if it is // set relative and absolute paths to have __init__.py at the end. $absolute_path = Filesystem::resolvePath($relative_path); if (is_dir($absolute_path)) { $relative_path .= '/__init__.py'; $absolute_path .= '/__init__.py'; } // then we check if the path with ".py" at the end is file (a Python // submodule), if it is - set relative and absolute paths to have // ".py" at the end. if (is_file($absolute_path.'.py')) { $relative_path .= '.py'; $absolute_path .= '.py'; } if (!file_exists($absolute_path)) { continue; } if (!in_array($relative_path, $paths)) { continue; } // get total line count in file $line_count = count(file($absolute_path)); $coverage = ''; $start_line = 1; $lines = $class->getElementsByTagName('line'); for ($ii = 0; $ii < $lines->length; $ii++) { $line = $lines->item($ii); $next_line = intval($line->getAttribute('number')); for ($start_line; $start_line < $next_line; $start_line++) { $coverage .= 'N'; } if (intval($line->getAttribute('hits')) == 0) { $coverage .= 'U'; } else if (intval($line->getAttribute('hits')) > 0) { $coverage .= 'C'; } $start_line++; } if ($start_line < $line_count) { foreach (range($start_line, $line_count) as $line_num) { $coverage .= 'N'; } } $reports[$relative_path] = $coverage; } return $reports; } } diff --git a/src/unit/engine/XUnitTestEngine.php b/src/unit/engine/XUnitTestEngine.php index 1824b5b8..cec076c5 100644 --- a/src/unit/engine/XUnitTestEngine.php +++ b/src/unit/engine/XUnitTestEngine.php @@ -1,454 +1,466 @@ projectRoot = $this->getWorkingCopy()->getProjectRoot(); // Determine build engine. if (Filesystem::binaryExists('msbuild')) { $this->buildEngine = 'msbuild'; } else if (Filesystem::binaryExists('xbuild')) { $this->buildEngine = 'xbuild'; } else { - throw new Exception('Unable to find msbuild or xbuild in PATH!'); + throw new Exception( + pht( + 'Unable to find %s or %s in %s!', + 'msbuild', + 'xbuild', + 'PATH')); } // Determine runtime engine (.NET or Mono). if (phutil_is_windows()) { $this->runtimeEngine = ''; } else if (Filesystem::binaryExists('mono')) { $this->runtimeEngine = Filesystem::resolveBinary('mono'); } else { - throw new Exception('Unable to find Mono and you are not on Windows!'); + throw new Exception( + pht('Unable to find Mono and you are not on Windows!')); } // Read the discovery rules. $this->discoveryRules = $this->getConfigurationManager()->getConfigFromAnySource( 'unit.csharp.discovery'); if ($this->discoveryRules === null) { throw new Exception( - 'You must configure discovery rules to map C# files '. - 'back to test projects (`unit.csharp.discovery` in .arcconfig).'); + pht( + 'You must configure discovery rules to map C# files '. + 'back to test projects (`%s` in %s).', + 'unit.csharp.discovery', + '.arcconfig')); } // Determine xUnit test runner path. if ($this->xunitHintPath === null) { $this->xunitHintPath = $this->getConfigurationManager()->getConfigFromAnySource( 'unit.csharp.xunit.binary'); } $xunit = $this->projectRoot.DIRECTORY_SEPARATOR.$this->xunitHintPath; if (file_exists($xunit) && $this->xunitHintPath !== null) { $this->testEngine = Filesystem::resolvePath($xunit); } else if (Filesystem::binaryExists('xunit.console.clr4.exe')) { $this->testEngine = 'xunit.console.clr4.exe'; } else { throw new Exception( - "Unable to locate xUnit console runner. Configure ". - "it with the `unit.csharp.xunit.binary' option in .arcconfig"); + pht( + "Unable to locate xUnit console runner. Configure ". + "it with the `%s' option in %s.", + 'unit.csharp.xunit.binary', + '.arcconfig')); } } /** * Main entry point for the test engine. Determines what assemblies to build * and test based on the files that have changed. * * @return array Array of test results. */ public function run() { $this->loadEnvironment(); if ($this->getRunAllTests()) { $paths = id(new FileFinder($this->projectRoot))->find(); } else { $paths = $this->getPaths(); } return $this->runAllTests($this->mapPathsToResults($paths)); } /** * Applies the discovery rules to the set of paths specified. * * @param array Array of paths. * @return array Array of paths to test projects and assemblies. */ public function mapPathsToResults(array $paths) { $results = array(); foreach ($this->discoveryRules as $regex => $targets) { $regex = str_replace('/', '\\/', $regex); foreach ($paths as $path) { if (preg_match('/'.$regex.'/', $path) === 1) { foreach ($targets as $target) { // Index 0 is the test project (.csproj file) // Index 1 is the output assembly (.dll file) $project = preg_replace('/'.$regex.'/', $target[0], $path); $project = $this->projectRoot.DIRECTORY_SEPARATOR.$project; $assembly = preg_replace('/'.$regex.'/', $target[1], $path); $assembly = $this->projectRoot.DIRECTORY_SEPARATOR.$assembly; if (file_exists($project)) { $project = Filesystem::resolvePath($project); $assembly = Filesystem::resolvePath($assembly); // Check to ensure uniqueness. $exists = false; foreach ($results as $existing) { if ($existing['assembly'] === $assembly) { $exists = true; break; } } if (!$exists) { $results[] = array( 'project' => $project, 'assembly' => $assembly, ); } } } } } } return $results; } /** * Builds and runs the specified test assemblies. * * @param array Array of paths to test project files. * @return array Array of test results. */ public function runAllTests(array $test_projects) { if (empty($test_projects)) { return array(); } $results = array(); $results[] = $this->generateProjects(); if ($this->resultsContainFailures($results)) { return array_mergev($results); } $results[] = $this->buildProjects($test_projects); if ($this->resultsContainFailures($results)) { return array_mergev($results); } $results[] = $this->testAssemblies($test_projects); return array_mergev($results); } /** * Determine whether or not a current set of results contains any failures. * This is needed since we build the assemblies as part of the unit tests, but * we can't run any of the unit tests if the build fails. * * @param array Array of results to check. * @return bool If there are any failures in the results. */ private function resultsContainFailures(array $results) { $results = array_mergev($results); foreach ($results as $result) { if ($result->getResult() != ArcanistUnitTestResult::RESULT_PASS) { return true; } } return false; } /** * If the `Build` directory exists, we assume that this is a multi-platform * project that requires generation of C# project files. Because we want to * test that the generation and subsequent build is whole, we need to * regenerate any projects in case the developer has added files through an * IDE and then forgotten to add them to the respective `.definitions` file. * By regenerating the projects we ensure that any missing definition entries * will cause the build to fail. * * @return array Array of test results. */ private function generateProjects() { // No "Build" directory; so skip generation of projects. if (!is_dir(Filesystem::resolvePath($this->projectRoot.'/Build'))) { return array(); } // No "Protobuild.exe" file; so skip generation of projects. if (!is_file(Filesystem::resolvePath( $this->projectRoot.'/Protobuild.exe'))) { return array(); } // Work out what platform the user is building for already. $platform = phutil_is_windows() ? 'Windows' : 'Linux'; $files = Filesystem::listDirectory($this->projectRoot); foreach ($files as $file) { if (strtolower(substr($file, -4)) == '.sln') { $parts = explode('.', $file); $platform = $parts[count($parts) - 2]; break; } } $regenerate_start = microtime(true); $regenerate_future = new ExecFuture( '%C Protobuild.exe --resync %s', $this->runtimeEngine, $platform); $regenerate_future->setCWD(Filesystem::resolvePath( $this->projectRoot)); $results = array(); $result = new ArcanistUnitTestResult(); - $result->setName("(regenerate projects for $platform)"); + $result->setName(pht('(regenerate projects for %s)', $platform)); try { $regenerate_future->resolvex(); $result->setResult(ArcanistUnitTestResult::RESULT_PASS); } catch(CommandException $exc) { if ($exc->getError() > 1) { throw $exc; } $result->setResult(ArcanistUnitTestResult::RESULT_FAIL); $result->setUserdata($exc->getStdout()); } $result->setDuration(microtime(true) - $regenerate_start); $results[] = $result; return $results; } /** * Build the projects relevant for the specified test assemblies and return * the results of the builds as test results. This build also passes the * "SkipTestsOnBuild" parameter when building the projects, so that MSBuild * conditionals can be used to prevent any tests running as part of the * build itself (since the unit tester is about to run each of the tests * individually). * * @param array Array of test assemblies. * @return array Array of test results. */ private function buildProjects(array $test_assemblies) { $build_futures = array(); $build_failed = false; $build_start = microtime(true); $results = array(); foreach ($test_assemblies as $test_assembly) { $build_future = new ExecFuture( '%C %s', $this->buildEngine, '/p:SkipTestsOnBuild=True'); $build_future->setCWD(Filesystem::resolvePath( dirname($test_assembly['project']))); $build_futures[$test_assembly['project']] = $build_future; } $iterator = id(new FutureIterator($build_futures))->limit(1); foreach ($iterator as $test_assembly => $future) { $result = new ArcanistUnitTestResult(); $result->setName('(build) '.$test_assembly); try { $future->resolvex(); $result->setResult(ArcanistUnitTestResult::RESULT_PASS); } catch(CommandException $exc) { if ($exc->getError() > 1) { throw $exc; } $result->setResult(ArcanistUnitTestResult::RESULT_FAIL); $result->setUserdata($exc->getStdout()); $build_failed = true; } $result->setDuration(microtime(true) - $build_start); $results[] = $result; } return $results; } /** * Build the future for running a unit test. This can be overridden to enable * support for code coverage via another tool. * * @param string Name of the test assembly. * @return array The future, output filename and coverage filename * stored in an array. */ protected function buildTestFuture($test_assembly) { // FIXME: Can't use TempFile here as xUnit doesn't like // UNIX-style full paths. It sees the leading / as the // start of an option flag, even when quoted. $xunit_temp = Filesystem::readRandomCharacters(10).'.results.xml'; if (file_exists($xunit_temp)) { unlink($xunit_temp); } $future = new ExecFuture( '%C %s /xml %s', trim($this->runtimeEngine.' '.$this->testEngine), $test_assembly, $xunit_temp); $folder = Filesystem::resolvePath($this->projectRoot); $future->setCWD($folder); $combined = $folder.'/'.$xunit_temp; if (phutil_is_windows()) { $combined = $folder.'\\'.$xunit_temp; } return array($future, $combined, null); } /** * Run the xUnit test runner on each of the assemblies and parse the * resulting XML. * * @param array Array of test assemblies. * @return array Array of test results. */ private function testAssemblies(array $test_assemblies) { $results = array(); // Build the futures for running the tests. $futures = array(); $outputs = array(); $coverages = array(); foreach ($test_assemblies as $test_assembly) { list($future_r, $xunit_temp, $coverage) = $this->buildTestFuture($test_assembly['assembly']); $futures[$test_assembly['assembly']] = $future_r; $outputs[$test_assembly['assembly']] = $xunit_temp; $coverages[$test_assembly['assembly']] = $coverage; } // Run all of the tests. $futures = id(new FutureIterator($futures)) ->limit(8); foreach ($futures as $test_assembly => $future) { list($err, $stdout, $stderr) = $future->resolve(); if (file_exists($outputs[$test_assembly])) { $result = $this->parseTestResult( $outputs[$test_assembly], $coverages[$test_assembly]); $results[] = $result; unlink($outputs[$test_assembly]); } else { // FIXME: There's a bug in Mono which causes a segmentation fault // when xUnit.NET runs; this causes the XML file to not appear // (depending on when the segmentation fault occurs). See // https://bugzilla.xamarin.com/show_bug.cgi?id=16379 // for more information. // Since it's not possible for the user to correct this error, we // ignore the fact the tests didn't run here. } } return array_mergev($results); } /** * Returns null for this implementation as xUnit does not support code * coverage directly. Override this method in another class to provide code * coverage information (also see @{class:CSharpToolsUnitEngine}). * * @param string The name of the coverage file if one was provided by * `buildTestFuture`. * @return array Code coverage results, or null. */ protected function parseCoverageResult($coverage) { return null; } /** * Parses the test results from xUnit. * * @param string The name of the xUnit results file. * @param string The name of the coverage file if one was provided by * `buildTestFuture`. This is passed through to * `parseCoverageResult`. * @return array Test results. */ private function parseTestResult($xunit_tmp, $coverage) { $xunit_dom = new DOMDocument(); $xunit_dom->loadXML(Filesystem::readFile($xunit_tmp)); $results = array(); $tests = $xunit_dom->getElementsByTagName('test'); foreach ($tests as $test) { $name = $test->getAttribute('name'); $time = $test->getAttribute('time'); $status = ArcanistUnitTestResult::RESULT_UNSOUND; switch ($test->getAttribute('result')) { case 'Pass': $status = ArcanistUnitTestResult::RESULT_PASS; break; case 'Fail': $status = ArcanistUnitTestResult::RESULT_FAIL; break; case 'Skip': $status = ArcanistUnitTestResult::RESULT_SKIP; break; } $userdata = ''; $reason = $test->getElementsByTagName('reason'); $failure = $test->getElementsByTagName('failure'); if ($reason->length > 0 || $failure->length > 0) { $node = ($reason->length > 0) ? $reason : $failure; $message = $node->item(0)->getElementsByTagName('message'); if ($message->length > 0) { $userdata = $message->item(0)->nodeValue; } $stacktrace = $node->item(0)->getElementsByTagName('stack-trace'); if ($stacktrace->length > 0) { $userdata .= "\n".$stacktrace->item(0)->nodeValue; } } $result = new ArcanistUnitTestResult(); $result->setName($name); $result->setResult($status); $result->setDuration($time); $result->setUserData($userdata); if ($coverage != null) { $result->setCoverage($this->parseCoverageResult($coverage)); } $results[] = $result; } return $results; } } diff --git a/src/unit/engine/__tests__/PhutilUnitTestEngineTestCase.php b/src/unit/engine/__tests__/PhutilUnitTestEngineTestCase.php index aa055910..561e108b 100644 --- a/src/unit/engine/__tests__/PhutilUnitTestEngineTestCase.php +++ b/src/unit/engine/__tests__/PhutilUnitTestEngineTestCase.php @@ -1,112 +1,120 @@ assertEqual( 1, self::$allTestsCounter, - 'Expect willRunTests() has been called once.'); + pht( + 'Expect %s has been called once.', + 'willRunTests()')); self::$allTestsCounter--; $actual_test_count = 4; $this->assertEqual( $actual_test_count, count(self::$distinctWillRunTests), - 'Expect willRunOneTest() was called once for each test.'); + pht( + 'Expect %s was called once for each test.', + 'willRunOneTest()')); $this->assertEqual( $actual_test_count, count(self::$distinctDidRunTests), - 'Expect didRunOneTest() was called once for each test.'); + pht( + 'Expect %s was called once for each test.', + 'didRunOneTest()')); $this->assertEqual( self::$distinctWillRunTests, self::$distinctDidRunTests, - 'Expect same tests had pre- and post-run callbacks invoked.'); + pht('Expect same tests had pre-run and post-run callbacks invoked.')); } public function __destruct() { if (self::$allTestsCounter !== 0) { throw new Exception( - 'didRunTests() was not called correctly after tests completed!'); + pht( + '%s was not called correctly after tests completed!', + 'didRunTests()')); } } protected function willRunOneTest($test) { self::$distinctWillRunTests[$test] = true; self::$oneTestCounter++; } protected function didRunOneTest($test) { $this->assertEqual( 1, self::$oneTestCounter, - 'Expect willRunOneTest depth to be one.'); + pht('Expect %s depth to be one.', 'willRunOneTest()')); self::$distinctDidRunTests[$test] = true; self::$oneTestCounter--; } public function testPass() { - $this->assertEqual(1, 1, 'This test is expected to pass.'); + $this->assertEqual(1, 1, pht('This test is expected to pass.')); } public function testFailSkip() { $failed = 0; $skipped = 0; $test_case = new ArcanistPhutilTestCaseTestCase(); foreach ($test_case->run() as $result) { if ($result->getResult() == ArcanistUnitTestResult::RESULT_FAIL) { $failed++; } else if ($result->getResult() == ArcanistUnitTestResult::RESULT_SKIP) { $skipped++; } else { - $this->assertFailure('These tests should either fail or skip.'); + $this->assertFailure(pht('These tests should either fail or skip.')); } } - $this->assertEqual(1, $failed, 'One test was expected to fail.'); - $this->assertEqual(1, $skipped, 'One test was expected to skip.'); + $this->assertEqual(1, $failed, pht('One test was expected to fail.')); + $this->assertEqual(1, $skipped, pht('One test was expected to skip.')); } public function testTryTestCases() { $this->tryTestCases( array( true, false, ), array( true, false, ), array($this, 'throwIfFalsey')); } public function testTryTestMap() { $this->tryTestCaseMap( array( 1 => true, 0 => false, ), array($this, 'throwIfFalsey')); } protected function throwIfFalsey($input) { if (!$input) { - throw new Exception('This is a negative test case!'); + throw new Exception(pht('This is a negative test case!')); } } } diff --git a/src/unit/engine/phutil/ArcanistPhutilTestCase.php b/src/unit/engine/phutil/ArcanistPhutilTestCase.php index 96b9fdeb..3e10052b 100644 --- a/src/unit/engine/phutil/ArcanistPhutilTestCase.php +++ b/src/unit/engine/phutil/ArcanistPhutilTestCase.php @@ -1,704 +1,714 @@ assertions++; return; } $this->failAssertionWithExpectedValue('false', $result, $message); } /** * Assert that a value is `true`, strictly. The test fails if it is not. * * @param wild The empirically derived value, generated by executing the * test. * @param string A human-readable description of what these values represent, * and particularly of what a discrepancy means. * * @return void * @task assert */ final protected function assertTrue($result, $message = null) { if ($result === true) { $this->assertions++; return; } $this->failAssertionWithExpectedValue('true', $result, $message); } /** * Assert that two values are equal, strictly. The test fails if they are not. * * NOTE: This method uses PHP's strict equality test operator (`===`) to * compare values. This means values and types must be equal, key order must * be identical in arrays, and objects must be referentially identical. * * @param wild The theoretically expected value, generated by careful * reasoning about the properties of the system. * @param wild The empirically derived value, generated by executing the * test. * @param string A human-readable description of what these values represent, * and particularly of what a discrepancy means. * * @return void * @task assert */ final protected function assertEqual($expect, $result, $message = null) { if ($expect === $result) { $this->assertions++; return; } $expect = PhutilReadableSerializer::printableValue($expect); $result = PhutilReadableSerializer::printableValue($result); $caller = self::getCallerInfo(); $file = $caller['file']; $line = $caller['line']; if ($message !== null) { $output = pht( 'Assertion failed, expected values to be equal (at %s:%d): %s', $file, $line, $message); } else { $output = pht( 'Assertion failed, expected values to be equal (at %s:%d).', $file, $line); } $output .= "\n"; if (strpos($expect, "\n") === false && strpos($result, "\n") === false) { - $output .= "Expected: {$expect}\n"; - $output .= " Actual: {$result}"; + $output .= pht("Expected: %s\n Actual: %s", $expect, $result); } else { - $output .= "Expected vs Actual Output Diff\n"; - $output .= ArcanistDiffUtils::renderDifferences( - $expect, - $result, - $lines = 0xFFFF); + $output .= pht( + "Expected vs Actual Output Diff\n%s", + ArcanistDiffUtils::renderDifferences( + $expect, + $result, + $lines = 0xFFFF)); } $this->failTest($output); throw new ArcanistPhutilTestTerminatedException($output); } /** * Assert an unconditional failure. This is just a convenience method that * better indicates intent than using dummy values with assertEqual(). This * causes test failure. * * @param string Human-readable description of the reason for test failure. * @return void * @task assert */ final protected function assertFailure($message) { $this->failTest($message); throw new ArcanistPhutilTestTerminatedException($message); } /** * End this test by asserting that the test should be skipped for some * reason. * * @param string Reason for skipping this test. * @return void * @task assert */ final protected function assertSkipped($message) { $this->skipTest($message); throw new ArcanistPhutilTestSkippedException($message); } /* -( Exception Handling )------------------------------------------------- */ /** * This simplest way to assert exceptions are thrown. * * @param exception The expected exception. * @param callable The thing which throws the exception. * * @return void * @task exceptions */ final protected function assertException($expected_exception_class, $callable) { $this->tryTestCases( array('assertException' => array()), array(false), $callable, $expected_exception_class); } /** * Straightforward method for writing unit tests which check if some block of * code throws an exception. For example, this allows you to test the * exception behavior of ##is_a_fruit()## on various inputs: * * public function testFruit() { * $this->tryTestCases( * array( * 'apple is a fruit' => new Apple(), * 'rock is not a fruit' => new Rock(), * ), * array( * true, * false, * ), * array($this, 'tryIsAFruit'), * 'NotAFruitException'); * } * * protected function tryIsAFruit($input) { * is_a_fruit($input); * } * * @param map Map of test case labels to test case inputs. * @param list List of expected results, true to indicate that the case * is expected to succeed and false to indicate that the case * is expected to throw. * @param callable Callback to invoke for each test case. * @param string Optional exception class to catch, defaults to * 'Exception'. * @return void * @task exceptions */ final protected function tryTestCases( array $inputs, array $expect, $callable, $exception_class = 'Exception') { if (count($inputs) !== count($expect)) { $this->assertFailure( - 'Input and expectations must have the same number of values.'); + pht('Input and expectations must have the same number of values.')); } $labels = array_keys($inputs); $inputs = array_values($inputs); $expecting = array_values($expect); foreach ($inputs as $idx => $input) { $expect = $expecting[$idx]; $label = $labels[$idx]; $caught = null; try { call_user_func($callable, $input); } catch (Exception $ex) { if ($ex instanceof ArcanistPhutilTestTerminatedException) { throw $ex; } if (!($ex instanceof $exception_class)) { throw $ex; } $caught = $ex; } $actual = !($caught instanceof Exception); if ($expect === $actual) { if ($expect) { - $message = "Test case '{$label}' did not throw, as expected."; + $message = pht("Test case '%s' did not throw, as expected.", $label); } else { - $message = "Test case '{$label}' threw, as expected."; + $message = pht("Test case '%s' threw, as expected.", $label); } } else { if ($expect) { - $message = "Test case '{$label}' was expected to succeed, but it ". - "raised an exception of class ".get_class($ex)." with ". - "message: ".$ex->getMessage(); + $message = pht( + "Test case '%s' was expected to succeed, but it ". + "raised an exception of class %s with message: %s", + $label, + get_class($ex), + $ex->getMessage()); } else { - $message = "Test case '{$label}' was expected to raise an ". - "exception, but it did not throw anything."; + $message = pht( + "Test case '%s' was expected to raise an ". + "exception, but it did not throw anything.", + $label); } } $this->assertEqual($expect, $actual, $message); } } /** * Convenience wrapper around @{method:tryTestCases} for cases where your * inputs are scalar. For example: * * public function testFruit() { * $this->tryTestCaseMap( * array( * 'apple' => true, * 'rock' => false, * ), * array($this, 'tryIsAFruit'), * 'NotAFruitException'); * } * * protected function tryIsAFruit($input) { * is_a_fruit($input); * } * * For cases where your inputs are not scalar, use @{method:tryTestCases}. * * @param map Map of scalar test inputs to expected success (true * expects success, false expects an exception). * @param callable Callback to invoke for each test case. * @param string Optional exception class to catch, defaults to * 'Exception'. * @return void * @task exceptions */ final protected function tryTestCaseMap( array $map, $callable, $exception_class = 'Exception') { return $this->tryTestCases( array_fuse(array_keys($map)), array_values($map), $callable, $exception_class); } /* -( Hooks for Setup and Teardown )--------------------------------------- */ /** * This hook is invoked once, before any tests in this class are run. It * gives you an opportunity to perform setup steps for the entire class. * * @return void * @task hook */ protected function willRunTests() { return; } /** * This hook is invoked once, after any tests in this class are run. It gives * you an opportunity to perform teardown steps for the entire class. * * @return void * @task hook */ protected function didRunTests() { return; } /** * This hook is invoked once per test, before the test method is invoked. * * @param string Method name of the test which will be invoked. * @return void * @task hook */ protected function willRunOneTest($test_method_name) { return; } /** * This hook is invoked once per test, after the test method is invoked. * * @param string Method name of the test which was invoked. * @return void * @task hook */ protected function didRunOneTest($test_method_name) { return; } /** * This hook is invoked once, before any test cases execute. It gives you * an opportunity to perform setup steps for the entire suite of test cases. * * @param list List of test cases to be run. * @return void * @task hook */ public function willRunTestCases(array $test_cases) { return; } /** * This hook is invoked once, after all test cases execute. * * @param list List of test cases that ran. * @return void * @task hook */ public function didRunTestCases(array $test_cases) { return; } /* -( Internals )---------------------------------------------------------- */ /** * Construct a new test case. This method is ##final##, use willRunTests() to * provide test-wide setup logic. * * @task internal */ final public function __construct() {} /** * Mark the currently-running test as a failure. * * @param string Human-readable description of problems. * @return void * * @task internal */ final private function failTest($reason) { $this->resultTest(ArcanistUnitTestResult::RESULT_FAIL, $reason); } /** * This was a triumph. I'm making a note here: HUGE SUCCESS. * * @param string Human-readable overstatement of satisfaction. * @return void * * @task internal */ final private function passTest($reason) { $this->resultTest(ArcanistUnitTestResult::RESULT_PASS, $reason); } /** * Mark the current running test as skipped. * * @param string Description for why this test was skipped. * @return void * @task internal */ final private function skipTest($reason) { $this->resultTest(ArcanistUnitTestResult::RESULT_SKIP, $reason); } final private function resultTest($test_result, $reason) { $coverage = $this->endCoverage(); $result = new ArcanistUnitTestResult(); $result->setCoverage($coverage); $result->setNamespace(get_class($this)); $result->setName($this->runningTest); $result->setLink($this->getLink($this->runningTest)); $result->setResult($test_result); $result->setDuration(microtime(true) - $this->testStartTime); $result->setUserData($reason); $this->results[] = $result; if ($this->renderer) { echo $this->renderer->renderUnitResult($result); } } /** * Execute the tests in this test case. You should not call this directly; * use @{class:PhutilUnitTestEngine} to orchestrate test execution. * * @return void * @task internal */ final public function run() { $this->results = array(); $reflection = new ReflectionClass($this); $methods = $reflection->getMethods(); // Try to ensure that poorly-written tests which depend on execution order // (and are thus not properly isolated) will fail. shuffle($methods); $this->willRunTests(); foreach ($methods as $method) { $name = $method->getName(); if (preg_match('/^test/', $name)) { $this->runningTest = $name; $this->assertions = 0; $this->testStartTime = microtime(true); try { $this->willRunOneTest($name); $this->beginCoverage(); $exceptions = array(); try { call_user_func_array( array($this, $name), array()); $this->passTest(pht('%d assertion(s) passed.', $this->assertions)); } catch (Exception $ex) { $exceptions['Execution'] = $ex; } try { $this->didRunOneTest($name); } catch (Exception $ex) { $exceptions['Shutdown'] = $ex; } if ($exceptions) { if (count($exceptions) == 1) { throw head($exceptions); } else { throw new PhutilAggregateException( - 'Multiple exceptions were raised during test execution.', + pht('Multiple exceptions were raised during test execution.'), $exceptions); } } if (!$this->assertions) { $this->failTest( pht( 'This test case made no assertions. Test cases must make at '. 'least one assertion.')); } } catch (ArcanistPhutilTestTerminatedException $ex) { // Continue with the next test. } catch (ArcanistPhutilTestSkippedException $ex) { // Continue with the next test. } catch (Exception $ex) { $ex_class = get_class($ex); $ex_message = $ex->getMessage(); $ex_trace = $ex->getTraceAsString(); - $message = "EXCEPTION ({$ex_class}): {$ex_message}\n{$ex_trace}"; + $message = sprintf( + "%s (%s): %s\n%s", + pht('EXCEPTION'), + $ex_class, + $ex_message, + $ex_trace); $this->failTest($message); } } } $this->didRunTests(); return $this->results; } final public function setEnableCoverage($enable_coverage) { $this->enableCoverage = $enable_coverage; return $this; } /** * @phutil-external-symbol function xdebug_start_code_coverage */ final private function beginCoverage() { if (!$this->enableCoverage) { return; } $this->assertCoverageAvailable(); xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE); } /** * @phutil-external-symbol function xdebug_get_code_coverage * @phutil-external-symbol function xdebug_stop_code_coverage */ final private function endCoverage() { if (!$this->enableCoverage) { return; } $result = xdebug_get_code_coverage(); xdebug_stop_code_coverage($cleanup = false); $coverage = array(); foreach ($result as $file => $report) { if (strncmp($file, $this->projectRoot, strlen($this->projectRoot))) { continue; } $max = max(array_keys($report)); $str = ''; for ($ii = 1; $ii <= $max; $ii++) { $c = idx($report, $ii); if ($c === -1) { $str .= 'U'; // Un-covered. } else if ($c === -2) { // TODO: This indicates "unreachable", but it flags the closing braces // of functions which end in "return", which is super ridiculous. Just // ignore it for now. // // See http://bugs.xdebug.org/view.php?id=1041 $str .= 'N'; // Not executable. } else if ($c === 1) { $str .= 'C'; // Covered. } else { $str .= 'N'; // Not executable. } } $coverage[substr($file, strlen($this->projectRoot) + 1)] = $str; } // Only keep coverage information for files modified by the change. In // the case of --everything, we won't have paths, so just return all the // coverage data. if ($this->paths) { $coverage = array_select_keys($coverage, $this->paths); } return $coverage; } final private function assertCoverageAvailable() { if (!function_exists('xdebug_start_code_coverage')) { throw new Exception( - "You've enabled code coverage but XDebug is not installed."); + pht("You've enabled code coverage but XDebug is not installed.")); } } final public function setProjectRoot($project_root) { $this->projectRoot = $project_root; return $this; } final public function setPaths(array $paths) { $this->paths = $paths; return $this; } protected function getLink($method) { return null; } public function setRenderer(ArcanistUnitRenderer $renderer) { $this->renderer = $renderer; return $this; } /** * Returns info about the caller function. * * @return map */ private static final function getCallerInfo() { $callee = array(); $caller = array(); $seen = false; foreach (array_slice(debug_backtrace(), 1) as $location) { $function = idx($location, 'function'); if (!$seen && preg_match('/^assert[A-Z]/', $function)) { $seen = true; $caller = $location; } else if ($seen && !preg_match('/^assert[A-Z]/', $function)) { $callee = $location; break; } } return array( 'file' => basename(idx($caller, 'file')), 'line' => idx($caller, 'line'), 'function' => idx($callee, 'function'), 'class' => idx($callee, 'class'), 'object' => idx($caller, 'object'), 'type' => idx($callee, 'type'), 'args' => idx($caller, 'args'), ); } /** * Fail an assertion which checks that some result is equal to a specific * value, like 'true' or 'false'. This prints a readable error message and * fails the current test. * * This method throws and does not return. * * @param string Human readable description of the expected value. * @param string The actual value. * @param string|null Optional assertion message. * @return void * @task internal */ private function failAssertionWithExpectedValue( $expect_description, $actual_result, $message) { $caller = self::getCallerInfo(); $file = $caller['file']; $line = $caller['line']; if ($message !== null) { $description = pht( "Assertion failed, expected '%s' (at %s:%d): %s", $expect_description, $file, $line, $message); } else { $description = pht( "Assertion failed, expected '%s' (at %s:%d).", $expect_description, $file, $line); } $actual_result = PhutilReadableSerializer::printableValue($actual_result); $header = pht('ACTUAL VALUE'); $output = $description."\n\n".$header."\n".$actual_result; $this->failTest($output); throw new ArcanistPhutilTestTerminatedException($output); } } diff --git a/src/unit/parser/ArcanistGoTestResultParser.php b/src/unit/parser/ArcanistGoTestResultParser.php index 41463f10..fa0a2672 100644 --- a/src/unit/parser/ArcanistGoTestResultParser.php +++ b/src/unit/parser/ArcanistGoTestResultParser.php @@ -1,134 +1,133 @@ $line) { if (strncmp($line, '--- PASS', 8) === 0) { // We have a passing test $meta = array(); preg_match( '/^--- PASS: (?P.+) \((?P