array_unshift($args, 'help');
}
- $global_config = ArcanistBaseWorkflow::readGlobalArcConfig();
- $system_config = ArcanistBaseWorkflow::readSystemArcConfig();
+ $configuration_manager = new ArcanistConfigurationManager();
+
+ $global_config = $configuration_manager->readUserArcConfig();
+ $system_config = $configuration_manager->readSystemArcConfig();
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);
}
- $user_config = ArcanistBaseWorkflow::readUserConfigurationFile();
+ $user_config = $configuration_manager->readUserConfigurationFile();
$config_class = $working_copy->getConfig('arcanist_configuration');
if ($config_class) {
$command = strtolower($args[0]);
$args = array_slice($args, 1);
- $workflow = $config->selectWorkflow($command, $args, $working_copy, $console);
+ $workflow = $config->selectWorkflow(
+ $command,
+ $args,
+ $configuration_manager,
+ $console);
+ $workflow->setConfigurationManager($configuration_manager);
$workflow->setArcanistConfiguration($config);
$workflow->setCommand($command);
$workflow->setWorkingDirectory($working_directory);
"This command must be run in a Git, Mercurial or Subversion working ".
"copy.");
}
- $workflow->setWorkingCopy($working_copy);
+ $configuration_manager->setWorkingCopyIdentity($working_copy);
}
if ($force_conduit) {
$conduit_uri = $force_conduit;
} else {
- if ($working_copy->getConduitURI()) {
- $conduit_uri = $working_copy->getConduitURI();
+ $project_conduit_uri =
+ $configuration_manager->getProjectConfig('conduit_uri');
+ if ($project_conduit_uri) {
+ $conduit_uri = $project_conduit_uri;
} else {
$conduit_uri = idx($global_config, 'default');
}
$workflow->setConduitURI($conduit_uri);
// Apply global CA bundle from configs.
- if ($ca_bundle = $working_copy->getConfigFromAnySource('https.cabundle')) {
+ $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 = $working_copy->getConfigFromAnySource($blind_key);
+ $blind_trust = $configuration_manager->getConfigFromAnySource($blind_key);
if ($blind_trust) {
HTTPSFuture::setBlindlyTrustDomains($blind_trust);
}
}
if ($need_repository_api || ($want_repository_api && $working_copy)) {
- $repository_api = ArcanistRepositoryAPI::newAPIFromWorkingCopyIdentity(
- $working_copy);
+ $repository_api = ArcanistRepositoryAPI::newAPIFromConfigurationManager(
+ $configuration_manager);
$workflow->setRepositoryAPI($repository_api);
}
- $listeners = $working_copy->getConfigFromAnySource('events.listeners');
+ $listeners = $configuration_manager->getConfigFromAnySource(
+ 'events.listeners');
if ($listeners) {
foreach ($listeners as $listener) {
$console->writeLog(
'ArcanistConduitLinter' => 'lint/linter/ArcanistConduitLinter.php',
'ArcanistConfiguration' => 'configuration/ArcanistConfiguration.php',
'ArcanistConfigurationDrivenLintEngine' => 'lint/engine/ArcanistConfigurationDrivenLintEngine.php',
+ 'ArcanistConfigurationManager' => 'configuration/ArcanistConfigurationManager.php',
'ArcanistCoverWorkflow' => 'workflow/ArcanistCoverWorkflow.php',
'ArcanistCppcheckLinter' => 'lint/linter/ArcanistCppcheckLinter.php',
'ArcanistCpplintLinter' => 'lint/linter/ArcanistCpplintLinter.php',
final public function selectWorkflow(
&$command,
array &$args,
- ArcanistWorkingCopyIdentity $working_copy,
+ ArcanistConfigurationManager $configuration_manager,
PhutilConsole $console) {
// First, try to build a workflow with the exact name provided. We always
// and substitute it. We do this only after trying to resolve the workflow
// normally to prevent you from doing silly things like aliasing 'alias'
// to something else.
- $aliases = ArcanistAliasWorkflow::getAliases($working_copy);
+ $aliases = ArcanistAliasWorkflow::getAliases($configuration_manager);
list($new_command, $args) = ArcanistAliasWorkflow::resolveAliases(
$command,
$this,
$args,
- $working_copy);
+ $configuration_manager);
$full_alias = idx($aliases, $command, array());
$full_alias = implode(' ', $full_alias);
--- /dev/null
+<?php
+
+/**
+ * This class holds everything related to configuration and configuration files.
+ *
+ * @group config
+ */
+final class ArcanistConfigurationManager {
+
+ private $runtimeConfig = array();
+ private $workingCopy = null;
+
+ public function setWorkingCopyIdentity(
+ ArcanistWorkingCopyIdentity $working_copy) {
+ $this->workingCopy = $working_copy;
+ }
+
+/* -( Get config )--------------------------------------------------------- */
+
+ const CONFIG_SOURCE_RUNTIME = 'runtime';
+ const CONFIG_SOURCE_LOCAL = 'local';
+ const CONFIG_SOURCE_PROJECT = 'project';
+ const CONFIG_SOURCE_USER = 'user';
+ const CONFIG_SOURCE_SYSTEM = 'system';
+
+ public function getProjectConfig($key) {
+ if ($this->workingCopy) {
+ return $this->workingCopy->getConfig($key);
+ }
+ return null;
+ }
+
+ public function getLocalConfig($key) {
+ if ($this->workingCopy) {
+ return $this->workingCopy->getLocalConfig($key);
+ }
+ return null;
+ }
+
+ public function getWorkingCopyIdentity() {
+ return $this->workingCopy;
+ }
+
+ /**
+ * Read a configuration directive from any available configuration source.
+ * This includes the directive in local, user and system configuration in
+ * addition to project configuration, and configuration provided as command
+ * arguments ("runtime").
+ * The precedence is runtime > local > project > user > system
+ *
+ * @param key Key to read.
+ * @param wild Default value if key is not found.
+ * @return wild Value, or default value if not found.
+ *
+ * @task config
+ */
+ public function getConfigFromAnySource($key, $default = null) {
+ $all = $this->getConfigFromAllSources($key);
+ return empty($all) ? $default : head($all);
+ }
+
+ /**
+ * For the advanced case where you want customized configuratin handling.
+ *
+ * Reads the configuration from all available sources, returning a map (array)
+ * of results, with the source as key. Missing values will not be in the map,
+ * so an empty array will be returned if no results are found.
+ *
+ * The map is ordered by the cannonical sources precedence, which is:
+ * runtime > local > project > user > system
+ *
+ * @param key Key to read
+ * @return array Mapping of source => value read. Sources with no value are
+ * not in the array.
+ *
+ * @task config
+ */
+ public function getConfigFromAllSources($key) {
+ $results = array();
+ $settings = new ArcanistSettings();
+
+ $pval = idx($this->runtimeConfig, $key);
+ if ($pval !== null) {
+ $results[self::CONFIG_SOURCE_RUNTIME] =
+ $settings->willReadValue($key, $pval);
+ }
+
+ $pval = $this->getLocalConfig($key);
+ if ($pval !== null) {
+ $results[self::CONFIG_SOURCE_LOCAL] =
+ $settings->willReadValue($key, $pval);
+ }
+
+ $pval = $this->getProjectConfig($key);
+ if ($pval !== null) {
+ $results[self::CONFIG_SOURCE_PROJECT] =
+ $settings->willReadValue($key, $pval);
+ }
+
+ $user_config = $this->readUserArcConfig();
+ $pval = idx($user_config, $key);
+ if ($pval !== null) {
+ $results[self::CONFIG_SOURCE_USER] =
+ $settings->willReadValue($key, $pval);
+ }
+
+ $system_config = $this->readSystemArcConfig();
+ $pval = idx($system_config, $key);
+ if ($pval !== null) {
+ $results[self::CONFIG_SOURCE_SYSTEM] =
+ $settings->willReadValue($key, $pval);
+ }
+
+ return $results;
+ }
+
+ /**
+ * Sets a runtime config value that takes precedence over any static
+ * config values.
+ *
+ * @param key Key to set.
+ * @param value The value of the key.
+ *
+ * @task config
+ */
+ public function setRuntimeConfig($key, $value) {
+ $this->runtimeConfig[$key] = $value;
+
+ return $this;
+ }
+
+/* -( Read/write config )--------------------------------------------------- */
+
+ public function readLocalArcConfig() {
+ if ($this->workingCopy) {
+ return $this->workingCopy->readLocalArcConfig();
+ }
+
+ return array();
+ }
+
+ public function writeLocalArcConfig(array $config) {
+ if ($this->workingCopy) {
+ return $this->workingCopy->writeLocalArcConfig($config);
+ }
+
+ return false;
+ }
+
+
+ /**
+ * This is probably not the method you're looking for; try
+ * @{method:readUserArcConfig}.
+ */
+ public function readUserConfigurationFile() {
+ $user_config = array();
+ $user_config_path = self::getUserConfigurationFileLocation();
+ if (Filesystem::pathExists($user_config_path)) {
+
+ if (!phutil_is_windows()) {
+ $mode = fileperms($user_config_path);
+ if (!$mode) {
+ throw new Exception("Unable to get perms of '{$user_config_path}'!");
+ }
+ if ($mode & 0177) {
+ // Mode should allow only owner access.
+ $prompt = "File permissions on your ~/.arcrc are too open. ".
+ "Fix them by chmod'ing to 600?";
+ if (!phutil_console_confirm($prompt, $default_no = false)) {
+ throw new ArcanistUsageException("Set ~/.arcrc to file mode 600.");
+ }
+ execx('chmod 600 %s', $user_config_path);
+
+ // Drop the stat cache so we don't read the old permissions if
+ // we end up here again. If we don't do this, we may prompt the user
+ // to fix permissions multiple times.
+ clearstatcache();
+ }
+ }
+
+ $user_config_data = Filesystem::readFile($user_config_path);
+ $user_config = json_decode($user_config_data, true);
+ if (!is_array($user_config)) {
+ throw new ArcanistUsageException(
+ "Your '~/.arcrc' file is not a valid JSON file.");
+ }
+ }
+ return $user_config;
+ }
+
+ /**
+ * This is probably not the method you're looking for; try
+ * @{method:writeUserArcConfig}.
+ */
+ public function writeUserConfigurationFile($config) {
+ $json_encoder = new PhutilJSON();
+ $json = $json_encoder->encodeFormatted($config);
+
+ $path = self::getUserConfigurationFileLocation();
+ Filesystem::writeFile($path, $json);
+
+ if (!phutil_is_windows()) {
+ execx('chmod 600 %s', $path);
+ }
+ }
+
+ public function getUserConfigurationFileLocation() {
+ if (phutil_is_windows()) {
+ return getenv('APPDATA').'/.arcrc';
+ } else {
+ return getenv('HOME').'/.arcrc';
+ }
+ }
+
+ public function readUserArcConfig() {
+ return idx(self::readUserConfigurationFile(), 'config', array());
+ }
+
+ public function writeUserArcConfig(array $options) {
+ $config = self::readUserConfigurationFile();
+ $config['config'] = $options;
+ self::writeUserConfigurationFile($config);
+ }
+
+
+ public function getSystemArcConfigLocation() {
+ if (phutil_is_windows()) {
+ return Filesystem::resolvePath(
+ 'Phabricator/Arcanist/config',
+ getenv('ProgramData'));
+ } else {
+ return '/etc/arcconfig';
+ }
+ }
+
+ public function readSystemArcConfig() {
+ $system_config = array();
+ $system_config_path = self::getSystemArcConfigLocation();
+ if (Filesystem::pathExists($system_config_path)) {
+ $file = Filesystem::readFile($system_config_path);
+ if ($file) {
+ $system_config = json_decode($file, true);
+ }
+ }
+ return $system_config;
+ }
+
+}
* @task config
*/
private function getConfiguredScript() {
- $working_copy = $this->getEngine()->getWorkingCopy();
$key = 'linter.scriptandregex.script';
- $config = $working_copy->getConfigFromAnySource($key);
+ $config = $this->getConfigFromAnySource($key);
if (!$config) {
throw new ArcanistUsageException(
* @task config
*/
private function getConfiguredRegex() {
- $working_copy = $this->getEngine()->getWorkingCopy();
$key = 'linter.scriptandregex.regex';
- $config = $working_copy->getConfigFromAnySource($key);
+ $config = $this->getConfigFromAnySource($key);
if (!$config) {
throw new ArcanistUsageException(
$global_string = $global->getConcreteString();
$globals_map[$global_string] = true;
$names[] = array(
- 'global',
+ 'user',
$global_string,
$global,
public function resolveBaseCommit(array $specs) {
$specs += array(
- 'args' => '',
+ 'runtime' => '',
'local' => '',
'project' => '',
- 'global' => '',
+ 'user' => '',
'system' => '',
);
}
$this->try = array(
- 'args',
+ 'runtime',
'local',
'project',
- 'global',
+ 'user',
'system',
);
* Handle resolving "arc:*" rules.
*/
private function resolveArcRule($rule, $name, $source) {
+ $name = $this->updateLegacyRuleName($name);
+
switch ($name) {
case 'verbose':
$this->verbose = true;
$this->api->setBaseCommitExplanation($reason);
return phutil_console_prompt('Against which commit?');
case 'local':
- case 'global':
+ case 'user':
case 'project':
- case 'args':
+ case 'runtime':
case 'system':
// Push the other source on top of the list.
array_unshift($this->try, $name);
}
}
+ private function updateLegacyRuleName($name) {
+ $updated = array(
+ 'global' => 'user',
+ 'args' => 'runtime',
+ );
+ $new_name = idx($updated, $name);
+ if ($new_name) {
+ $this->log("translating legacy name '$name' to '$new_name'");
+ return $new_name;
+ }
+ return $name;
+ }
+
}
'Literal',
'xyz',
array(
- 'args' => 'literal:xyz',
+ 'runtime' => 'literal:xyz',
));
}
array(
'local' => 'literal:n',
'project' => 'literal:n',
- 'args' => 'literal:y',
- 'global' => 'literal:n',
+ 'runtime' => 'literal:y',
+ 'user' => 'literal:n',
));
$this->assertCommit(
array(
'project' => 'literal:n',
'local' => 'literal:y',
- 'global' => 'literal:n',
+ 'user' => 'literal:n',
));
$this->assertCommit(
'y',
array(
'project' => 'literal:y',
- 'global' => 'literal:n',
+ 'user' => 'literal:n',
));
$this->assertCommit(
'Order: Global',
'y',
array(
- 'global' => 'literal:y',
+ 'user' => 'literal:y',
));
}
+ public function testLegacyRule() {
+ // 'global' should translate to 'user'
+ $this->assertCommit(
+ '"global" name',
+ 'y',
+ array(
+ 'runtime' => 'arc:global, arc:halt',
+ 'local' => 'arc:halt',
+ 'project' => 'arc:halt',
+ 'user' => 'literal:y',
+ ));
+
+ // 'args' should translate to 'runtime'
+ $this->assertCommit(
+ '"args" name',
+ 'y',
+ array(
+ 'runtime' => 'arc:project, literal:y',
+ 'local' => 'arc:halt',
+ 'project' => 'arc:args',
+ 'user' => 'arc:halt',
+ ));
+
+ }
+
public function testHalt() {
// 'arc:halt' should halt all processing.
'Halt',
null,
array(
- 'args' => 'arc:halt',
+ 'runtime' => 'arc:halt',
'local' => 'literal:xyz',
));
}
'Yield',
'xyz',
array(
- 'args' => 'arc:yield, literal:abc',
+ 'runtime' => 'arc:yield, literal:abc',
'local' => 'literal:xyz',
));
- // This one should return to 'args' after exhausting 'local'.
+ // This one should return to 'runtime' after exhausting 'local'.
$this->assertCommit(
'Yield + Return',
'abc',
array(
- 'args' => 'arc:yield, literal:abc',
+ 'runtime' => 'arc:yield, literal:abc',
'local' => 'arc:skip',
));
}
'Jump',
'abc',
array(
- 'args' => 'arc:project, arc:halt',
+ 'runtime' => 'arc:project, arc:halt',
'local' => 'literal:abc',
- 'project' => 'arc:global, arc:halt',
- 'global' => 'arc:local, arc:halt',
+ 'project' => 'arc:user, arc:halt',
+ 'user' => 'arc:local, arc:halt',
));
}
public function testJumpReturn() {
- // After jumping to project, we should return to 'args'.
+ // After jumping to project, we should return to 'runtime'.
$this->assertCommit(
'Jump Return',
'xyz',
array(
- 'args' => 'arc:project, literal:xyz',
+ 'runtime' => 'arc:project, literal:xyz',
'local' => 'arc:halt',
'project' => '',
- 'global' => 'arc:halt',
+ 'user' => 'arc:halt',
));
}
// isolation for repository-oriented test cases.
$root = dirname(phutil_get_library_root('arcanist'));
- $copy = ArcanistWorkingCopyIdentity::newFromPath($root);
- $repo = ArcanistRepositoryAPI::newAPIFromWorkingCopyIdentity($copy);
+ $working_copy = ArcanistWorkingCopyIdentity::newFromPath($root);
+ $configuration_manager = new ArcanistConfigurationManager();
+ $configuration_manager->setWorkingCopyIdentity($working_copy);
+ $repo = ArcanistRepositoryAPI::newAPIFromConfigurationManager(
+ $configuration_manager);
return new ArcanistBaseCommitParser($repo);
}
$fixture_path = $fixture->getPath();
$working_copy = ArcanistWorkingCopyIdentity::newFromPath($fixture_path);
- $repository_api = ArcanistRepositoryAPI::newAPIFromWorkingCopyIdentity(
- $working_copy);
+ $configuration_manager = new ArcanistConfigurationManager();
+ $configuration_manager->setWorkingCopyIdentity($working_copy);
+ $repository_api = ArcanistRepositoryAPI::newAPIFromConfigurationManager(
+ $configuration_manager);
+
$repository_api->setBaseCommitArgumentRules('arc:this');
$diff = $repository_api->getFullGitDiff();
}
if ($this->getBaseCommitArgumentRules() ||
- $this->getWorkingCopyIdentity()->getConfigFromAnySource('base')) {
+ $this->getConfigurationManager()->getConfigFromAnySource('base')) {
$base = $this->resolveBaseCommit();
if (!$base) {
throw new ArcanistUsageException(
protected $path;
protected $diffLinesOfContext = 0x7FFF;
private $baseCommitExplanation = '???';
- private $workingCopyIdentity;
+ private $configurationManager;
private $baseCommitArgumentRules;
private $uncommittedStatusCache;
}
public function getWorkingCopyIdentity() {
- return $this->workingCopyIdentity;
+ return $this->configurationManager->getWorkingCopyIdentity();
}
- public static function newAPIFromWorkingCopyIdentity(
- ArcanistWorkingCopyIdentity $working_copy) {
+ public function getConfigurationManager() {
+ return $this->configurationManager;
+ }
+
+ public static function newAPIFromConfigurationManager(
+ ArcanistConfigurationManager $configuration_manager) {
+
+ $working_copy = $configuration_manager->getWorkingCopyIdentity();
+
+ if (!$working_copy) {
+ throw new Exception(
+ "Trying to create a RepositoryApi without a working copy");
+ }
$root = $working_copy->getProjectRoot();
if (Filesystem::pathExists($root.'/.hg')) {
$api = new ArcanistMercurialAPI($root);
- $api->workingCopyIdentity = $working_copy;
+ $api->configurationManager = $configuration_manager;
return $api;
}
}
$api = new ArcanistGitAPI($root);
- $api->workingCopyIdentity = $working_copy;
+ $api->configurationManager = $configuration_manager;
return $api;
}
foreach (Filesystem::walkToRoot($root) as $dir) {
if (Filesystem::pathExists($dir . '/.svn')) {
$api = new ArcanistSubversionAPI($root);
- $api->workingCopyIdentity = $working_copy;
+ $api->configurationManager = $configuration_manager;
return $api;
}
}
}
public function resolveBaseCommit() {
- $working_copy = $this->getWorkingCopyIdentity();
- $global_config = ArcanistBaseWorkflow::readGlobalArcConfig();
- $system_config = ArcanistBaseWorkflow::readSystemArcConfig();
+ $base_commit_rules = array(
+ 'runtime' => $this->getBaseCommitArgumentRules(),
+ 'local' => '',
+ 'project' => '',
+ 'user' => '',
+ 'system' => '',
+ );
+ $all_sources = $this->configurationManager->getConfigFromAllSources('base');
+
+ $base_commit_rules = $all_sources + $base_commit_rules;
$parser = new ArcanistBaseCommitParser($this);
- $commit = $parser->resolveBaseCommit(
- array(
- 'args' => $this->getBaseCommitArgumentRules(),
- 'local' => $working_copy->getLocalConfig('base', ''),
- 'project' => $working_copy->getConfig('base', ''),
- 'global' => idx($global_config, 'base', ''),
- 'system' => idx($system_config, 'base', ''),
- ));
+ $commit = $parser->resolveBaseCommit($base_commit_rules);
return $commit;
}
$fixture_path = $fixture->getPath();
$working_copy = ArcanistWorkingCopyIdentity::newFromPath($fixture_path);
+ $configuration_manager = new ArcanistConfigurationManager();
+ $configuration_manager->setWorkingCopyIdentity($working_copy);
+ $api = ArcanistRepositoryAPI::newAPIFromConfigurationManager(
+ $configuration_manager);
- $api = ArcanistRepositoryAPI::newAPIFromWorkingCopyIdentity(
- $working_copy);
$api->setBaseCommitArgumentRules('arc:this');
if ($api instanceof ArcanistSubversionAPI) {
);
}
- public static function getAliases($working_copy) {
- $working_copy_config_aliases = $working_copy->getConfig('aliases');
+ public static function getAliases(
+ ArcanistConfigurationManager $configuration_manager) {
+
+ $working_copy_config_aliases =
+ $configuration_manager->getProjectConfig('aliases');
if (!$working_copy_config_aliases) {
$working_copy_config_aliases = array();
}
- $user_config_aliases =
- idx(self::readUserConfigurationFile(), 'aliases', array());
+ $user_config_aliases = idx(
+ $configuration_manager->readUserConfigurationFile(),
+ 'aliases',
+ array());
return $user_config_aliases + $working_copy_config_aliases;
}
private function writeAliases(array $aliases) {
- $config = self::readUserConfigurationFile();
+ $config = $this->getConfigurationManager()->readUserConfigurationFile();
$config['aliases'] = $aliases;
- self::writeUserConfigurationFile($config);
+ $this->getConfigurationManager()->writeUserConfigurationFile($config);
}
public function run() {
- // We might not be in a working directory, so we don't want to require a
- // working copy identity here.
- $working_copy = ArcanistWorkingCopyIdentity::newFromPath(getcwd());
- $aliases = self::getAliases($working_copy);
+ $aliases = self::getAliases($this->getConfigurationManager());
$argv = $this->getArgument('argv');
if (count($argv) == 0) {
$command,
ArcanistConfiguration $config,
array $argv,
- ArcanistWorkingCopyIdentity $working_copy) {
+ ArcanistConfigurationManager $configuration_manager) {
- $aliases = ArcanistAliasWorkflow::getAliases($working_copy);
+ $aliases = ArcanistAliasWorkflow::getAliases($configuration_manager);
if (!isset($aliases[$command])) {
return array(null, $argv);
}
private $userPHID;
private $userName;
private $repositoryAPI;
+ private $configurationManager;
private $workingCopy;
private $arguments;
private $passedArguments;
$this->conduit->setTimeout($this->conduitTimeout);
}
- $user = $this->getConfigFromWhateverSourceAvailiable('http.basicauth.user');
- $pass = $this->getConfigFromWhateverSourceAvailiable('http.basicauth.pass');
+ $user = $this->getConfigFromAnySource('http.basicauth.user');
+ $pass = $this->getConfigFromAnySource('http.basicauth.pass');
if ($user !== null && $pass !== null) {
$this->conduit->setBasicAuthCredentials($user, $pass);
}
return $this;
}
- final public function getConfigFromWhateverSourceAvailiable($key) {
- if ($this->requiresWorkingCopy()) {
- $working_copy = $this->getWorkingCopy();
- return $working_copy->getConfigFromAnySource($key);
- } else {
- $global_config = self::readGlobalArcConfig();
- $pval = idx($global_config, $key);
-
- if ($pval === null) {
- $system_config = self::readSystemArcConfig();
- $pval = idx($system_config, $key);
- }
-
- return $pval;
- }
+ final public function getConfigFromAnySource($key) {
+ return $this->configurationManager->getConfigFromAnySource($key);
}
return $this->arcanistConfiguration;
}
+ public function setConfigurationManager(
+ ArcanistConfigurationManager $arcanist_configuration_manager) {
+
+ $this->configurationManager = $arcanist_configuration_manager;
+ return $this;
+ }
+
+ public function getConfigurationManager() {
+ return $this->configurationManager;
+ }
+
public function requiresWorkingCopy() {
return false;
}
$workflow = $arc_config->buildWorkflow($command);
$workflow->setParentWorkflow($this);
$workflow->setCommand($command);
+ $workflow->setConfigurationManager($this->getConfigurationManager());
if ($this->repositoryAPI) {
$workflow->setRepositoryAPI($this->repositoryAPI);
}
public function getWorkingCopy() {
- if (!$this->workingCopy) {
+ $working_copy = $this->getConfigurationManager()->getWorkingCopyIdentity();
+ if (!$working_copy) {
$workflow = get_class($this);
throw new Exception(
"This workflow ('{$workflow}') requires a working copy, override ".
"requiresWorkingCopy() to return true.");
}
- return $this->workingCopy;
+ return $working_copy;
}
public function setWorkingCopy(
$api->addToCommit($unstaged);
$must_commit += array_flip($unstaged);
} else {
- $permit_autostash = $this->getWorkingCopy()->getConfigFromAnySource(
+ $permit_autostash = $this->getConfigFromAnySource(
'arc.autostash',
false);
if ($permit_autostash && $api->canStashChanges()) {
return $argv;
}
- public static function getSystemArcConfigLocation() {
- if (phutil_is_windows()) {
- return Filesystem::resolvePath(
- 'Phabricator/Arcanist/config',
- getenv('ProgramData'));
- } else {
- return '/etc/arcconfig';
- }
- }
-
- public static function readSystemArcConfig() {
- $system_config = array();
- $system_config_path = self::getSystemArcConfigLocation();
- if (Filesystem::pathExists($system_config_path)) {
- $file = Filesystem::readFile($system_config_path);
- if ($file) {
- $system_config = json_decode($file, true);
- }
- }
- return $system_config;
- }
- public static function getUserConfigurationFileLocation() {
- if (phutil_is_windows()) {
- return getenv('APPDATA').'/.arcrc';
- } else {
- return getenv('HOME').'/.arcrc';
- }
- }
-
- public static function readUserConfigurationFile() {
- $user_config = array();
- $user_config_path = self::getUserConfigurationFileLocation();
- if (Filesystem::pathExists($user_config_path)) {
-
- if (!phutil_is_windows()) {
- $mode = fileperms($user_config_path);
- if (!$mode) {
- throw new Exception("Unable to get perms of '{$user_config_path}'!");
- }
- if ($mode & 0177) {
- // Mode should allow only owner access.
- $prompt = "File permissions on your ~/.arcrc are too open. ".
- "Fix them by chmod'ing to 600?";
- if (!phutil_console_confirm($prompt, $default_no = false)) {
- throw new ArcanistUsageException("Set ~/.arcrc to file mode 600.");
- }
- execx('chmod 600 %s', $user_config_path);
-
- // Drop the stat cache so we don't read the old permissions if
- // we end up here again. If we don't do this, we may prompt the user
- // to fix permissions multiple times.
- clearstatcache();
- }
- }
-
- $user_config_data = Filesystem::readFile($user_config_path);
- $user_config = json_decode($user_config_data, true);
- if (!is_array($user_config)) {
- throw new ArcanistUsageException(
- "Your '~/.arcrc' file is not a valid JSON file.");
- }
- }
- return $user_config;
- }
-
-
- public static function writeUserConfigurationFile($config) {
- $json_encoder = new PhutilJSON();
- $json = $json_encoder->encodeFormatted($config);
-
- $path = self::getUserConfigurationFileLocation();
- Filesystem::writeFile($path, $json);
-
- if (!phutil_is_windows()) {
- execx('chmod 600 %s', $path);
- }
- }
-
- public static function readGlobalArcConfig() {
- return idx(self::readUserConfigurationFile(), 'config', array());
- }
-
- public static function writeGlobalArcConfig(array $options) {
- $config = self::readUserConfigurationFile();
- $config['config'] = $options;
- self::writeUserConfigurationFile($config);
- }
-
- public function readLocalArcConfig() {
- $local = array();
- $file = $this->readScratchFile('config');
- if ($file) {
- $local = json_decode($file, true);
- }
-
- return $local;
- }
-
- public function writeLocalArcConfig(array $config) {
- $json_encoder = new PhutilJSON();
- $json = $json_encoder->encodeFormatted($config);
-
- $this->writeScratchFile('config', $json);
-
- return $this;
- }
-
-
/**
* Write a message to stderr so that '--json' flags or stdout which is meant
* to be piped somewhere aren't disrupted.
protected function isHistoryImmutable() {
$repository_api = $this->getRepositoryAPI();
- $working_copy = $this->getWorkingCopy();
- $config = $working_copy->getConfigFromAnySource('history.immutable');
+ $config = $this->getConfigFromAnySource('history.immutable');
if ($config !== null) {
return $config;
}
protected function newInteractiveEditor($text) {
$editor = new PhutilInteractiveEditor($text);
- $preferred = $this->getWorkingCopy()->getConfigFromAnySource('editor');
+ $preferred = $this->getConfigFromAnySource('editor');
if ($preferred) {
$editor->setPreferredEditor($preferred);
}
}
private function getBrowserCommand() {
- $config = $this->getWorkingCopy()->getConfigFromAnySource('browser');
+ $config = $this->getConfigFromAnySource('browser');
if ($config) {
return $config;
}
$settings = new ArcanistSettings();
+ $configuration_manager = $this->getConfigurationManager();
$configs = array(
- 'system' => self::readSystemArcConfig(),
- 'global' => self::readGlobalArcConfig(),
- 'project' => $this->getWorkingCopy()->getProjectConfig(),
- 'local' => $this->readLocalArcConfig(),
+ ArcanistConfigurationManager::CONFIG_SOURCE_SYSTEM =>
+ $configuration_manager->readSystemArcConfig(),
+ ArcanistConfigurationManager::CONFIG_SOURCE_USER =>
+ $configuration_manager->readUserArcConfig(),
+ ArcanistConfigurationManager::CONFIG_SOURCE_PROJECT =>
+ $this->getWorkingCopy()->readProjectConfig(),
+ ArcanistConfigurationManager::CONFIG_SOURCE_LOCAL =>
+ $configuration_manager->readLocalArcConfig(),
);
if ($argv) {
}
foreach ($configs as $name => $config) {
switch ($name) {
- case 'project':
+ case ArcanistConfigurationManager::CONFIG_SOURCE_PROJECT:
// Respect older names in project config.
$val = $this->getWorkingCopy()->getConfig($key);
break;
$uri = $this->determineConduitURI();
$this->setConduitURI($uri);
+ $configuration_manager = $this->getConfigurationManager();
echo "Installing certificate for '{$uri}'...\n";
- $config = self::readUserConfigurationFile();
+ $config = $configuration_manager->readUserConfigurationFile();
echo "Trying to connect to server...\n";
$conduit = $this->establishConduit()->getConduit();
);
echo "Writing ~/.arcrc...\n";
- self::writeUserConfigurationFile($config);
+ $configuration_manager->writeUserConfigurationFile($config);
echo phutil_console_format(
"<bg:green>** SUCCESS! **</bg> Certificate installed.\n");
$this->branch = head($branch);
$this->keepBranch = $this->getArgument('keep-branch');
- $working_copy = $this->getWorkingCopy();
- $update_strategy = $working_copy->getConfigFromAnySource(
+ $update_strategy = $this->getConfigFromAnySource(
'arc.land.update.default',
'merge');
$this->shouldUpdateWithRebase = $update_strategy == 'rebase';
$onto_default = $this->isGit ? 'master' : 'default';
$onto_default = nonempty(
- $working_copy->getConfigFromAnySource('arc.land.onto.default'),
+ $this->getConfigFromAnySource('arc.land.onto.default'),
$onto_default);
$this->onto = $this->getArgument('onto', $onto_default);
$this->ontoType = $this->getBranchType($this->onto);
public function run() {
$console = PhutilConsole::getConsole();
$working_copy = $this->getWorkingCopy();
+ $configuration_manager = $this->getConfigurationManager();
$engine = $this->getArgument('engine');
if (!$engine) {
- $engine = $working_copy->getConfigFromAnySource('lint.engine');
+ $engine = $configuration_manager->getConfigFromAnySource('lint.engine');
}
if (!$engine) {
"flag lints every file.");
}
if ($use_cache === null) {
- $use_cache = (bool)$working_copy->getConfigFromAnySource(
+ $use_cache = (bool)$configuration_manager->getConfigFromAnySource(
'arc.lint.cache',
false);
}
$engine = newv($engine, array());
$this->engine = $engine;
- $engine->setWorkingCopy($working_copy);
+ $engine->setWorkingCopy($working_copy); // todo setConfig?
$engine->setMinimumSeverity(
$this->getArgument('severity', self::DEFAULT_SEVERITY));
Supports: cli
Sets an arc configuration option.
- Options are either global (apply to all arc commands you invoke
+ Options are either user (apply to all arc commands you invoke
from the current user) or local (apply only to the current working
- copy). By default, global configuration is written. Use __--local__
+ copy). By default, user configuration is written. Use __--local__
to write local configuration.
- Global values are written to '~/.arcrc' on Linux and Mac OS X, and an
+ User values are written to '~/.arcrc' on Linux and Mac OS X, and an
undisclosed location on Windows. Local values are written to an arc
directory under either .git, .hg, or .svn as appropriate.
'help' => 'Show available configuration values.',
),
'local' => array(
- 'help' => 'Set a local config value instead of a global one',
+ 'help' => 'Set a local config value instead of a user one',
),
'*' => 'argv',
);
throw new ArcanistUsageException(
"Specify a key and a value, or --show.");
}
+ $configuration_manager = $this->getConfigurationManager();
$is_local = $this->getArgument('local');
if ($is_local) {
- $config = $this->readLocalArcConfig();
+ $config = $configuration_manager->readLocalArcConfig();
$which = 'local';
} else {
- $config = self::readGlobalArcConfig();
- $which = 'global';
+ $config = $configuration_manager->readUserArcConfig();
+ $which = 'user';
}
$key = $argv[0];
if (!strlen($val)) {
unset($config[$key]);
if ($is_local) {
- $this->writeLocalArcConfig($config);
+ $configuration_manager->writeLocalArcConfig($config);
} else {
- self::writeGlobalArcConfig($config);
+ $configuration_manager->writeUserArcConfig($config);
}
$old = $settings->formatConfigValueForDisplay($key, $old);
$config[$key] = $val;
if ($is_local) {
- $this->writeLocalArcConfig($config);
+ $configuration_manager->writeLocalArcConfig($config);
} else {
- self::writeGlobalArcConfig($config);
+ $configuration_manager->writeUserArcConfig($config);
}
$val = $settings->formatConfigValueForDisplay($key, $val);
}
private function show() {
- $config = self::readGlobalArcConfig();
+ $config = $this->getConfigurationManager()->readUserArcConfig();
$settings = new ArcanistSettings();
echo phutil_console_format(" Example: %s\n", $example);
}
if (strlen($value)) {
- echo phutil_console_format(" Global Setting: %s\n", $value);
+ echo phutil_console_format(" User Setting: %s\n", $value);
}
echo "\n";
echo phutil_console_wrap($help, 4);
// but commands can raise more detailed errors.
$working_copy = ArcanistWorkingCopyIdentity::newFromPath(getcwd());
if ($working_copy->getProjectRoot()) {
- $repository_api = ArcanistRepositoryAPI::newAPIFromWorkingCopyIdentity(
- $working_copy);
+ $configuration_manager = $this->getConfigurationManager();
+ $configuration_manager->setWorkingCopyIdentity($working_copy);
+ $repository_api = ArcanistRepositoryAPI::newAPIFromConfigurationManager(
+ $configuration_manager);
+
$vcs = $repository_api->getSourceControlSystemName();
}
}
// Also permit autocompletion of "arc alias" commands.
- foreach (
- ArcanistAliasWorkflow::getAliases($working_copy) as $key => $value) {
+ $aliases = ArcanistAliasWorkflow::getAliases($configuration_manager);
+ foreach ($aliases as $key => $value) {
$complete[] = $key;
}
$argv[1],
$arc_config,
array_slice($argv, 2),
- $working_copy);
+ $configuration_manager);
if ($new_command) {
$workflow = $arc_config->buildWorkflow($new_command);
}
$working_copy = ArcanistWorkingCopyIdentity::newFromRootAndConfigFile(
$project_root,
+ $this->getConfigurationManager(),
$config,
$config_file." (svnlook: {$transaction} {$repository})");
$engine_class = $this->getArgument(
'engine',
- $working_copy->getConfigFromAnySource('unit.engine'));
+ $this->getConfigurationManager()->getConfigFromAnySource('unit.engine'));
if (!$engine_class) {
throw new ArcanistNoEngineException(
$working_copy = ArcanistWorkingCopyIdentity::newFromPath($root);
- $repository_api = ArcanistRepositoryAPI::newAPIFromWorkingCopyIdentity(
- $working_copy);
+ $configuration_manager = clone $this->getConfigurationManager();
+ $configuration_manager->setWorkingCopyIndentity($working_copy);
+ $repository_api = ArcanistRepositoryAPI::newAPIFromConfigurationManager(
+ $configuration_manager);
$this->setRepositoryAPI($repository_api);
protected $localConfig;
protected $projectConfig;
- protected $runtimeConfig;
protected $projectRoot;
+ protected $localMetaDir;
public static function newDummyWorkingCopy() {
- return new ArcanistWorkingCopyIdentity('/', array());
+ return new ArcanistWorkingCopyIdentity('/', array());
}
public static function newFromPath($path) {
$this->projectRoot = $root;
$this->projectConfig = $config;
$this->localConfig = array();
- $this->runtimeConfig = array();
+ $this->localMetaDir = null;
$vc_dirs = array(
'.git',
$this->projectRoot);
if (Filesystem::pathExists($meta_path)) {
$found_meta_dir = true;
+ $this->localMetaDir = $meta_path;
$local_path = Filesystem::resolvePath(
'arc/config',
$meta_path);
- if (Filesystem::pathExists($local_path)) {
- $file = Filesystem::readFile($local_path);
- if ($file) {
- $this->localConfig = json_decode($file, true);
- }
- }
+ $this->localConfig = $this->readLocalArcConfig();
break;
}
}
if (!$found_meta_dir) {
// Try for a single higher-level .svn directory as used by svn 1.7+
foreach (Filesystem::walkToRoot($this->projectRoot) as $parent_path) {
+ $meta_path = Filesystem::resolvePath(
+ '.svn',
+ $parent_path);
$local_path = Filesystem::resolvePath(
'.svn/arc/config',
$parent_path);
if (Filesystem::pathExists($local_path)) {
- $file = Filesystem::readFile($local_path);
- if ($file) {
- $this->localConfig = json_decode($file, true);
- }
+ $this->localMetaDir = $meta_path;
+ $this->localConfig = $this->readLocalArcConfig();
}
}
}
return $this->projectRoot.'/'.$to_file;
}
- public function getConduitURI() {
- return $this->getConfig('conduit_uri');
- }
-
/* -( Config )------------------------------------------------------------- */
- public function getProjectConfig() {
+ public function readProjectConfig() {
return $this->projectConfig;
}
return $pval;
}
-
/**
* Read a configuration directive from local configuration. This
* reads ONLY the per-working copy configuration,
return idx($this->localConfig, $key, $default);
}
- /**
- * Read a configuration directive from any available configuration source.
- * In contrast to @{method:getConfig}, this will look for the directive in
- * local and user configuration in addition to project configuration.
- * The precedence is local > project > user
- *
- * @param key Key to read.
- * @param wild Default value if key is not found.
- * @return wild Value, or default value if not found.
- *
- * @task config
- */
- public function getConfigFromAnySource($key, $default = null) {
- $settings = new ArcanistSettings();
-
- // try runtime config first
- $pval = idx($this->runtimeConfig, $key);
-
- // try local config
- if ($pval === null) {
- $pval = $this->getLocalConfig($key);
+ public function readLocalArcConfig() {
+ if (strlen($this->localMetaDir)) {
+ $local_path = Filesystem::resolvePath(
+ 'arc/config',
+ $this->localMetaDir);
+ if (Filesystem::pathExists($local_path)) {
+ $file = Filesystem::readFile($local_path);
+ if ($file) {
+ return json_decode($file, true);
+ }
+ }
}
+ return array();
+ }
- // then per-project config
- if ($pval === null) {
- $pval = $this->getConfig($key);
+ public function writeLocalArcConfig(array $config) {
+ $dir = $this->localMetaDir;
+ if (!strlen($dir)) {
+ return false;
}
- // now try global (i.e. user-level) config
- if ($pval === null) {
- $global_config = ArcanistBaseWorkflow::readGlobalArcConfig();
- $pval = idx($global_config, $key);
+ if (!Filesystem::pathExists($dir)) {
+ try {
+ Filesystem::createDirectory($dir);
+ } catch (Exception $ex) {
+ return false;
+ }
}
- // Finally, try system-level config.
- if ($pval === null) {
- $system_config = ArcanistBaseWorkflow::readSystemArcConfig();
- $pval = idx($system_config, $key);
- }
+ $json_encoder = new PhutilJSON();
+ $json = $json_encoder->encodeFormatted($config);
- if ($pval === null) {
- $pval = $default;
- } else {
- $pval = $settings->willReadValue($key, $pval);
+ $config_file = Filesystem::resolvePath('arc/config', $dir);
+ try {
+ Filesystem::writeFile($config_file, $json);
+ } catch (FilesystemException $ex) {
+ return false;
}
- return $pval;
-
- }
-
- /**
- * Sets a runtime config value that takes precedence over any static
- * config values.
- *
- * @param key Key to set.
- * @param value The value of the key.
- *
- * @task config
- */
- public function setRuntimeConfig($key, $value) {
- $this->runtimeConfig[$key] = $value;
-
- return $this;
+ return true;
}
}