Browse Source

Code reorgs and make bool values real booleans, refs #1637

Jordi Boggiano 12 years ago
parent
commit
f06c0cb580

+ 4 - 2
doc/04-schema.md

@@ -623,8 +623,10 @@ The following options are supported:
   define a notification URL, so that they get notified whenever a package from
   that repository is installed. This option allows you to disable that behaviour.
 * **discard-changes:** Defaults to `false` and can be any of `true`, `false` or
-  `stash`. This option allows you to set the default style of handling dirty
-  updates, specially useful for non-interactive mode.
+  `"stash"`. This option allows you to set the default style of handling dirty
+  updates when in non-interactive mode. `true` will always discard changes in
+  vendors, while `"stash"` will try to stash and reapply. Use this for CI
+  servers or deploy scripts if you tend to have modified vendors.
 
 Example:
 

+ 2 - 2
res/composer-schema.json

@@ -169,8 +169,8 @@
                     "description": "The cache max size for the files cache, defaults to \"300MiB\"."
                 },
                 "discard-changes": {
-                    "type": ["string"],
-                    "description": "The default style of handling dirty updates, defaults to \"false\" and can be any of \"true\", \"false\" or \"stash\"."
+                    "type": ["string", "boolean"],
+                    "description": "The default style of handling dirty updates, defaults to false and can be any of true, false or \"stash\"."
                 }
             }
         },

+ 9 - 4
src/Composer/Command/ConfigCommand.php

@@ -252,11 +252,11 @@ EOT
         $uniqueConfigValues = array(
             'process-timeout' => array('is_numeric', 'intval'),
             'use-include-path' => array(
-                function ($val) { return true; },
+                function ($val) { return in_array($val, array('true', 'false', '1', '0'), true); },
                 function ($val) { return $val !== 'false' && (bool) $val; }
             ),
             'notify-on-install' => array(
-                function ($val) { return true; },
+                function ($val) { return in_array($val, array('true', 'false', '1', '0'), true); },
                 function ($val) { return $val !== 'false' && (bool) $val; }
             ),
             'vendor-dir' => array('is_string', function ($val) { return $val; }),
@@ -272,8 +272,13 @@ EOT
                 function ($val) { return $val; }
             ),
             'discard-changes' => array(
-                function ($val) { return in_array($val, array('true', 'false', 'stash')); },
-                function ($val) { return $val; }
+                function ($val) { return in_array($val, array('stash', 'true', 'false', '1', '0'), true); },
+                function ($val) {
+                    if ('stash' === $val) {
+                        return 'stash';
+                    }
+                    return $val !== 'false' && (bool) $val;
+                }
             ),
         );
         $multiConfigValues = array(

+ 4 - 4
src/Composer/Config.php

@@ -33,7 +33,7 @@ class Config
         'cache-ttl' => 15552000, // 6 months
         'cache-files-ttl' => null, // fallback to cache-ttl
         'cache-files-maxsize' => '300MiB',
-        'discard-changes' => 'false',
+        'discard-changes' => false,
     );
 
     public static $defaultRepositories = array(
@@ -145,7 +145,7 @@ class Config
             case 'cache-files-maxsize':
                 if (!preg_match('/^\s*([0-9.]+)\s*(?:([kmg])(?:i?b)?)?\s*$/i', $this->config[$key], $matches)) {
                     throw new \RuntimeException(
-                        "Could not parse the value of 'cache-files-maxsize' from your config: {$this->config[$key]}"
+                        "Could not parse the value of 'cache-files-maxsize': {$this->config[$key]}"
                     );
                 }
                 $size = $matches[1];
@@ -176,9 +176,9 @@ class Config
                 return rtrim($this->process($this->config[$key]), '/\\');
 
             case 'discard-changes':
-                if (!in_array($this->config[$key], array('true', 'false', 'stash'))) {
+                if (!in_array($this->config[$key], array(true, false, 'stash'), true)) {
                     throw new \RuntimeException(
-                        "Invalid value of 'discard-changes' from your config: {$this->config[$key]}"
+                        "Invalid value for 'discard-changes': {$this->config[$key]}"
                     );
                 }
 

+ 11 - 14
src/Composer/Downloader/GitDownloader.php

@@ -94,23 +94,20 @@ class GitDownloader extends VcsDownloader
             return;
         }
 
-        $discardChanges = $this->config->get('discard-changes');
         if (!$this->io->isInteractive()) {
-            switch ($discardChanges) {
-                case 'true':
-                    return $this->discardChanges($path);
-
-                case 'stash':
-                    if (!$update) {
-                        return parent::cleanChanges($path, $update);
-                    }
-
-                    return $this->stashChanges($path);
-
-                case 'false':
-                default:
+            $discardChanges = $this->config->get('discard-changes');
+            if (true === $discardChanges) {
+                return $this->discardChanges($path);
+            }
+            if ('stash' === $discardChanges) {
+                if (!$update) {
                     return parent::cleanChanges($path, $update);
+                }
+
+                return $this->stashChanges($path);
             }
+
+            return parent::cleanChanges($path, $update);
         }
 
         $changes = array_map(function ($elem) {

+ 4 - 9
src/Composer/Downloader/SvnDownloader.php

@@ -92,17 +92,12 @@ class SvnDownloader extends VcsDownloader
             return;
         }
 
-        $discardChanges = $this->config->get('discard-changes');
         if (!$this->io->isInteractive()) {
-            switch ($discardChanges) {
-                case 'true':
-                    return $this->discardChanges($path);
-
-                case 'false':
-                case 'stash':
-                default:
-                    return parent::cleanChanges($path, $update);
+            if (true === $this->config->get('discard-changes')) {
+                return $this->discardChanges($path);
             }
+
+            return parent::cleanChanges($path, $update);
         }
 
         $changes = array_map(function ($elem) {