diff --git a/scripts/arcanist.php b/scripts/arcanist.php index 1f692c16..8714bcd1 100755 --- a/scripts/arcanist.php +++ b/scripts/arcanist.php @@ -1,652 +1,652 @@ #!/usr/bin/env php parseStandardArguments(); $base_args->parsePartial( array( array( 'name' => 'load-phutil-library', 'param' => 'path', 'help' => 'Load a libphutil library.', 'repeat' => true, ), array( 'name' => 'skip-arcconfig', ), array( 'name' => 'arcrc-file', 'param' => 'filename', ), array( 'name' => 'conduit-uri', 'param' => 'uri', 'help' => 'Connect to Phabricator install specified by __uri__.', ), array( 'name' => 'conduit-version', 'param' => 'version', 'help' => '(Developers) Mock client version in protocol handshake.', ), array( 'name' => 'conduit-timeout', 'param' => 'timeout', 'help' => 'Set Conduit timeout (in seconds).', ), array( 'name' => 'config', 'param' => 'key=value', 'repeat' => true, 'help' => 'Specify a runtime configuration value. This will take precedence '. - 'over static values, and only affect the current arcanist invocation.' + 'over static values, and only affect the current arcanist invocation.', ), )); $config_trace_mode = $base_args->getArg('trace'); $force_conduit = $base_args->getArg('conduit-uri'); $force_conduit_version = $base_args->getArg('conduit-version'); $conduit_timeout = $base_args->getArg('conduit-timeout'); $skip_arcconfig = $base_args->getArg('skip-arcconfig'); $custom_arcrc = $base_args->getArg('arcrc-file'); $load = $base_args->getArg('load-phutil-library'); $help = $base_args->getArg('help'); $args = array_values($base_args->getUnconsumedArgumentVector()); $working_directory = getcwd(); $console = PhutilConsole::getConsole(); $config = null; $workflow = null; try { $console->writeLog( "libphutil loaded from '%s'.\n", phutil_get_library_root('phutil')); $console->writeLog( "arcanist loaded from '%s'.\n", phutil_get_library_root('arcanist')); if (!$args) { if ($help) { $args = array('help'); } else { throw new ArcanistUsageException("No command provided. Try 'arc help'."); } } else if ($help) { array_unshift($args, 'help'); } $configuration_manager = new ArcanistConfigurationManager(); if ($custom_arcrc) { $configuration_manager->setUserConfigurationFileLocation($custom_arcrc); } $global_config = $configuration_manager->readUserArcConfig(); $system_config = $configuration_manager->readSystemArcConfig(); $runtime_config = $configuration_manager->applyRuntimeArcConfig($base_args); if ($skip_arcconfig) { $working_copy = ArcanistWorkingCopyIdentity::newDummyWorkingCopy(); } else { $working_copy = ArcanistWorkingCopyIdentity::newFromPath($working_directory); } $configuration_manager->setWorkingCopyIdentity($working_copy); reenter_if_this_is_arcanist_or_libphutil( $console, $working_copy, $original_argv); // Load additional libraries, which can provide new classes like configuration // overrides, linters and lint engines, unit test engines, etc. // If the user specified "--load-phutil-library" one or more times from // the command line, we load those libraries **instead** of whatever else // is configured. This is basically a debugging feature to let you force // specific libraries to load regardless of the state of the world. if ($load) { $console->writeLog( "Using '--load-phutil-library' flag, configuration will be ignored ". "and configured libraries will not be loaded."."\n"); // Load the flag libraries. These must load, since the user specified them // explicitly. arcanist_load_libraries( $load, $must_load = true, $lib_source = 'a "--load-phutil-library" flag', $working_copy); } else { // Load libraries in system 'load' config. In contrast to global config, we // fail hard here because this file is edited manually, so if 'arc' breaks // that doesn't make it any more difficult to correct. arcanist_load_libraries( idx($system_config, 'load', array()), $must_load = true, $lib_source = 'the "load" setting in system config', $working_copy); // Load libraries in global 'load' config, as per "arc set-config load". We // need to fail softly if these break because errors would prevent the user // from running "arc set-config" to correct them. arcanist_load_libraries( idx($global_config, 'load', array()), $must_load = false, $lib_source = 'the "load" setting in global config', $working_copy); // Load libraries in ".arcconfig". Libraries here must load. arcanist_load_libraries( $working_copy->getProjectConfig('load'), $must_load = true, $lib_source = 'the "load" setting in ".arcconfig"', $working_copy); // Load libraries in ".arcconfig". Libraries here must load. arcanist_load_libraries( idx($runtime_config, 'load', array()), $must_load = true, $lib_source = 'the --config "load=[...]" argument', $working_copy); } $user_config = $configuration_manager->readUserConfigurationFile(); $config_class = $working_copy->getProjectConfig('arcanist_configuration'); if ($config_class) { $config = new $config_class(); } else { $config = new ArcanistConfiguration(); } $command = strtolower($args[0]); $args = array_slice($args, 1); $workflow = $config->selectWorkflow( $command, $args, $configuration_manager, $console); $workflow->setConfigurationManager($configuration_manager); $workflow->setArcanistConfiguration($config); $workflow->setCommand($command); $workflow->setWorkingDirectory($working_directory); $workflow->parseArguments($args); // Write the command into the environment so that scripts (for example, local // Git commit hooks) can detect that they're being run via `arc` and change // their behaviors. putenv('ARCANIST='.$command); if ($force_conduit_version) { $workflow->forceConduitVersion($force_conduit_version); } if ($conduit_timeout) { $workflow->setConduitTimeout($conduit_timeout); } $need_working_copy = $workflow->requiresWorkingCopy(); $need_conduit = $workflow->requiresConduit(); $need_auth = $workflow->requiresAuthentication(); $need_repository_api = $workflow->requiresRepositoryAPI(); $want_repository_api = $workflow->desiresRepositoryAPI(); $want_working_copy = $workflow->desiresWorkingCopy() || $want_repository_api; $need_conduit = $need_conduit || $need_auth; $need_working_copy = $need_working_copy || $need_repository_api; if ($need_working_copy || $want_working_copy) { if ($need_working_copy && !$working_copy->getVCSType()) { throw new ArcanistUsageException( 'This command must be run in a Git, Mercurial or Subversion working '. 'copy.'); } $configuration_manager->setWorkingCopyIdentity($working_copy); } if ($force_conduit) { $conduit_uri = $force_conduit; } else { $conduit_uri = $configuration_manager->getConfigFromAnySource( 'phabricator.uri'); if ($conduit_uri === null) { $conduit_uri = $configuration_manager->getConfigFromAnySource( 'default'); } } if ($conduit_uri) { // Set the URI path to '/api/'. TODO: Originally, I contemplated letting // you deploy Phabricator somewhere other than the domain root, but ended // up never pursuing that. We should get rid of all "/api/" silliness // in things users are expected to configure. This is already happening // to some degree, e.g. "arc install-certificate" does it for you. $conduit_uri = new PhutilURI($conduit_uri); $conduit_uri->setPath('/api/'); $conduit_uri = (string)$conduit_uri; } $workflow->setConduitURI($conduit_uri); // Apply global CA bundle from configs. $ca_bundle = $configuration_manager->getConfigFromAnySource('https.cabundle'); if ($ca_bundle) { $ca_bundle = Filesystem::resolvePath( $ca_bundle, $working_copy->getProjectRoot()); HTTPSFuture::setGlobalCABundleFromPath($ca_bundle); } $blind_key = 'https.blindly-trust-domains'; $blind_trust = $configuration_manager->getConfigFromAnySource($blind_key); if ($blind_trust) { HTTPSFuture::setBlindlyTrustDomains($blind_trust); } if ($need_conduit) { if (!$conduit_uri) { $message = phutil_console_format( "This command requires arc to connect to a Phabricator install, but ". "no Phabricator installation is configured. To configure a ". "Phabricator URI:\n\n". " - set a default location with `arc set-config default `; or\n". " - specify '--conduit-uri=uri' explicitly; or\n". " - run 'arc' in a working copy with an '.arcconfig'.\n"); $message = phutil_console_wrap($message); throw new ArcanistUsageException($message); } $workflow->establishConduit(); } $hosts_config = idx($user_config, 'hosts', array()); $host_config = idx($hosts_config, $conduit_uri, array()); $user_name = idx($host_config, 'user'); $certificate = idx($host_config, 'cert'); $description = implode(' ', $original_argv); $credentials = array( 'user' => $user_name, 'certificate' => $certificate, 'description' => $description, ); $workflow->setConduitCredentials($credentials); if ($need_auth) { if (!$user_name || !$certificate) { $arc = 'arc'; if ($force_conduit) { $arc .= csprintf(' --conduit-uri=%s', $conduit_uri); } throw new ArcanistUsageException( phutil_console_format( "YOU NEED TO __INSTALL A CERTIFICATE__ TO LOGIN TO PHABRICATOR\n\n". "You are trying to connect to '{$conduit_uri}' but do not have ". "a certificate installed for this host. Run:\n\n". " $ **{$arc} install-certificate**\n\n". "...to install one.")); } $workflow->authenticateConduit(); } if ($need_repository_api || ($want_repository_api && $working_copy->getVCSType())) { $repository_api = ArcanistRepositoryAPI::newAPIFromConfigurationManager( $configuration_manager); $workflow->setRepositoryAPI($repository_api); } $listeners = $configuration_manager->getConfigFromAnySource( 'events.listeners'); if ($listeners) { foreach ($listeners as $listener) { $console->writeLog( "Registering event listener '%s'.\n", $listener); try { id(new $listener())->register(); } catch (PhutilMissingSymbolException $ex) { // Continue anyway, since you may otherwise be unable to run commands // like `arc set-config events.listeners` in order to repair the damage // you've caused. We're writing out the entire exception here because // it might not have been triggered by the listener itself (for example, // the listener might use a bad class in its register() method). $console->writeErr( "ERROR: Failed to load event listener '%s': %s\n", $listener, $ex->getMessage()); } } } $config->willRunWorkflow($command, $workflow); $workflow->willRunWorkflow(); try { $err = $workflow->run(); $config->didRunWorkflow($command, $workflow, $err); } catch (Exception $e) { $workflow->finalize(); throw $e; } $workflow->finalize(); exit((int)$err); } catch (ArcanistNoEffectException $ex) { echo $ex->getMessage()."\n"; } catch (Exception $ex) { $is_usage = ($ex instanceof ArcanistUsageException); if ($is_usage) { echo phutil_console_format( "**Usage Exception:** %s\n", $ex->getMessage()); } if ($config) { $config->didAbortWorkflow($command, $workflow, $ex); } if ($config_trace_mode) { echo "\n"; throw $ex; } if (!$is_usage) { echo phutil_console_format("**Exception**\n"); while ($ex) { echo $ex->getMessage()."\n"; if ($ex instanceof PhutilProxyException) { $ex = $ex->getPreviousException(); } else { $ex = null; } } echo "(Run with --trace for a full exception trace.)\n"; } exit(1); } /** * Perform some sanity checks against the possible diversity of PHP builds in * the wild, like very old versions and builds that were compiled with flags * that exclude core functionality. */ function sanity_check_environment() { // NOTE: We don't have phutil_is_windows() yet here. $is_windows = (DIRECTORY_SEPARATOR != '/'); // We use stream_socket_pair() which is not available on Windows earlier. $min_version = ($is_windows ? '5.3.0' : '5.2.3'); $cur_version = phpversion(); if (version_compare($cur_version, $min_version, '<')) { die_with_bad_php( "You are running PHP version '{$cur_version}', which is older than ". "the minimum version, '{$min_version}'. Update to at least ". "'{$min_version}'."); } if ($is_windows) { $need_functions = array( 'curl_init' => array('builtin-dll', 'php_curl.dll'), ); } else { $need_functions = array( 'curl_init' => array( 'text', "You need to install the cURL PHP extension, maybe with ". "'apt-get install php5-curl' or 'yum install php53-curl' or ". - "something similar."), + "something similar.",), 'json_decode' => array('flag', '--without-json'), ); } $problems = array(); $config = null; $show_config = false; foreach ($need_functions as $fname => $resolution) { if (function_exists($fname)) { continue; } static $info; if ($info === null) { ob_start(); phpinfo(INFO_GENERAL); $info = ob_get_clean(); $matches = null; if (preg_match('/^Configure Command =>\s*(.*?)$/m', $info, $matches)) { $config = $matches[1]; } } $generic = true; list($what, $which) = $resolution; if ($what == 'flag' && strpos($config, $which) !== false) { $show_config = true; $generic = false; $problems[] = "This build of PHP was compiled with the configure flag '{$which}', ". "which means it does not have the function '{$fname}()'. This ". "function is required for arc to run. Rebuild PHP without this flag. ". "You may also be able to build or install the relevant extension ". "separately."; } if ($what == 'builtin-dll') { $generic = false; $problems[] = "Your install of PHP does not have the '{$which}' extension enabled. ". "Edit your php.ini file and uncomment the line which reads ". "'extension={$which}'."; } if ($what == 'text') { $generic = false; $problems[] = $which; } if ($generic) { $problems[] = "This build of PHP is missing the required function '{$fname}()'. ". "Rebuild PHP or install the extension which provides '{$fname}()'."; } } if ($problems) { if ($show_config) { $problems[] = "PHP was built with this configure command:\n\n{$config}"; } die_with_bad_php(implode("\n\n", $problems)); } } function die_with_bad_php($message) { echo "\nPHP CONFIGURATION ERRORS\n\n"; echo $message; echo "\n\n"; exit(1); } function arcanist_load_libraries( $load, $must_load, $lib_source, ArcanistWorkingCopyIdentity $working_copy) { if (!$load) { return; } if (!is_array($load)) { $error = "Libraries specified by {$lib_source} are invalid; expected ". "a list. Check your configuration."; $console = PhutilConsole::getConsole(); $console->writeErr("WARNING: %s\n", $error); return; } foreach ($load as $location) { // Try to resolve the library location. We look in several places, in // order: // // 1. Inside the working copy. This is for phutil libraries within the // project. For instance "library/src" will resolve to // "./library/src" if it exists. // 2. In the same directory as the working copy. This allows you to // check out a library alongside a working copy and reference it. // If we haven't resolved yet, "library/src" will try to resolve to // "../library/src" if it exists. // 3. Using normal libphutil resolution rules. Generally, this means // that it checks for libraries next to libphutil, then libraries // in the PHP include_path. // // Note that absolute paths will just resolve absolutely through rule (1). $resolved = false; // Check inside the working copy. This also checks absolute paths, since // they'll resolve absolute and just ignore the project root. $resolved_location = Filesystem::resolvePath( $location, $working_copy->getProjectRoot()); if (Filesystem::pathExists($resolved_location)) { $location = $resolved_location; $resolved = true; } // If we didn't find anything, check alongside the working copy. if (!$resolved) { $resolved_location = Filesystem::resolvePath( $location, dirname($working_copy->getProjectRoot())); if (Filesystem::pathExists($resolved_location)) { $location = $resolved_location; $resolved = true; } } $console = PhutilConsole::getConsole(); $console->writeLog( "Loading phutil library from '%s'...\n", $location); $error = null; try { phutil_load_library($location); } catch (PhutilBootloaderException $ex) { $error = "Failed to load phutil library at location '{$location}'. ". "This library is specified by {$lib_source}. Check that the ". "setting is correct and the library is located in the right ". "place."; if ($must_load) { throw new ArcanistUsageException($error); } else { fwrite(STDERR, phutil_console_wrap('WARNING: '.$error."\n\n")); } } catch (PhutilLibraryConflictException $ex) { if ($ex->getLibrary() != 'arcanist') { throw $ex; } $arc_dir = dirname(dirname(__FILE__)); $error = "You are trying to run one copy of Arcanist on another copy of ". "Arcanist. This operation is not supported. To execute Arcanist ". "operations against this working copy, run './bin/arc' (from the ". "current working copy) not some other copy of 'arc' (you ran one ". "from '{$arc_dir}')."; throw new ArcanistUsageException($error); } } } /** * NOTE: SPOOKY BLACK MAGIC * * When arc is run in a copy of arcanist other than itself, or a copy of * libphutil other than the one we loaded, reenter the script and force it * to use the current working directory instead of the default. * * In the case of execution inside arcanist/, we force execution of the local * arc binary. * * In the case of execution inside libphutil/, we force the local copy to load * instead of the one selected by default rules. * * @param PhutilConsole Console. * @param ArcanistWorkingCopyIdentity The current working copy. * @param array Original arc arguments. * @return void */ function reenter_if_this_is_arcanist_or_libphutil( PhutilConsole $console, ArcanistWorkingCopyIdentity $working_copy, array $original_argv) { $project_id = $working_copy->getProjectID(); if ($project_id != 'arcanist' && $project_id != 'libphutil') { // We're not in a copy of arcanist or libphutil. return; } $library_names = array( 'arcanist' => 'arcanist', 'libphutil' => 'phutil', ); $library_root = phutil_get_library_root($library_names[$project_id]); $project_root = $working_copy->getProjectRoot(); if (Filesystem::isDescendant($library_root, $project_root)) { // We're in a copy of arcanist or libphutil, but already loaded the correct // copy. Continue execution normally. return; } if ($project_id == 'libphutil') { $console->writeLog( "This is libphutil! Forcing this copy to load...\n"); $original_argv[0] = dirname(phutil_get_library_root('arcanist')).'/bin/arc'; $libphutil_path = $project_root; } else { $console->writeLog( "This is arcanist! Forcing this copy to run...\n"); $original_argv[0] = $project_root.'/bin/arc'; $libphutil_path = dirname(phutil_get_library_root('phutil')); } if (phutil_is_windows()) { $err = phutil_passthru( 'set ARC_PHUTIL_PATH=%s & %Ls', $libphutil_path, $original_argv); } else { $err = phutil_passthru( 'ARC_PHUTIL_PATH=%s %Ls', $libphutil_path, $original_argv); } exit($err); } diff --git a/src/configuration/ArcanistSettings.php b/src/configuration/ArcanistSettings.php index 796bc177..b4c54a9f 100644 --- a/src/configuration/ArcanistSettings.php +++ b/src/configuration/ArcanistSettings.php @@ -1,328 +1,328 @@ array( 'type' => 'string', 'help' => 'The URI of a Phabricator install to connect to by default, if '. 'arc is run in a project without a Phabricator URI or run outside '. 'of a project.', 'example' => '"http://phabricator.example.com/"', ), 'base' => array( 'type' => 'string', 'help' => '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' => '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.', 'example' => '["/var/arc/customlib/src"]', 'default' => array(), ), 'repository.callsign' => array( 'type' => 'string', 'example' => '"X"', 'help' => pht( 'Associate the working copy with a specific Phabricator repository. '. 'Normally, arc 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.'), ), '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' => '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' => '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' => 'The name of the default branch to create the new feature branch '. 'off of.', 'example' => '"develop"', ), 'arc.land.onto.default' => array( 'type' => 'string', 'help' => 'The name of the default branch to land changes onto when '. '`arc land` is run.', 'example' => '"develop"', ), 'arc.land.update.default' => array( 'type' => 'string', 'help' => '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' => "Enable the lint cache by default. When enabled, 'arc lint' ". "attempts to use cached results if possible. Currently, the cache ". "is not always invalidated correctly and may cause 'arc lint' to ". "report incorrect results, particularly while developing linters. ". "This is probably worth enabling only if your linters are very slow.", 'example' => 'false', 'default' => false, ), 'history.immutable' => array( 'type' => 'bool', 'legacy' => 'immutable_history', 'help' => 'If true, arc 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.', 'example' => 'false', ), 'editor' => array( 'type' => 'string', 'help' => "Command to use to invoke an interactive editor, like 'nano' or ". "'vim'. This setting overrides the EDITOR environmental variable.", 'example' => '"nano"', ), 'https.cabundle' => array( 'type' => 'string', 'help' => "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' + 'example' => 'support/yourca.pem', ), 'https.blindly-trust-domains' => array( 'type' => 'list', 'help' => 'List of domains to blindly trust SSL certificates for. '. 'Disables peer verification.', 'example' => '["secure.mycompany.com"]', 'default' => array(), ), 'browser' => array( 'type' => 'string', 'help' => 'Command to use to invoke a web browser.', 'example' => '"gnome-www-browser"', ), 'events.listeners' => array( 'type' => 'list', 'help' => 'List of event listener classes to install at startup.', 'example' => '["ExampleEventListener"]', 'default' => array(), ), 'http.basicauth.user' => array( 'type' => 'string', 'help' => 'Username to use for basic auth over http transports', 'example' => '"bob"', ), 'http.basicauth.pass' => array( 'type' => 'string', 'help' => 'Password to use for basic auth over http transports', 'example' => '"bobhasasecret"', ), 'arc.autostash' => array( 'type' => 'bool', 'help' => 'Whether arc 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.', 'example' => 'false', 'default' => 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( "Type of setting '{$key}' must be boolean, like 'true' or ". "'false'."); } break; case 'list': if (is_array($value)) { break; } if (is_string($value)) { $list = json_decode($value, true); if (is_array($list)) { $value = $list; break; } } $list_example = '["apple", "banana", "cherry"]'; throw new ArcanistUsageException( "Type of setting '{$key}' must be list. You can specify a list ". "in JSON, like: {$list_example}"); case 'string': if (!is_scalar($value)) { throw new ArcanistUsageException( "Type of setting '{$key}' must be string."); } $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( "Type of setting '{$key}' must be string."); } break; case 'bool': if ($value !== true && $value !== false) { throw new ArcanistUsageException( "Type of setting '{$key}' must be boolean."); } break; case 'list': if (!is_array($value)) { throw new ArcanistUsageException( "Type of setting '{$key}' must be list."); } 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('@(?'; $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/hgdaemon/ArcanistHgProxyServer.php b/src/hgdaemon/ArcanistHgProxyServer.php index 93ef5af4..a6e19a25 100644 --- a/src/hgdaemon/ArcanistHgProxyServer.php +++ b/src/hgdaemon/ArcanistHgProxyServer.php @@ -1,487 +1,487 @@ 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->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() + 'except' => $socket ? array($socket) : array(), )); if (!$hg->update()) { throw new Exception('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->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'); 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)'); 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)'); return; } if ($remaining <= 5) { $this->log(null, 'Exiting in '.$remaining.' seconds'); } } } } /** * 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->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}"); } $ok = stream_set_blocking($socket, 0); if ($ok === false) { throw new Exception('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; } else { $message = '[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!'); } 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/lint/linter/ArcanistExternalLinter.php b/src/lint/linter/ArcanistExternalLinter.php index e5efb990..f85d09da 100644 --- a/src/lint/linter/ArcanistExternalLinter.php +++ b/src/lint/linter/ArcanistExternalLinter.php @@ -1,531 +1,531 @@ Mandatory flags, like `"--format=xml"`. * @task bin */ protected function getMandatoryFlags() { return array(); } /** * Provide default, overridable flags to the linter. Generally these are * configuration flags which affect behavior but aren't critical. Flags * which are required should be provided in @{method:getMandatoryFlags} * instead. * * Default flags can be overridden with @{method:setFlags}. * * @return list Overridable default flags. * @task bin */ protected function getDefaultFlags() { return array(); } /** * Override default flags with custom flags. If not overridden, flags provided * by @{method:getDefaultFlags} are used. * * @param list New flags. * @return this * @task bin */ final public function setFlags($flags) { $this->flags = (array)$flags; return $this; } /** * Return the binary or script to execute. This method synthesizes defaults * and configuration. You can override the binary with @{method:setBinary}. * * @return string Binary to execute. * @task bin */ final public function getBinary() { return coalesce($this->bin, $this->getDefaultBinary()); } /** * Override the default binary with a new one. * * @param string New binary. * @return this * @task bin */ final public function setBinary($bin) { $this->bin = $bin; return $this; } /** * Return true if this linter should use an interpreter (like "python" or * "node") in addition to the script. * * After overriding this method to return `true`, override * @{method:getDefaultInterpreter} to set a default. * * @return bool True to use an interpreter. * @task bin */ public function shouldUseInterpreter() { return false; } /** * Return the default interpreter, like "python" or "node". This method is * only invoked if @{method:shouldUseInterpreter} has been overridden to * return `true`. * * @return string Default interpreter. * @task bin */ public function getDefaultInterpreter() { throw new Exception('Incomplete implementation!'); } /** * Get the effective interpreter. This method synthesizes configuration and * defaults. * * @return string Effective interpreter. * @task bin */ final public function getInterpreter() { return coalesce($this->interpreter, $this->getDefaultInterpreter()); } /** * Set the interpreter, overriding any default. * * @param string New interpreter. * @return this * @task bin */ final public function setInterpreter($interpreter) { $this->interpreter = $interpreter; return $this; } /* -( Parsing Linter Output )---------------------------------------------- */ /** * Parse the output of the external lint program into objects of class * @{class:ArcanistLintMessage} which `arc` can consume. Generally, this * means examining the output and converting each warning or error into a * message. * * If parsing fails, returning `false` will cause the caller to throw an * appropriate exception. (You can also throw a more specific exception if * you're able to detect a more specific condition.) Otherwise, return a list * of messages. * * @param string Path to the file being linted. * @param int Exit code of the linter. * @param string Stdout of the linter. * @param string Stderr of the linter. * @return list|false List of lint messages, or false * to indicate parser failure. * @task parse */ abstract protected function parseLinterOutput($path, $err, $stdout, $stderr); /* -( Executing the Linter )----------------------------------------------- */ /** * Check that the binary and interpreter (if applicable) exist, and throw * an exception with a message about how to install them if they do not. * * @return void */ final public function checkBinaryConfiguration() { $interpreter = null; if ($this->shouldUseInterpreter()) { $interpreter = $this->getInterpreter(); } $binary = $this->getBinary(); // NOTE: If we have an interpreter, we don't require the script to be // executable (so we just check that the path exists). Otherwise, the // binary must be executable. if ($interpreter) { if (!Filesystem::binaryExists($interpreter)) { throw new ArcanistUsageException( pht( 'Unable to locate interpreter "%s" to run linter %s. You may '. 'need to install the interpreter, or adjust your linter '. 'configuration.'. "\nTO INSTALL: %s", $interpreter, get_class($this), $this->getInstallInstructions())); } if (!Filesystem::pathExists($binary)) { throw new ArcanistUsageException( pht( 'Unable to locate script "%s" to run linter %s. You may need '. 'to install the script, or adjust your linter configuration. '. "\nTO INSTALL: %s", $binary, get_class($this), $this->getInstallInstructions())); } } else { if (!Filesystem::binaryExists($binary)) { throw new ArcanistUsageException( pht( 'Unable to locate binary "%s" to run linter %s. You may need '. 'to install the binary, or adjust your linter configuration. '. "\nTO INSTALL: %s", $binary, get_class($this), $this->getInstallInstructions())); } } } /** * Get the composed executable command, including the interpreter and binary * but without flags or paths. This can be used to execute `--version` * commands. * * @return string Command to execute the raw linter. * @task exec */ final protected function getExecutableCommand() { $this->checkBinaryConfiguration(); $interpreter = null; if ($this->shouldUseInterpreter()) { $interpreter = $this->getInterpreter(); } $binary = $this->getBinary(); if ($interpreter) { $bin = csprintf('%s %s', $interpreter, $binary); } else { $bin = csprintf('%s', $binary); } return $bin; } /** * Get the composed flags for the executable, including both mandatory and * configured flags. * * @return list Composed flags. * @task exec */ final protected function getCommandFlags() { $mandatory_flags = $this->getMandatoryFlags(); if (!is_array($mandatory_flags)) { phutil_deprecated( 'String support for flags.', 'You should use list instead.'); $mandatory_flags = (array) $mandatory_flags; } $flags = nonempty($this->flags, $this->getDefaultFlags()); if (!is_array($flags)) { phutil_deprecated( 'String support for flags.', 'You should use list instead.'); $flags = (array) $flags; } return array_merge($mandatory_flags, $flags); } public function getCacheVersion() { $version = $this->getVersion(); if ($version) { return $version.'-'.json_encode($this->getCommandFlags()); } else { // Either we failed to parse the version number or the `getVersion` // function hasn't been implemented. return json_encode($this->getCommandFlags()); } } /** * Prepare the path to be added to the command string. * * This method is expected to return an already escaped string. * * @param string Path to the file being linted * @return string The command-ready file argument */ protected function getPathArgumentForLinterFuture($path) { return csprintf('%s', $path); } final protected function buildFutures(array $paths) { $executable = $this->getExecutableCommand(); $bin = csprintf('%C %Ls', $executable, $this->getCommandFlags()); $futures = array(); foreach ($paths as $path) { if ($this->supportsReadDataFromStdin()) { $future = new ExecFuture( '%C %C', $bin, $this->getReadDataFromStdinFilename()); $future->write($this->getEngine()->loadData($path)); } else { // TODO: In commit hook mode, we need to do more handling here. $disk_path = $this->getEngine()->getFilePathOnDisk($path); $path_argument = $this->getPathArgumentForLinterFuture($disk_path); $future = new ExecFuture('%C %C', $bin, $path_argument); } $future->setCWD($this->getEngine()->getWorkingCopy()->getProjectRoot()); $futures[$path] = $future; } return $futures; } final protected function resolveFuture($path, Future $future) { list($err, $stdout, $stderr) = $future->resolve(); if ($err && !$this->shouldExpectCommandErrors()) { $future->resolvex(); } $messages = $this->parseLinterOutput($path, $err, $stdout, $stderr); if ($messages === false) { if ($err) { $future->resolvex(); } else { throw new Exception( "Linter failed to parse output!\n\n{$stdout}\n\n{$stderr}"); } } foreach ($messages as $message) { $this->addLintMessage($message); } } public function getLinterConfigurationOptions() { $options = array( 'bin' => array( 'type' => 'optional string | list', 'help' => pht( 'Specify a string (or list of strings) identifying the binary '. 'which should be invoked to execute this linter. This overrides '. 'the default binary. If you provide a list of possible binaries, '. - 'the first one which exists will be used.') + 'the first one which exists will be used.'), ), 'flags' => array( 'type' => 'optional list', 'help' => pht( 'Provide a list of additional flags to pass to the linter on the '. 'command line.'), ), ); if ($this->shouldUseInterpreter()) { $options['interpreter'] = array( 'type' => 'optional string | list', 'help' => pht( 'Specify a string (or list of strings) identifying the interpreter '. 'which should be used to invoke the linter binary. If you provide '. 'a list of possible interpreters, the first one that exists '. 'will be used.'), ); } return $options + parent::getLinterConfigurationOptions(); } public function setLinterConfigurationValue($key, $value) { switch ($key) { case 'interpreter': $working_copy = $this->getEngine()->getWorkingCopy(); $root = $working_copy->getProjectRoot(); foreach ((array)$value as $path) { if (Filesystem::binaryExists($path)) { $this->setInterpreter($path); return; } $path = Filesystem::resolvePath($path, $root); if (Filesystem::binaryExists($path)) { $this->setInterpreter($path); return; } } throw new Exception( pht('None of the configured interpreters can be located.')); case 'bin': $is_script = $this->shouldUseInterpreter(); $working_copy = $this->getEngine()->getWorkingCopy(); $root = $working_copy->getProjectRoot(); foreach ((array)$value as $path) { if (!$is_script && Filesystem::binaryExists($path)) { $this->setBinary($path); return; } $path = Filesystem::resolvePath($path, $root); if ((!$is_script && Filesystem::binaryExists($path)) || ($is_script && Filesystem::pathExists($path))) { $this->setBinary($path); return; } } throw new Exception( pht('None of the configured binaries can be located.')); case 'flags': if (!is_array($value)) { phutil_deprecated( 'String support for flags.', 'You should use list instead.'); $value = (array) $value; } $this->setFlags($value); return; } return parent::setLinterConfigurationValue($key, $value); } /** * 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; } } diff --git a/src/lint/linter/ArcanistLesscLinter.php b/src/lint/linter/ArcanistLesscLinter.php index 9e6647e1..00a40436 100644 --- a/src/lint/linter/ArcanistLesscLinter.php +++ b/src/lint/linter/ArcanistLesscLinter.php @@ -1,198 +1,199 @@ 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`.'); } public function shouldExpectCommandErrors() { return true; } public function supportsReadDataFromStdin() { // Technically `lessc` can read data from standard input however, when doing // so, relative imports cannot be resolved. Therefore, this functionality is // disabled. return false; } public function getReadDataFromStdinFilename() { return '-'; } protected function getMandatoryFlags() { return array( '--lint', '--no-color', '--strict-math='.($this->strictMath ? 'on' : 'off'), - '--strict-units='.($this->strictUnits ? '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)); } $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 76ff9c64..fa43496c 100644 --- a/src/lint/linter/ArcanistLinter.php +++ b/src/lint/linter/ArcanistLinter.php @@ -1,502 +1,502 @@ getLinterName(), $this->getLinterConfigurationName(), get_class($this)); } public function getLinterPriority() { return 1.0; } public function setCustomSeverityMap(array $map) { $this->customSeverityMap = $map; return $this; } public function setCustomSeverityRules(array $rules) { $this->customSeverityRules = $rules; return $this; } final public function getActivePath() { return $this->activePath; } 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; return $this; } final public function setPaths(array $paths) { $this->paths = $paths; 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). */ final private function filterPaths($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() { return $this->filterPaths(array_values($this->paths)); } 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]; } final public function setEngine(ArcanistLintEngine $engine) { $this->engine = $engine; return $this; } final protected function getEngine() { return $this->engine; } 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!'; } final protected function addLintMessage(ArcanistLintMessage $message) { if (!$this->getEngine()->getCommitHookMode()) { $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 willLintPath($path) { $this->stopAllLinters = false; $this->activePath = $path; } public function canRun() { return true; } public function willLintPaths(array $paths) { return; } abstract public function lintPath($path); abstract public function getLinterName(); public function getVersion() { return null; } public function didRunLinters() { // This is a hook. } 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.') + '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, '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}!"); } 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 '. 'Guide: Lint" in the documentation for more information.', $key)); return $result; } return $default; } } diff --git a/src/lint/linter/ArcanistPuppetLintLinter.php b/src/lint/linter/ArcanistPuppetLintLinter.php index 230ce540..efe83e9d 100644 --- a/src/lint/linter/ArcanistPuppetLintLinter.php +++ b/src/lint/linter/ArcanistPuppetLintLinter.php @@ -1,140 +1,142 @@ getExecutableCommand()); $matches = array(); $regex = '/^Puppet-lint (?P\d+\.\d+\.\d+)$/'; if (preg_match($regex, $stdout, $matches)) { return $matches['version']; } else { return false; } } public function getInstallInstructions() { return pht('Install puppet-lint using `gem install puppet-lint`.'); } public function shouldExpectCommandErrors() { return true; } public function supportsReadDataFromStdin() { return false; } protected function getMandatoryFlags() { return array(sprintf('--log-format=%s', implode('|', array( '%{linenumber}', '%{column}', '%{kind}', '%{check}', - '%{message}')))); + '%{message}', + ))), + ); } public function getLinterConfigurationOptions() { $options = array( 'puppet-lint.config' => array( 'type' => 'optional string', 'help' => pht('Pass in a custom configuration file path.'), ), ); return $options + parent::getLinterConfigurationOptions(); } public function setLinterConfigurationValue($key, $value) { switch ($key) { case 'puppet-lint.config': $this->config = $value; return; } return parent::setLinterConfigurationValue($key, $value); } protected function getDefaultFlags() { $options = array(); if ($this->config) { $options[] = '--config='.$this->config; } return $options; } protected function parseLinterOutput($path, $err, $stdout, $stderr) { $lines = phutil_split_lines($stdout, false); $messages = array(); foreach ($lines as $line) { $matches = explode('|', $line, 5); if (count($matches) === 5) { $message = new ArcanistLintMessage(); $message->setPath($path); $message->setLine($matches[0]); $message->setChar($matches[1]); $message->setName(ucwords(str_replace('_', ' ', $matches[3]))); $message->setDescription(ucfirst($matches[4])); switch ($matches[2]) { case 'warning': $message->setSeverity(ArcanistLintSeverity::SEVERITY_WARNING); break; case 'error': $message->setSeverity(ArcanistLintSeverity::SEVERITY_ERROR); break; default: $message->setSeverity(ArcanistLintSeverity::SEVERITY_ADVICE); break; } $messages[] = $message; } } if ($err && !$messages) { return false; } return $messages; } } diff --git a/src/lint/linter/ArcanistXHPASTLinter.php b/src/lint/linter/ArcanistXHPASTLinter.php index 183da16e..2a84ed30 100644 --- a/src/lint/linter/ArcanistXHPASTLinter.php +++ b/src/lint/linter/ArcanistXHPASTLinter.php @@ -1,2808 +1,2809 @@ 'PHP Syntax Error!', self::LINT_UNABLE_TO_PARSE => 'Unable to Parse', self::LINT_VARIABLE_VARIABLE => 'Use of Variable Variable', self::LINT_EXTRACT_USE => 'Use of extract()', self::LINT_UNDECLARED_VARIABLE => 'Use of Undeclared Variable', self::LINT_PHP_SHORT_TAG => 'Use of Short Tag " 'Use of Echo Tag " 'Use of Close Tag "?>"', self::LINT_NAMING_CONVENTIONS => 'Naming Conventions', self::LINT_IMPLICIT_CONSTRUCTOR => 'Implicit Constructor', self::LINT_DYNAMIC_DEFINE => 'Dynamic define()', self::LINT_STATIC_THIS => 'Use of $this in Static Context', self::LINT_PREG_QUOTE_MISUSE => 'Misuse of preg_quote()', self::LINT_PHP_OPEN_TAG => 'Expected Open Tag', self::LINT_TODO_COMMENT => 'TODO Comment', self::LINT_EXIT_EXPRESSION => 'Exit Used as Expression', self::LINT_COMMENT_STYLE => 'Comment Style', self::LINT_CLASS_FILENAME_MISMATCH => 'Class-Filename Mismatch', self::LINT_TAUTOLOGICAL_EXPRESSION => 'Tautological Expression', self::LINT_PLUS_OPERATOR_ON_STRINGS => 'Not String Concatenation', self::LINT_DUPLICATE_KEYS_IN_ARRAY => 'Duplicate Keys in Array', self::LINT_REUSED_ITERATORS => 'Reuse of Iterator Variable', self::LINT_BRACE_FORMATTING => 'Brace placement', self::LINT_PARENTHESES_SPACING => 'Spaces Inside Parentheses', self::LINT_CONTROL_STATEMENT_SPACING => 'Space After Control Statement', self::LINT_BINARY_EXPRESSION_SPACING => 'Space Around Binary Operator', self::LINT_ARRAY_INDEX_SPACING => 'Spacing Before Array Index', self::LINT_IMPLICIT_FALLTHROUGH => 'Implicit Fallthrough', self::LINT_REUSED_AS_ITERATOR => 'Variable Reused As Iterator', self::LINT_COMMENT_SPACING => 'Comment Spaces', self::LINT_SLOWNESS => 'Slow Construct', self::LINT_CLOSING_CALL_PAREN => 'Call Formatting', self::LINT_CLOSING_DECL_PAREN => 'Declaration Formatting', self::LINT_REUSED_ITERATOR_REFERENCE => 'Reuse of Iterator References', self::LINT_KEYWORD_CASING => 'Keyword Conventions', self::LINT_DOUBLE_QUOTE => 'Unnecessary Double Quotes', self::LINT_ELSEIF_USAGE => 'ElseIf Usage', self::LINT_SEMICOLON_SPACING => 'Semicolon Spacing', self::LINT_CONCATENATION_OPERATOR => 'Concatenation Spacing', self::LINT_PHP_COMPATIBILITY => 'PHP Compatibility', self::LINT_LANGUAGE_CONSTRUCT_PAREN => 'Language Construct Parentheses', self::LINT_EMPTY_STATEMENT => 'Empty Block Statement', self::LINT_ARRAY_SEPARATOR => 'Array Separator', ); } public function getLinterName() { return 'XHP'; } public function getLinterConfigurationName() { return 'xhpast'; } public function getLintSeverityMap() { $disabled = ArcanistLintSeverity::SEVERITY_DISABLED; $advice = ArcanistLintSeverity::SEVERITY_ADVICE; $warning = ArcanistLintSeverity::SEVERITY_WARNING; return array( self::LINT_TODO_COMMENT => $disabled, self::LINT_UNABLE_TO_PARSE => $warning, self::LINT_NAMING_CONVENTIONS => $warning, self::LINT_PREG_QUOTE_MISUSE => $advice, self::LINT_BRACE_FORMATTING => $warning, self::LINT_PARENTHESES_SPACING => $warning, self::LINT_CONTROL_STATEMENT_SPACING => $warning, self::LINT_BINARY_EXPRESSION_SPACING => $warning, self::LINT_ARRAY_INDEX_SPACING => $warning, self::LINT_IMPLICIT_FALLTHROUGH => $warning, self::LINT_SLOWNESS => $warning, self::LINT_COMMENT_SPACING => $advice, self::LINT_CLOSING_CALL_PAREN => $warning, self::LINT_CLOSING_DECL_PAREN => $warning, self::LINT_REUSED_ITERATOR_REFERENCE => $warning, self::LINT_KEYWORD_CASING => $warning, self::LINT_DOUBLE_QUOTE => $advice, self::LINT_ELSEIF_USAGE => $advice, self::LINT_SEMICOLON_SPACING => $advice, self::LINT_CONCATENATION_OPERATOR => $warning, self::LINT_LANGUAGE_CONSTRUCT_PAREN => $warning, self::LINT_EMPTY_STATEMENT => $advice, self::LINT_ARRAY_SEPARATOR => $advice, ); } public function getLinterConfigurationOptions() { return parent::getLinterConfigurationOptions() + array( 'xhpast.naminghook' => array( 'type' => 'optional string', 'help' => pht( 'Name of a concrete subclass of ArcanistXHPASTLintNamingHook which '. 'enforces more granular naming convention rules for symbols.'), ), 'xhpast.switchhook' => array( 'type' => 'optional string', 'help' => pht( 'Name of a concrete subclass of ArcanistXHPASTLintSwitchHook which '. 'tunes the analysis of switch() statements for this linter.'), ), 'xhpast.php-version' => array( 'type' => 'optional string', 'help' => pht('PHP version to target.'), ), 'xhpast.php-version.windows' => array( 'type' => 'optional string', 'help' => pht('PHP version to target on Windows.'), ), ); } public function setLinterConfigurationValue($key, $value) { switch ($key) { case 'xhpast.naminghook': $this->naminghook = $value; return; case 'xhpast.switchhook': $this->switchhook = $value; return; case 'xhpast.php-version': $this->version = $value; return; case 'xhpast.php-version.windows': $this->windowsVersion = $value; return; } return parent::setLinterConfigurationValue($key, $value); } public function getVersion() { // The version number should be incremented whenever a new rule is added. return '9'; } protected function resolveFuture($path, Future $future) { $tree = $this->getXHPASTTreeForPath($path); if (!$tree) { $ex = $this->getXHPASTExceptionForPath($path); if ($ex instanceof XHPASTSyntaxErrorException) { $this->raiseLintAtLine( $ex->getErrorLine(), 1, self::LINT_PHP_SYNTAX_ERROR, 'This file contains a syntax error: '.$ex->getMessage()); } else if ($ex instanceof Exception) { $this->raiseLintAtPath(self::LINT_UNABLE_TO_PARSE, $ex->getMessage()); } return; } $root = $tree->getRootNode(); $method_codes = array( 'lintStrstrUsedForCheck' => self::LINT_SLOWNESS, 'lintStrposUsedForStart' => self::LINT_SLOWNESS, 'lintImplicitFallthrough' => self::LINT_IMPLICIT_FALLTHROUGH, 'lintBraceFormatting' => self::LINT_BRACE_FORMATTING, 'lintTautologicalExpressions' => self::LINT_TAUTOLOGICAL_EXPRESSION, 'lintCommentSpaces' => self::LINT_COMMENT_SPACING, 'lintHashComments' => self::LINT_COMMENT_STYLE, 'lintReusedIterators' => self::LINT_REUSED_ITERATORS, 'lintReusedIteratorReferences' => self::LINT_REUSED_ITERATOR_REFERENCE, 'lintVariableVariables' => self::LINT_VARIABLE_VARIABLE, 'lintUndeclaredVariables' => array( self::LINT_EXTRACT_USE, self::LINT_REUSED_AS_ITERATOR, self::LINT_UNDECLARED_VARIABLE, ), 'lintPHPTagUse' => array( self::LINT_PHP_SHORT_TAG, self::LINT_PHP_ECHO_TAG, self::LINT_PHP_OPEN_TAG, self::LINT_PHP_CLOSE_TAG, ), 'lintNamingConventions' => self::LINT_NAMING_CONVENTIONS, 'lintSurpriseConstructors' => self::LINT_IMPLICIT_CONSTRUCTOR, 'lintParenthesesShouldHugExpressions' => self::LINT_PARENTHESES_SPACING, 'lintSpaceAfterControlStatementKeywords' => self::LINT_CONTROL_STATEMENT_SPACING, 'lintSpaceAroundBinaryOperators' => self::LINT_BINARY_EXPRESSION_SPACING, 'lintDynamicDefines' => self::LINT_DYNAMIC_DEFINE, 'lintUseOfThisInStaticMethods' => self::LINT_STATIC_THIS, 'lintPregQuote' => self::LINT_PREG_QUOTE_MISUSE, 'lintExitExpressions' => self::LINT_EXIT_EXPRESSION, 'lintArrayIndexWhitespace' => self::LINT_ARRAY_INDEX_SPACING, 'lintTODOComments' => self::LINT_TODO_COMMENT, 'lintPrimaryDeclarationFilenameMatch' => self::LINT_CLASS_FILENAME_MISMATCH, 'lintPlusOperatorOnStrings' => self::LINT_PLUS_OPERATOR_ON_STRINGS, 'lintDuplicateKeysInArray' => self::LINT_DUPLICATE_KEYS_IN_ARRAY, 'lintClosingCallParen' => self::LINT_CLOSING_CALL_PAREN, 'lintClosingDeclarationParen' => self::LINT_CLOSING_DECL_PAREN, 'lintKeywordCasing' => self::LINT_KEYWORD_CASING, 'lintStrings' => self::LINT_DOUBLE_QUOTE, 'lintElseIfStatements' => self::LINT_ELSEIF_USAGE, 'lintSemicolons' => self::LINT_SEMICOLON_SPACING, 'lintSpaceAroundConcatenationOperators' => self::LINT_CONCATENATION_OPERATOR, 'lintPHPCompatibility' => self::LINT_PHP_COMPATIBILITY, 'lintLanguageConstructParentheses' => self::LINT_LANGUAGE_CONSTRUCT_PAREN, 'lintEmptyBlockStatements' => self::LINT_EMPTY_STATEMENT, 'lintArraySeparator' => self::LINT_ARRAY_SEPARATOR, ); 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 lintStrstrUsedForCheck(XHPASTNode $root) { $expressions = $root->selectDescendantsOfType('n_BINARY_EXPRESSION'); foreach ($expressions as $expression) { $operator = $expression->getChildOfType(1, 'n_OPERATOR'); $operator = $operator->getConcreteString(); if ($operator !== '===' && $operator !== '!==') { continue; } $false = $expression->getChildByIndex(0); if ($false->getTypeName() === 'n_SYMBOL_NAME' && $false->getConcreteString() === 'false') { $strstr = $expression->getChildByIndex(2); } else { $strstr = $false; $false = $expression->getChildByIndex(2); if ($false->getTypeName() !== 'n_SYMBOL_NAME' || $false->getConcreteString() !== 'false') { continue; } } if ($strstr->getTypeName() !== 'n_FUNCTION_CALL') { continue; } $name = strtolower($strstr->getChildByIndex(0)->getConcreteString()); if ($name === 'strstr' || $name === 'strchr') { $this->raiseLintAtNode( $strstr, self::LINT_SLOWNESS, 'Use strpos() for checking if the string contains something.'); } else if ($name === 'stristr') { $this->raiseLintAtNode( $strstr, self::LINT_SLOWNESS, 'Use stripos() for checking if the string contains something.'); } } } private function lintStrposUsedForStart(XHPASTNode $root) { $expressions = $root->selectDescendantsOfType('n_BINARY_EXPRESSION'); foreach ($expressions as $expression) { $operator = $expression->getChildOfType(1, 'n_OPERATOR'); $operator = $operator->getConcreteString(); if ($operator !== '===' && $operator !== '!==') { continue; } $zero = $expression->getChildByIndex(0); if ($zero->getTypeName() === 'n_NUMERIC_SCALAR' && $zero->getConcreteString() === '0') { $strpos = $expression->getChildByIndex(2); } else { $strpos = $zero; $zero = $expression->getChildByIndex(2); if ($zero->getTypeName() !== 'n_NUMERIC_SCALAR' || $zero->getConcreteString() !== '0') { continue; } } if ($strpos->getTypeName() !== 'n_FUNCTION_CALL') { continue; } $name = strtolower($strpos->getChildByIndex(0)->getConcreteString()); if ($name === 'strpos') { $this->raiseLintAtNode( $strpos, self::LINT_SLOWNESS, 'Use strncmp() for checking if the string starts with something.'); } else if ($name === 'stripos') { $this->raiseLintAtNode( $strpos, self::LINT_SLOWNESS, 'Use strncasecmp() for checking if the string starts with '. 'something.'); } } } private function lintPHPCompatibility(XHPASTNode $root) { if (!$this->version) { return; } $target = phutil_get_library_root('phutil'). '/../resources/php_compat_info.json'; $compat_info = phutil_json_decode(Filesystem::readFile($target)); // Create a whitelist for symbols which are being used conditionally. $whitelist = array( 'class' => array(), 'function' => array(), ); $conditionals = $root->selectDescendantsOfType('n_IF'); foreach ($conditionals as $conditional) { $condition = $conditional->getChildOfType(0, 'n_CONTROL_CONDITION'); $function = $condition->getChildByIndex(0); if ($function->getTypeName() != 'n_FUNCTION_CALL') { continue; } $function_token = $function ->getChildByIndex(0); if ($function_token->getTypeName() != 'n_SYMBOL_NAME') { // This may be `Class::method(...)` or `$var(...)`. continue; } $function_name = $function_token->getConcreteString(); switch ($function_name) { case 'class_exists': case 'function_exists': case 'interface_exists': $type = null; switch ($function_name) { case 'class_exists': $type = 'class'; break; case 'function_exists': $type = 'function'; break; case 'interface_exists': $type = 'interface'; break; } $params = $function->getChildOfType(1, 'n_CALL_PARAMETER_LIST'); $symbol = $params->getChildByIndex(0); if (!$symbol->isStaticScalar()) { continue; } $symbol_name = $symbol->evalStatic(); if (!idx($whitelist[$type], $symbol_name)) { $whitelist[$type][$symbol_name] = array(); } $span = $conditional ->getChildByIndex(1) ->getTokens(); $whitelist[$type][$symbol_name][] = range( head_key($span), last_key($span)); break; } } $calls = $root->selectDescendantsOfType('n_FUNCTION_CALL'); foreach ($calls as $call) { $node = $call->getChildByIndex(0); $name = $node->getConcreteString(); $version = idx($compat_info['functions'], $name); if ($version && version_compare($version['min'], $this->version, '>')) { // Check if whitelisted. $whitelisted = false; foreach (idx($whitelist['function'], $name, array()) as $range) { if (array_intersect($range, array_keys($node->getTokens()))) { $whitelisted = true; break; } } if ($whitelisted) { continue; } $this->raiseLintAtNode( $node, self::LINT_PHP_COMPATIBILITY, "This codebase targets PHP {$this->version}, but `{$name}()` was ". "not introduced until PHP {$version['min']}."); } else if (array_key_exists($name, $compat_info['params'])) { $params = $call->getChildOfType(1, 'n_CALL_PARAMETER_LIST'); foreach (array_values($params->getChildren()) as $i => $param) { $version = idx($compat_info['params'][$name], $i); if ($version && version_compare($version, $this->version, '>')) { $this->raiseLintAtNode( $param, self::LINT_PHP_COMPATIBILITY, "This codebase targets PHP {$this->version}, but parameter ". ($i + 1)." of `{$name}()` was not introduced until PHP ". "{$version}."); } } } if ($this->windowsVersion) { $windows = idx($compat_info['functions_windows'], $name); if ($windows === false) { $this->raiseLintAtNode( $node, self::LINT_PHP_COMPATIBILITY, "This codebase targets PHP {$this->windowsVersion} on Windows, ". "but `{$name}()` is not available there."); } else if (version_compare($windows, $this->windowsVersion, '>')) { $this->raiseLintAtNode( $node, self::LINT_PHP_COMPATIBILITY, "This codebase targets PHP {$this->windowsVersion} on Windows, ". "but `{$name}()` is not available there until PHP ". "{$this->windowsVersion}."); } } } $classes = $root->selectDescendantsOfType('n_CLASS_NAME'); foreach ($classes as $node) { $name = $node->getConcreteString(); $version = idx($compat_info['interfaces'], $name); $version = idx($compat_info['classes'], $name, $version); if ($version && version_compare($version['min'], $this->version, '>')) { // Check if whitelisted. $whitelisted = false; foreach (idx($whitelist['class'], $name, array()) as $range) { if (array_intersect($range, array_keys($node->getTokens()))) { $whitelisted = true; break; } } if ($whitelisted) { continue; } $this->raiseLintAtNode( $node, self::LINT_PHP_COMPATIBILITY, "This codebase targets PHP {$this->version}, but `{$name}` was not ". "introduced until PHP {$version['min']}."); } } // TODO: Technically, this will include function names. This is unlikely to // cause any issues (unless, of course, there existed a function that had // the same name as some constant). $constants = $root->selectDescendantsOfType('n_SYMBOL_NAME'); foreach ($constants as $node) { $name = $node->getConcreteString(); $version = idx($compat_info['constants'], $name); if ($version && version_compare($version['min'], $this->version, '>')) { $this->raiseLintAtNode( $node, self::LINT_PHP_COMPATIBILITY, "This codebase targets PHP {$this->version}, but `{$name}` was not ". "introduced until PHP {$version['min']}."); } } if (version_compare($this->version, '5.3.0') < 0) { $this->lintPHP53Features($root); } else { $this->lintPHP53Incompatibilities($root); } if (version_compare($this->version, '5.4.0') < 0) { $this->lintPHP54Features($root); } else { $this->lintPHP54Incompatibilities($root); } } private function lintPHP53Features(XHPASTNode $root) { $functions = $root->selectTokensOfType('T_FUNCTION'); foreach ($functions as $function) { $next = $function->getNextToken(); while ($next) { if ($next->isSemantic()) { break; } $next = $next->getNextToken(); } if ($next) { if ($next->getTypeName() === '(') { $this->raiseLintAtToken( $function, self::LINT_PHP_COMPATIBILITY, "This codebase targets PHP {$this->version}, but anonymous ". "functions were not introduced until PHP 5.3."); } } } $namespaces = $root->selectTokensOfType('T_NAMESPACE'); foreach ($namespaces as $namespace) { $this->raiseLintAtToken( $namespace, self::LINT_PHP_COMPATIBILITY, "This codebase targets PHP {$this->version}, but namespaces were not ". "introduced until PHP 5.3."); } // NOTE: This is only "use x;", in anonymous functions the node type is // n_LEXICAL_VARIABLE_LIST even though both tokens are T_USE. // TODO: We parse n_USE in a slightly crazy way right now; that would be // a better selector once it's fixed. $uses = $root->selectDescendantsOfType('n_USE_LIST'); foreach ($uses as $use) { $this->raiseLintAtNode( $use, self::LINT_PHP_COMPATIBILITY, "This codebase targets PHP {$this->version}, but namespaces were not ". "introduced until PHP 5.3."); } $statics = $root->selectDescendantsOfType('n_CLASS_STATIC_ACCESS'); foreach ($statics as $static) { $name = $static->getChildByIndex(0); if ($name->getTypeName() != 'n_CLASS_NAME') { continue; } if ($name->getConcreteString() === 'static') { $this->raiseLintAtNode( $name, self::LINT_PHP_COMPATIBILITY, "This codebase targets PHP {$this->version}, but `static::` was not ". "introduced until PHP 5.3."); } } $ternaries = $root->selectDescendantsOfType('n_TERNARY_EXPRESSION'); foreach ($ternaries as $ternary) { $yes = $ternary->getChildByIndex(1); if ($yes->getTypeName() === 'n_EMPTY') { $this->raiseLintAtNode( $ternary, self::LINT_PHP_COMPATIBILITY, "This codebase targets PHP {$this->version}, but short ternary was ". "not introduced until PHP 5.3."); } } $heredocs = $root->selectDescendantsOfType('n_HEREDOC'); foreach ($heredocs as $heredoc) { if (preg_match('/^<<<[\'"]/', $heredoc->getConcreteString())) { $this->raiseLintAtNode( $heredoc, self::LINT_PHP_COMPATIBILITY, "This codebase targets PHP {$this->version}, but nowdoc was not ". "introduced until PHP 5.3."); } } } private function lintPHP53Incompatibilities(XHPASTNode $root) {} private function lintPHP54Features(XHPASTNode $root) { $indexes = $root->selectDescendantsOfType('n_INDEX_ACCESS'); foreach ($indexes as $index) { switch ($index->getChildByIndex(0)->getTypeName()) { case 'n_FUNCTION_CALL': case 'n_METHOD_CALL': $this->raiseLintAtNode( $index->getChildByIndex(1), self::LINT_PHP_COMPATIBILITY, pht( 'The `%s` syntax was not introduced until PHP 5.4, but this '. 'codebase targets an earlier version of PHP. You can rewrite '. 'this expression using `%s`.', 'f()[...]', 'idx()')); break; } } } private function lintPHP54Incompatibilities(XHPASTNode $root) { $breaks = $root->selectDescendantsOfTypes(array('n_BREAK', 'n_CONTINUE')); foreach ($breaks as $break) { $arg = $break->getChildByIndex(0); switch ($arg->getTypeName()) { case 'n_EMPTY': break; case 'n_NUMERIC_SCALAR': if ($arg->getConcreteString() != '0') { break; } default: $this->raiseLintAtNode( $break->getChildByIndex(0), self::LINT_PHP_COMPATIBILITY, pht( 'The `%s` and `%s` statements no longer accept '. 'variable arguments.', 'break', 'continue')); break; } } } private function lintImplicitFallthrough(XHPASTNode $root) { $hook_obj = null; $working_copy = $this->getEngine()->getWorkingCopy(); if ($working_copy) { $hook_class = $this->switchhook ? $this->switchhook : $this->getDeprecatedConfiguration('lint.xhpast.switchhook'); if ($hook_class) { $hook_obj = newv($hook_class, array()); assert_instances_of(array($hook_obj), 'ArcanistXHPASTLintSwitchHook'); } } $switches = $root->selectDescendantsOfType('n_SWITCH'); foreach ($switches as $switch) { $blocks = array(); $cases = $switch->selectDescendantsOfType('n_CASE'); foreach ($cases as $case) { $blocks[] = $case; } $defaults = $switch->selectDescendantsOfType('n_DEFAULT'); foreach ($defaults as $default) { $blocks[] = $default; } foreach ($blocks as $key => $block) { // Collect all the tokens in this block which aren't at top level. // We want to ignore "break", and "continue" in these blocks. $lower_level = $block->selectDescendantsOfType('n_WHILE'); $lower_level->add($block->selectDescendantsOfType('n_DO_WHILE')); $lower_level->add($block->selectDescendantsOfType('n_FOR')); $lower_level->add($block->selectDescendantsOfType('n_FOREACH')); $lower_level->add($block->selectDescendantsOfType('n_SWITCH')); $lower_level_tokens = array(); foreach ($lower_level as $lower_level_block) { $lower_level_tokens += $lower_level_block->getTokens(); } // Collect all the tokens in this block which aren't in this scope // (because they're inside class, function or interface declarations). // We want to ignore all of these tokens. $decls = $block->selectDescendantsOfType('n_FUNCTION_DECLARATION'); $decls->add($block->selectDescendantsOfType('n_CLASS_DECLARATION')); // For completeness; these can't actually have anything. $decls->add($block->selectDescendantsOfType('n_INTERFACE_DECLARATION')); $different_scope_tokens = array(); foreach ($decls as $decl) { $different_scope_tokens += $decl->getTokens(); } $lower_level_tokens += $different_scope_tokens; // Get all the trailing nonsemantic tokens, since we need to look for // "fallthrough" comments past the end of the semantic block. $tokens = $block->getTokens(); $last = end($tokens); while ($last && $last = $last->getNextToken()) { if ($last->isSemantic()) { break; } $tokens[$last->getTokenID()] = $last; } $blocks[$key] = array( $tokens, $lower_level_tokens, $different_scope_tokens, ); } foreach ($blocks as $token_lists) { list( $tokens, $lower_level_tokens, $different_scope_tokens) = $token_lists; // Test each block (case or default statement) to see if it's OK. It's // OK if: // // - it is empty; or // - it ends in break, return, throw, continue or exit at top level; or // - it has a comment with "fallthrough" in its text. // Empty blocks are OK, so we start this at `true` and only set it to // false if we find a statement. $block_ok = true; // Keeps track of whether the current statement is one that validates // the block (break, return, throw, continue) or something else. $statement_ok = false; foreach ($tokens as $token_id => $token) { if (!$token->isSemantic()) { // Liberally match "fall" in the comment text so that comments like // "fallthru", "fall through", "fallthrough", etc., are accepted. if (preg_match('/fall/i', $token->getValue())) { $block_ok = true; break; } continue; } $tok_type = $token->getTypeName(); if ($tok_type === 'T_FUNCTION' || $tok_type === 'T_CLASS' || $tok_type === 'T_INTERFACE') { // These aren't statements, but mark the block as nonempty anyway. $block_ok = false; continue; } if ($tok_type === ';') { if ($statement_ok) { $statment_ok = false; } else { $block_ok = false; } continue; } if ($tok_type === 'T_BREAK' || $tok_type === 'T_CONTINUE') { if (empty($lower_level_tokens[$token_id])) { $statement_ok = true; $block_ok = true; } continue; } if ($tok_type === 'T_RETURN' || $tok_type === 'T_THROW' || $tok_type === 'T_EXIT' || ($hook_obj && $hook_obj->checkSwitchToken($token))) { if (empty($different_scope_tokens[$token_id])) { $statement_ok = true; $block_ok = true; } continue; } } if (!$block_ok) { $this->raiseLintAtToken( head($tokens), self::LINT_IMPLICIT_FALLTHROUGH, "This 'case' or 'default' has a nonempty block which does not ". "end with 'break', 'continue', 'return', 'throw' or 'exit'. Did ". "you forget to add one of those? If you intend to fall through, ". "add a '// fallthrough' comment to silence this warning."); } } } } private function lintBraceFormatting(XHPASTNode $root) { foreach ($root->selectDescendantsOfType('n_STATEMENT_LIST') as $list) { $tokens = $list->getTokens(); if (!$tokens || head($tokens)->getValue() != '{') { continue; } list($before, $after) = $list->getSurroundingNonsemanticTokens(); if (!$before) { $first = head($tokens); // Only insert the space if we're after a closing parenthesis. If // we're in a construct like "else{}", other rules will insert space // after the 'else' correctly. $prev = $first->getPrevToken(); if (!$prev || $prev->getValue() !== ')') { continue; } $this->raiseLintAtToken( $first, self::LINT_BRACE_FORMATTING, 'Put opening braces on the same line as control statements and '. 'declarations, with a single space before them.', ' '.$first->getValue()); } else if (count($before) === 1) { $before = reset($before); if ($before->getValue() !== ' ') { $this->raiseLintAtToken( $before, self::LINT_BRACE_FORMATTING, 'Put opening braces on the same line as control statements and '. 'declarations, with a single space before them.', ' '); } } } } private function lintTautologicalExpressions(XHPASTNode $root) { $expressions = $root->selectDescendantsOfType('n_BINARY_EXPRESSION'); static $operators = array( '-' => true, '/' => true, '-=' => true, '/=' => true, '<=' => true, '<' => true, '==' => true, '===' => true, '!=' => true, '!==' => true, '>=' => true, '>' => true, ); static $logical = array( '||' => true, '&&' => true, ); foreach ($expressions as $expr) { $operator = $expr->getChildByIndex(1)->getConcreteString(); if (!empty($operators[$operator])) { $left = $expr->getChildByIndex(0)->getSemanticString(); $right = $expr->getChildByIndex(2)->getSemanticString(); if ($left === $right) { $this->raiseLintAtNode( $expr, self::LINT_TAUTOLOGICAL_EXPRESSION, 'Both sides of this expression are identical, so it always '. 'evaluates to a constant.'); } } if (!empty($logical[$operator])) { $left = $expr->getChildByIndex(0)->getSemanticString(); $right = $expr->getChildByIndex(2)->getSemanticString(); // NOTE: These will be null to indicate "could not evaluate". $left = $this->evaluateStaticBoolean($left); $right = $this->evaluateStaticBoolean($right); if (($operator === '||' && ($left === true || $right === true)) || ($operator === '&&' && ($left === false || $right === false))) { $this->raiseLintAtNode( $expr, self::LINT_TAUTOLOGICAL_EXPRESSION, 'The logical value of this expression is static. Did you forget '. 'to remove some debugging code?'); } } } } /** * Statically evaluate a boolean value from an XHP tree. * * TODO: Improve this and move it to XHPAST proper? * * @param string The "semantic string" of a single value. * @return mixed ##true## or ##false## if the value could be evaluated * statically; ##null## if static evaluation was not possible. */ private function evaluateStaticBoolean($string) { switch (strtolower($string)) { case '0': case 'null': case 'false': return false; case '1': case 'true': return true; } return null; } protected function lintCommentSpaces(XHPASTNode $root) { foreach ($root->selectTokensOfType('T_COMMENT') as $comment) { $value = $comment->getValue(); if ($value[0] !== '#') { $match = null; if (preg_match('@^(/[/*]+)[^/*\s]@', $value, $match)) { $this->raiseLintAtOffset( $comment->getOffset(), self::LINT_COMMENT_SPACING, 'Put space after comment start.', $match[1], $match[1].' '); } } } } protected function lintHashComments(XHPASTNode $root) { foreach ($root->selectTokensOfType('T_COMMENT') as $comment) { $value = $comment->getValue(); if ($value[0] !== '#') { continue; } $this->raiseLintAtOffset( $comment->getOffset(), self::LINT_COMMENT_STYLE, 'Use "//" single-line comments, not "#".', '#', (preg_match('/^#\S/', $value) ? '// ' : '//')); } } /** * Find cases where loops get nested inside each other but use the same * iterator variable. For example: * * COUNTEREXAMPLE * foreach ($list as $thing) { * foreach ($stuff as $thing) { // <-- Raises an error for reuse of $thing * // ... * } * } * */ private function lintReusedIterators(XHPASTNode $root) { $used_vars = array(); $for_loops = $root->selectDescendantsOfType('n_FOR'); foreach ($for_loops as $for_loop) { $var_map = array(); // Find all the variables that are assigned to in the for() expression. $for_expr = $for_loop->getChildOfType(0, 'n_FOR_EXPRESSION'); $bin_exprs = $for_expr->selectDescendantsOfType('n_BINARY_EXPRESSION'); foreach ($bin_exprs as $bin_expr) { if ($bin_expr->getChildByIndex(1)->getConcreteString() === '=') { $var = $bin_expr->getChildByIndex(0); $var_map[$var->getConcreteString()] = $var; } } $used_vars[$for_loop->getID()] = $var_map; } $foreach_loops = $root->selectDescendantsOfType('n_FOREACH'); foreach ($foreach_loops as $foreach_loop) { $var_map = array(); $foreach_expr = $foreach_loop->getChildOftype(0, 'n_FOREACH_EXPRESSION'); // We might use one or two vars, i.e. "foreach ($x as $y => $z)" or // "foreach ($x as $y)". $possible_used_vars = array( $foreach_expr->getChildByIndex(1), $foreach_expr->getChildByIndex(2), ); foreach ($possible_used_vars as $var) { if ($var->getTypeName() === 'n_EMPTY') { continue; } $name = $var->getConcreteString(); $name = trim($name, '&'); // Get rid of ref silliness. $var_map[$name] = $var; } $used_vars[$foreach_loop->getID()] = $var_map; } $all_loops = $for_loops->add($foreach_loops); foreach ($all_loops as $loop) { $child_for_loops = $loop->selectDescendantsOfType('n_FOR'); $child_foreach_loops = $loop->selectDescendantsOfType('n_FOREACH'); $child_loops = $child_for_loops->add($child_foreach_loops); $outer_vars = $used_vars[$loop->getID()]; foreach ($child_loops as $inner_loop) { $inner_vars = $used_vars[$inner_loop->getID()]; $shared = array_intersect_key($outer_vars, $inner_vars); if ($shared) { $shared_desc = implode(', ', array_keys($shared)); $message = $this->raiseLintAtNode( $inner_loop->getChildByIndex(0), self::LINT_REUSED_ITERATORS, "This loop reuses iterator variables ({$shared_desc}) from an ". "outer loop. You might be clobbering the outer iterator. Change ". "the inner loop to use a different iterator name."); $locations = array(); foreach ($shared as $var) { $locations[] = $this->getOtherLocation($var->getOffset()); } $message->setOtherLocations($locations); } } } } /** * Find cases where a foreach loop is being iterated using a variable * reference and the same variable is used outside of the loop without * calling unset() or reassigning the variable to another variable * reference. * * COUNTEREXAMPLE * foreach ($ar as &$a) { * // ... * } * $a = 1; // <-- Raises an error for using $a * */ protected function lintReusedIteratorReferences(XHPASTNode $root) { $fdefs = $root->selectDescendantsOfType('n_FUNCTION_DECLARATION'); $mdefs = $root->selectDescendantsOfType('n_METHOD_DECLARATION'); $defs = $fdefs->add($mdefs); foreach ($defs as $def) { $body = $def->getChildByIndex(5); if ($body->getTypeName() === 'n_EMPTY') { // Abstract method declaration. continue; } $exclude = array(); // Exclude uses of variables, unsets, and foreach loops // within closures - they are checked on their own $func_defs = $body->selectDescendantsOfType('n_FUNCTION_DECLARATION'); foreach ($func_defs as $func_def) { $vars = $func_def->selectDescendantsOfType('n_VARIABLE'); foreach ($vars as $var) { $exclude[$var->getID()] = true; } $unset_lists = $func_def->selectDescendantsOfType('n_UNSET_LIST'); foreach ($unset_lists as $unset_list) { $exclude[$unset_list->getID()] = true; } $foreaches = $func_def->selectDescendantsOfType('n_FOREACH'); foreach ($foreaches as $foreach) { $exclude[$foreach->getID()] = true; } } // Find all variables that are unset within the scope $unset_vars = array(); $unset_lists = $body->selectDescendantsOfType('n_UNSET_LIST'); foreach ($unset_lists as $unset_list) { if (isset($exclude[$unset_list->getID()])) { continue; } $unset_list_vars = $unset_list->selectDescendantsOfType('n_VARIABLE'); foreach ($unset_list_vars as $var) { $concrete = $this->getConcreteVariableString($var); $unset_vars[$concrete][] = $var->getOffset(); $exclude[$var->getID()] = true; } } // Find all reference variables in foreach expressions $reference_vars = array(); $foreaches = $body->selectDescendantsOfType('n_FOREACH'); foreach ($foreaches as $foreach) { if (isset($exclude[$foreach->getID()])) { continue; } $foreach_expr = $foreach->getChildOfType(0, 'n_FOREACH_EXPRESSION'); $var = $foreach_expr->getChildByIndex(2); if ($var->getTypeName() !== 'n_VARIABLE_REFERENCE') { continue; } $reference = $var->getChildByIndex(0); if ($reference->getTypeName() !== 'n_VARIABLE') { continue; } $reference_name = $this->getConcreteVariableString($reference); $reference_vars[$reference_name][] = $reference->getOffset(); $exclude[$reference->getID()] = true; // Exclude uses of the reference variable within the foreach loop $foreach_vars = $foreach->selectDescendantsOfType('n_VARIABLE'); foreach ($foreach_vars as $var) { $name = $this->getConcreteVariableString($var); if ($name === $reference_name) { $exclude[$var->getID()] = true; } } } // Allow usage if the reference variable is assigned to another // reference variable $binary = $body->selectDescendantsOfType('n_BINARY_EXPRESSION'); foreach ($binary as $expr) { if ($expr->getChildByIndex(1)->getConcreteString() !== '=') { continue; } $lval = $expr->getChildByIndex(0); if ($lval->getTypeName() !== 'n_VARIABLE') { continue; } $rval = $expr->getChildByIndex(2); if ($rval->getTypeName() !== 'n_VARIABLE_REFERENCE') { continue; } // Counts as unsetting a variable $concrete = $this->getConcreteVariableString($lval); $unset_vars[$concrete][] = $lval->getOffset(); $exclude[$lval->getID()] = true; } $all_vars = array(); $all = $body->selectDescendantsOfType('n_VARIABLE'); foreach ($all as $var) { if (isset($exclude[$var->getID()])) { continue; } $name = $this->getConcreteVariableString($var); if (!isset($reference_vars[$name])) { continue; } // Find the closest reference offset to this variable $reference_offset = null; foreach ($reference_vars[$name] as $offset) { if ($offset < $var->getOffset()) { $reference_offset = $offset; } else { break; } } if (!$reference_offset) { continue; } // Check if an unset exists between reference and usage of this // variable $warn = true; if (isset($unset_vars[$name])) { foreach ($unset_vars[$name] as $unset_offset) { if ($unset_offset > $reference_offset && $unset_offset < $var->getOffset()) { $warn = false; break; } } } if ($warn) { $this->raiseLintAtNode( $var, self::LINT_REUSED_ITERATOR_REFERENCE, 'This variable was used already as a by-reference iterator '. 'variable. Such variables survive outside the foreach loop, '. 'do not reuse.'); } } } } protected function lintVariableVariables(XHPASTNode $root) { $vvars = $root->selectDescendantsOfType('n_VARIABLE_VARIABLE'); foreach ($vvars as $vvar) { $this->raiseLintAtNode( $vvar, self::LINT_VARIABLE_VARIABLE, 'Rewrite this code to use an array. Variable variables are unclear '. 'and hinder static analysis.'); } } private function lintUndeclaredVariables(XHPASTNode $root) { // These things declare variables in a function: // Explicit parameters // Assignment // Assignment via list() // Static // Global // Lexical vars // Builtins ($this) // foreach() // catch // // These things make lexical scope unknowable: // Use of extract() // Assignment to variable variables ($$x) // Global with variable variables // // These things don't count as "using" a variable: // isset() // empty() // Static class variables // // The general approach here is to find each function/method declaration, // then: // // 1. Identify all the variable declarations, and where they first occur // in the function/method declaration. // 2. Identify all the uses that don't really count (as above). // 3. Everything else must be a use of a variable. // 4. For each variable, check if any uses occur before the declaration // and warn about them. // // We also keep track of where lexical scope becomes unknowable (e.g., // because the function calls extract() or uses dynamic variables, // preventing us from keeping track of which variables are defined) so we // can stop issuing warnings after that. // // TODO: Support functions defined inside other functions which is commonly // used with anonymous functions. $fdefs = $root->selectDescendantsOfType('n_FUNCTION_DECLARATION'); $mdefs = $root->selectDescendantsOfType('n_METHOD_DECLARATION'); $defs = $fdefs->add($mdefs); foreach ($defs as $def) { // We keep track of the first offset where scope becomes unknowable, and // silence any warnings after that. Default it to INT_MAX so we can min() // it later to keep track of the first problem we encounter. $scope_destroyed_at = PHP_INT_MAX; $declarations = array( '$this' => 0, ) + array_fill_keys($this->getSuperGlobalNames(), 0); $declaration_tokens = array(); $exclude_tokens = array(); $vars = array(); // First up, find all the different kinds of declarations, as explained // above. Put the tokens into the $vars array. $param_list = $def->getChildOfType(3, 'n_DECLARATION_PARAMETER_LIST'); $param_vars = $param_list->selectDescendantsOfType('n_VARIABLE'); foreach ($param_vars as $var) { $vars[] = $var; } // This is PHP5.3 closure syntax: function () use ($x) {}; $lexical_vars = $def ->getChildByIndex(4) ->selectDescendantsOfType('n_VARIABLE'); foreach ($lexical_vars as $var) { $vars[] = $var; } $body = $def->getChildByIndex(5); if ($body->getTypeName() === 'n_EMPTY') { // Abstract method declaration. continue; } $static_vars = $body ->selectDescendantsOfType('n_STATIC_DECLARATION') ->selectDescendantsOfType('n_VARIABLE'); foreach ($static_vars as $var) { $vars[] = $var; } $global_vars = $body ->selectDescendantsOfType('n_GLOBAL_DECLARATION_LIST'); foreach ($global_vars as $var_list) { foreach ($var_list->getChildren() as $var) { if ($var->getTypeName() === 'n_VARIABLE') { $vars[] = $var; } else { // Dynamic global variable, i.e. "global $$x;". $scope_destroyed_at = min($scope_destroyed_at, $var->getOffset()); // An error is raised elsewhere, no need to raise here. } } } // Include "catch (Exception $ex)", but not variables in the body of the // catch block. $catches = $body->selectDescendantsOfType('n_CATCH'); foreach ($catches as $catch) { $vars[] = $catch->getChildOfType(1, 'n_VARIABLE'); } $binary = $body->selectDescendantsOfType('n_BINARY_EXPRESSION'); foreach ($binary as $expr) { if ($expr->getChildByIndex(1)->getConcreteString() !== '=') { continue; } $lval = $expr->getChildByIndex(0); if ($lval->getTypeName() === 'n_VARIABLE') { $vars[] = $lval; } else if ($lval->getTypeName() === 'n_LIST') { // Recursivey grab everything out of list(), since the grammar // permits list() to be nested. Also note that list() is ONLY valid // as an lval assignments, so we could safely lift this out of the // n_BINARY_EXPRESSION branch. $assign_vars = $lval->selectDescendantsOfType('n_VARIABLE'); foreach ($assign_vars as $var) { $vars[] = $var; } } if ($lval->getTypeName() === 'n_VARIABLE_VARIABLE') { $scope_destroyed_at = min($scope_destroyed_at, $lval->getOffset()); // No need to raise here since we raise an error elsewhere. } } $calls = $body->selectDescendantsOfType('n_FUNCTION_CALL'); foreach ($calls as $call) { $name = strtolower($call->getChildByIndex(0)->getConcreteString()); if ($name === 'empty' || $name === 'isset') { $params = $call ->getChildOfType(1, 'n_CALL_PARAMETER_LIST') ->selectDescendantsOfType('n_VARIABLE'); foreach ($params as $var) { $exclude_tokens[$var->getID()] = true; } continue; } if ($name !== 'extract') { continue; } $scope_destroyed_at = min($scope_destroyed_at, $call->getOffset()); $this->raiseLintAtNode( $call, self::LINT_EXTRACT_USE, 'Avoid extract(). It is confusing and hinders static analysis.'); } // Now we have every declaration except foreach(), handled below. Build // two maps, one which just keeps track of which tokens are part of // declarations ($declaration_tokens) and one which has the first offset // where a variable is declared ($declarations). foreach ($vars as $var) { $concrete = $this->getConcreteVariableString($var); $declarations[$concrete] = min( idx($declarations, $concrete, PHP_INT_MAX), $var->getOffset()); $declaration_tokens[$var->getID()] = true; } // Excluded tokens are ones we don't "count" as being used, described // above. Put them into $exclude_tokens. $class_statics = $body ->selectDescendantsOfType('n_CLASS_STATIC_ACCESS'); $class_static_vars = $class_statics ->selectDescendantsOfType('n_VARIABLE'); foreach ($class_static_vars as $var) { $exclude_tokens[$var->getID()] = true; } // Find all the variables in scope, and figure out where they are used. // We want to find foreach() iterators which are both declared before and // used after the foreach() loop. $uses = array(); $all_vars = $body->selectDescendantsOfType('n_VARIABLE'); $all = array(); // NOTE: $all_vars is not a real array so we can't unset() it. foreach ($all_vars as $var) { // Be strict since it's easier; we don't let you reuse an iterator you // declared before a loop after the loop, even if you're just assigning // to it. $concrete = $this->getConcreteVariableString($var); $uses[$concrete][$var->getID()] = $var->getOffset(); if (isset($declaration_tokens[$var->getID()])) { // We know this is part of a declaration, so it's fine. continue; } if (isset($exclude_tokens[$var->getID()])) { // We know this is part of isset() or similar, so it's fine. continue; } $all[$var->getOffset()] = $concrete; } // Do foreach() last, we want to handle implicit redeclaration of a // variable already in scope since this probably means we're ovewriting a // local. // NOTE: Processing foreach expressions in order allows programs which // reuse iterator variables in other foreach() loops -- this is fine. We // have a separate warning to prevent nested loops from reusing the same // iterators. $foreaches = $body->selectDescendantsOfType('n_FOREACH'); $all_foreach_vars = array(); foreach ($foreaches as $foreach) { $foreach_expr = $foreach->getChildOfType(0, 'n_FOREACH_EXPRESSION'); $foreach_vars = array(); // Determine the end of the foreach() loop. $foreach_tokens = $foreach->getTokens(); $last_token = end($foreach_tokens); $foreach_end = $last_token->getOffset(); $key_var = $foreach_expr->getChildByIndex(1); if ($key_var->getTypeName() === 'n_VARIABLE') { $foreach_vars[] = $key_var; } $value_var = $foreach_expr->getChildByIndex(2); if ($value_var->getTypeName() === 'n_VARIABLE') { $foreach_vars[] = $value_var; } else { // The root-level token may be a reference, as in: // foreach ($a as $b => &$c) { ... } // Reach into the n_VARIABLE_REFERENCE node to grab the n_VARIABLE // node. $var = $value_var->getChildByIndex(0); if ($var->getTypeName() === 'n_VARIABLE_VARIABLE') { $var = $var->getChildByIndex(0); } $foreach_vars[] = $var; } // Remove all uses of the iterators inside of the foreach() loop from // the $uses map. foreach ($foreach_vars as $var) { $concrete = $this->getConcreteVariableString($var); $offset = $var->getOffset(); foreach ($uses[$concrete] as $id => $use_offset) { if (($use_offset >= $offset) && ($use_offset < $foreach_end)) { unset($uses[$concrete][$id]); } } $all_foreach_vars[] = $var; } } foreach ($all_foreach_vars as $var) { $concrete = $this->getConcreteVariableString($var); $offset = $var->getOffset(); // If a variable was declared before a foreach() and is used after // it, raise a message. if (isset($declarations[$concrete])) { if ($declarations[$concrete] < $offset) { if (!empty($uses[$concrete]) && max($uses[$concrete]) > $offset) { $message = $this->raiseLintAtNode( $var, self::LINT_REUSED_AS_ITERATOR, 'This iterator variable is a previously declared local '. 'variable. To avoid overwriting locals, do not reuse them '. 'as iterator variables.'); $message->setOtherLocations(array( $this->getOtherLocation($declarations[$concrete]), $this->getOtherLocation(max($uses[$concrete])), )); } } } // This is a declaration, exclude it from the "declare variables prior // to use" check below. unset($all[$var->getOffset()]); $vars[] = $var; } // Now rebuild declarations to include foreach(). foreach ($vars as $var) { $concrete = $this->getConcreteVariableString($var); $declarations[$concrete] = min( idx($declarations, $concrete, PHP_INT_MAX), $var->getOffset()); $declaration_tokens[$var->getID()] = true; } foreach (array('n_STRING_SCALAR', 'n_HEREDOC') as $type) { foreach ($body->selectDescendantsOfType($type) as $string) { foreach ($string->getStringVariables() as $offset => $var) { $all[$string->getOffset() + $offset - 1] = '$'.$var; } } } // Issue a warning for every variable token, unless it appears in a // declaration, we know about a prior declaration, we have explicitly // exlcuded it, or scope has been made unknowable before it appears. $issued_warnings = array(); foreach ($all as $offset => $concrete) { if ($offset >= $scope_destroyed_at) { // This appears after an extract() or $$var so we have no idea // whether it's legitimate or not. We raised a harshly-worded warning // when scope was made unknowable, so just ignore anything we can't // figure out. continue; } if ($offset >= idx($declarations, $concrete, PHP_INT_MAX)) { // The use appears after the variable is declared, so it's fine. continue; } if (!empty($issued_warnings[$concrete])) { // We've already issued a warning for this variable so we don't need // to issue another one. continue; } $this->raiseLintAtOffset( $offset, self::LINT_UNDECLARED_VARIABLE, 'Declare variables prior to use (even if you are passing them '. 'as reference parameters). You may have misspelled this '. 'variable name.', $concrete); $issued_warnings[$concrete] = true; } } } private function getConcreteVariableString(XHPASTNode $var) { $concrete = $var->getConcreteString(); // Strip off curly braces as in $obj->{$property}. $concrete = trim($concrete, '{}'); return $concrete; } private function lintPHPTagUse(XHPASTNode $root) { $tokens = $root->getTokens(); foreach ($tokens as $token) { if ($token->getTypeName() === 'T_OPEN_TAG') { if (trim($token->getValue()) === 'raiseLintAtToken( $token, self::LINT_PHP_SHORT_TAG, 'Use the full form of the PHP open tag, "getTypeName() === 'T_OPEN_TAG_WITH_ECHO') { $this->raiseLintAtToken( $token, self::LINT_PHP_ECHO_TAG, 'Avoid the PHP echo short form, "getValue())) { $this->raiseLintAtToken( $token, self::LINT_PHP_OPEN_TAG, 'PHP files should start with "selectTokensOfType('T_CLOSE_TAG') as $token) { $this->raiseLintAtToken( $token, self::LINT_PHP_CLOSE_TAG, 'Do not use the PHP closing tag, "?>".'); } } private function lintNamingConventions(XHPASTNode $root) { // We're going to build up a list of tuples // and then try to instantiate a hook class which has the opportunity to // override us. $names = array(); $classes = $root->selectDescendantsOfType('n_CLASS_DECLARATION'); foreach ($classes as $class) { $name_token = $class->getChildByIndex(1); $name_string = $name_token->getConcreteString(); $names[] = array( 'class', $name_string, $name_token, ArcanistXHPASTLintNamingHook::isUpperCamelCase($name_string) ? null : 'Follow naming conventions: classes should be named using '. 'UpperCamelCase.', ); } $ifaces = $root->selectDescendantsOfType('n_INTERFACE_DECLARATION'); foreach ($ifaces as $iface) { $name_token = $iface->getChildByIndex(1); $name_string = $name_token->getConcreteString(); $names[] = array( 'interface', $name_string, $name_token, ArcanistXHPASTLintNamingHook::isUpperCamelCase($name_string) ? null : 'Follow naming conventions: interfaces should be named using '. 'UpperCamelCase.', ); } $functions = $root->selectDescendantsOfType('n_FUNCTION_DECLARATION'); foreach ($functions as $function) { $name_token = $function->getChildByIndex(2); if ($name_token->getTypeName() === 'n_EMPTY') { // Unnamed closure. continue; } $name_string = $name_token->getConcreteString(); $names[] = array( 'function', $name_string, $name_token, ArcanistXHPASTLintNamingHook::isLowercaseWithUnderscores( ArcanistXHPASTLintNamingHook::stripPHPFunction($name_string)) ? null : 'Follow naming conventions: functions should be named using '. 'lowercase_with_underscores.', ); } $methods = $root->selectDescendantsOfType('n_METHOD_DECLARATION'); foreach ($methods as $method) { $name_token = $method->getChildByIndex(2); $name_string = $name_token->getConcreteString(); $names[] = array( 'method', $name_string, $name_token, ArcanistXHPASTLintNamingHook::isLowerCamelCase( ArcanistXHPASTLintNamingHook::stripPHPFunction($name_string)) ? null : 'Follow naming conventions: methods should be named using '. 'lowerCamelCase.', ); } $param_tokens = array(); $params = $root->selectDescendantsOfType('n_DECLARATION_PARAMETER_LIST'); foreach ($params as $param_list) { foreach ($param_list->getChildren() as $param) { $name_token = $param->getChildByIndex(1); if ($name_token->getTypeName() === 'n_VARIABLE_REFERENCE') { $name_token = $name_token->getChildOfType(0, 'n_VARIABLE'); } $param_tokens[$name_token->getID()] = true; $name_string = $name_token->getConcreteString(); $names[] = array( 'parameter', $name_string, $name_token, ArcanistXHPASTLintNamingHook::isLowercaseWithUnderscores( ArcanistXHPASTLintNamingHook::stripPHPVariable($name_string)) ? null : 'Follow naming conventions: parameters should be named using '. 'lowercase_with_underscores.', ); } } $constants = $root->selectDescendantsOfType( 'n_CLASS_CONSTANT_DECLARATION_LIST'); foreach ($constants as $constant_list) { foreach ($constant_list->getChildren() as $constant) { $name_token = $constant->getChildByIndex(0); $name_string = $name_token->getConcreteString(); $names[] = array( 'constant', $name_string, $name_token, ArcanistXHPASTLintNamingHook::isUppercaseWithUnderscores($name_string) ? null : 'Follow naming conventions: class constants should be named '. 'using UPPERCASE_WITH_UNDERSCORES.', ); } } $member_tokens = array(); $props = $root->selectDescendantsOfType('n_CLASS_MEMBER_DECLARATION_LIST'); foreach ($props as $prop_list) { foreach ($prop_list->getChildren() as $token_id => $prop) { if ($prop->getTypeName() === 'n_CLASS_MEMBER_MODIFIER_LIST') { continue; } $name_token = $prop->getChildByIndex(0); $member_tokens[$name_token->getID()] = true; $name_string = $name_token->getConcreteString(); $names[] = array( 'member', $name_string, $name_token, ArcanistXHPASTLintNamingHook::isLowerCamelCase( ArcanistXHPASTLintNamingHook::stripPHPVariable($name_string)) ? null : 'Follow naming conventions: class properties should be named '. 'using lowerCamelCase.', ); } } $superglobal_map = array_fill_keys( $this->getSuperGlobalNames(), true); $fdefs = $root->selectDescendantsOfType('n_FUNCTION_DECLARATION'); $mdefs = $root->selectDescendantsOfType('n_METHOD_DECLARATION'); $defs = $fdefs->add($mdefs); foreach ($defs as $def) { $globals = $def->selectDescendantsOfType('n_GLOBAL_DECLARATION_LIST'); $globals = $globals->selectDescendantsOfType('n_VARIABLE'); $globals_map = array(); foreach ($globals as $global) { $global_string = $global->getConcreteString(); $globals_map[$global_string] = true; $names[] = array( 'user', $global_string, $global, // No advice for globals, but hooks have an option to provide some. - null); + null, + ); } // Exclude access of static properties, since lint will be raised at // their declaration if they're invalid and they may not conform to // variable rules. This is slightly overbroad (includes the entire // rhs of a "Class::..." token) to cover cases like "Class:$x[0]". These // variables are simply made exempt from naming conventions. $exclude_tokens = array(); $statics = $def->selectDescendantsOfType('n_CLASS_STATIC_ACCESS'); foreach ($statics as $static) { $rhs = $static->getChildByIndex(1); $rhs_vars = $def->selectDescendantsOfType('n_VARIABLE'); foreach ($rhs_vars as $var) { $exclude_tokens[$var->getID()] = true; } } $vars = $def->selectDescendantsOfType('n_VARIABLE'); foreach ($vars as $token_id => $var) { if (isset($member_tokens[$token_id])) { continue; } if (isset($param_tokens[$token_id])) { continue; } if (isset($exclude_tokens[$token_id])) { continue; } $var_string = $var->getConcreteString(); // Awkward artifact of "$o->{$x}". $var_string = trim($var_string, '{}'); if (isset($superglobal_map[$var_string])) { continue; } if (isset($globals_map[$var_string])) { continue; } $names[] = array( 'variable', $var_string, $var, ArcanistXHPASTLintNamingHook::isLowercaseWithUnderscores( ArcanistXHPASTLintNamingHook::stripPHPVariable($var_string)) ? null : 'Follow naming conventions: variables should be named using '. 'lowercase_with_underscores.', ); } } $engine = $this->getEngine(); $working_copy = $engine->getWorkingCopy(); if ($working_copy) { // If a naming hook is configured, give it a chance to override the // default results for all the symbol names. $hook_class = $this->naminghook ? $this->naminghook : $working_copy->getProjectConfig('lint.xhpast.naminghook'); if ($hook_class) { $hook_obj = newv($hook_class, array()); foreach ($names as $k => $name_attrs) { list($type, $name, $token, $default) = $name_attrs; $result = $hook_obj->lintSymbolName($type, $name, $default); $names[$k][3] = $result; } } } // Raise anything we're left with. foreach ($names as $k => $name_attrs) { list($type, $name, $token, $result) = $name_attrs; if ($result) { $this->raiseLintAtNode( $token, self::LINT_NAMING_CONVENTIONS, $result); } } } private function lintSurpriseConstructors(XHPASTNode $root) { $classes = $root->selectDescendantsOfType('n_CLASS_DECLARATION'); foreach ($classes as $class) { $class_name = $class->getChildByIndex(1)->getConcreteString(); $methods = $class->selectDescendantsOfType('n_METHOD_DECLARATION'); foreach ($methods as $method) { $method_name_token = $method->getChildByIndex(2); $method_name = $method_name_token->getConcreteString(); if (strtolower($class_name) === strtolower($method_name)) { $this->raiseLintAtNode( $method_name_token, self::LINT_IMPLICIT_CONSTRUCTOR, 'Name constructors __construct() explicitly. This method is a '. 'constructor because it has the same name as the class it is '. 'defined in.'); } } } } private function lintParenthesesShouldHugExpressions(XHPASTNode $root) { $calls = $root->selectDescendantsOfType('n_CALL_PARAMETER_LIST'); $controls = $root->selectDescendantsOfType('n_CONTROL_CONDITION'); $fors = $root->selectDescendantsOfType('n_FOR_EXPRESSION'); $foreach = $root->selectDescendantsOfType('n_FOREACH_EXPRESSION'); $decl = $root->selectDescendantsOfType('n_DECLARATION_PARAMETER_LIST'); $all_paren_groups = $calls ->add($controls) ->add($fors) ->add($foreach) ->add($decl); foreach ($all_paren_groups as $group) { $tokens = $group->getTokens(); $token_o = array_shift($tokens); $token_c = array_pop($tokens); if ($token_o->getTypeName() !== '(') { throw new Exception('Expected open paren!'); } if ($token_c->getTypeName() !== ')') { throw new Exception('Expected close paren!'); } $nonsem_o = $token_o->getNonsemanticTokensAfter(); $nonsem_c = $token_c->getNonsemanticTokensBefore(); if (!$nonsem_o) { continue; } $raise = array(); $string_o = implode('', mpull($nonsem_o, 'getValue')); if (preg_match('/^[ ]+$/', $string_o)) { $raise[] = array($nonsem_o, $string_o); } if ($nonsem_o !== $nonsem_c) { $string_c = implode('', mpull($nonsem_c, 'getValue')); if (preg_match('/^[ ]+$/', $string_c)) { $raise[] = array($nonsem_c, $string_c); } } foreach ($raise as $warning) { list($tokens, $string) = $warning; $this->raiseLintAtOffset( reset($tokens)->getOffset(), self::LINT_PARENTHESES_SPACING, 'Parentheses should hug their contents.', $string, ''); } } } private function lintSpaceAfterControlStatementKeywords(XHPASTNode $root) { foreach ($root->getTokens() as $id => $token) { switch ($token->getTypeName()) { case 'T_IF': case 'T_ELSE': case 'T_FOR': case 'T_FOREACH': case 'T_WHILE': case 'T_DO': case 'T_SWITCH': $after = $token->getNonsemanticTokensAfter(); if (empty($after)) { $this->raiseLintAtToken( $token, self::LINT_CONTROL_STATEMENT_SPACING, 'Convention: put a space after control statements.', $token->getValue().' '); } else if (count($after) === 1) { $space = head($after); // If we have an else clause with braces, $space may not be // a single white space. e.g., // // if ($x) // echo 'foo' // else // <- $space is not " " but "\n ". // echo 'bar' // // We just require it starts with either a whitespace or a newline. if ($token->getTypeName() === 'T_ELSE' || $token->getTypeName() === 'T_DO') { break; } if ($space->isAnyWhitespace() && $space->getValue() !== ' ') { $this->raiseLintAtToken( $space, self::LINT_CONTROL_STATEMENT_SPACING, 'Convention: put a single space after control statements.', ' '); } } break; } } } private function lintSpaceAroundBinaryOperators(XHPASTNode $root) { $expressions = $root->selectDescendantsOfType('n_BINARY_EXPRESSION'); foreach ($expressions as $expression) { $operator = $expression->getChildByIndex(1); $operator_value = $operator->getConcreteString(); list($before, $after) = $operator->getSurroundingNonsemanticTokens(); $replace = null; if (empty($before) && empty($after)) { $replace = " {$operator_value} "; } else if (empty($before)) { $replace = " {$operator_value}"; } else if (empty($after)) { $replace = "{$operator_value} "; } if ($replace !== null) { $this->raiseLintAtNode( $operator, self::LINT_BINARY_EXPRESSION_SPACING, 'Convention: logical and arithmetic operators should be '. 'surrounded by whitespace.', $replace); } } $tokens = $root->selectTokensOfType(','); foreach ($tokens as $token) { $next = $token->getNextToken(); switch ($next->getTypeName()) { case ')': case 'T_WHITESPACE': break; default: $this->raiseLintAtToken( $token, self::LINT_BINARY_EXPRESSION_SPACING, 'Convention: comma should be followed by space.', ', '); break; } } $tokens = $root->selectTokensOfType('T_DOUBLE_ARROW'); foreach ($tokens as $token) { $prev = $token->getPrevToken(); $next = $token->getNextToken(); $prev_type = $prev->getTypeName(); $next_type = $next->getTypeName(); $prev_space = ($prev_type === 'T_WHITESPACE'); $next_space = ($next_type === 'T_WHITESPACE'); $replace = null; if (!$prev_space && !$next_space) { $replace = ' => '; } else if ($prev_space && !$next_space) { $replace = '=> '; } else if (!$prev_space && $next_space) { $replace = ' =>'; } if ($replace !== null) { $this->raiseLintAtToken( $token, self::LINT_BINARY_EXPRESSION_SPACING, 'Convention: double arrow should be surrounded by whitespace.', $replace); } } $parameters = $root->selectDescendantsOfType('n_DECLARATION_PARAMETER'); foreach ($parameters as $parameter) { if ($parameter->getChildByIndex(2)->getTypeName() == 'n_EMPTY') { continue; } $operator = head($parameter->selectTokensOfType('=')); $before = $operator->getNonsemanticTokensBefore(); $after = $operator->getNonsemanticTokensAfter(); $replace = null; if (empty($before) && empty($after)) { $replace = ' = '; } else if (empty($before)) { $replace = ' ='; } else if (empty($after)) { $replace = '= '; } if ($replace !== null) { $this->raiseLintAtToken( $operator, self::LINT_BINARY_EXPRESSION_SPACING, 'Convention: logical and arithmetic operators should be '. 'surrounded by whitespace.', $replace); } } } private function lintSpaceAroundConcatenationOperators(XHPASTNode $root) { $tokens = $root->selectTokensOfType('.'); foreach ($tokens as $token) { $prev = $token->getPrevToken(); $next = $token->getNextToken(); foreach (array('prev' => $prev, 'next' => $next) as $wtoken) { if ($wtoken->getTypeName() !== 'T_WHITESPACE') { continue; } $value = $wtoken->getValue(); if (strpos($value, "\n") !== false) { // If the whitespace has a newline, it's conventional. continue; } $next = $wtoken->getNextToken(); if ($next && $next->getTypeName() === 'T_COMMENT') { continue; } $this->raiseLintAtToken( $wtoken, self::LINT_CONCATENATION_OPERATOR, 'Convention: no spaces around "." (string concatenation) operator.', ''); } } } private function lintDynamicDefines(XHPASTNode $root) { $calls = $root->selectDescendantsOfType('n_FUNCTION_CALL'); foreach ($calls as $call) { $name = $call->getChildByIndex(0)->getConcreteString(); if (strtolower($name) === 'define') { $parameter_list = $call->getChildOfType(1, 'n_CALL_PARAMETER_LIST'); $defined = $parameter_list->getChildByIndex(0); if (!$defined->isStaticScalar()) { $this->raiseLintAtNode( $defined, self::LINT_DYNAMIC_DEFINE, 'First argument to define() must be a string literal.'); } } } } private function lintUseOfThisInStaticMethods(XHPASTNode $root) { $classes = $root->selectDescendantsOfType('n_CLASS_DECLARATION'); foreach ($classes as $class) { $methods = $class->selectDescendantsOfType('n_METHOD_DECLARATION'); foreach ($methods as $method) { $attributes = $method ->getChildByIndex(0, 'n_METHOD_MODIFIER_LIST') ->selectDescendantsOfType('n_STRING'); $method_is_static = false; $method_is_abstract = false; foreach ($attributes as $attribute) { if (strtolower($attribute->getConcreteString()) === 'static') { $method_is_static = true; } if (strtolower($attribute->getConcreteString()) === 'abstract') { $method_is_abstract = true; } } if ($method_is_abstract) { continue; } if (!$method_is_static) { continue; } $body = $method->getChildOfType(5, 'n_STATEMENT_LIST'); $variables = $body->selectDescendantsOfType('n_VARIABLE'); foreach ($variables as $variable) { if ($method_is_static && strtolower($variable->getConcreteString()) === '$this') { $this->raiseLintAtNode( $variable, self::LINT_STATIC_THIS, 'You can not reference "$this" inside a static method.'); } } } } } /** * preg_quote() takes two arguments, but the second one is optional because * it is possible to use (), [] or {} as regular expression delimiters. If * you don't pass a second argument, you're probably going to get something * wrong. */ private function lintPregQuote(XHPASTNode $root) { $function_calls = $root->selectDescendantsOfType('n_FUNCTION_CALL'); foreach ($function_calls as $call) { $name = $call->getChildByIndex(0)->getConcreteString(); if (strtolower($name) === 'preg_quote') { $parameter_list = $call->getChildOfType(1, 'n_CALL_PARAMETER_LIST'); if (count($parameter_list->getChildren()) !== 2) { $this->raiseLintAtNode( $call, self::LINT_PREG_QUOTE_MISUSE, 'If you use pattern delimiters that require escaping (such as //, '. 'but not ()) then you should pass two arguments to preg_quote(), '. 'so that preg_quote() knows which delimiter to escape.'); } } } } /** * Exit is parsed as an expression, but using it as such is almost always * wrong. That is, this is valid: * * strtoupper(33 * exit - 6); * * When exit is used as an expression, it causes the program to terminate with * exit code 0. This is likely not what is intended; these statements have * different effects: * * exit(-1); * exit -1; * * The former exits with a failure code, the latter with a success code! */ private function lintExitExpressions(XHPASTNode $root) { $unaries = $root->selectDescendantsOfType('n_UNARY_PREFIX_EXPRESSION'); foreach ($unaries as $unary) { $operator = $unary->getChildByIndex(0)->getConcreteString(); if (strtolower($operator) === 'exit') { if ($unary->getParentNode()->getTypeName() !== 'n_STATEMENT') { $this->raiseLintAtNode( $unary, self::LINT_EXIT_EXPRESSION, 'Use exit as a statement, not an expression.'); } } } } private function lintArrayIndexWhitespace(XHPASTNode $root) { $indexes = $root->selectDescendantsOfType('n_INDEX_ACCESS'); foreach ($indexes as $index) { $tokens = $index->getChildByIndex(0)->getTokens(); $last = array_pop($tokens); $trailing = $last->getNonsemanticTokensAfter(); $trailing_text = implode('', mpull($trailing, 'getValue')); if (preg_match('/^ +$/', $trailing_text)) { $this->raiseLintAtOffset( $last->getOffset() + strlen($last->getValue()), self::LINT_ARRAY_INDEX_SPACING, 'Convention: no spaces before index access.', $trailing_text, ''); } } } private function lintTODOComments(XHPASTNode $root) { $comments = $root->selectTokensOfType('T_COMMENT') + $root->selectTokensOfType('T_DOC_COMMENT'); foreach ($comments as $token) { $value = $token->getValue(); if ($token->getTypeName() === 'T_DOC_COMMENT') { $regex = '/(TODO|@todo)/'; } else { $regex = '/TODO/'; } $matches = null; $preg = preg_match_all( $regex, $value, $matches, PREG_OFFSET_CAPTURE); foreach ($matches[0] as $match) { list($string, $offset) = $match; $this->raiseLintAtOffset( $token->getOffset() + $offset, self::LINT_TODO_COMMENT, 'This comment has a TODO.', $string); } } } /** * Lint that if the file declares exactly one interface or class, * the name of the file matches the name of the class, * unless the classname is funky like an XHP element. */ private function lintPrimaryDeclarationFilenameMatch(XHPASTNode $root) { $classes = $root->selectDescendantsOfType('n_CLASS_DECLARATION'); $interfaces = $root->selectDescendantsOfType('n_INTERFACE_DECLARATION'); if (count($classes) + count($interfaces) !== 1) { return; } $declarations = count($classes) ? $classes : $interfaces; $declarations->rewind(); $declaration = $declarations->current(); $decl_name = $declaration->getChildByIndex(1); $decl_string = $decl_name->getConcreteString(); // Exclude strangely named classes, e.g. XHP tags. if (!preg_match('/^\w+$/', $decl_string)) { return; } $rename = $decl_string.'.php'; $path = $this->getActivePath(); $filename = basename($path); if ($rename === $filename) { return; } $this->raiseLintAtNode( $decl_name, self::LINT_CLASS_FILENAME_MISMATCH, "The name of this file differs from the name of the class or interface ". "it declares. Rename the file to '{$rename}'."); } private function lintPlusOperatorOnStrings(XHPASTNode $root) { $binops = $root->selectDescendantsOfType('n_BINARY_EXPRESSION'); foreach ($binops as $binop) { $op = $binop->getChildByIndex(1); if ($op->getConcreteString() !== '+') { continue; } $left = $binop->getChildByIndex(0); $right = $binop->getChildByIndex(2); if (($left->getTypeName() === 'n_STRING_SCALAR') || ($right->getTypeName() === 'n_STRING_SCALAR')) { $this->raiseLintAtNode( $binop, self::LINT_PLUS_OPERATOR_ON_STRINGS, "In PHP, '.' is the string concatenation operator, not '+'. This ". "expression uses '+' with a string literal as an operand."); } } } /** * Finds duplicate keys in array initializers, as in * array(1 => 'anything', 1 => 'foo'). Since the first entry is ignored, * this is almost certainly an error. */ private function lintDuplicateKeysInArray(XHPASTNode $root) { $array_literals = $root->selectDescendantsOfType('n_ARRAY_LITERAL'); foreach ($array_literals as $array_literal) { $nodes_by_key = array(); $keys_warn = array(); $list_node = $array_literal->getChildByIndex(0); foreach ($list_node->getChildren() as $array_entry) { $key_node = $array_entry->getChildByIndex(0); switch ($key_node->getTypeName()) { case 'n_STRING_SCALAR': case 'n_NUMERIC_SCALAR': // Scalars: array(1 => 'v1', '1' => 'v2'); $key = 'scalar:'.(string)$key_node->evalStatic(); break; case 'n_SYMBOL_NAME': case 'n_VARIABLE': case 'n_CLASS_STATIC_ACCESS': // Constants: array(CONST => 'v1', CONST => 'v2'); // Variables: array($a => 'v1', $a => 'v2'); // Class constants and vars: array(C::A => 'v1', C::A => 'v2'); $key = $key_node->getTypeName().':'.$key_node->getConcreteString(); break; default: $key = null; break; } if ($key !== null) { if (isset($nodes_by_key[$key])) { $keys_warn[$key] = true; } $nodes_by_key[$key][] = $key_node; } } foreach ($keys_warn as $key => $_) { $node = array_pop($nodes_by_key[$key]); $message = $this->raiseLintAtNode( $node, self::LINT_DUPLICATE_KEYS_IN_ARRAY, 'Duplicate key in array initializer. PHP will ignore all '. 'but the last entry.'); $locations = array(); foreach ($nodes_by_key[$key] as $node) { $locations[] = $this->getOtherLocation($node->getOffset()); } $message->setOtherLocations($locations); } } } private function lintClosingCallParen(XHPASTNode $root) { $calls = $root->selectDescendantsOfType('n_FUNCTION_CALL'); $calls = $calls->add($root->selectDescendantsOfType('n_METHOD_CALL')); foreach ($calls as $call) { // If the last parameter of a call is a HEREDOC, don't apply this rule. $params = $call ->getChildOfType(1, 'n_CALL_PARAMETER_LIST') ->getChildren(); if ($params) { $last_param = last($params); if ($last_param->getTypeName() === 'n_HEREDOC') { continue; } } $tokens = $call->getTokens(); $last = array_pop($tokens); $trailing = $last->getNonsemanticTokensBefore(); $trailing_text = implode('', mpull($trailing, 'getValue')); if (preg_match('/^\s+$/', $trailing_text)) { $this->raiseLintAtOffset( $last->getOffset() - strlen($trailing_text), self::LINT_CLOSING_CALL_PAREN, 'Convention: no spaces before closing parenthesis in calls.', $trailing_text, ''); } } } private function lintClosingDeclarationParen(XHPASTNode $root) { $decs = $root->selectDescendantsOfType('n_FUNCTION_DECLARATION'); $decs = $decs->add($root->selectDescendantsOfType('n_METHOD_DECLARATION')); foreach ($decs as $dec) { $params = $dec->getChildOfType(3, 'n_DECLARATION_PARAMETER_LIST'); $tokens = $params->getTokens(); $last = array_pop($tokens); $trailing = $last->getNonsemanticTokensBefore(); $trailing_text = implode('', mpull($trailing, 'getValue')); if (preg_match('/^\s+$/', $trailing_text)) { $this->raiseLintAtOffset( $last->getOffset() - strlen($trailing_text), self::LINT_CLOSING_DECL_PAREN, 'Convention: no spaces before closing parenthesis in function and '. 'method declarations.', $trailing_text, ''); } } } private function lintKeywordCasing(XHPASTNode $root) { $keywords = array(); $symbols = $root->selectDescendantsOfType('n_SYMBOL_NAME'); foreach ($symbols as $symbol) { $keywords[] = head($symbol->getTokens()); } $arrays = $root->selectDescendantsOfType('n_ARRAY_LITERAL'); foreach ($arrays as $array) { $keywords[] = head($array->getTokens()); } $typehints = $root->selectDescendantsOfType('n_TYPE_NAME'); foreach ($typehints as $typehint) { $keywords[] = head($typehint->getTokens()); } $new_invocations = $root->selectDescendantsOfType('n_NEW'); foreach ($new_invocations as $invocation) { $keywords[] = head($invocation->getTokens()); } // NOTE: Although PHP generally allows arbitrary casing for all language // keywords, it's exceedingly rare for anyone to type, e.g., "CLASS" or // "cLaSs" in the wild. This list just attempts to cover unconventional // spellings which see some level of use, not all keywords exhaustively. // There is no token or node type which spans all keywords, so this is // significantly simpler. static $keyword_map = array( 'true' => 'true', 'false' => 'false', 'null' => 'null', 'array' => 'array', 'new' => 'new', ); foreach ($keywords as $keyword) { $value = $keyword->getValue(); $value_key = strtolower($value); if (!isset($keyword_map[$value_key])) { continue; } $expected_spelling = $keyword_map[$value_key]; if ($value !== $expected_spelling) { $this->raiseLintAtToken( $keyword, self::LINT_KEYWORD_CASING, "Convention: spell keyword '{$value}' as '{$expected_spelling}'.", $expected_spelling); } } } private function lintStrings(XHPASTNode $root) { $nodes = $root->selectDescendantsOfTypes(array( 'n_CONCATENATION_LIST', 'n_STRING_SCALAR', )); foreach ($nodes as $node) { $strings = array(); if ($node->getTypeName() === 'n_CONCATENATION_LIST') { $strings = $node->selectDescendantsOfType('n_STRING_SCALAR'); } else if ($node->getTypeName() === 'n_STRING_SCALAR') { $strings = array($node); if ($node->getParentNode()->getTypeName() === 'n_CONCATENATION_LIST') { continue; } } $valid = false; $invalid_nodes = array(); $fixes = array(); foreach ($strings as $string) { $concrete_string = $string->getConcreteString(); $single_quoted = ($concrete_string[0] === "'"); $contents = substr($concrete_string, 1, -1); // Double quoted strings are allowed when the string contains the // following characters. static $allowed_chars = array( '\n', '\r', '\t', '\v', '\e', '\f', '\'', '\0', '\1', '\2', '\3', '\4', '\5', '\6', '\7', '\x', ); $contains_special_chars = false; foreach ($allowed_chars as $allowed_char) { if (strpos($contents, $allowed_char) !== false) { $contains_special_chars = true; } } if (!$string->isConstantString()) { $valid = true; } else if ($contains_special_chars && !$single_quoted) { $valid = true; } else if (!$contains_special_chars && !$single_quoted) { $invalid_nodes[] = $string; $fixes[$string->getID()] = "'".str_replace('\"', '"', $contents)."'"; } } if (!$valid) { foreach ($invalid_nodes as $invalid_node) { $this->raiseLintAtNode( $invalid_node, self::LINT_DOUBLE_QUOTE, pht( 'String does not require double quotes. For consistency, '. 'prefer single quotes.'), $fixes[$invalid_node->getID()]); } } } } protected function lintElseIfStatements(XHPASTNode $root) { $tokens = $root->selectTokensOfType('T_ELSEIF'); foreach ($tokens as $token) { $this->raiseLintAtToken( $token, self::LINT_ELSEIF_USAGE, pht('Usage of `else if` is preferred over `elseif`.'), 'else if'); } } protected function lintSemicolons(XHPASTNode $root) { $tokens = $root->selectTokensOfType(';'); foreach ($tokens as $token) { $prev = $token->getPrevToken(); if ($prev->isAnyWhitespace()) { $this->raiseLintAtToken( $prev, self::LINT_SEMICOLON_SPACING, pht('Space found before semicolon.'), ''); } } } protected function lintLanguageConstructParentheses(XHPASTNode $root) { $nodes = $root->selectDescendantsOfTypes(array( 'n_INCLUDE_FILE', 'n_ECHO_LIST', )); foreach ($nodes as $node) { $child = head($node->getChildren()); if ($child->getTypeName() === 'n_PARENTHETICAL_EXPRESSION') { list($before, $after) = $child->getSurroundingNonsemanticTokens(); $replace = preg_replace( '/^\((.*)\)$/', '$1', $child->getConcreteString()); if (!$before) { $replace = ' '.$replace; } $this->raiseLintAtNode( $child, self::LINT_LANGUAGE_CONSTRUCT_PAREN, pht('Language constructs do not require parentheses.'), $replace); } } } protected function lintEmptyBlockStatements(XHPASTNode $root) { $nodes = $root->selectDescendantsOfType('n_STATEMENT_LIST'); foreach ($nodes as $node) { $tokens = $node->getTokens(); $token = head($tokens); if (count($tokens) <= 2) { continue; } // Safety check... if the first token isn't an opening brace then // there's nothing to do here. if ($token->getTypeName() != '{') { continue; } $only_whitespace = true; for ($token = $token->getNextToken(); $token && $token->getTypeName() != '}'; $token = $token->getNextToken()) { $only_whitespace = $only_whitespace && $token->isAnyWhitespace(); } if (count($tokens) > 2 && $only_whitespace) { $this->raiseLintAtNode( $node, self::LINT_EMPTY_STATEMENT, pht( "Braces for an empty block statement shouldn't ". "contain only whitespace."), '{}'); } } } protected function lintArraySeparator(XHPASTNode $root) { $arrays = $root->selectDescendantsOfType('n_ARRAY_LITERAL'); foreach ($arrays as $array) { $value_list = $array->getChildOfType(0, 'n_ARRAY_VALUE_LIST'); $values = $value_list->getChildrenOfType('n_ARRAY_VALUE'); if (!$values) { // There is no need to check an empty array. continue; } $multiline = $array->getLineNumber() != $array->getEndLineNumber(); $value = last($values); $after = last($value->getTokens())->getNextToken(); if ($multiline && (!$after || $after->getValue() != ',')) { if ($value->getChildByIndex(1)->getTypeName() == 'n_HEREDOC') { continue; } $this->raiseLintAtNode( $value, self::LINT_ARRAY_SEPARATOR, pht('Multi-lined arrays should have trailing commas.'), $value->getConcreteString().','); } else if (!$multiline && $after && $after->getValue() == ',') { $this->raiseLintAtToken( $after, self::LINT_ARRAY_SEPARATOR, pht('Single lined arrays should not have a trailing comma.'), ''); } } } public function getSuperGlobalNames() { return array( '$GLOBALS', '$_SERVER', '$_GET', '$_POST', '$_FILES', '$_COOKIE', '$_SESSION', '$_REQUEST', '$_ENV', ); } } diff --git a/src/parser/ArcanistDiffParser.php b/src/parser/ArcanistDiffParser.php index 658601c3..bbe01d0f 100644 --- a/src/parser/ArcanistDiffParser.php +++ b/src/parser/ArcanistDiffParser.php @@ -1,1420 +1,1420 @@ 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}."); } $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}."); } } return $this->changes; } public function parseDiff($diff) { if (!strlen(trim($diff))) { throw new Exception("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)."); } 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."); } $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.'); 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:'."); } $line = $this->nextLine(); if (!preg_match('/^Date: /', $line)) { $this->didFailParse("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 '______________________'."); } $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'."); } $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.'); } $target = 'new'; $line = substr($trimline, $prop_index); } else if ($trimline && $trimline[0] == '-') { if ($op == 'Added') { $this->didFailParse('Unexpected "-" 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!'); } $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.*)' + '(?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."); } 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."); } 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).'); } } 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)'."); } $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'); } $line = $this->nextLine(); return $this->parsePropertyHunk($change); } $this->didFailParse("Expected hunk header '@@ -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'."); } 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.'); } $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."); } } } } 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."); } $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/repository/api/ArcanistMercurialAPI.php b/src/repository/api/ArcanistMercurialAPI.php index 27e579e8..0bd6e60d 100644 --- a/src/repository/api/ArcanistMercurialAPI.php +++ b/src/repository/api/ArcanistMercurialAPI.php @@ -1,1090 +1,1091 @@ 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."); } 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."); } return $stdout; } public function getSourceControlPath() { return '/'; } public function getBranchName() { if (!$this->branch) { list($stdout) = $this->execxLocal('branch'); $this->branch = trim($stdout); } return $this->branch; } public 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."); } } $this->setBaseCommitExplanation( '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."); } 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.'); 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).'); } else { $this->setBaseCommitExplanation( '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; } foreach (Futures($futures)->limit(4) 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}"); } $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!'); } } public function getFinalizedRevisionMessage() { return "You may now push this commit upstream, as appropriate (e.g. with ". "'hg push' or by printing and faxing it)."; } 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'."; } 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'] = 'A mercurial commit hash in the commit range is already attached '. '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)'; } 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->reloadWorkingCopy(); if (!$this->getUncommittedStatus()) { throw new ArcanistUsageException( "{$commit_hash} has already been reverted."); } } public function getBackoutMessage($commit_hash) { return 'Backed out changeset '.$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."); 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."); return trim($commit); } } break; case 'arc': switch ($name) { case 'empty': $this->setBaseCommitExplanation( "you specified '{$rule}' in your {$source} 'base' ". "configuration."); 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."); 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."); // 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"); return trim($bookmark_base); } break; case 'this': $this->setBaseCommitExplanation( "you specified '{$rule}' in your {$source} 'base' ". "configuration."); 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."); 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); + '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/unit/engine/CSharpToolsTestEngine.php b/src/unit/engine/CSharpToolsTestEngine.php index aec00c73..03ad75bb 100644 --- a/src/unit/engine/CSharpToolsTestEngine.php +++ b/src/unit/engine/CSharpToolsTestEngine.php @@ -1,280 +1,282 @@ 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"); } $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?)'); } } /** * 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); + $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')); + '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/workflow/ArcanistBackoutWorkflow.php b/src/workflow/ArcanistBackoutWorkflow.php index 6b12bc04..7d37853a 100644 --- a/src/workflow/ArcanistBackoutWorkflow.php +++ b/src/workflow/ArcanistBackoutWorkflow.php @@ -1,191 +1,191 @@ | Entering a differential revision will only work if there is only one commit associated with the revision. This requires your working copy is up to date and that the commit exists in the working copy. EOTEXT ); } public function getArguments() { return array( '*' => 'input', ); } public function requiresWorkingCopy() { return true; } public function requiresRepositoryAPI() { return true; } public function requiresAuthentication() { return true; } /** * Given a differential revision ID, fetches the commit ID. */ private function getCommitIDFromRevisionID($revision_id) { $conduit = $this->getConduit(); $revisions = $conduit->callMethodSynchronous( 'differential.query', array( 'ids' => array($revision_id), )); if (!$revisions) { throw new ArcanistUsageException( 'The revision you provided does not exist!'); } $revision = $revisions[0]; $commits = $revision['commits']; if (!$commits) { throw new ArcanistUsageException( 'This revision has not been committed yet!'); } else if (count($commits) > 1) { throw new ArcanistUsageException( 'The revision you provided has multiple commits!'); } $commit_phid = $commits[0]; $commit = $conduit->callMethodSynchronous( 'phid.query', array( 'phids' => array($commit_phid), )); $commit_id = $commit[$commit_phid]['name']; return $commit_id; } /** * Fetches an array of commit info provided a Commit_id in the form of * rE123456 (not local commit hash). */ private function getDiffusionCommit($commit_id) { $result = $this->getConduit()->callMethodSynchronous( 'diffusion.getcommits', array( 'commits' => array($commit_id), )); $commit = $result[$commit_id]; // This commit was not found in Diffusion if (array_key_exists('error', $commit)) { return null; } return $commit; } /** * Retrieves default template from differential and pre-fills info. */ private function buildCommitMessage($commit_hash) { $conduit = $this->getConduit(); $repository_api = $this->getRepositoryAPI(); $summary = $repository_api->getBackoutMessage($commit_hash); $fields = array( 'summary' => $summary, 'testPlan' => 'revert-hammer', ); $template = $conduit->callMethodSynchronous( 'differential.getcommitmessage', array( 'revision_id' => null, 'edit' => 'create', - 'fields' => $fields + 'fields' => $fields, )); $template = $this->newInteractiveEditor($template) ->setName('new-commit') ->editInteractively(); return $template; } /** * Performs the backout/revert of a revision and creates a commit. */ public function run() { $console = PhutilConsole::getConsole(); $conduit = $this->getConduit(); $repository_api = $this->getRepositoryAPI(); $is_git_svn = $repository_api instanceof ArcanistGitAPI && $repository_api->isGitSubversionRepo(); $is_hg_svn = $repository_api instanceof ArcanistMercurialAPI && $repository_api->isHgSubversionRepo(); $revision_id = null; if (!($repository_api instanceof ArcanistGitAPI) && !($repository_api instanceof ArcanistMercurialAPI)) { throw new ArcanistUsageException( 'Backout currently only supports Git and Mercurial' ); } $console->writeOut("Starting backout\n"); $input = $this->getArgument('input'); if (!$input || count($input) != 1) { throw new ArcanistUsageException( 'You must specify one commit to backout!'); } // Input looks like a Differential revision, so // we try to find the commit attached to it $matches = array(); if (preg_match('/^D(\d+)$/i', $input[0], $matches)) { $revision_id = $matches[1]; $commit_id = $this->getCommitIDFromRevisionID($revision_id); $commit = $this->getDiffusionCommit($commit_id); $commit_hash = $commit['commitIdentifier']; // Convert commit hash from SVN to Git/HG (for FB case) if ($is_git_svn || $is_hg_svn) { $commit_hash = $repository_api-> getHashFromFromSVNRevisionNumber($commit_hash); } } else { // Assume input is a commit hash $commit_hash = $input[0]; } if (!$repository_api->hasLocalCommit($commit_hash)) { throw new ArcanistUsageException( 'Invalid commit provided or does not exist in the working copy!'); } // Run 'backout'. $subject = $repository_api->getCommitSummary($commit_hash); $console->writeOut("Backing out commit {$commit_hash} {$subject} \n"); $repository_api->backoutCommit($commit_hash); // Create commit message and execute the commit $message = $this->buildCommitMessage($commit_hash); $repository_api->doCommit($message); $console->writeOut("Double-check the commit and push when ready\n"); } } diff --git a/src/workflow/ArcanistCloseWorkflow.php b/src/workflow/ArcanistCloseWorkflow.php index 76cde414..083ec355 100644 --- a/src/workflow/ArcanistCloseWorkflow.php +++ b/src/workflow/ArcanistCloseWorkflow.php @@ -1,146 +1,146 @@ statusData = $this->getConduit()->callMethodSynchronous( 'maniphest.querystatuses', array()); return $this; } private function getStatusOptions() { if ($this->statusData === null) { throw new Exception('loadStatusData first!'); } return idx($this->statusData, 'statusMap'); } private function getDefaultClosedStatus() { if ($this->statusData === null) { throw new Exception('loadStatusData first!'); } return idx($this->statusData, 'defaultClosedStatus'); } public function getWorkflowName() { return 'close'; } public function getCommandSynopses() { return phutil_console_format(<< 'task_id', 'message' => array( 'short' => 'm', 'param' => 'comment', 'help' => pht('Provide a comment with your status change.'), ), 'status' => array( 'param' => 'status', 'short' => 's', 'help' => pht( 'Specify a new status. Valid status options can be '. 'seen with the `list-statuses` argument.'), ), 'list-statuses' => array( 'help' => 'Show available status options and exit.', ), ); } public function run() { $this->loadStatusData(); $list_statuses = $this->getArgument('list-statuses'); if ($list_statuses) { echo phutil_console_format(pht( "Valid status options are:\n". "\t%s\n", implode($this->getStatusOptions(), ', '))); return 0; } $ids = $this->getArgument('task_id'); $message = $this->getArgument('message'); $status = strtolower($this->getArgument('status')); $status_options = $this->getStatusOptions(); if (!isset($status) || $status == '') { $status = $this->getDefaultClosedStatus(); } if (!isset($status_options[$status])) { $options = array_keys($status_options); $last = array_pop($options); echo "Invalid status {$status}, valid options are ". implode(', ', $options).", or {$last}.\n"; return; } foreach ($ids as $id) { if (!preg_match('/^T?\d+$/', $id)) { echo "Invalid Task ID: {$id}.\n"; return 1; } $id = ltrim($id, 'T'); $result = $this->closeTask($id, $status, $message); $current_status = $status_options[$status]; if ($result) { echo "T{$id}'s status is now set to {$current_status}.\n"; } else { echo "T{$id} is already set to {$current_status}.\n"; } } return 0; } private function closeTask($task_id, $status, $comment = '') { $conduit = $this->getConduit(); $info = $conduit->callMethodSynchronous( 'maniphest.info', array( - 'task_id' => $task_id + 'task_id' => $task_id, )); if ($info['status'] == $status) { return false; } return $conduit->callMethodSynchronous( 'maniphest.update', array( 'id' => $task_id, 'status' => $status, - 'comments' => $comment + 'comments' => $comment, )); } } diff --git a/src/workflow/ArcanistCommitWorkflow.php b/src/workflow/ArcanistCommitWorkflow.php index 840cc261..dbd2b791 100644 --- a/src/workflow/ArcanistCommitWorkflow.php +++ b/src/workflow/ArcanistCommitWorkflow.php @@ -1,339 +1,339 @@ revisionID; } public function getArguments() { return array( 'show' => array( 'help' => 'Show the command which would be issued, but do not actually '. - 'commit anything.' + 'commit anything.', ), 'revision' => array( 'param' => 'revision_id', 'help' => 'Commit a specific revision. If you do not specify a revision, '. 'arc will look for committable revisions.', - ) + ), ); } public function run() { $repository_api = $this->getRepositoryAPI(); if (!($repository_api instanceof ArcanistSubversionAPI)) { throw new ArcanistUsageException( "'arc commit' is only supported under svn."); } $revision_id = $this->normalizeRevisionID($this->getArgument('revision')); if (!$revision_id) { $revisions = $repository_api->loadWorkingCopyDifferentialRevisions( $this->getConduit(), array( 'authors' => array($this->getUserPHID()), 'status' => 'status-accepted', )); if (count($revisions) == 0) { throw new ArcanistUsageException( "Unable to identify the revision in the working copy. Use ". "'--revision ' to select a revision."); } else if (count($revisions) > 1) { throw new ArcanistUsageException( "More than one revision exists in the working copy:\n\n". $this->renderRevisionList($revisions)."\n". "Use '--revision ' to select a revision."); } } else { $revisions = $this->getConduit()->callMethodSynchronous( 'differential.query', array( 'ids' => array($revision_id), )); if (count($revisions) == 0) { throw new ArcanistUsageException( "Revision 'D{$revision_id}' does not exist."); } } $revision = head($revisions); $this->revisionID = $revision['id']; $revision_id = $revision['id']; $is_show = $this->getArgument('show'); if (!$is_show) { $this->runSanityChecks($revision); } $message = $this->getConduit()->callMethodSynchronous( 'differential.getcommitmessage', array( 'revision_id' => $revision_id, 'edit' => false, )); $event = $this->dispatchEvent( ArcanistEventType::TYPE_COMMIT_WILLCOMMITSVN, array( 'message' => $message, )); $message = $event->getValue('message'); if ($is_show) { echo $message."\n"; return 0; } $revision_title = $revision['title']; echo "Committing 'D{$revision_id}: {$revision_title}'...\n"; $files = $this->getCommitFileList($revision); $tmp_file = new TempFile(); Filesystem::writeFile($tmp_file, $message); $command = csprintf( 'svn commit %Ls --encoding utf-8 -F %s', $files, $tmp_file); // make sure to specify LANG on non-windows systems to suppress any fancy // warnings; see @{method:getSVNLangEnvVar}. if (!phutil_is_windows()) { $command = csprintf('LANG=%C %C', $this->getSVNLangEnvVar(), $command); } chdir($repository_api->getPath()); $err = phutil_passthru('%C', $command); if ($err) { throw new Exception("Executing 'svn commit' failed!"); } $this->askForRepositoryUpdate(); $mark_workflow = $this->buildChildWorkflow( 'close-revision', array( '--finalize', $revision_id, )); $mark_workflow->run(); return $err; } protected function getCommitFileList(array $revision) { $repository_api = $this->getRepositoryAPI(); $revision_id = $revision['id']; $commit_paths = $this->getConduit()->callMethodSynchronous( 'differential.getcommitpaths', array( 'revision_id' => $revision_id, )); $dir_paths = array(); foreach ($commit_paths as $path) { $path = dirname($path); while ($path != '.') { $dir_paths[$path] = true; $path = dirname($path); } } $commit_paths = array_fill_keys($commit_paths, true); $status = $repository_api->getSVNStatus(); $modified_but_not_included = array(); foreach ($status as $path => $mask) { if (!empty($dir_paths[$path])) { $commit_paths[$path] = true; } if (!empty($commit_paths[$path])) { continue; } foreach ($commit_paths as $will_commit => $ignored) { if (Filesystem::isDescendant($path, $will_commit)) { throw new ArcanistUsageException( "This commit includes the directory '{$will_commit}', but ". "it contains a modified path ('{$path}') which is NOT included ". "in the commit. Subversion can not handle this operation and ". "will commit the path anyway. You need to sort out the working ". "copy changes to '{$path}' before you may proceed with the ". "commit."); } } $modified_but_not_included[] = $path; } if ($modified_but_not_included) { $prefix = pht( 'Locally modified path(s) are not included in this revision:', count($modified_but_not_included)); $prompt = pht( 'They will NOT be committed. Commit this revision anyway?', count($modified_but_not_included)); $this->promptFileWarning($prefix, $prompt, $modified_but_not_included); } $do_not_exist = array(); foreach ($commit_paths as $path => $ignored) { $disk_path = $repository_api->getPath($path); if (file_exists($disk_path)) { continue; } if (is_link($disk_path)) { continue; } if (idx($status, $path) & ArcanistRepositoryAPI::FLAG_DELETED) { continue; } $do_not_exist[] = $path; unset($commit_paths[$path]); } if ($do_not_exist) { $prefix = pht( 'Revision includes changes to path(s) that do not exist:', count($do_not_exist)); $prompt = 'Commit this revision anyway?'; $this->promptFileWarning($prefix, $prompt, $do_not_exist); } $files = array_keys($commit_paths); $files = ArcanistSubversionAPI::escapeFileNamesForSVN($files); if (empty($files)) { throw new ArcanistUsageException( 'There is nothing left to commit. None of the modified paths exist.'); } return $files; } protected function promptFileWarning($prefix, $prompt, array $paths) { echo $prefix."\n\n"; foreach ($paths as $path) { echo " ".$path."\n"; } if (!phutil_console_confirm($prompt)) { throw new ArcanistUserAbortException(); } } protected function getSupportedRevisionControlSystems() { return array('svn'); } /** * On some systems, we need to specify "en_US.UTF-8" instead of "en_US.utf8", * and SVN spews some bewildering warnings if we don't: * * svn: warning: cannot set LC_CTYPE locale * svn: warning: environment variable LANG is en_US.utf8 * svn: warning: please check that your locale name is correct * * For example, it happens on epriestley's Mac (10.6.7) with * Subversion 1.6.15. */ private function getSVNLangEnvVar() { $locale = 'en_US.utf8'; try { list($locales) = execx('locale -a'); $locales = explode("\n", trim($locales)); $locales = array_fill_keys($locales, true); if (isset($locales['en_US.UTF-8'])) { $locale = 'en_US.UTF-8'; } } catch (Exception $ex) { // Ignore. } return $locale; } private function runSanityChecks(array $revision) { $repository_api = $this->getRepositoryAPI(); $revision_id = $revision['id']; $revision_title = $revision['title']; $confirm = array(); if ($revision['status'] != ArcanistDifferentialRevisionStatus::ACCEPTED) { $confirm[] = "Revision 'D{$revision_id}: {$revision_title}' has not been accepted. ". "Commit this revision anyway?"; } if ($revision['authorPHID'] != $this->getUserPHID()) { $confirm[] = "You are not the author of 'D{$revision_id}: {$revision_title}'. ". "Commit this revision anyway?"; } $revision_source = idx($revision, 'branch'); $current_source = $repository_api->getBranchName(); if ($revision_source != $current_source) { $confirm[] = "Revision 'D{$revision_id}: {$revision_title}' was generated from ". "'{$revision_source}', but current working copy root is ". "'{$current_source}'. Commit this revision anyway?"; } foreach ($confirm as $thing) { if (!phutil_console_confirm($thing)) { throw new ArcanistUserAbortException(); } } } } diff --git a/src/workflow/ArcanistDiffWorkflow.php b/src/workflow/ArcanistDiffWorkflow.php index a8e13ab2..b2dd8ffc 100644 --- a/src/workflow/ArcanistDiffWorkflow.php +++ b/src/workflow/ArcanistDiffWorkflow.php @@ -1,2589 +1,2589 @@ null, 'unit' => null); private $testResults; private $diffID; private $revisionID; private $postponedLinters; private $haveUncommittedChanges = false; private $diffPropertyFutures = array(); private $commitMessageFromRevision; public function getWorkflowName() { return 'diff'; } public function getCommandSynopses() { return phutil_console_format(<<isRawDiffSource(); } public function requiresConduit() { return true; } public function requiresAuthentication() { return true; } public function requiresRepositoryAPI() { if (!$this->isRawDiffSource()) { return true; } if ($this->getArgument('use-commit-message')) { return true; } return false; } public function getDiffID() { return $this->diffID; } public function getArguments() { $arguments = array( 'message' => array( 'short' => 'm', 'param' => 'message', 'help' => 'When updating a revision, use the specified message instead of '. 'prompting.', ), 'message-file' => array( 'short' => 'F', 'param' => 'file', 'paramtype' => 'file', 'help' => 'When creating a revision, read revision information '. 'from this file.', ), 'use-commit-message' => array( 'supports' => array( 'git', // TODO: Support mercurial. ), 'short' => 'C', 'param' => 'commit', 'help' => 'Read revision information from a specific commit.', 'conflicts' => array( 'only' => null, 'preview' => null, 'update' => null, ), ), 'edit' => array( 'supports' => array( 'git', 'hg', ), 'nosupport' => array( 'svn' => 'Edit revisions via the web interface when using SVN.', ), 'help' => 'When updating a revision under git, edit revision information '. 'before updating.', ), 'raw' => array( 'help' => 'Read diff from stdin, not from the working copy. This disables '. 'many Arcanist/Phabricator features which depend on having access '. 'to the working copy.', 'conflicts' => array( 'less-context' => null, 'apply-patches' => '--raw disables lint.', 'never-apply-patches' => '--raw disables lint.', 'advice' => '--raw disables lint.', 'lintall' => '--raw disables lint.', 'create' => '--raw and --create both need stdin. '. 'Use --raw-command.', 'edit' => '--raw and --edit both need stdin. '. 'Use --raw-command.', 'raw-command' => null, ), ), 'raw-command' => array( 'param' => 'command', 'help' => 'Generate diff by executing a specified command, not from the '. 'working copy. This disables many Arcanist/Phabricator features '. 'which depend on having access to the working copy.', 'conflicts' => array( 'less-context' => null, 'apply-patches' => '--raw-command disables lint.', 'never-apply-patches' => '--raw-command disables lint.', 'advice' => '--raw-command disables lint.', 'lintall' => '--raw-command disables lint.', ), ), 'create' => array( 'help' => 'Always create a new revision.', 'conflicts' => array( 'edit' => '--create can not be used with --edit.', 'only' => '--create can not be used with --only.', 'preview' => '--create can not be used with --preview.', 'update' => '--create can not be used with --update.', ), ), 'update' => array( 'param' => 'revision_id', 'help' => 'Always update a specific revision.', ), 'nounit' => array( 'help' => 'Do not run unit tests.', ), 'nolint' => array( 'help' => 'Do not run lint.', 'conflicts' => array( 'lintall' => '--nolint suppresses lint.', 'advice' => '--nolint suppresses lint.', 'apply-patches' => '--nolint suppresses lint.', 'never-apply-patches' => '--nolint suppresses lint.', ), ), 'only' => array( 'help' => 'Only generate a diff, without running lint, unit tests, or other '. 'auxiliary steps. See also --preview.', 'conflicts' => array( 'preview' => null, 'message' => '--only does not affect revisions.', 'edit' => '--only does not affect revisions.', 'lintall' => '--only suppresses lint.', 'advice' => '--only suppresses lint.', 'apply-patches' => '--only suppresses lint.', 'never-apply-patches' => '--only suppresses lint.', ), ), 'preview' => array( 'help' => 'Instead of creating or updating a revision, only create a diff, '. 'which you may later attach to a revision. This still runs lint '. 'unit tests. See also --only.', 'conflicts' => array( 'only' => null, 'edit' => '--preview does affect revisions.', 'message' => '--preview does not update any revision.', ), ), 'plan-changes' => array( 'help' => 'Create or update a revision without requesting a code review.', 'conflicts' => array( 'only' => '--only does not affect revisions.', 'preview' => '--preview does not affect revisions.', ), ), 'encoding' => array( 'param' => 'encoding', 'help' => 'Attempt to convert non UTF-8 hunks into specified encoding.', ), 'allow-untracked' => array( 'help' => 'Skip checks for untracked files in the working copy.', ), 'excuse' => array( 'param' => 'excuse', 'help' => 'Provide a prepared in advance excuse for any lints/tests'. ' shall they fail.', ), 'less-context' => array( 'help' => "Normally, files are diffed with full context: the entire file is ". "sent to Differential so reviewers can 'show more' and see it. If ". "you are making changes to very large files with tens of thousands ". "of lines, this may not work well. With this flag, a diff will ". "be created that has only a few lines of context.", ), 'lintall' => array( 'help' => 'Raise all lint warnings, not just those on lines you changed.', 'passthru' => array( 'lint' => true, ), ), 'advice' => array( 'help' => 'Require excuse for lint advice in addition to lint warnings and '. 'errors.', ), 'only-new' => array( 'param' => 'bool', 'help' => 'Display only lint messages not present in the original code.', 'passthru' => array( 'lint' => true, ), ), 'apply-patches' => array( 'help' => 'Apply patches suggested by lint to the working copy without '. 'prompting.', 'conflicts' => array( 'never-apply-patches' => true, ), 'passthru' => array( 'lint' => true, ), ), 'never-apply-patches' => array( 'help' => 'Never apply patches suggested by lint.', 'conflicts' => array( 'apply-patches' => true, ), 'passthru' => array( 'lint' => true, ), ), 'amend-all' => array( 'help' => 'When linting git repositories, amend HEAD with all patches '. 'suggested by lint without prompting.', 'passthru' => array( 'lint' => true, ), ), 'amend-autofixes' => array( 'help' => 'When linting git repositories, amend HEAD with autofix '. 'patches suggested by lint without prompting.', 'passthru' => array( 'lint' => true, ), ), 'add-all' => array( 'short' => 'a', 'help' => 'Automatically add all untracked, unstaged and uncommitted files to '. 'the commit.', ), 'json' => array( 'help' => 'Emit machine-readable JSON. EXPERIMENTAL! Probably does not work!', ), 'no-amend' => array( 'help' => 'Never amend commits in the working copy with lint patches.', ), 'uncommitted' => array( 'help' => 'Suppress warning about uncommitted changes.', 'supports' => array( 'hg', ), ), 'verbatim' => array( 'help' => 'When creating a revision, try to use the working copy '. 'commit message verbatim, without prompting to edit it. '. 'When updating a revision, update some fields from the '. 'local commit message.', 'supports' => array( 'hg', 'git', ), 'conflicts' => array( 'use-commit-message' => true, 'update' => true, 'only' => true, 'preview' => true, 'raw' => true, 'raw-command' => true, 'message-file' => true, ), ), 'reviewers' => array( 'param' => 'usernames', 'help' => 'When creating a revision, add reviewers.', 'conflicts' => array( 'only' => true, 'preview' => true, 'update' => true, ), ), 'cc' => array( 'param' => 'usernames', 'help' => 'When creating a revision, add CCs.', 'conflicts' => array( 'only' => true, 'preview' => true, 'update' => true, ), ), 'skip-binaries' => array( 'help' => 'Do not upload binaries (like images).', ), 'ignore-unsound-tests' => array( 'help' => 'Ignore unsound test failures without prompting.', ), 'base' => array( 'param' => 'rules', 'help' => 'Additional rules for determining base revision.', 'nosupport' => array( 'svn' => 'Subversion does not use base commits.', ), 'supports' => array('git', 'hg'), ), 'no-diff' => array( 'help' => 'Only run lint and unit tests. Intended for internal use.', ), 'cache' => array( 'param' => 'bool', 'help' => '0 to disable lint cache, 1 to enable (default).', 'passthru' => array( 'lint' => true, ), ), 'coverage' => array( 'help' => 'Always enable coverage information.', 'conflicts' => array( 'no-coverage' => null, ), 'passthru' => array( 'unit' => true, ), ), 'no-coverage' => array( 'help' => 'Always disable coverage information.', 'passthru' => array( 'unit' => true, ), ), 'browse' => array( 'help' => pht( 'After creating a diff or revision, open it in a web browser.'), ), '*' => 'paths', 'head' => array( 'param' => 'commit', 'help' => pht( 'Specify the end of the commit range. This disables many '. 'Arcanist/Phabricator features which depend on having access to '. 'the working copy.'), 'supports' => array('git'), 'nosupport' => array( 'svn' => pht('Subversion does not support commit ranges.'), 'hg' => pht('Mercurial does not support --head yet.'), ), 'conflicts' => array( 'lintall' => '--head suppresses lint.', 'advice' => '--head suppresses lint.', ), - ) + ), ); return $arguments; } public function isRawDiffSource() { return $this->getArgument('raw') || $this->getArgument('raw-command'); } public function run() { $this->console = PhutilConsole::getConsole(); $this->runRepositoryAPISetup(); if ($this->getArgument('no-diff')) { $this->removeScratchFile('diff-result.json'); $data = $this->runLintUnit(); $this->writeScratchJSONFile('diff-result.json', $data); return 0; } $this->runDiffSetupBasics(); $commit_message = $this->buildCommitMessage(); $this->dispatchEvent( ArcanistEventType::TYPE_DIFF_DIDBUILDMESSAGE, array( 'message' => $commit_message, )); if (!$this->shouldOnlyCreateDiff()) { $revision = $this->buildRevisionFromCommitMessage($commit_message); } $server = $this->console->getServer(); $server->setHandler(array($this, 'handleServerMessage')); $data = $this->runLintUnit(); $lint_result = $data['lintResult']; $this->unresolvedLint = $data['unresolvedLint']; $this->postponedLinters = $data['postponedLinters']; $unit_result = $data['unitResult']; $this->testResults = $data['testResults']; if ($this->getArgument('nolint')) { $this->excuses['lint'] = $this->getSkipExcuse( 'Provide explanation for skipping lint or press Enter to abort:', 'lint-excuses'); } if ($this->getArgument('nounit')) { $this->excuses['unit'] = $this->getSkipExcuse( 'Provide explanation for skipping unit tests or press Enter to abort:', 'unit-excuses'); } $changes = $this->generateChanges(); if (!$changes) { throw new ArcanistUsageException( 'There are no changes to generate a diff from!'); } $diff_spec = array( 'changes' => mpull($changes, 'toDictionary'), 'lintStatus' => $this->getLintStatus($lint_result), 'unitStatus' => $this->getUnitStatus($unit_result), ) + $this->buildDiffSpecification(); $conduit = $this->getConduit(); $diff_info = $conduit->callMethodSynchronous( 'differential.creatediff', $diff_spec); $this->diffID = $diff_info['diffid']; $event = $this->dispatchEvent( ArcanistEventType::TYPE_DIFF_WASCREATED, array( 'diffID' => $diff_info['diffid'], 'lintResult' => $lint_result, 'unitResult' => $unit_result, )); $this->updateLintDiffProperty(); $this->updateUnitDiffProperty(); $this->updateLocalDiffProperty(); $this->resolveDiffPropertyUpdates(); $output_json = $this->getArgument('json'); if ($this->shouldOnlyCreateDiff()) { if (!$output_json) { echo phutil_console_format( "Created a new Differential diff:\n". " **Diff URI:** __%s__\n\n", $diff_info['uri']); } else { $human = ob_get_clean(); echo json_encode(array( 'diffURI' => $diff_info['uri'], 'diffID' => $this->getDiffID(), 'human' => $human, ))."\n"; ob_start(); } if ($this->shouldOpenCreatedObjectsInBrowser()) { $this->openURIsInBrowser(array($diff_info['uri'])); } } else { $revision['diffid'] = $this->getDiffID(); if ($commit_message->getRevisionID()) { $result = $conduit->callMethodSynchronous( 'differential.updaterevision', $revision); foreach (array('edit-messages.json', 'update-messages.json') as $file) { $messages = $this->readScratchJSONFile($file); unset($messages[$revision['id']]); $this->writeScratchJSONFile($file, $messages); } echo "Updated an existing Differential revision:\n"; } else { $revision = $this->dispatchWillCreateRevisionEvent($revision); $result = $conduit->callMethodSynchronous( 'differential.createrevision', $revision); $revised_message = $conduit->callMethodSynchronous( 'differential.getcommitmessage', array( 'revision_id' => $result['revisionid'], )); if ($this->shouldAmend()) { $repository_api = $this->getRepositoryAPI(); if ($repository_api->supportsAmend()) { echo "Updating commit message...\n"; $repository_api->amendCommit($revised_message); } else { echo 'Commit message was not amended. Amending commit message is '. 'only supported in git and hg (version 2.2 or newer)'; } } echo "Created a new Differential revision:\n"; } $uri = $result['uri']; echo phutil_console_format( " **Revision URI:** __%s__\n\n", $uri); if ($this->getArgument('plan-changes')) { $conduit->callMethodSynchronous( 'differential.createcomment', array( 'revision_id' => $result['revisionid'], 'action' => 'rethink', )); echo "Planned changes to the revision.\n"; } if ($this->shouldOpenCreatedObjectsInBrowser()) { $this->openURIsInBrowser(array($uri)); } } echo "Included changes:\n"; foreach ($changes as $change) { echo ' '.$change->renderTextSummary()."\n"; } if ($output_json) { ob_get_clean(); } $this->removeScratchFile('create-message'); return 0; } private function runRepositoryAPISetup() { if (!$this->requiresRepositoryAPI()) { return; } $repository_api = $this->getRepositoryAPI(); if ($this->getArgument('less-context')) { $repository_api->setDiffLinesOfContext(3); } $repository_api->setBaseCommitArgumentRules( $this->getArgument('base', '')); if ($repository_api->supportsCommitRanges()) { $this->parseBaseCommitArgument($this->getArgument('paths')); } $head_commit = $this->getArgument('head'); if ($head_commit !== null) { $repository_api->setHeadCommit($head_commit); } } private function runDiffSetupBasics() { $output_json = $this->getArgument('json'); if ($output_json) { // TODO: We should move this to a higher-level and put an indirection // layer between echoing stuff and stdout. ob_start(); } if ($this->requiresWorkingCopy()) { $repository_api = $this->getRepositoryAPI(); try { if ($this->getArgument('add-all')) { $this->setCommitMode(self::COMMIT_ENABLE); } else if ($this->getArgument('uncommitted')) { $this->setCommitMode(self::COMMIT_DISABLE); } else { $this->setCommitMode(self::COMMIT_ALLOW); } if ($repository_api instanceof ArcanistSubversionAPI) { $repository_api->limitStatusToPaths($this->getArgument('paths')); } if (!$this->getArgument('head')) { $this->requireCleanWorkingCopy(); } } catch (ArcanistUncommittedChangesException $ex) { if ($repository_api instanceof ArcanistMercurialAPI) { $use_dirty_changes = false; if ($this->getArgument('uncommitted')) { // OK. } else { $ok = phutil_console_confirm( "You have uncommitted changes in your working copy. You can ". "include them in the diff, or abort and deal with them. (Use ". "'--uncommitted' to include them and skip this prompt.) ". "Do you want to include uncommitted changes in the diff?"); if (!$ok) { throw $ex; } } $this->haveUncommittedChanges = true; } else { throw $ex; } } } $this->dispatchEvent( ArcanistEventType::TYPE_DIFF_DIDCOLLECTCHANGES, array()); } private function buildRevisionFromCommitMessage( ArcanistDifferentialCommitMessage $message) { $conduit = $this->getConduit(); $revision_id = $message->getRevisionID(); $revision = array( 'fields' => $message->getFields(), ); if ($revision_id) { // With '--verbatim', pass the (possibly modified) local fields. This // allows the user to edit some fields (like "title" and "summary") // locally without '--edit' and have changes automatically synchronized. // Without '--verbatim', we do not update the revision to reflect local // commit message changes. if ($this->getArgument('verbatim')) { $use_fields = $message->getFields(); } else { $use_fields = array(); } $should_edit = $this->getArgument('edit'); $edit_messages = $this->readScratchJSONFile('edit-messages.json'); $remote_corpus = idx($edit_messages, $revision_id); if (!$should_edit || !$remote_corpus || $use_fields) { if ($this->commitMessageFromRevision) { $remote_corpus = $this->commitMessageFromRevision; } else { $remote_corpus = $conduit->callMethodSynchronous( 'differential.getcommitmessage', array( 'revision_id' => $revision_id, 'edit' => 'edit', 'fields' => $use_fields, )); } } if ($should_edit) { $edited = $this->newInteractiveEditor($remote_corpus) ->setName('differential-edit-revision-info') ->editInteractively(); if ($edited != $remote_corpus) { $remote_corpus = $edited; $edit_messages[$revision_id] = $remote_corpus; $this->writeScratchJSONFile('edit-messages.json', $edit_messages); } } if ($this->commitMessageFromRevision == $remote_corpus) { $new_message = $message; } else { $remote_corpus = ArcanistCommentRemover::removeComments( $remote_corpus); $new_message = ArcanistDifferentialCommitMessage::newFromRawCorpus( $remote_corpus); $new_message->pullDataFromConduit($conduit); } $revision['fields'] = $new_message->getFields(); $revision['id'] = $revision_id; $this->revisionID = $revision_id; $revision['message'] = $this->getArgument('message'); if (!strlen($revision['message'])) { $update_messages = $this->readScratchJSONFile('update-messages.json'); $update_messages[$revision_id] = $this->getUpdateMessage( $revision['fields'], idx($update_messages, $revision_id)); $revision['message'] = ArcanistCommentRemover::removeComments( $update_messages[$revision_id]); if (!strlen(trim($revision['message']))) { throw new ArcanistUserAbortException(); } $this->writeScratchJSONFile('update-messages.json', $update_messages); } } return $revision; } protected function shouldOnlyCreateDiff() { if ($this->getArgument('create')) { return false; } if ($this->getArgument('update')) { return false; } if ($this->getArgument('use-commit-message')) { return false; } if ($this->isRawDiffSource()) { return true; } return $this->getArgument('preview') || $this->getArgument('only'); } private function generateAffectedPaths() { if ($this->isRawDiffSource()) { return array(); } $repository_api = $this->getRepositoryAPI(); if ($repository_api instanceof ArcanistSubversionAPI) { $file_list = new FileList($this->getArgument('paths', array())); $paths = $repository_api->getSVNStatus($externals = true); foreach ($paths as $path => $mask) { if (!$file_list->contains($repository_api->getPath($path), true)) { unset($paths[$path]); } } $warn_externals = array(); foreach ($paths as $path => $mask) { $any_mod = ($mask & ArcanistRepositoryAPI::FLAG_ADDED) || ($mask & ArcanistRepositoryAPI::FLAG_MODIFIED) || ($mask & ArcanistRepositoryAPI::FLAG_DELETED); if ($mask & ArcanistRepositoryAPI::FLAG_EXTERNALS) { unset($paths[$path]); if ($any_mod) { $warn_externals[] = $path; } } } if ($warn_externals && !$this->hasWarnedExternals) { echo phutil_console_format( "The working copy includes changes to 'svn:externals' paths. These ". "changes will not be included in the diff because SVN can not ". "commit 'svn:externals' changes alongside normal changes.". "\n\n". "Modified 'svn:externals' files:". "\n\n". phutil_console_wrap(implode("\n", $warn_externals), 8)); $prompt = 'Generate a diff (with just local changes) anyway?'; if (!phutil_console_confirm($prompt)) { throw new ArcanistUserAbortException(); } else { $this->hasWarnedExternals = true; } } } else { $paths = $repository_api->getWorkingCopyStatus(); } foreach ($paths as $path => $mask) { if ($mask & ArcanistRepositoryAPI::FLAG_UNTRACKED) { unset($paths[$path]); } } return $paths; } protected function generateChanges() { $parser = $this->newDiffParser(); $is_raw = $this->isRawDiffSource(); if ($is_raw) { if ($this->getArgument('raw')) { fwrite(STDERR, "Reading diff from stdin...\n"); $raw_diff = file_get_contents('php://stdin'); } else if ($this->getArgument('raw-command')) { list($raw_diff) = execx('%C', $this->getArgument('raw-command')); } else { throw new Exception('Unknown raw diff source.'); } $changes = $parser->parseDiff($raw_diff); foreach ($changes as $key => $change) { // Remove "message" changes, e.g. from "git show". if ($change->getType() == ArcanistDiffChangeType::TYPE_MESSAGE) { unset($changes[$key]); } } return $changes; } $repository_api = $this->getRepositoryAPI(); if ($repository_api instanceof ArcanistSubversionAPI) { $paths = $this->generateAffectedPaths(); $this->primeSubversionWorkingCopyData($paths); // Check to make sure the user is diffing from a consistent base revision. // This is mostly just an abuse sanity check because it's silly to do this // and makes the code more difficult to effectively review, but it also // affects patches and makes them nonportable. $bases = $repository_api->getSVNBaseRevisions(); // Remove all files with baserev "0"; these files are new. foreach ($bases as $path => $baserev) { if ($bases[$path] <= 0) { unset($bases[$path]); } } if ($bases) { $rev = reset($bases); $revlist = array(); foreach ($bases as $path => $baserev) { $revlist[] = " Revision {$baserev}, {$path}"; } $revlist = implode("\n", $revlist); foreach ($bases as $path => $baserev) { if ($baserev !== $rev) { throw new ArcanistUsageException( "Base revisions of changed paths are mismatched. Update all ". "paths to the same base revision before creating a diff: ". "\n\n". $revlist); } } // If you have a change which affects several files, all of which are // at a consistent base revision, treat that revision as the effective // base revision. The use case here is that you made a change to some // file, which updates it to HEAD, but want to be able to change it // again without updating the entire working copy. This is a little // sketchy but it arises in Facebook Ops workflows with config files and // doesn't have any real material tradeoffs (e.g., these patches are // perfectly applyable). $repository_api->overrideSVNBaseRevisionNumber($rev); } $changes = $parser->parseSubversionDiff( $repository_api, $paths); } else if ($repository_api instanceof ArcanistGitAPI) { $diff = $repository_api->getFullGitDiff( $repository_api->getBaseCommit(), $repository_api->getHeadCommit()); if (!strlen($diff)) { throw new ArcanistUsageException( 'No changes found. (Did you specify the wrong commit range?)'); } $changes = $parser->parseDiff($diff); } else if ($repository_api instanceof ArcanistMercurialAPI) { $diff = $repository_api->getFullMercurialDiff(); if (!strlen($diff)) { throw new ArcanistUsageException( 'No changes found. (Did you specify the wrong commit range?)'); } $changes = $parser->parseDiff($diff); } else { throw new Exception('Repository API is not supported.'); } if (count($changes) > 250) { $count = number_format(count($changes)); $link = 'http://www.phabricator.com/docs/phabricator/article/'. 'Differential_User_Guide_Large_Changes.html'; $message = "This diff has a very large number of changes ({$count}). ". "Differential works best for changes which will receive detailed ". "human review, and not as well for large automated changes or ". "bulk checkins. See {$link} for information about reviewing big ". "checkins. Continue anyway?"; if (!phutil_console_confirm($message)) { throw new ArcanistUsageException( 'Aborted generation of gigantic diff.'); } } $limit = 1024 * 1024 * 4; foreach ($changes as $change) { $size = 0; foreach ($change->getHunks() as $hunk) { $size += strlen($hunk->getCorpus()); } if ($size > $limit) { $file_name = $change->getCurrentPath(); $change_size = number_format($size); $byte_warning = "Diff for '{$file_name}' with context is {$change_size} bytes in ". "length. Generally, source changes should not be this large."; if (!$this->getArgument('less-context')) { $byte_warning .= " If this file is a huge text file, try using the ". "'--less-context' flag."; } if ($repository_api instanceof ArcanistSubversionAPI) { throw new ArcanistUsageException( "{$byte_warning} If the file is not a text file, mark it as ". "binary with:". "\n\n". " $ svn propset svn:mime-type application/octet-stream ". "\n"); } else { $confirm = "{$byte_warning} If the file is not a text file, you can ". "mark it 'binary'. Mark this file as 'binary' and continue?"; if (phutil_console_confirm($confirm)) { $change->convertToBinaryChange($repository_api); } else { throw new ArcanistUsageException( 'Aborted generation of gigantic diff.'); } } } } $try_encoding = nonempty($this->getArgument('encoding'), null); $utf8_problems = array(); foreach ($changes as $change) { foreach ($change->getHunks() as $hunk) { $corpus = $hunk->getCorpus(); if (!phutil_is_utf8($corpus)) { // If this corpus is heuristically binary, don't try to convert it. // mb_check_encoding() and mb_convert_encoding() are both very very // liberal about what they're willing to process. $is_binary = ArcanistDiffUtils::isHeuristicBinaryFile($corpus); if (!$is_binary) { if (!$try_encoding) { try { $try_encoding = $this->getRepositoryEncoding(); } catch (ConduitClientException $e) { if ($e->getErrorCode() == 'ERR-BAD-ARCANIST-PROJECT') { echo phutil_console_wrap( "Lookup of encoding in arcanist project failed\n". $e->getMessage()); } else { throw $e; } } } if ($try_encoding) { $corpus = phutil_utf8_convert($corpus, 'UTF-8', $try_encoding); $name = $change->getCurrentPath(); if (phutil_is_utf8($corpus)) { $this->writeStatusMessage( "Converted a '{$name}' hunk from '{$try_encoding}' ". "to UTF-8.\n"); $hunk->setCorpus($corpus); continue; } } } $utf8_problems[] = $change; break; } } } // If there are non-binary files which aren't valid UTF-8, warn the user // and treat them as binary changes. See D327 for discussion of why Arcanist // has this behavior. if ($utf8_problems) { $utf8_warning = pht( 'This diff includes file(s) which are not valid UTF-8 (they contain '. 'invalid byte sequences). You can either stop this workflow and '. 'fix these files, or continue. If you continue, these files will '. 'be marked as binary.', count($utf8_problems))."\n\n". "You can learn more about how Phabricator handles character encodings ". "(and how to configure encoding settings and detect and correct ". "encoding problems) by reading 'User Guide: UTF-8 and Character ". "Encoding' in the Phabricator documentation.\n\n". " ".pht('AFFECTED FILE(S)', count($utf8_problems))."\n"; $confirm = pht( 'Do you want to mark these files as binary and continue?', count($utf8_problems)); echo phutil_console_format("**Invalid Content Encoding (Non-UTF8)**\n"); echo phutil_console_wrap($utf8_warning); $file_list = mpull($utf8_problems, 'getCurrentPath'); $file_list = ' '.implode("\n ", $file_list); echo $file_list; if (!phutil_console_confirm($confirm, $default_no = false)) { throw new ArcanistUsageException('Aborted workflow to fix UTF-8.'); } else { foreach ($utf8_problems as $change) { $change->convertToBinaryChange($repository_api); } } } $this->uploadFilesForChanges($changes); return $changes; } private function getGitParentLogInfo() { $info = array( 'parent' => null, 'base_revision' => null, 'base_path' => null, 'uuid' => null, ); $repository_api = $this->getRepositoryAPI(); $parser = $this->newDiffParser(); $history_messages = $repository_api->getGitHistoryLog(); if (!$history_messages) { // This can occur on the initial commit. return $info; } $history_messages = $parser->parseDiff($history_messages); foreach ($history_messages as $key => $change) { try { $message = ArcanistDifferentialCommitMessage::newFromRawCorpus( $change->getMetadata('message')); if ($message->getRevisionID() && $info['parent'] === null) { $info['parent'] = $message->getRevisionID(); } if ($message->getGitSVNBaseRevision() && $info['base_revision'] === null) { $info['base_revision'] = $message->getGitSVNBaseRevision(); $info['base_path'] = $message->getGitSVNBasePath(); } if ($message->getGitSVNUUID()) { $info['uuid'] = $message->getGitSVNUUID(); } if ($info['parent'] && $info['base_revision']) { break; } } catch (ArcanistDifferentialCommitMessageParserException $ex) { // Ignore. } catch (ArcanistUsageException $ex) { // Ignore an invalid Differential Revision field in the parent commit } } return $info; } protected function primeSubversionWorkingCopyData($paths) { $repository_api = $this->getRepositoryAPI(); $futures = array(); $targets = array(); foreach ($paths as $path => $mask) { $futures[] = $repository_api->buildDiffFuture($path); $targets[] = array('command' => 'diff', 'path' => $path); $futures[] = $repository_api->buildInfoFuture($path); $targets[] = array('command' => 'info', 'path' => $path); } foreach (Futures($futures)->limit(8) as $key => $future) { $target = $targets[$key]; if ($target['command'] == 'diff') { $repository_api->primeSVNDiffResult( $target['path'], $future->resolve()); } else { $repository_api->primeSVNInfoResult( $target['path'], $future->resolve()); } } } private function shouldAmend() { if ($this->isRawDiffSource()) { return false; } if ($this->haveUncommittedChanges) { return false; } if ($this->getArgument('no-amend')) { return false; } if ($this->getArgument('head') !== null) { return false; } // Run this last: with --raw or --raw-command, we won't have a repository // API. if ($this->isHistoryImmutable()) { return false; } return true; } /* -( Lint and Unit Tests )------------------------------------------------ */ /** * @task lintunit */ private function runLintUnit() { $lint_result = $this->runLint(); $unit_result = $this->runUnit(); return array( 'lintResult' => $lint_result, 'unresolvedLint' => $this->unresolvedLint, 'postponedLinters' => $this->postponedLinters, 'unitResult' => $unit_result, 'testResults' => $this->testResults, ); } /** * @task lintunit */ private function runLint() { if ($this->getArgument('nolint') || $this->getArgument('only') || $this->isRawDiffSource() || $this->getArgument('head')) { return ArcanistLintWorkflow::RESULT_SKIP; } $repository_api = $this->getRepositoryAPI(); $this->console->writeOut("Linting...\n"); try { $argv = $this->getPassthruArgumentsAsArgv('lint'); if ($repository_api->supportsCommitRanges()) { $argv[] = '--rev'; $argv[] = $repository_api->getBaseCommit(); } $lint_workflow = $this->buildChildWorkflow('lint', $argv); if ($this->shouldAmend()) { // TODO: We should offer to create a checkpoint commit. $lint_workflow->setShouldAmendChanges(true); } $lint_result = $lint_workflow->run(); switch ($lint_result) { case ArcanistLintWorkflow::RESULT_OKAY: if ($this->getArgument('advice') && $lint_workflow->getUnresolvedMessages()) { $this->getErrorExcuse( 'lint', 'Lint issued unresolved advice.', 'lint-excuses'); } else { $this->console->writeOut( "** LINT OKAY ** No lint problems.\n"); } break; case ArcanistLintWorkflow::RESULT_WARNINGS: $this->getErrorExcuse( 'lint', 'Lint issued unresolved warnings.', 'lint-excuses'); break; case ArcanistLintWorkflow::RESULT_ERRORS: $this->console->writeOut( "** LINT ERRORS ** Lint raised errors!\n"); $this->getErrorExcuse( 'lint', 'Lint issued unresolved errors!', 'lint-excuses'); break; case ArcanistLintWorkflow::RESULT_POSTPONED: $this->console->writeOut( "** LINT POSTPONED ** ". "Lint results are postponed.\n"); break; } $this->unresolvedLint = array(); foreach ($lint_workflow->getUnresolvedMessages() as $message) { $this->unresolvedLint[] = $message->toDictionary(); } $this->postponedLinters = $lint_workflow->getPostponedLinters(); return $lint_result; } catch (ArcanistNoEngineException $ex) { $this->console->writeOut("No lint engine configured for this project.\n"); } catch (ArcanistNoEffectException $ex) { $this->console->writeOut($ex->getMessage()."\n"); } return null; } /** * @task lintunit */ private function runUnit() { if ($this->getArgument('nounit') || $this->getArgument('only') || $this->isRawDiffSource() || $this->getArgument('head')) { return ArcanistUnitWorkflow::RESULT_SKIP; } $repository_api = $this->getRepositoryAPI(); $this->console->writeOut("Running unit tests...\n"); try { $argv = $this->getPassthruArgumentsAsArgv('unit'); if ($repository_api->supportsCommitRanges()) { $argv[] = '--rev'; $argv[] = $repository_api->getBaseCommit(); } $unit_workflow = $this->buildChildWorkflow('unit', $argv); $unit_result = $unit_workflow->run(); switch ($unit_result) { case ArcanistUnitWorkflow::RESULT_OKAY: $this->console->writeOut( "** UNIT OKAY ** No unit test failures.\n"); break; case ArcanistUnitWorkflow::RESULT_UNSOUND: if ($this->getArgument('ignore-unsound-tests')) { echo phutil_console_format( "** UNIT UNSOUND ** Unit testing raised errors, ". "but all failing tests are unsound.\n"); } else { $continue = $this->console->confirm( 'Unit test results included failures, but all failing tests '. 'are known to be unsound. Ignore unsound test failures?'); if (!$continue) { throw new ArcanistUserAbortException(); } } break; case ArcanistUnitWorkflow::RESULT_FAIL: $this->console->writeOut( "** UNIT ERRORS ** Unit testing raised errors!\n"); $this->getErrorExcuse( 'unit', 'Unit test results include failures!', 'unit-excuses'); break; } $this->testResults = array(); foreach ($unit_workflow->getTestResults() as $test) { $this->testResults[] = array( 'name' => $test->getName(), 'link' => $test->getLink(), 'result' => $test->getResult(), 'userdata' => $test->getUserData(), 'coverage' => $test->getCoverage(), 'extra' => $test->getExtraData(), ); } return $unit_result; } catch (ArcanistNoEngineException $ex) { $this->console->writeOut( "No unit test engine is configured for this project.\n"); } catch (ArcanistNoEffectException $ex) { $this->console->writeOut($ex->getMessage()."\n"); } return null; } public function getTestResults() { return $this->testResults; } private function getSkipExcuse($prompt, $history) { $excuse = $this->getArgument('excuse'); if ($excuse === null) { $history = $this->getRepositoryAPI()->getScratchFilePath($history); $excuse = phutil_console_prompt($prompt, $history); if ($excuse == '') { throw new ArcanistUserAbortException(); } } return $excuse; } private function getErrorExcuse($type, $prompt, $history) { if ($this->getArgument('excuse')) { $this->console->sendMessage(array( 'type' => $type, 'confirm' => $prompt.' Ignore them?', )); return; } $history = $this->getRepositoryAPI()->getScratchFilePath($history); $prompt .= ' Provide explanation to continue or press Enter to abort.'; $this->console->writeOut("\n\n%s", phutil_console_wrap($prompt)); $this->console->sendMessage(array( 'type' => $type, 'prompt' => 'Explanation:', 'history' => $history, )); } public function handleServerMessage(PhutilConsoleMessage $message) { $data = $message->getData(); if ($this->getArgument('excuse')) { try { phutil_console_require_tty(); } catch (PhutilConsoleStdinNotInteractiveException $ex) { $this->excuses[$data['type']] = $this->getArgument('excuse'); return null; } } $response = ''; if (isset($data['prompt'])) { $response = phutil_console_prompt($data['prompt'], idx($data, 'history')); } else if (phutil_console_confirm($data['confirm'])) { $response = $this->getArgument('excuse'); } if ($response == '') { throw new ArcanistUserAbortException(); } $this->excuses[$data['type']] = $response; return null; } /* -( Commit and Update Messages )----------------------------------------- */ /** * @task message */ private function buildCommitMessage() { if ($this->getArgument('preview') || $this->getArgument('only')) { return null; } $is_create = $this->getArgument('create'); $is_update = $this->getArgument('update'); $is_raw = $this->isRawDiffSource(); $is_message = $this->getArgument('use-commit-message'); $is_verbatim = $this->getArgument('verbatim'); if ($is_message) { return $this->getCommitMessageFromCommit($is_message); } if ($is_verbatim) { return $this->getCommitMessageFromUser(); } if (!$is_raw && !$is_create && !$is_update) { $repository_api = $this->getRepositoryAPI(); $revisions = $repository_api->loadWorkingCopyDifferentialRevisions( $this->getConduit(), array( 'authors' => array($this->getUserPHID()), 'status' => 'status-open', )); if (!$revisions) { $is_create = true; } else if (count($revisions) == 1) { $revision = head($revisions); $is_update = $revision['id']; } else { throw new ArcanistUsageException( "There are several revisions which match the working copy:\n\n". $this->renderRevisionList($revisions)."\n". "Use '--update' to choose one, or '--create' to create a new ". "revision."); } } $message = null; if ($is_create) { $message_file = $this->getArgument('message-file'); if ($message_file) { return $this->getCommitMessageFromFile($message_file); } else { return $this->getCommitMessageFromUser(); } } else if ($is_update) { $revision_id = $this->normalizeRevisionID($is_update); if (!is_numeric($revision_id)) { throw new ArcanistUsageException( 'Parameter to --update must be a Differential Revision number'); } return $this->getCommitMessageFromRevision($revision_id); } else { // This is --raw without enough info to create a revision, so force just // a diff. return null; } } /** * @task message */ private function getCommitMessageFromCommit($commit) { $text = $this->getRepositoryAPI()->getCommitMessage($commit); $message = ArcanistDifferentialCommitMessage::newFromRawCorpus($text); $message->pullDataFromConduit($this->getConduit()); $this->validateCommitMessage($message); return $message; } /** * @task message */ private function getCommitMessageFromUser() { $conduit = $this->getConduit(); $template = null; if (!$this->getArgument('verbatim')) { $saved = $this->readScratchFile('create-message'); if ($saved) { $where = $this->getReadableScratchFilePath('create-message'); $preview = explode("\n", $saved); $preview = array_shift($preview); $preview = trim($preview); $preview = id(new PhutilUTF8StringTruncator()) ->setMaximumGlyphs(64) ->truncateString($preview); if ($preview) { $preview = "Message begins:\n\n {$preview}\n\n"; } else { $preview = null; } echo "You have a saved revision message in '{$where}'.\n". "{$preview}". "You can use this message, or discard it."; $use = phutil_console_confirm( 'Do you want to use this message?', $default_no = false); if ($use) { $template = $saved; } else { $this->removeScratchFile('create-message'); } } } $template_is_default = false; $notes = array(); $included = array(); list($fields, $notes, $included_commits) = $this->getDefaultCreateFields(); if ($template) { $fields = array(); $notes = array(); } else { if (!$fields) { $template_is_default = true; } if ($notes) { $commit = head($this->getRepositoryAPI()->getLocalCommitInformation()); $template = $commit['message']; } else { $template = $conduit->callMethodSynchronous( 'differential.getcommitmessage', array( 'revision_id' => null, 'edit' => 'create', 'fields' => $fields, )); } } $old_message = $template; $included = array(); if ($included_commits) { foreach ($included_commits as $commit) { $included[] = ' '.$commit; } $in_branch = ''; if (!$this->isRawDiffSource()) { $in_branch = ' in branch '.$this->getRepositoryAPI()->getBranchName(); } $included = array_merge( array( '', "Included commits{$in_branch}:", '', ), $included); } $issues = array_merge( array( 'NEW DIFFERENTIAL REVISION', 'Describe the changes in this new revision.', ), $included, array( '', 'arc could not identify any existing revision in your working copy.', 'If you intended to update an existing revision, use:', '', ' $ arc diff --update ', )); if ($notes) { $issues = array_merge($issues, array(''), $notes); } $done = false; $first = true; while (!$done) { $template = rtrim($template, "\r\n")."\n\n"; foreach ($issues as $issue) { $template .= '# '.$issue."\n"; } $template .= "\n"; if ($first && $this->getArgument('verbatim') && !$template_is_default) { $new_template = $template; } else { $new_template = $this->newInteractiveEditor($template) ->setName('new-commit') ->editInteractively(); } $first = false; if ($template_is_default && ($new_template == $template)) { throw new ArcanistUsageException('Template not edited.'); } $template = ArcanistCommentRemover::removeComments($new_template); // With --raw-command, we may not have a repository API. if ($this->hasRepositoryAPI()) { $repository_api = $this->getRepositoryAPI(); // special check for whether to amend here. optimizes a common git // workflow. we can't do this for mercurial because the mq extension // is popular and incompatible with hg commit --amend ; see T2011. $should_amend = (count($included_commits) == 1 && $repository_api instanceof ArcanistGitAPI && $this->shouldAmend()); } else { $should_amend = false; } if ($should_amend) { $wrote = (rtrim($old_message) != rtrim($template)); if ($wrote) { $repository_api->amendCommit($template); $where = 'commit message'; } } else { $wrote = $this->writeScratchFile('create-message', $template); $where = "'".$this->getReadableScratchFilePath('create-message')."'"; } try { $message = ArcanistDifferentialCommitMessage::newFromRawCorpus( $template); $message->pullDataFromConduit($conduit); $this->validateCommitMessage($message); $done = true; } catch (ArcanistDifferentialCommitMessageParserException $ex) { echo "Commit message has errors:\n\n"; $issues = array('Resolve these errors:'); foreach ($ex->getParserErrors() as $error) { echo phutil_console_wrap("- ".$error."\n", 6); $issues[] = ' - '.$error; } echo "\n"; echo 'You must resolve these errors to continue.'; $again = phutil_console_confirm( 'Do you want to edit the message?', $default_no = false); if ($again) { // Keep going. } else { $saved = null; if ($wrote) { $saved = "A copy was saved to {$where}."; } throw new ArcanistUsageException( "Message has unresolved errrors. {$saved}"); } } catch (Exception $ex) { if ($wrote) { echo phutil_console_wrap("(Message saved to {$where}.)\n"); } throw $ex; } } return $message; } /** * @task message */ private function getCommitMessageFromFile($file) { $conduit = $this->getConduit(); $data = Filesystem::readFile($file); $message = ArcanistDifferentialCommitMessage::newFromRawCorpus($data); $message->pullDataFromConduit($conduit); $this->validateCommitMessage($message); return $message; } /** * @task message */ private function getCommitMessageFromRevision($revision_id) { $id = $revision_id; $revision = $this->getConduit()->callMethodSynchronous( 'differential.query', array( 'ids' => array($id), )); $revision = head($revision); if (!$revision) { throw new ArcanistUsageException( "Revision '{$revision_id}' does not exist!"); } $this->checkRevisionOwnership($revision); $message = $this->getConduit()->callMethodSynchronous( 'differential.getcommitmessage', array( 'revision_id' => $id, 'edit' => false, )); $this->commitMessageFromRevision = $message; $obj = ArcanistDifferentialCommitMessage::newFromRawCorpus($message); $obj->pullDataFromConduit($this->getConduit()); return $obj; } /** * @task message */ private function validateCommitMessage( ArcanistDifferentialCommitMessage $message) { $futures = array(); $revision_id = $message->getRevisionID(); if ($revision_id) { $futures['revision'] = $this->getConduit()->callMethod( 'differential.query', array( 'ids' => array($revision_id), )); } $reviewers = $message->getFieldValue('reviewerPHIDs'); if (!$reviewers) { $confirm = 'You have not specified any reviewers. Continue anyway?'; if (!phutil_console_confirm($confirm)) { throw new ArcanistUsageException('Specify reviewers and retry.'); } } else { $futures['reviewers'] = $this->getConduit()->callMethod( 'user.query', array( 'phids' => $reviewers, )); } foreach (Futures($futures) as $key => $future) { $result = $future->resolve(); switch ($key) { case 'revision': if (empty($result)) { throw new ArcanistUsageException( "There is no revision D{$revision_id}."); } $this->checkRevisionOwnership(head($result)); break; case 'reviewers': $untils = array(); foreach ($result as $user) { if (idx($user, 'currentStatus') == 'away') { $untils[] = $user['currentStatusUntil']; } } if (count($untils) == count($reviewers)) { $until = date('l, M j Y', min($untils)); $confirm = "All reviewers are away until {$until}. ". "Continue anyway?"; if (!phutil_console_confirm($confirm)) { throw new ArcanistUsageException( 'Specify available reviewers and retry.'); } } break; } } } /** * @task message */ private function getUpdateMessage(array $fields, $template = '') { if ($this->getArgument('raw')) { throw new ArcanistUsageException( "When using '--raw' to update a revision, specify an update message ". "with '--message'. (Normally, we'd launch an editor to ask you for a ". "message, but can not do that because stdin is the diff source.)"); } // When updating a revision using git without specifying '--message', try // to prefill with the message in HEAD if it isn't a template message. The // idea is that if you do: // // $ git commit -a -m 'fix some junk' // $ arc diff // // ...you shouldn't have to retype the update message. Similar things apply // to Mercurial. if ($template == '') { $comments = $this->getDefaultUpdateMessage(); $template = rtrim($comments). "\n\n". "# Updating D{$fields['revisionID']}: {$fields['title']}\n". "#\n". "# Enter a brief description of the changes included in this update.\n". "# The first line is used as subject, next lines as comment.\n". "#\n". "# If you intended to create a new revision, use:\n". "# $ arc diff --create\n". "\n"; } $comments = $this->newInteractiveEditor($template) ->setName('differential-update-comments') ->editInteractively(); return $comments; } private function getDefaultCreateFields() { $result = array(array(), array(), array()); if ($this->isRawDiffSource()) { return $result; } $repository_api = $this->getRepositoryAPI(); $local = $repository_api->getLocalCommitInformation(); if ($local) { $result = $this->parseCommitMessagesIntoFields($local); if ($this->getArgument('create')) { unset($result[0]['revisionID']); } } $result[0] = $this->dispatchWillBuildEvent($result[0]); return $result; } /** * Convert a list of commits from `getLocalCommitInformation()` into * a format usable by arc to create a new diff. Specifically, we emit: * * - A dictionary of commit message fields. * - A list of errors encountered while parsing the messages. * - A human-readable list of the commits themselves. * * For example, if the user runs "arc diff HEAD^^^" and selects a diff range * which includes several diffs, we attempt to merge them somewhat * intelligently into a single message, because we can only send one * "Summary:", "Reviewers:", etc., field to Differential. We also return * errors (e.g., if the user typed a reviewer name incorrectly) and a * summary of the commits themselves. * * @param dict Local commit information. * @return list Complex output, see summary. * @task message */ private function parseCommitMessagesIntoFields(array $local) { $conduit = $this->getConduit(); $local = ipull($local, null, 'commit'); // If the user provided "--reviewers" or "--ccs", add a faux message to // the list with the implied fields. $faux_message = array(); if ($this->getArgument('reviewers')) { $faux_message[] = 'Reviewers: '.$this->getArgument('reviewers'); } if ($this->getArgument('cc')) { $faux_message[] = 'CC: '.$this->getArgument('cc'); } if ($faux_message) { $faux_message = implode("\n\n", $faux_message); $local = array( '(Flags) ' => array( 'message' => $faux_message, 'summary' => 'Command-Line Flags', ), ) + $local; } // Build a human-readable list of the commits, so we can show the user which // commits are included in the diff. $included = array(); foreach ($local as $hash => $info) { $included[] = substr($hash, 0, 12).' '.$info['summary']; } // Parse all of the messages into fields. $messages = array(); foreach ($local as $hash => $info) { $text = $info['message']; if (trim($text) == self::AUTO_COMMIT_TITLE) { continue; } $obj = ArcanistDifferentialCommitMessage::newFromRawCorpus($text); $messages[$hash] = $obj; } $notes = array(); $fields = array(); foreach ($messages as $hash => $message) { try { $message->pullDataFromConduit($conduit, $partial = true); $fields[$hash] = $message->getFields(); } catch (ArcanistDifferentialCommitMessageParserException $ex) { if ($this->getArgument('verbatim')) { // In verbatim mode, just bail when we hit an error. The user can // rerun without --verbatim if they want to fix it manually. Most // users will probably `git commit --amend` instead. throw $ex; } $fields[$hash] = $message->getFields(); $frev = substr($hash, 0, 12); $notes[] = "NOTE: commit {$frev} could not be completely parsed:"; foreach ($ex->getParserErrors() as $error) { $notes[] = " - {$error}"; } } } // Merge commit message fields. We do this somewhat-intelligently so that // multiple "Reviewers" or "CC" fields will merge into the concatenation // of all values. // We have special parsing rules for 'title' because we can't merge // multiple titles, and one-line commit messages like "fix stuff" will // parse as titles. Instead, pick the first title we encounter. When we // encounter subsequent titles, treat them as part of the summary. Then // we merge all the summaries together below. $result = array(); // Process fields in oldest-first order, so earlier commits get to set the // title of record and reviewers/ccs are listed in chronological order. $fields = array_reverse($fields); foreach ($fields as $hash => $dict) { $title = idx($dict, 'title'); if (!strlen($title)) { continue; } if (!isset($result['title'])) { // We don't have a title yet, so use this one. $result['title'] = $title; } else { // We already have a title, so merge this new title into the summary. $summary = idx($dict, 'summary'); if ($summary) { $summary = $title."\n\n".$summary; } else { $summary = $title; } $fields[$hash]['summary'] = $summary; } } // Now, merge all the other fields in a general sort of way. foreach ($fields as $hash => $dict) { foreach ($dict as $key => $value) { if ($key == 'title') { // This has been handled above, and either assigned directly or // merged into the summary. continue; } if (is_array($value)) { // For array values, merge the arrays, appending the new values. // Examples are "Reviewers" and "Cc", where this produces a list of // all users specified as reviewers. $cur = idx($result, $key, array()); $new = array_merge($cur, $value); $result[$key] = $new; continue; } else { if (!strlen(trim($value))) { // Ignore empty fields. continue; } // For string values, append the new field to the old field with // a blank line separating them. Examples are "Test Plan" and // "Summary". $cur = idx($result, $key, ''); if (strlen($cur)) { $new = $cur."\n\n".$value; } else { $new = $value; } $result[$key] = $new; } } } return array($result, $notes, $included); } private function getDefaultUpdateMessage() { if ($this->isRawDiffSource()) { return null; } $repository_api = $this->getRepositoryAPI(); if ($repository_api instanceof ArcanistGitAPI) { return $this->getGitUpdateMessage(); } if ($repository_api instanceof ArcanistMercurialAPI) { return $this->getMercurialUpdateMessage(); } return null; } /** * Retrieve the git messages between HEAD and the last update. * * @task message */ private function getGitUpdateMessage() { $repository_api = $this->getRepositoryAPI(); $parser = $this->newDiffParser(); $commit_messages = $repository_api->getGitCommitLog(); $commit_messages = $parser->parseDiff($commit_messages); if (count($commit_messages) == 1) { // If there's only one message, assume this is an amend-based workflow and // that using it to prefill doesn't make sense. return null; } // We have more than one message, so figure out which ones are new. We // do this by pulling the current diff and comparing commit hashes in the // working copy with attached commit hashes. It's not super important that // we always get this 100% right, we're just trying to do something // reasonable. $local = $this->loadActiveLocalCommitInfo(); $hashes = ipull($local, null, 'commit'); $usable = array(); foreach ($commit_messages as $message) { $text = $message->getMetadata('message'); $parsed = ArcanistDifferentialCommitMessage::newFromRawCorpus($text); if ($parsed->getRevisionID()) { // If this is an amended commit message with a revision ID, it's // certainly not new. Stop marking commits as usable and break out. break; } if (isset($hashes[$message->getCommitHash()])) { // If this commit is currently part of the diff, stop using commit // messages, since anything older than this isn't new. break; } // Otherwise, this looks new, so it's a usable commit message. $usable[] = $text; } if (!$usable) { // No new commit messages, so we don't have anywhere to start from. return null; } return $this->formatUsableLogs($usable); } /** * Retrieve the hg messages between tip and the last update. * * @task message */ private function getMercurialUpdateMessage() { $repository_api = $this->getRepositoryAPI(); $messages = $repository_api->getCommitMessageLog(); if (count($messages) == 1) { // If there's only one message, assume this is an amend-based workflow and // that using it to prefill doesn't make sense. return null; } $local = $this->loadActiveLocalCommitInfo(); $hashes = ipull($local, null, 'commit'); $usable = array(); foreach ($messages as $rev => $message) { if (isset($hashes[$rev])) { // If this commit is currently part of the active diff on the revision, // stop using commit messages, since anything older than this isn't new. break; } // Otherwise, this looks new, so it's a usable commit message. $usable[] = $message; } if (!$usable) { // No new commit messages, so we don't have anywhere to start from. return null; } return $this->formatUsableLogs($usable); } /** * Format log messages to prefill a diff update. * * @task message */ private function formatUsableLogs(array $usable) { // Flip messages so they'll read chronologically (oldest-first) in the // template, e.g.: // // - Added foobar. // - Fixed foobar bug. // - Documented foobar. $usable = array_reverse($usable); $default = array(); foreach ($usable as $message) { // Pick the first line out of each message. $text = trim($message); if ($text == self::AUTO_COMMIT_TITLE) { continue; } $text = head(explode("\n", $text)); $default[] = ' - '.$text."\n"; } return implode('', $default); } private function loadActiveLocalCommitInfo() { $current_diff = $this->getConduit()->callMethodSynchronous( 'differential.getdiff', array( 'revision_id' => $this->revisionID, )); $properties = idx($current_diff, 'properties', array()); return idx($properties, 'local:commits', array()); } /* -( Diff Specification )------------------------------------------------- */ /** * @task diffspec */ private function getLintStatus($lint_result) { $map = array( ArcanistLintWorkflow::RESULT_OKAY => 'okay', ArcanistLintWorkflow::RESULT_ERRORS => 'fail', ArcanistLintWorkflow::RESULT_WARNINGS => 'warn', ArcanistLintWorkflow::RESULT_SKIP => 'skip', ArcanistLintWorkflow::RESULT_POSTPONED => 'postponed', ); return idx($map, $lint_result, 'none'); } /** * @task diffspec */ private function getUnitStatus($unit_result) { $map = array( ArcanistUnitWorkflow::RESULT_OKAY => 'okay', ArcanistUnitWorkflow::RESULT_FAIL => 'fail', ArcanistUnitWorkflow::RESULT_UNSOUND => 'warn', ArcanistUnitWorkflow::RESULT_SKIP => 'skip', ArcanistUnitWorkflow::RESULT_POSTPONED => 'postponed', ); return idx($map, $unit_result, 'none'); } /** * @task diffspec */ private function buildDiffSpecification() { $base_revision = null; $base_path = null; $vcs = null; $repo_uuid = null; $parent = null; $source_path = null; $branch = null; $bookmark = null; if (!$this->isRawDiffSource()) { $repository_api = $this->getRepositoryAPI(); $base_revision = $repository_api->getSourceControlBaseRevision(); $base_path = $repository_api->getSourceControlPath(); $vcs = $repository_api->getSourceControlSystemName(); $source_path = $repository_api->getPath(); $branch = $repository_api->getBranchName(); $repo_uuid = $repository_api->getRepositoryUUID(); if ($repository_api instanceof ArcanistGitAPI) { $info = $this->getGitParentLogInfo(); if ($info['parent']) { $parent = $info['parent']; } if ($info['base_revision']) { $base_revision = $info['base_revision']; } if ($info['base_path']) { $base_path = $info['base_path']; } if ($info['uuid']) { $repo_uuid = $info['uuid']; } } else if ($repository_api instanceof ArcanistMercurialAPI) { $bookmark = $repository_api->getActiveBookmark(); $svn_info = $repository_api->getSubversionInfo(); $repo_uuid = idx($svn_info, 'uuid'); $base_path = idx($svn_info, 'base_path', $base_path); $base_revision = idx($svn_info, 'base_revision', $base_revision); // TODO: provide parent info } } $project_id = null; if ($this->requiresWorkingCopy()) { $project_id = $this->getWorkingCopy()->getProjectID(); } $data = array( 'sourceMachine' => php_uname('n'), 'sourcePath' => $source_path, 'branch' => $branch, 'bookmark' => $bookmark, 'sourceControlSystem' => $vcs, 'sourceControlPath' => $base_path, 'sourceControlBaseRevision' => $base_revision, 'creationMethod' => 'arc', 'arcanistProject' => $project_id, ); if (!$this->isRawDiffSource()) { $repository_phid = $this->getRepositoryPHID(); if ($repository_phid) { $data['repositoryPHID'] = $repository_phid; } } return $data; } /* -( Diff Properties )---------------------------------------------------- */ /** * Update lint information for the diff. * * @return void * * @task diffprop */ private function updateLintDiffProperty() { if (strlen($this->excuses['lint'])) { $this->updateDiffProperty('arc:lint-excuse', json_encode($this->excuses['lint'])); } if ($this->unresolvedLint) { $this->updateDiffProperty('arc:lint', json_encode($this->unresolvedLint)); } $postponed = $this->postponedLinters; if ($postponed) { $this->updateDiffProperty('arc:lint-postponed', json_encode($postponed)); } } /** * Update unit test information for the diff. * * @return void * * @task diffprop */ private function updateUnitDiffProperty() { if (strlen($this->excuses['unit'])) { $this->updateDiffProperty('arc:unit-excuse', json_encode($this->excuses['unit'])); } if ($this->testResults) { $this->updateDiffProperty('arc:unit', json_encode($this->testResults)); } } /** * Update local commit information for the diff. * * @task diffprop */ private function updateLocalDiffProperty() { if ($this->isRawDiffSource()) { return; } $local_info = $this->getRepositoryAPI()->getLocalCommitInformation(); if (!$local_info) { return; } $this->updateDiffProperty('local:commits', json_encode($local_info)); } /** * Update an arbitrary diff property. * * @param string Diff property name. * @param string Diff property value. * @return void * * @task diffprop */ private function updateDiffProperty($name, $data) { $this->diffPropertyFutures[] = $this->getConduit()->callMethod( 'differential.setdiffproperty', array( 'diff_id' => $this->getDiffID(), 'name' => $name, 'data' => $data, )); } /** * Wait for finishing all diff property updates. * * @return void * * @task diffprop */ private function resolveDiffPropertyUpdates() { Futures($this->diffPropertyFutures)->resolveAll(); $this->diffPropertyFutures = array(); } private function dispatchWillCreateRevisionEvent(array $fields) { $event = $this->dispatchEvent( ArcanistEventType::TYPE_REVISION_WILLCREATEREVISION, array( 'specification' => $fields, )); return $event->getValue('specification'); } private function dispatchWillBuildEvent(array $fields) { $event = $this->dispatchEvent( ArcanistEventType::TYPE_DIFF_WILLBUILDMESSAGE, array( 'fields' => $fields, )); return $event->getValue('fields'); } private function checkRevisionOwnership(array $revision) { if ($revision['authorPHID'] == $this->getUserPHID()) { return; } $id = $revision['id']; $title = $revision['title']; throw new ArcanistUsageException( "You don't own revision D{$id} '{$title}'. You can only update ". "revisions you own. You can 'Commandeer' this revision from the web ". "interface if you want to become the owner."); } /* -( File Uploads )------------------------------------------------------- */ private function uploadFilesForChanges(array $changes) { assert_instances_of($changes, 'ArcanistDiffChange'); // Collect all the files we need to upload. $need_upload = array(); foreach ($changes as $key => $change) { if ($change->getFileType() != ArcanistDiffChangeType::FILE_BINARY) { continue; } if ($this->getArgument('skip-binaries')) { continue; } $name = basename($change->getCurrentPath()); $need_upload[] = array( 'type' => 'old', 'name' => $name, 'data' => $change->getOriginalFileData(), 'change' => $change, ); $need_upload[] = array( 'type' => 'new', 'name' => $name, 'data' => $change->getCurrentFileData(), 'change' => $change, ); } if (!$need_upload) { return; } // Determine mime types and file sizes. Update changes from "binary" to // "image" if the file is an image. Set image metadata. $type_image = ArcanistDiffChangeType::FILE_IMAGE; foreach ($need_upload as $key => $spec) { $change = $need_upload[$key]['change']; $type = $spec['type']; $size = strlen($spec['data']); $change->setMetadata("{$type}:file:size", $size); if ($spec['data'] === null) { // This covers the case where a file was added or removed; we don't // need to upload the other half of it (e.g., the old file data for // a file which was just added). This is distinct from an empty // file, which we do upload. unset($need_upload[$key]); continue; } $mime = $this->getFileMimeType($spec['data']); if (preg_match('@^image/@', $mime)) { $change->setFileType($type_image); } $change->setMetadata("{$type}:file:mime-type", $mime); } echo pht('Uploading %d files...', count($need_upload))."\n"; // Now we're ready to upload the actual file data. If possible, we'll just // transmit a hash of the file instead of the actual file data. If the data // already exists, Phabricator can share storage. Check if we can use // "file.uploadhash" yet (i.e., if the server is up to date enough). // TODO: Drop this check once we bump the protocol version. $conduit_methods = $this->getConduit()->callMethodSynchronous( 'conduit.query', array()); $can_use_hash_upload = isset($conduit_methods['file.uploadhash']); if ($can_use_hash_upload) { $hash_futures = array(); foreach ($need_upload as $key => $spec) { $hash_futures[$key] = $this->getConduit()->callMethod( 'file.uploadhash', array( 'name' => $spec['name'], 'hash' => sha1($spec['data']), )); } foreach (Futures($hash_futures)->limit(8) as $key => $future) { $type = $need_upload[$key]['type']; $change = $need_upload[$key]['change']; $name = $need_upload[$key]['name']; $phid = null; try { $phid = $future->resolve(); } catch (Exception $e) { // Just try uploading normally if the hash upload failed. continue; } if ($phid) { $change->setMetadata("{$type}:binary-phid", $phid); unset($need_upload[$key]); echo pht("Uploaded '%s' (%s).", $name, $type)."\n"; } } } $upload_futures = array(); foreach ($need_upload as $key => $spec) { $upload_futures[$key] = $this->getConduit()->callMethod( 'file.upload', array( 'name' => $spec['name'], 'data_base64' => base64_encode($spec['data']), )); } foreach (Futures($upload_futures)->limit(4) as $key => $future) { $type = $need_upload[$key]['type']; $change = $need_upload[$key]['change']; $name = $need_upload[$key]['name']; try { $phid = $future->resolve(); $change->setMetadata("{$type}:binary-phid", $phid); echo pht("Uploaded '%s' (%s).", $name, $type)."\n"; } catch (Exception $e) { echo "Failed to upload {$type} binary '{$name}'.\n\n"; echo $e->getMessage()."\n"; if (!phutil_console_confirm('Continue?', $default_no = false)) { throw new ArcanistUsageException( 'Aborted due to file upload failure. You can use --skip-binaries '. 'to skip binary uploads.'); } } } echo pht('Upload complete.')."\n"; } private function getFileMimeType($data) { $tmp = new TempFile(); Filesystem::writeFile($tmp, $data); return Filesystem::getMimeType($tmp); } private function shouldOpenCreatedObjectsInBrowser() { return $this->getArgument('browse'); } } diff --git a/src/workflow/ArcanistExportWorkflow.php b/src/workflow/ArcanistExportWorkflow.php index a7ebb981..01abb79f 100644 --- a/src/workflow/ArcanistExportWorkflow.php +++ b/src/workflow/ArcanistExportWorkflow.php @@ -1,272 +1,272 @@ array( 'help' => "Export change as a git patch. This format is more complete than ". "unified, but less complete than arc bundles. These patches can be ". "applied with 'git apply' or 'arc patch'.", ), 'unified' => array( 'help' => "Export change as a unified patch. This format is less complete ". "than git patches or arc bundles. These patches can be applied with ". "'patch' or 'arc patch'.", ), 'arcbundle' => array( 'param' => 'file', 'help' => "Export change as an arc bundle. This format can represent all ". "changes. These bundles can be applied with 'arc patch'.", ), 'encoding' => array( 'param' => 'encoding', 'help' => 'Attempt to convert non UTF-8 patch into specified encoding.', ), 'revision' => array( 'param' => 'revision_id', 'help' => 'Instead of exporting changes from the working copy, export them '. - 'from a Differential revision.' + 'from a Differential revision.', ), 'diff' => array( 'param' => 'diff_id', 'help' => 'Instead of exporting changes from the working copy, export them '. - 'from a Differential diff.' + 'from a Differential diff.', ), '*' => 'paths', ); } protected function didParseArguments() { $source = self::SOURCE_LOCAL; $requested = 0; if ($this->getArgument('revision')) { $source = self::SOURCE_REVISION; $requested++; $source_id = $this->getArgument($source); $this->sourceID = $this->normalizeRevisionID($source_id); } if ($this->getArgument('diff')) { $source = self::SOURCE_DIFF; $requested++; $this->sourceID = $this->getArgument($source); } $this->source = $source; if ($requested > 1) { throw new ArcanistUsageException( "Options '--revision' and '--diff' are not compatible. Choose exactly ". "one change source."); } $format = null; $requested = 0; if ($this->getArgument('git')) { $format = self::FORMAT_GIT; $requested++; } if ($this->getArgument('unified')) { $format = self::FORMAT_UNIFIED; $requested++; } if ($this->getArgument('arcbundle')) { $format = self::FORMAT_BUNDLE; $requested++; } if ($requested === 0) { throw new ArcanistUsageException( "Specify one of '--git', '--unified' or '--arcbundle ' to ". "choose an export format."); } else if ($requested > 1) { throw new ArcanistUsageException( "Options '--git', '--unified' and '--arcbundle' are not compatible. ". "Choose exactly one export format."); } $this->format = $format; } public function requiresConduit() { return true; } public function requiresAuthentication() { return $this->requiresConduit(); } public function requiresRepositoryAPI() { return $this->getSource() == self::SOURCE_LOCAL; } public function requiresWorkingCopy() { return $this->getSource() == self::SOURCE_LOCAL; } private function getSource() { return $this->source; } private function getSourceID() { return $this->sourceID; } private function getFormat() { return $this->format; } public function run() { $source = $this->getSource(); switch ($source) { case self::SOURCE_LOCAL: $repository_api = $this->getRepositoryAPI(); $parser = new ArcanistDiffParser(); $parser->setRepositoryAPI($repository_api); if ($repository_api instanceof ArcanistGitAPI) { $this->parseBaseCommitArgument($this->getArgument('paths')); $diff = $repository_api->getFullGitDiff( $repository_api->getBaseCommit(), $repository_api->getHeadCommit()); $changes = $parser->parseDiff($diff); $authors = $this->getConduit()->callMethodSynchronous( 'user.query', array( 'phids' => array($this->getUserPHID()), )); $author_dict = reset($authors); list($email) = $repository_api->execxLocal('config user.email'); $author = sprintf('%s <%s>', $author_dict['realName'], $email); } else if ($repository_api instanceof ArcanistMercurialAPI) { $this->parseBaseCommitArgument($this->getArgument('paths')); $diff = $repository_api->getFullMercurialDiff(); $changes = $parser->parseDiff($diff); $authors = $this->getConduit()->callMethodSynchronous( 'user.query', array( 'phids' => array($this->getUserPHID()), )); list($author) = $repository_api->execxLocal('showconfig ui.username'); } else { // TODO: paths support $paths = $repository_api->getWorkingCopyStatus(); $changes = $parser->parseSubversionDiff( $repository_api, $paths); $author = $this->getUserName(); } $bundle = ArcanistBundle::newFromChanges($changes); $bundle->setProjectID($this->getWorkingCopy()->getProjectID()); $bundle->setBaseRevision( $repository_api->getSourceControlBaseRevision()); // NOTE: we can't get a revision ID for SOURCE_LOCAL $parser = new PhutilEmailAddress($author); $bundle->setAuthorName($parser->getDisplayName()); $bundle->setAuthorEmail($parser->getAddress()); break; case self::SOURCE_REVISION: $bundle = $this->loadRevisionBundleFromConduit( $this->getConduit(), $this->getSourceID()); break; case self::SOURCE_DIFF: $bundle = $this->loadDiffBundleFromConduit( $this->getConduit(), $this->getSourceID()); break; } $try_encoding = nonempty($this->getArgument('encoding'), null); if (!$try_encoding) { try { $project_info = $this->getConduit()->callMethodSynchronous( 'arcanist.projectinfo', array( 'name' => $bundle->getProjectID(), )); $try_encoding = $project_info['encoding']; } catch (ConduitClientException $e) { $try_encoding = null; } } if ($try_encoding) { $bundle->setEncoding($try_encoding); } $format = $this->getFormat(); switch ($format) { case self::FORMAT_GIT: echo $bundle->toGitPatch(); break; case self::FORMAT_UNIFIED: echo $bundle->toUnifiedDiff(); break; case self::FORMAT_BUNDLE: $path = $this->getArgument('arcbundle'); echo "Writing bundle to '{$path}'...\n"; $bundle->writeToDisk($path); echo "done.\n"; break; } return 0; } } diff --git a/src/workflow/ArcanistLintWorkflow.php b/src/workflow/ArcanistLintWorkflow.php index 396e3ed8..20f636a6 100644 --- a/src/workflow/ArcanistLintWorkflow.php +++ b/src/workflow/ArcanistLintWorkflow.php @@ -1,636 +1,636 @@ shouldAmendChanges = $should_amend; return $this; } public function setShouldAmendWithoutPrompt($should_amend) { $this->shouldAmendWithoutPrompt = $should_amend; return $this; } public function setShouldAmendAutofixesWithoutPrompt($should_amend) { $this->shouldAmendAutofixesWithoutPrompt = $should_amend; return $this; } public function getCommandSynopses() { return phutil_console_format(<< array( 'help' => 'Show all lint warnings, not just those on changed lines. When '. 'paths are specified, this is the default behavior.', 'conflicts' => array( 'only-changed' => true, ), ), 'only-changed' => array( 'help' => 'Show lint warnings just on changed lines. When no paths are '. 'specified, this is the default. This differs from only-new '. 'in cases where line modifications introduce lint on other '. 'unmodified lines.', 'conflicts' => array( 'lintall' => true, ), ), 'rev' => array( 'param' => 'revision', 'help' => 'Lint changes since a specific revision.', 'supports' => array( 'git', 'hg', ), 'nosupport' => array( 'svn' => 'Lint does not currently support --rev in SVN.', ), ), 'output' => array( 'param' => 'format', 'help' => "With 'summary', show lint warnings in a more compact format. ". "With 'json', show lint warnings in machine-readable JSON format. ". "With 'none', show no lint warnings. ". "With 'compiler', show lint warnings in suitable for your editor. ". - "With 'xml', show lint warnings in the Checkstyle XML format." + "With 'xml', show lint warnings in the Checkstyle XML format.", ), 'only-new' => array( 'param' => 'bool', 'supports' => array('git', 'hg'), // TODO: svn 'help' => 'Display only messages not present in the original code.', ), 'engine' => array( 'param' => 'classname', 'help' => - 'Override configured lint engine for this project.' + 'Override configured lint engine for this project.', ), 'apply-patches' => array( 'help' => 'Apply patches suggested by lint to the working copy without '. 'prompting.', 'conflicts' => array( 'never-apply-patches' => true, ), ), 'never-apply-patches' => array( 'help' => 'Never apply patches suggested by lint.', 'conflicts' => array( 'apply-patches' => true, ), ), 'amend-all' => array( 'help' => 'When linting git repositories, amend HEAD with all patches '. 'suggested by lint without prompting.', ), 'amend-autofixes' => array( 'help' => 'When linting git repositories, amend HEAD with autofix '. 'patches suggested by lint without prompting.', ), 'everything' => array( 'help' => 'Lint all files in the project.', 'conflicts' => array( 'cache' => '--everything lints all files', - 'rev' => '--everything lints all files' + 'rev' => '--everything lints all files', ), ), 'severity' => array( 'param' => 'string', 'help' => "Set minimum message severity. One of: '". implode( "', '", array_keys(ArcanistLintSeverity::getLintSeverities())). "'. Defaults to '".self::DEFAULT_SEVERITY."'.", ), 'cache' => array( 'param' => 'bool', 'help' => "0 to disable cache, 1 to enable. The default value is ". "determined by 'arc.lint.cache' in configuration, which defaults ". "to off. See notes in 'arc.lint.cache'.", ), '*' => 'paths', ); } public function requiresAuthentication() { return (bool)$this->getArgument('only-new'); } public function requiresWorkingCopy() { return true; } public function requiresRepositoryAPI() { return true; } private function getCacheKey() { return implode("\n", array( get_class($this->engine), $this->getArgument('severity', self::DEFAULT_SEVERITY), $this->shouldLintAll, )); } public function run() { $console = PhutilConsole::getConsole(); $working_copy = $this->getWorkingCopy(); $configuration_manager = $this->getConfigurationManager(); $engine = $this->newLintEngine($this->getArgument('engine')); $rev = $this->getArgument('rev'); $paths = $this->getArgument('paths'); $use_cache = $this->getArgument('cache', null); $everything = $this->getArgument('everything'); if ($everything && $paths) { throw new ArcanistUsageException( 'You can not specify paths with --everything. The --everything '. 'flag lints every file.'); } if ($use_cache === null) { $use_cache = (bool)$configuration_manager->getConfigFromAnySource( 'arc.lint.cache', false); } if ($rev && $paths) { throw new ArcanistUsageException('Specify either --rev or paths.'); } // NOTE: When the user specifies paths, we imply --lintall and show all // warnings for the paths in question. This is easier to deal with for // us and less confusing for users. $this->shouldLintAll = $paths ? true : false; if ($this->getArgument('lintall')) { $this->shouldLintAll = true; } else if ($this->getArgument('only-changed')) { $this->shouldLintAll = false; } if ($everything) { $paths = iterator_to_array($this->getRepositoryApi()->getAllFiles()); $this->shouldLintAll = true; } else { $paths = $this->selectPathsForWorkflow($paths, $rev); } $this->engine = $engine; $engine->setMinimumSeverity( $this->getArgument('severity', self::DEFAULT_SEVERITY)); $file_hashes = array(); if ($use_cache) { $engine->setRepositoryVersion($this->getRepositoryVersion()); $cache = $this->readScratchJSONFile('lint-cache.json'); $cache = idx($cache, $this->getCacheKey(), array()); $cached = array(); foreach ($paths as $path) { $abs_path = $engine->getFilePathOnDisk($path); if (!Filesystem::pathExists($abs_path)) { continue; } $file_hashes[$abs_path] = md5_file($abs_path); if (!isset($cache[$path])) { continue; } $messages = idx($cache[$path], $file_hashes[$abs_path]); if ($messages !== null) { $cached[$path] = $messages; } } if ($cached) { $console->writeErr( pht("Using lint cache, use '--cache 0' to disable it.")."\n"); } $engine->setCachedResults($cached); } // Propagate information about which lines changed to the lint engine. // This is used so that the lint engine can drop warning messages // concerning lines that weren't in the change. $engine->setPaths($paths); if (!$this->shouldLintAll) { foreach ($paths as $path) { // Note that getChangedLines() returns null to indicate that a file // is binary or a directory (i.e., changed lines are not relevant). $engine->setPathChangedLines( $path, $this->getChangedLines($path, 'new')); } } // Enable possible async linting only for 'arc diff' not 'arc lint' if ($this->getParentWorkflow()) { $engine->setEnableAsyncLint(true); } else { $engine->setEnableAsyncLint(false); } if ($this->getArgument('only-new')) { $conduit = $this->getConduit(); $api = $this->getRepositoryAPI(); if ($rev) { $api->setBaseCommit($rev); } $svn_root = id(new PhutilURI($api->getSourceControlPath()))->getPath(); $all_paths = array(); foreach ($paths as $path) { $path = str_replace(DIRECTORY_SEPARATOR, '/', $path); $full_paths = array($path); $change = $this->getChange($path); $type = $change->getType(); if (ArcanistDiffChangeType::isOldLocationChangeType($type)) { $full_paths = $change->getAwayPaths(); } else if (ArcanistDiffChangeType::isNewLocationChangeType($type)) { continue; } else if (ArcanistDiffChangeType::isDeleteChangeType($type)) { continue; } foreach ($full_paths as $full_path) { $all_paths[$svn_root.'/'.$full_path] = $path; } } $lint_future = $conduit->callMethod('diffusion.getlintmessages', array( 'arcanistProject' => $this->getWorkingCopy()->getProjectID(), 'branch' => '', // TODO: Tracking branch. 'commit' => $api->getBaseCommit(), 'files' => array_keys($all_paths), )); } $failed = null; try { $engine->run(); } catch (Exception $ex) { $failed = $ex; } $results = $engine->getResults(); if ($this->getArgument('only-new')) { $total = 0; foreach ($results as $result) { $total += count($result->getMessages()); } // Don't wait for response with default value of --only-new. $timeout = null; if ($this->getArgument('only-new') === null || !$total) { $timeout = 0; } $raw_messages = $this->resolveCall($lint_future, $timeout); if ($raw_messages && $total) { $old_messages = array(); $line_maps = array(); foreach ($raw_messages as $message) { $path = $all_paths[$message['path']]; $line = $message['line']; $code = $message['code']; if (!isset($line_maps[$path])) { $line_maps[$path] = $this->getChange($path)->buildLineMap(); } $new_lines = idx($line_maps[$path], $line); if (!$new_lines) { // Unmodified lines after last hunk. $last_old = ($line_maps[$path] ? last_key($line_maps[$path]) : 0); $news = array_filter($line_maps[$path]); $last_new = ($news ? last(end($news)) : 0); $new_lines = array($line + $last_new - $last_old); } $error = array($code => array(true)); foreach ($new_lines as $new) { if (isset($old_messages[$path][$new])) { $old_messages[$path][$new][$code][] = true; break; } $old_messages[$path][$new] = &$error; } unset($error); } foreach ($results as $result) { foreach ($result->getMessages() as $message) { $path = str_replace(DIRECTORY_SEPARATOR, '/', $message->getPath()); $line = $message->getLine(); $code = $message->getCode(); if (!empty($old_messages[$path][$line][$code])) { $message->setObsolete(true); array_pop($old_messages[$path][$line][$code]); } } $result->sortAndFilterMessages(); } } } // It'd be nice to just return a single result from the run method above // which contains both the lint messages and the postponed linters. // However, to maintain compatibility with existing lint subclasses, use // a separate method call to grab the postponed linters. $this->postponedLinters = $engine->getPostponedLinters(); if ($this->getArgument('never-apply-patches')) { $apply_patches = false; } else { $apply_patches = true; } if ($this->getArgument('apply-patches')) { $prompt_patches = false; } else { $prompt_patches = true; } if ($this->getArgument('amend-all')) { $this->shouldAmendChanges = true; $this->shouldAmendWithoutPrompt = true; } if ($this->getArgument('amend-autofixes')) { $prompt_autofix_patches = false; $this->shouldAmendChanges = true; $this->shouldAmendAutofixesWithoutPrompt = true; } else { $prompt_autofix_patches = true; } $repository_api = $this->getRepositoryAPI(); if ($this->shouldAmendChanges) { $this->shouldAmendChanges = $repository_api->supportsAmend() && !$this->isHistoryImmutable(); } $wrote_to_disk = false; switch ($this->getArgument('output')) { case 'json': $renderer = new ArcanistJSONLintRenderer(); $prompt_patches = false; $apply_patches = $this->getArgument('apply-patches'); break; case 'summary': $renderer = new ArcanistSummaryLintRenderer(); break; case 'none': $prompt_patches = false; $apply_patches = $this->getArgument('apply-patches'); $renderer = new ArcanistNoneLintRenderer(); break; case 'compiler': $renderer = new ArcanistCompilerLikeLintRenderer(); $prompt_patches = false; $apply_patches = $this->getArgument('apply-patches'); break; case 'xml': $renderer = new ArcanistCheckstyleXMLLintRenderer(); $prompt_patches = false; $apply_patches = $this->getArgument('apply-patches'); break; default: $renderer = new ArcanistConsoleLintRenderer(); $renderer->setShowAutofixPatches($prompt_autofix_patches); break; } $all_autofix = true; $console->writeOut('%s', $renderer->renderPreamble()); foreach ($results as $result) { $result_all_autofix = $result->isAllAutofix(); if (!$result->getMessages() && !$result_all_autofix) { continue; } if (!$result_all_autofix) { $all_autofix = false; } $lint_result = $renderer->renderLintResult($result); if ($lint_result) { $console->writeOut('%s', $lint_result); } if ($apply_patches && $result->isPatchable()) { $patcher = ArcanistLintPatcher::newFromArcanistLintResult($result); $old_file = $result->getFilePathOnDisk(); if ($prompt_patches && !($result_all_autofix && !$prompt_autofix_patches)) { if (!Filesystem::pathExists($old_file)) { $old_file = '/dev/null'; } $new_file = new TempFile(); $new = $patcher->getModifiedFileContent(); Filesystem::writeFile($new_file, $new); // TODO: Improve the behavior here, make it more like // difference_render(). list(, $stdout, $stderr) = exec_manual('diff -u %s %s', $old_file, $new_file); $console->writeOut('%s', $stdout); $console->writeErr('%s', $stderr); $prompt = phutil_console_format( 'Apply this patch to __%s__?', $result->getPath()); if (!$console->confirm($prompt, $default_no = false)) { continue; } } $patcher->writePatchToDisk(); $wrote_to_disk = true; $file_hashes[$old_file] = md5_file($old_file); } } $console->writeOut('%s', $renderer->renderPostamble()); if ($wrote_to_disk && $this->shouldAmendChanges) { if ($this->shouldAmendWithoutPrompt || ($this->shouldAmendAutofixesWithoutPrompt && $all_autofix)) { $console->writeOut( "** LINT NOTICE ** Automatically amending HEAD ". "with lint patches.\n"); $amend = true; } else { $amend = $console->confirm('Amend HEAD with lint patches?'); } if ($amend) { if ($repository_api instanceof ArcanistGitAPI) { // Add the changes to the index before amending $repository_api->execxLocal('add -u'); } $repository_api->amendCommit(); } else { throw new ArcanistUsageException( 'Sort out the lint changes that were applied to the working '. 'copy and relint.'); } } if ($this->getArgument('output') == 'json') { // NOTE: Required by save_lint.php in Phabricator. return 0; } if ($failed) { if ($failed instanceof ArcanistNoEffectException) { if ($renderer instanceof ArcanistNoneLintRenderer) { return 0; } } throw $failed; } $unresolved = array(); $has_warnings = false; $has_errors = false; foreach ($results as $result) { foreach ($result->getMessages() as $message) { if (!$message->isPatchApplied()) { if ($message->isError()) { $has_errors = true; } else if ($message->isWarning()) { $has_warnings = true; } $unresolved[] = $message; } } } $this->unresolvedMessages = $unresolved; $cache = $this->readScratchJSONFile('lint-cache.json'); $cached = idx($cache, $this->getCacheKey(), array()); if ($cached || $use_cache) { $stopped = $engine->getStoppedPaths(); foreach ($results as $result) { $path = $result->getPath(); if (!$use_cache) { unset($cached[$path]); continue; } $abs_path = $engine->getFilePathOnDisk($path); if (!Filesystem::pathExists($abs_path)) { continue; } $version = $result->getCacheVersion(); $cached_path = array(); if (isset($stopped[$path])) { $cached_path['stopped'] = $stopped[$path]; } $cached_path['repository_version'] = $this->getRepositoryVersion(); foreach ($result->getMessages() as $message) { $granularity = $message->getGranularity(); if ($granularity == ArcanistLinter::GRANULARITY_GLOBAL) { continue; } if (!$message->isPatchApplied()) { $cached_path[] = $message->toDictionary(); } } $hash = idx($file_hashes, $abs_path); if (!$hash) { $hash = md5_file($abs_path); } $cached[$path] = array($hash => array($version => $cached_path)); } $cache[$this->getCacheKey()] = $cached; // TODO: Garbage collection. $this->writeScratchJSONFile('lint-cache.json', $cache); } // Take the most severe lint message severity and use that // as the result code. if ($has_errors) { $result_code = self::RESULT_ERRORS; } else if ($has_warnings) { $result_code = self::RESULT_WARNINGS; } else if (!empty($this->postponedLinters)) { $result_code = self::RESULT_POSTPONED; } else { $result_code = self::RESULT_OKAY; } if (!$this->getParentWorkflow()) { if ($result_code == self::RESULT_OKAY) { $console->writeOut('%s', $renderer->renderOkayResult()); } } return $result_code; } public function getUnresolvedMessages() { return $this->unresolvedMessages; } public function getPostponedLinters() { return $this->postponedLinters; } } diff --git a/src/workflow/ArcanistPatchWorkflow.php b/src/workflow/ArcanistPatchWorkflow.php index 1e7d21bb..167704de 100644 --- a/src/workflow/ArcanistPatchWorkflow.php +++ b/src/workflow/ArcanistPatchWorkflow.php @@ -1,1079 +1,1081 @@ array( 'param' => 'revision_id', 'paramtype' => 'complete', 'help' => "Apply changes from a Differential revision, using the most recent ". "diff that has been attached to it. You can run 'arc patch D12345' ". "as a shorthand.", ), 'diff' => array( 'param' => 'diff_id', 'help' => 'Apply changes from a Differential diff. Normally you want to use '. '--revision to get the most recent changes, but you can '. 'specifically apply an out-of-date diff or a diff which was never '. 'attached to a revision by using this flag.', ), 'arcbundle' => array( 'param' => 'bundlefile', 'paramtype' => 'file', 'help' => "Apply changes from an arc bundle generated with 'arc export'.", ), 'patch' => array( 'param' => 'patchfile', 'paramtype' => 'file', 'help' => 'Apply changes from a git patchfile or unified patchfile.', ), 'encoding' => array( 'param' => 'encoding', 'help' => 'Attempt to convert non UTF-8 patch into specified encoding.', ), 'update' => array( 'supports' => array('git', 'svn', 'hg'), 'help' => 'Update the local working copy before applying the patch.', 'conflicts' => array( 'nobranch' => true, 'bookmark' => true, ), ), 'nocommit' => array( 'supports' => array('git', 'hg'), 'help' => 'Normally under git/hg, if the patch is successful, the changes '. 'are committed to the working copy. This flag prevents the commit.', ), 'skip-dependencies' => array( 'supports' => array('git', 'hg'), 'help' => 'Normally, if a patch has dependencies that are not present in the '. 'working copy, arc tries to apply them as well. This flag prevents '. 'such work.', ), 'nobranch' => array( 'supports' => array('git', 'hg'), 'help' => 'Normally, a new branch (git) or bookmark (hg) is created and then '. 'the patch is applied and committed in the new branch/bookmark. '. 'This flag cherry-picks the resultant commit onto the original '. 'branch and deletes the temporary branch.', 'conflicts' => array( 'update' => true, ), ), 'force' => array( 'help' => 'Do not run any sanity checks.', ), '*' => 'name', ); } protected function didParseArguments() { $source = null; $requested = 0; if ($this->getArgument('revision')) { $source = self::SOURCE_REVISION; $requested++; } if ($this->getArgument('diff')) { $source = self::SOURCE_DIFF; $requested++; } if ($this->getArgument('arcbundle')) { $source = self::SOURCE_BUNDLE; $requested++; } if ($this->getArgument('patch')) { $source = self::SOURCE_PATCH; $requested++; } $use_revision_id = null; if ($this->getArgument('name')) { $namev = $this->getArgument('name'); if (count($namev) > 1) { throw new ArcanistUsageException('Specify at most one revision name.'); } $source = self::SOURCE_REVISION; $requested++; $use_revision_id = $this->normalizeRevisionID(head($namev)); } if ($requested === 0) { throw new ArcanistUsageException( "Specify one of 'D12345', '--revision ' (to select the ". "current changes attached to a Differential revision), ". "'--diff ' (to select a specific, out-of-date diff or a ". "diff which is not attached to a revision), '--arcbundle ' ". "or '--patch ' to choose a patch source."); } else if ($requested > 1) { throw new ArcanistUsageException( "Options 'D12345', '--revision', '--diff', '--arcbundle' and ". "'--patch' are not compatible. Choose exactly one patch source."); } $this->source = $source; $this->sourceParam = nonempty( $use_revision_id, $this->getArgument($source)); } public function requiresConduit() { return ($this->getSource() != self::SOURCE_PATCH); } public function requiresRepositoryAPI() { return true; } public function requiresWorkingCopy() { return true; } private function getSource() { return $this->source; } private function getSourceParam() { return $this->sourceParam; } private function shouldCommit() { return !$this->getArgument('nocommit', false); } private function canBranch() { $repository_api = $this->getRepositoryAPI(); return ($repository_api instanceof ArcanistGitAPI) || ($repository_api instanceof ArcanistMercurialAPI); } private function shouldBranch() { $no_branch = $this->getArgument('nobranch', false); if ($no_branch) { return false; } return true; } private function getBranchName(ArcanistBundle $bundle) { $branch_name = null; $repository_api = $this->getRepositoryAPI(); $revision_id = $bundle->getRevisionID(); $base_name = 'arcpatch'; if ($revision_id) { $base_name .= "-D{$revision_id}"; } $suffixes = array(null, '_1', '_2', '_3'); foreach ($suffixes as $suffix) { $proposed_name = $base_name.$suffix; list($err) = $repository_api->execManualLocal( 'rev-parse --verify %s', $proposed_name); // no error means git rev-parse found a branch if (!$err) { echo phutil_console_format( "Branch name {$proposed_name} already exists; trying a new name.\n"); continue; } else { $branch_name = $proposed_name; break; } } if (!$branch_name) { throw new Exception( 'Arc was unable to automagically make a name for this patch. '. 'Please clean up your working copy and try again.' ); } return $branch_name; } private function getBookmarkName(ArcanistBundle $bundle) { $bookmark_name = null; $repository_api = $this->getRepositoryAPI(); $revision_id = $bundle->getRevisionID(); $base_name = 'arcpatch'; if ($revision_id) { $base_name .= "-D{$revision_id}"; } $suffixes = array(null, '-1', '-2', '-3'); foreach ($suffixes as $suffix) { $proposed_name = $base_name.$suffix; list($err) = $repository_api->execManualLocal( 'log -r %s', hgsprintf('%s', $proposed_name)); // no error means hg log found a bookmark if (!$err) { echo phutil_console_format( "Bookmark name %s already exists; trying a new name.\n", $proposed_name); continue; } else { $bookmark_name = $proposed_name; break; } } if (!$bookmark_name) { throw new Exception( 'Arc was unable to automagically make a name for this patch. '. 'Please clean up your working copy and try again.' ); } return $bookmark_name; } private function createBranch(ArcanistBundle $bundle, $has_base_revision) { $repository_api = $this->getRepositoryAPI(); if ($repository_api instanceof ArcanistGitAPI) { $branch_name = $this->getBranchName($bundle); $base_revision = $bundle->getBaseRevision(); if ($base_revision && $has_base_revision) { $base_revision = $repository_api->getCanonicalRevisionName( $base_revision); $repository_api->execxLocal( 'checkout -b %s %s', $branch_name, $base_revision); } else { $repository_api->execxLocal( 'checkout -b %s', $branch_name); } echo phutil_console_format( "Created and checked out branch %s.\n", $branch_name); } else if ($repository_api instanceof ArcanistMercurialAPI) { $branch_name = $this->getBookmarkName($bundle); $base_revision = $bundle->getBaseRevision(); if ($base_revision && $has_base_revision) { $base_revision = $repository_api->getCanonicalRevisionName( $base_revision); echo "Updating to the revision's base commit\n"; $repository_api->execPassthru( 'update %s', $base_revision); } $repository_api->execxLocal('bookmark %s', $branch_name); echo phutil_console_format( "Created and checked out bookmark %s.\n", $branch_name); } return $branch_name; } private function shouldApplyDependencies() { return !$this->getArgument('skip-dependencies', false); } private function shouldUpdateWorkingCopy() { return $this->getArgument('update', false); } private function updateWorkingCopy() { echo "Updating working copy...\n"; $this->getRepositoryAPI()->updateWorkingCopy(); echo "Done.\n"; } public function run() { $source = $this->getSource(); $param = $this->getSourceParam(); try { switch ($source) { case self::SOURCE_PATCH: if ($param == '-') { $patch = @file_get_contents('php://stdin'); if (!strlen($patch)) { throw new ArcanistUsageException( 'Failed to read patch from stdin!'); } } else { $patch = Filesystem::readFile($param); } $bundle = ArcanistBundle::newFromDiff($patch); break; case self::SOURCE_BUNDLE: $path = $this->getArgument('arcbundle'); $bundle = ArcanistBundle::newFromArcBundle($path); break; case self::SOURCE_REVISION: $bundle = $this->loadRevisionBundleFromConduit( $this->getConduit(), $param); break; case self::SOURCE_DIFF: $bundle = $this->loadDiffBundleFromConduit( $this->getConduit(), $param); break; } } catch (ConduitClientException $ex) { if ($ex->getErrorCode() == 'ERR-INVALID-SESSION') { // Phabricator is not configured to allow anonymous access to // Differential. $this->authenticateConduit(); return $this->run(); } else { throw $ex; } } $try_encoding = nonempty($this->getArgument('encoding'), null); if (!$try_encoding) { if ($this->requiresConduit()) { try { $try_encoding = $this->getRepositoryEncoding(); } catch (ConduitClientException $e) { $try_encoding = null; } } } if ($try_encoding) { $bundle->setEncoding($try_encoding); } $sanity_check = !$this->getArgument('force', false); // we should update the working copy before we do ANYTHING else to // the working copy if ($this->shouldUpdateWorkingCopy()) { $this->updateWorkingCopy(); } if ($sanity_check) { $this->requireCleanWorkingCopy(); } $repository_api = $this->getRepositoryAPI(); $has_base_revision = $repository_api->hasLocalCommit( $bundle->getBaseRevision()); if ($this->canBranch() && ($this->shouldBranch() || ($this->shouldCommit() && $has_base_revision))) { if ($repository_api instanceof ArcanistGitAPI) { $original_branch = $repository_api->getBranchName(); } else if ($repository_api instanceof ArcanistMercurialAPI) { $original_branch = $repository_api->getActiveBookmark(); } // If we weren't on a branch, then record the ref we'll return to // instead. if ($original_branch === null) { if ($repository_api instanceof ArcanistGitAPI) { $original_branch = $repository_api->getCanonicalRevisionName('HEAD'); } else if ($repository_api instanceof ArcanistMercurialAPI) { $original_branch = $repository_api->getCanonicalRevisionName('.'); } } $new_branch = $this->createBranch($bundle, $has_base_revision); } if (!$has_base_revision && $this->shouldApplyDependencies()) { $this->applyDependencies($bundle); } if ($sanity_check) { $this->sanityCheck($bundle); } if ($repository_api instanceof ArcanistSubversionAPI) { $patch_err = 0; $copies = array(); $deletes = array(); $patches = array(); $propset = array(); $adds = array(); $symlinks = array(); $changes = $bundle->getChanges(); foreach ($changes as $change) { $type = $change->getType(); $should_patch = true; $filetype = $change->getFileType(); switch ($filetype) { case ArcanistDiffChangeType::FILE_SYMLINK: $should_patch = false; $symlinks[] = $change; break; } switch ($type) { case ArcanistDiffChangeType::TYPE_MOVE_AWAY: case ArcanistDiffChangeType::TYPE_MULTICOPY: case ArcanistDiffChangeType::TYPE_DELETE: $path = $change->getCurrentPath(); $fpath = $repository_api->getPath($path); if (!@file_exists($fpath)) { $ok = phutil_console_confirm( "Patch deletes file '{$path}', but the file does not exist in ". "the working copy. Continue anyway?"); if (!$ok) { throw new ArcanistUserAbortException(); } } else { $deletes[] = $change->getCurrentPath(); } $should_patch = false; break; case ArcanistDiffChangeType::TYPE_COPY_HERE: case ArcanistDiffChangeType::TYPE_MOVE_HERE: $path = $change->getOldPath(); $fpath = $repository_api->getPath($path); if (!@file_exists($fpath)) { $cpath = $change->getCurrentPath(); if ($type == ArcanistDiffChangeType::TYPE_COPY_HERE) { $verbs = 'copies'; } else { $verbs = 'moves'; } $ok = phutil_console_confirm( "Patch {$verbs} '{$path}' to '{$cpath}', but source path ". "does not exist in the working copy. Continue anyway?"); if (!$ok) { throw new ArcanistUserAbortException(); } } else { $copies[] = array( $change->getOldPath(), - $change->getCurrentPath()); + $change->getCurrentPath(), + ); } break; case ArcanistDiffChangeType::TYPE_ADD: $adds[] = $change->getCurrentPath(); break; } if ($should_patch) { $cbundle = ArcanistBundle::newFromChanges(array($change)); $patches[$change->getCurrentPath()] = $cbundle->toUnifiedDiff(); $prop_old = $change->getOldProperties(); $prop_new = $change->getNewProperties(); $props = $prop_old + $prop_new; foreach ($props as $key => $ignored) { if (idx($prop_old, $key) !== idx($prop_new, $key)) { $propset[$change->getCurrentPath()][$key] = idx($prop_new, $key); } } } } // Before we start doing anything, create all the directories we're going // to add files to if they don't already exist. foreach ($copies as $copy) { list($src, $dst) = $copy; $this->createParentDirectoryOf($dst); } foreach ($patches as $path => $patch) { $this->createParentDirectoryOf($path); } foreach ($adds as $add) { $this->createParentDirectoryOf($add); } // TODO: The SVN patch workflow likely does not work on windows because // of the (cd ...) stuff. foreach ($copies as $copy) { list($src, $dst) = $copy; passthru( csprintf( '(cd %s; svn cp %s %s)', $repository_api->getPath(), ArcanistSubversionAPI::escapeFileNameForSVN($src), ArcanistSubversionAPI::escapeFileNameForSVN($dst))); } foreach ($deletes as $delete) { passthru( csprintf( '(cd %s; svn rm %s)', $repository_api->getPath(), ArcanistSubversionAPI::escapeFileNameForSVN($delete))); } foreach ($symlinks as $symlink) { $link_target = $symlink->getSymlinkTarget(); $link_path = $symlink->getCurrentPath(); switch ($symlink->getType()) { case ArcanistDiffChangeType::TYPE_ADD: case ArcanistDiffChangeType::TYPE_CHANGE: case ArcanistDiffChangeType::TYPE_MOVE_HERE: case ArcanistDiffChangeType::TYPE_COPY_HERE: execx( '(cd %s && ln -sf %s %s)', $repository_api->getPath(), $link_target, $link_path); break; } } foreach ($patches as $path => $patch) { $err = null; if ($patch) { $tmp = new TempFile(); Filesystem::writeFile($tmp, $patch); passthru( csprintf( '(cd %s; patch -p0 < %s)', $repository_api->getPath(), $tmp), $err); } else { passthru( csprintf( '(cd %s; touch %s)', $repository_api->getPath(), $path), $err); } if ($err) { $patch_err = max($patch_err, $err); } } foreach ($adds as $add) { passthru( csprintf( '(cd %s; svn add %s)', $repository_api->getPath(), ArcanistSubversionAPI::escapeFileNameForSVN($add))); } foreach ($propset as $path => $changes) { foreach ($changes as $prop => $value) { if ($prop == 'unix:filemode') { // Setting this property also changes the file mode. $prop = 'svn:executable'; $value = (octdec($value) & 0111 ? 'on' : null); } if ($value === null) { passthru( csprintf( '(cd %s; svn propdel %s %s)', $repository_api->getPath(), $prop, ArcanistSubversionAPI::escapeFileNameForSVN($path))); } else { passthru( csprintf( '(cd %s; svn propset %s %s %s)', $repository_api->getPath(), $prop, $value, ArcanistSubversionAPI::escapeFileNameForSVN($path))); } } } if ($patch_err == 0) { echo phutil_console_format( "** OKAY ** Successfully applied patch ". "to the working copy.\n"); } else { echo phutil_console_format( "\n\n** WARNING ** Some hunks could not be applied ". "cleanly by the unix 'patch' utility. Your working copy may be ". "different from the revision's base, or you may be in the wrong ". "subdirectory. You can export the raw patch file using ". "'arc export --unified', and then try to apply it by fiddling with ". "options to 'patch' (particularly, -p), or manually. The output ". "above, from 'patch', may be helpful in figuring out what went ". "wrong.\n"); } return $patch_err; } else if ($repository_api instanceof ArcanistGitAPI) { $patchfile = new TempFile(); Filesystem::writeFile($patchfile, $bundle->toGitPatch()); $passthru = new PhutilExecPassthru( 'git apply --index --reject -- %s', $patchfile); $passthru->setCWD($repository_api->getPath()); $err = $passthru->execute(); if ($err) { echo phutil_console_format( "\n** Patch Failed! **\n"); // NOTE: Git patches may fail if they change the case of a filename // (for instance, from 'example.c' to 'Example.c'). As of now, Git // can not apply these patches on case-insensitive filesystems and // there is no way to build a patch which works. throw new ArcanistUsageException('Unable to apply patch!'); } // in case there were any submodule changes involved $repository_api->execpassthru( 'submodule update --init --recursive'); if ($this->shouldCommit()) { if ($bundle->getFullAuthor()) { $author_cmd = csprintf('--author=%s', $bundle->getFullAuthor()); } else { $author_cmd = ''; } $commit_message = $this->getCommitMessage($bundle); $future = $repository_api->execFutureLocal( 'commit -a %C -F - --no-verify', $author_cmd); $future->write($commit_message); $future->resolvex(); $verb = 'committed'; } else { $verb = 'applied'; } if ($this->canBranch() && !$this->shouldBranch() && $this->shouldCommit() && $has_base_revision) { $repository_api->execxLocal('checkout %s', $original_branch); $ex = null; try { $repository_api->execxLocal('cherry-pick %s', $new_branch); } catch (Exception $ex) { // do nothing } $repository_api->execxLocal('branch -D %s', $new_branch); if ($ex) { echo phutil_console_format( "\n** Cherry Pick Failed!**\n"); throw $ex; } } echo phutil_console_format( "** OKAY ** Successfully {$verb} patch.\n"); } else if ($repository_api instanceof ArcanistMercurialAPI) { $future = $repository_api->execFutureLocal( 'import --no-commit -'); $future->write($bundle->toGitPatch()); try { $future->resolvex(); } catch (CommandException $ex) { echo phutil_console_format( "\n** Patch Failed! **\n"); $stderr = $ex->getStdErr(); if (preg_match('/case-folding collision/', $stderr)) { echo phutil_console_wrap( phutil_console_format( "\n** WARNING ** This patch may have failed ". "because it attempts to change the case of a filename (for ". "instance, from 'example.c' to 'Example.c'). Mercurial cannot ". "apply patches like this on case-insensitive filesystems. You ". "must apply this patch manually.\n")); } throw $ex; } if ($this->shouldCommit()) { $author = coalesce($bundle->getFullAuthor(), $bundle->getAuthorName()); if ($author !== null) { $author_cmd = csprintf('-u %s', $author); } else { $author_cmd = ''; } $commit_message = $this->getCommitMessage($bundle); $future = $repository_api->execFutureLocal( 'commit %C -l -', $author_cmd); $future->write($commit_message); $future->resolvex(); if (!$this->shouldBranch() && $has_base_revision) { $original_rev = $repository_api->getCanonicalRevisionName( $original_branch); $current_parent = $repository_api->getCanonicalRevisionName( hgsprintf('%s^', $new_branch)); $err = 0; if ($original_rev != $current_parent) { list($err) = $repository_api->execManualLocal( 'rebase --dest %s --rev %s', hgsprintf('%s', $original_branch), hgsprintf('%s', $new_branch)); } $repository_api->execxLocal('bookmark --delete %s', $new_branch); if ($err) { $repository_api->execManualLocal('rebase --abort'); throw new ArcanistUsageException(phutil_console_format( "\n** Rebase onto $original_branch failed!**\n")); } } $verb = 'committed'; } else { $verb = 'applied'; } echo phutil_console_format( "** OKAY ** Successfully {$verb} patch.\n"); } else { throw new Exception('Unknown version control system.'); } return 0; } private function getCommitMessage(ArcanistBundle $bundle) { $revision_id = $bundle->getRevisionID(); $commit_message = null; $prompt_message = null; // if we have a revision id the commit message is in differential // TODO: See T848 for the authenticated stuff. if ($revision_id && $this->isConduitAuthenticated()) { $conduit = $this->getConduit(); $commit_message = $conduit->callMethodSynchronous( 'differential.getcommitmessage', array( 'revision_id' => $revision_id, )); $prompt_message = " Note arcanist failed to load the commit message ". "from differential for revision D{$revision_id}."; } // no revision id or failed to fetch commit message so get it from the // user on the command line if (!$commit_message) { $template = "\n\n". "# Enter a commit message for this patch. If you just want to apply ". "the patch to the working copy without committing, re-run arc patch ". "with the --nocommit flag.". $prompt_message. "\n"; $commit_message = $this->newInteractiveEditor($template) ->setName('arcanist-patch-commit-message') ->editInteractively(); $commit_message = ArcanistCommentRemover::removeComments($commit_message); if (!strlen(trim($commit_message))) { throw new ArcanistUserAbortException(); } } return $commit_message; } public function getShellCompletions(array $argv) { // TODO: Pull open diffs from 'arc list'? return array('ARGUMENT'); } private function applyDependencies(ArcanistBundle $bundle) { // check for (and automagically apply on the user's be-hest) any revisions // this patch depends on $graph = $this->buildDependencyGraph($bundle); if ($graph) { $start_phid = $graph->getStartPHID(); $cycle_phids = $graph->detectCycles($start_phid); if ($cycle_phids) { $phids = array_keys($graph->getNodes()); $issue = 'The dependencies for this patch have a cycle. Applying them '. 'is not guaranteed to work. Continue anyway?'; $okay = phutil_console_confirm($issue, true); } else { $phids = $graph->getTopographicallySortedNodes(); $phids = array_reverse($phids); $okay = true; } if (!$okay) { return; } $dep_on_revs = $this->getConduit()->callMethodSynchronous( 'differential.query', array( 'phids' => $phids, - 'arcanistProjects' => array($bundle->getProjectID()) + 'arcanistProjects' => array($bundle->getProjectID()), )); $revs = array(); foreach ($dep_on_revs as $dep_on_rev) { $revs[$dep_on_rev['phid']] = 'D'.$dep_on_rev['id']; } // order them in case we got a topological sort earlier $revs = array_select_keys($revs, $phids); if (!empty($revs)) { $base_args = array( '--force', '--skip-dependencies', - '--nobranch'); + '--nobranch', + ); if (!$this->shouldCommit()) { $base_args[] = '--nocommit'; } foreach ($revs as $phid => $diff_id) { // we'll apply this, the actual patch, later // this should be the last in the list if ($phid == $start_phid) { continue; } $args = $base_args; $args[] = $diff_id; $apply_workflow = $this->buildChildWorkflow( 'patch', $args); $apply_workflow->run(); } } } } /** * Do the best we can to prevent PEBKAC and id10t issues. */ private function sanityCheck(ArcanistBundle $bundle) { $repository_api = $this->getRepositoryAPI(); // Check to see if the bundle's project id matches the working copy // project id $bundle_project_id = $bundle->getProjectID(); $working_copy_project_id = $this->getWorkingCopy()->getProjectID(); if (empty($bundle_project_id)) { // this means $source is SOURCE_PATCH || SOURCE_BUNDLE w/ $version = 0 // they don't come with a project id so just do nothing } else if ($bundle_project_id != $working_copy_project_id) { if ($working_copy_project_id) { $issue = "This patch is for the '{$bundle_project_id}' project, but the ". "working copy belongs to the '{$working_copy_project_id}' project."; } else { $issue = "This patch is for the '{$bundle_project_id}' project, but the ". "working copy does not have an '.arcconfig' file to identify which ". "project it belongs to."; } $ok = phutil_console_confirm( "{$issue} Still try to apply the patch?", $default_no = false); if (!$ok) { throw new ArcanistUserAbortException(); } } // Check to see if the bundle's base revision matches the working copy // base revision if ($repository_api->supportsLocalCommits()) { $bundle_base_rev = $bundle->getBaseRevision(); if (empty($bundle_base_rev)) { // this means $source is SOURCE_PATCH || SOURCE_BUNDLE w/ $version < 2 // they don't have a base rev so just do nothing $commit_exists = true; } else { $commit_exists = $repository_api->hasLocalCommit($bundle_base_rev); } if (!$commit_exists) { // we have a problem...! lots of work because we need to ask // differential for revision information for these base revisions // to improve our error message. $bundle_base_rev_str = null; $source_base_rev = $repository_api->getWorkingCopyRevision(); $source_base_rev_str = null; if ($repository_api instanceof ArcanistGitAPI) { $hash_type = ArcanistDifferentialRevisionHash::HASH_GIT_COMMIT; } else if ($repository_api instanceof ArcanistMercurialAPI) { $hash_type = ArcanistDifferentialRevisionHash::HASH_MERCURIAL_COMMIT; } else { $hash_type = null; } if ($hash_type) { // 2 round trips because even though we could send off one query // we wouldn't be able to tell which revisions were for which hash $hash = array($hash_type, $bundle_base_rev); $bundle_revision = $this->loadRevisionFromHash($hash); $hash = array($hash_type, $source_base_rev); $source_revision = $this->loadRevisionFromHash($hash); if ($bundle_revision) { $bundle_base_rev_str = $bundle_base_rev. ' \ D'.$bundle_revision['id']; } if ($source_revision) { $source_base_rev_str = $source_base_rev. ' \ D'.$source_revision['id']; } } $bundle_base_rev_str = nonempty( $bundle_base_rev_str, $bundle_base_rev); $source_base_rev_str = nonempty( $source_base_rev_str, $source_base_rev); $ok = phutil_console_confirm( "This diff is against commit {$bundle_base_rev_str}, but the ". "commit is nowhere in the working copy. Try to apply it against ". "the current working copy state? ({$source_base_rev_str})", $default_no = false); if (!$ok) { throw new ArcanistUserAbortException(); } } } } /** * Create parent directories one at a time, since we need to "svn add" each * one. (Technically we could "svn add" just the topmost new directory.) */ private function createParentDirectoryOf($path) { $repository_api = $this->getRepositoryAPI(); $dir = dirname($path); if (Filesystem::pathExists($dir)) { return; } else { // Make sure the parent directory exists before we make this one. $this->createParentDirectoryOf($dir); execx( '(cd %s && mkdir %s)', $repository_api->getPath(), $dir); passthru( csprintf( '(cd %s && svn add %s)', $repository_api->getPath(), $dir)); } } private function loadRevisionFromHash($hash) { // TODO -- de-hack this as permissions become more clear with things // like T848 (add scope to OAuth) if (!$this->isConduitAuthenticated()) { return null; } $conduit = $this->getConduit(); $revisions = $conduit->callMethodSynchronous( 'differential.query', array( 'commitHashes' => array($hash), )); // grab the latest closed revision only $found_revision = null; $revisions = isort($revisions, 'dateModified'); foreach ($revisions as $revision) { if ($revision['status'] == ArcanistDifferentialRevisionStatus::CLOSED) { $found_revision = $revision; } } return $found_revision; } private function buildDependencyGraph(ArcanistBundle $bundle) { $graph = null; if ($this->getRepositoryAPI() instanceof ArcanistSubversionAPI) { return $graph; } $revision_id = $bundle->getRevisionID(); if ($revision_id) { $revisions = $this->getConduit()->callMethodSynchronous( 'differential.query', array( 'ids' => array($revision_id), )); if ($revisions) { $revision = head($revisions); $rev_auxiliary = idx($revision, 'auxiliary', array()); $phids = idx($rev_auxiliary, 'phabricator:depends-on', array()); if ($phids) { $revision_phid = $revision['phid']; $graph = id(new ArcanistDifferentialDependencyGraph()) ->setConduit($this->getConduit()) ->setRepositoryAPI($this->getRepositoryAPI()) ->setStartPHID($revision_phid) ->addNodes(array($revision_phid => $phids)) ->loadGraph(); } } } return $graph; } } diff --git a/src/workflow/ArcanistStartWorkflow.php b/src/workflow/ArcanistStartWorkflow.php index 389023e5..483f334c 100644 --- a/src/workflow/ArcanistStartWorkflow.php +++ b/src/workflow/ArcanistStartWorkflow.php @@ -1,92 +1,92 @@ 'name', ); } public function run() { $conduit = $this->getConduit(); $started_phids = array(); $short_name = $this->getArgument('name'); foreach ($short_name as $object_name) { $object_lookup = $conduit->callMethodSynchronous( 'phid.lookup', array( 'names' => array($object_name), )); if (!array_key_exists($object_name, $object_lookup)) { echo "No such object '".$object_name."' found.\n"; return 1; } $object_phid = $object_lookup[$object_name]['phid']; $started_phids[] = $conduit->callMethodSynchronous( 'phrequent.push', array( - 'objectPHID' => $object_phid + 'objectPHID' => $object_phid, )); } $phid_query = $conduit->callMethodSynchronous( 'phid.query', array( 'phids' => $started_phids, )); $name = ''; foreach ($phid_query as $ref) { if ($name === '') { $name = $ref['fullName']; } else { $name .= ', '.$ref['fullName']; } } echo phutil_console_format( "Started: %s\n\n", $name); $this->printCurrentTracking(true); } } diff --git a/src/workflow/ArcanistUnitWorkflow.php b/src/workflow/ArcanistUnitWorkflow.php index f8d7a15b..f302ba75 100644 --- a/src/workflow/ArcanistUnitWorkflow.php +++ b/src/workflow/ArcanistUnitWorkflow.php @@ -1,356 +1,356 @@ array( 'param' => 'revision', 'help' => 'Run unit tests covering changes since a specific revision.', 'supports' => array( 'git', 'hg', ), 'nosupport' => array( 'svn' => 'Arc unit does not currently support --rev in SVN.', ), ), 'engine' => array( 'param' => 'classname', 'help' => - 'Override configured unit engine for this project.' + 'Override configured unit engine for this project.', ), 'coverage' => array( 'help' => 'Always enable coverage information.', 'conflicts' => array( 'no-coverage' => null, ), ), 'no-coverage' => array( 'help' => 'Always disable coverage information.', ), 'detailed-coverage' => array( 'help' => 'Show a detailed coverage report on the CLI. Implies '. '--coverage.', ), 'json' => array( 'help' => 'Report results in JSON format.', ), 'output' => array( 'param' => 'format', 'help' => "With 'full', show full pretty report (Default). ". "With 'json', report results in JSON format. ". "With 'ugly', use uglier (but more efficient) JSON formatting. ". "With 'none', don't print results. ", 'conflicts' => array( 'json' => 'Only one output format allowed', 'ugly' => 'Only one output format allowed', - ) + ), ), 'everything' => array( 'help' => 'Run every test.', 'conflicts' => array( 'rev' => '--everything runs all tests.', ), ), 'ugly' => array( 'help' => 'With --json, use uglier (but more efficient) formatting.', ), '*' => 'paths', ); } public function requiresWorkingCopy() { return true; } public function requiresRepositoryAPI() { return true; } public function getEngine() { return $this->engine; } public function run() { $working_copy = $this->getWorkingCopy(); $engine_class = $this->getArgument( 'engine', $this->getConfigurationManager()->getConfigFromAnySource('unit.engine')); if (!$engine_class) { throw new ArcanistNoEngineException( 'No unit test engine is configured for this project. Edit .arcconfig '. 'to specify a unit test engine.'); } $paths = $this->getArgument('paths'); $rev = $this->getArgument('rev'); $everything = $this->getArgument('everything'); if ($everything && $paths) { throw new ArcanistUsageException( 'You can not specify paths with --everything. The --everything '. 'flag runs every test.'); } $paths = $this->selectPathsForWorkflow($paths, $rev); if (!class_exists($engine_class) || !is_subclass_of($engine_class, 'ArcanistUnitTestEngine')) { throw new ArcanistUsageException( "Configured unit test engine '{$engine_class}' is not a subclass of ". "'ArcanistUnitTestEngine'."); } $this->engine = newv($engine_class, array()); $this->engine->setWorkingCopy($working_copy); $this->engine->setConfigurationManager($this->getConfigurationManager()); if ($everything) { $this->engine->setRunAllTests(true); } else { $this->engine->setPaths($paths); } $this->engine->setArguments($this->getPassthruArgumentsAsMap('unit')); $renderer = new ArcanistUnitConsoleRenderer(); $this->engine->setRenderer($renderer); $enable_coverage = null; // Means "default". if ($this->getArgument('coverage') || $this->getArgument('detailed-coverage')) { $enable_coverage = true; } else if ($this->getArgument('no-coverage')) { $enable_coverage = false; } $this->engine->setEnableCoverage($enable_coverage); // Enable possible async tests only for 'arc diff' not 'arc unit' if ($this->getParentWorkflow()) { $this->engine->setEnableAsyncTests(true); } else { $this->engine->setEnableAsyncTests(false); } $results = $this->engine->run(); $this->testResults = $results; $console = PhutilConsole::getConsole(); $output_format = $this->getOutputFormat(); if ($output_format !== 'full') { $console->disableOut(); } $unresolved = array(); $coverage = array(); $postponed_count = 0; foreach ($results as $result) { $result_code = $result->getResult(); if ($result_code == ArcanistUnitTestResult::RESULT_POSTPONED) { $postponed_count++; $unresolved[] = $result; } else { if ($this->engine->shouldEchoTestResults()) { $console->writeOut('%s', $renderer->renderUnitResult($result)); } if ($result_code != ArcanistUnitTestResult::RESULT_PASS) { $unresolved[] = $result; } } if ($result->getCoverage()) { foreach ($result->getCoverage() as $file => $report) { $coverage[$file][] = $report; } } } if ($postponed_count) { $console->writeOut( '%s', $renderer->renderPostponedResult($postponed_count)); } if ($coverage) { $file_coverage = array_fill_keys( $paths, 0); $file_reports = array(); foreach ($coverage as $file => $reports) { $report = ArcanistUnitTestResult::mergeCoverage($reports); $cov = substr_count($report, 'C'); $uncov = substr_count($report, 'U'); if ($cov + $uncov) { $coverage = $cov / ($cov + $uncov); } else { $coverage = 0; } $file_coverage[$file] = $coverage; $file_reports[$file] = $report; } $console->writeOut("\n__COVERAGE REPORT__\n"); asort($file_coverage); foreach ($file_coverage as $file => $coverage) { $console->writeOut( " **%s%%** %s\n", sprintf('% 3d', (int)(100 * $coverage)), $file); $full_path = $working_copy->getProjectRoot().'/'.$file; if ($this->getArgument('detailed-coverage') && Filesystem::pathExists($full_path) && is_file($full_path) && array_key_exists($file, $file_reports)) { $console->writeOut( '%s', $this->renderDetailedCoverageReport( Filesystem::readFile($full_path), $file_reports[$file])); } } } $this->unresolvedTests = $unresolved; $overall_result = self::RESULT_OKAY; foreach ($results as $result) { $result_code = $result->getResult(); if ($result_code == ArcanistUnitTestResult::RESULT_FAIL || $result_code == ArcanistUnitTestResult::RESULT_BROKEN) { $overall_result = self::RESULT_FAIL; break; } else if ($result_code == ArcanistUnitTestResult::RESULT_UNSOUND) { $overall_result = self::RESULT_UNSOUND; } else if ($result_code == ArcanistUnitTestResult::RESULT_POSTPONED && $overall_result != self::RESULT_UNSOUND) { $overall_result = self::RESULT_POSTPONED; } } if ($output_format !== 'full') { $console->enableOut(); } $data = array_values(mpull($results, 'toDictionary')); switch ($output_format) { case 'ugly': $console->writeOut('%s', json_encode($data)); break; case 'json': $json = new PhutilJSON(); $console->writeOut('%s', $json->encodeFormatted($data)); break; case 'full': // already printed break; case 'none': // do nothing break; } return $overall_result; } public function getUnresolvedTests() { return $this->unresolvedTests; } public function getTestResults() { return $this->testResults; } private function renderDetailedCoverageReport($data, $report) { $data = explode("\n", $data); $out = ''; $n = 0; foreach ($data as $line) { $out .= sprintf('% 5d ', $n + 1); $line = str_pad($line, 80, ' '); if (empty($report[$n])) { $c = 'N'; } else { $c = $report[$n]; } switch ($c) { case 'C': $out .= phutil_console_format( ' %s ', $line); break; case 'U': $out .= phutil_console_format( ' %s ', $line); break; case 'X': $out .= phutil_console_format( ' %s ', $line); break; default: $out .= ' '.$line.' '; break; } $out .= "\n"; $n++; } return $out; } private function getOutputFormat() { if ($this->getArgument('ugly')) { return 'ugly'; } if ($this->getArgument('json')) { return 'json'; } $format = $this->getArgument('output'); $known_formats = array( 'none' => 'none', 'json' => 'json', 'ugly' => 'ugly', 'full' => 'full', ); return idx($known_formats, $format, 'full'); } }