From 77e67ab39526f92f9171a98708b129201f8635d8 Mon Sep 17 00:00:00 2001
From: Tim Hilliard <timhilliard@gmail.com>
Date: Fri, 2 Sep 2011 02:26:57 +0100
Subject: [PATCH] #1265844 - added interactivity to dog-init

---
 dog.drush.inc               |  135 +++++++++++++++++++++++++++----------------
 lib/Dog/Repository/Core.php |    5 +-
 2 files changed, 87 insertions(+), 53 deletions(-)

diff --git a/dog.drush.inc b/dog.drush.inc
index d6fc32d..7b297bc 100644
--- a/dog.drush.inc
+++ b/dog.drush.inc
@@ -42,10 +42,8 @@ function dog_drush_command() {
       'collab' => 'The URI to use as the collab repository for this instance. If unspecified, the upstream URI is used.',
     ),
     'examples' => array(
-      'drush dog-init --upstream=drupal-6.x --collab=git://github.com/me/my-project my_project' =>
-        'Initialize a new Drupal 6.x project in a directory called "my_project" with the upstream from git.drupal.org and a collab repository on github.',
-      'drush dog-init --upstream=git@gitorious.org:pressflow/6.git --upstream-ref=master --collab=file:///var/git/our_pressflow_project my_pressflow_project' =>
-        'Initialize a new Pressflow project in "my_pressflow_project" from the master branch on a gitorious upstream, and a locally-hosted collab repository in /var/git.',
+      'drush dog-init --upstream=drupal-6.x --collab=git://github.com/me/my-project my_project' => 'Initialize a new Drupal 6.x project in a directory called "my_project" with the upstream from git.drupal.org and a collab repository on github.',
+      'drush dog-init --upstream=git@gitorious.org:pressflow/6.git --upstream-ref=master --collab=file:///var/git/our_pressflow_project my_pressflow_project' => 'Initialize a new Pressflow project in "my_pressflow_project" from the master branch on a gitorious upstream, and a locally-hosted collab repository in /var/git.',
     ),
   ) + $shared;
 
@@ -60,10 +58,8 @@ function dog_drush_command() {
       'collab' => 'A writeable Git remote repository URI to be registered for the "collab" remote.',
     ),
     'examples' => array(
-      'drush dog-dl views-6.2.x' =>
-        'Initialize views 6.2 in the project repository in the default location',
-      'drush dog-dl https://github.com/kete/tiny_mce.git sites/all/libraries/tiny_mce' =>
-        'Initialize tiny_mce from github in sits/all/libraries',
+      'drush dog-dl views-6.2.x' => 'Initialize views 6.2 in the project repository in the default location',
+      'drush dog-dl https://github.com/kete/tiny_mce.git sites/all/libraries/tiny_mce' => 'Initialize tiny_mce from github in sits/all/libraries',
     ),
   ) + $shared;
 
@@ -79,8 +75,7 @@ function dog_drush_command() {
       'target-dir' => 'Path to the module or theme directory, or drupal root where the collab should be added. Defaults to current working directory.',
     ),
     'examples' => array(
-      'drush dog-collab git://github.com/me/my-views sites/all/modules/views' =>
-        'Add a collab repo from github to the views module',
+      'drush dog-collab git://github.com/me/my-views sites/all/modules/views' => 'Add a collab repo from github to the views module',
     ),
   ) + $shared;
 
@@ -170,8 +165,7 @@ function dog_drush_engine_package_handler() {
 function _dog_verify_git() {
   try {
     $version = substr(trim(_dog_git_invoke('version')), 12);
-  }
-  catch (Exception $e) {
+  } catch (Exception $e) {
     $git_path = drush_get_option('git-path', 'git');
     if ($git_path === 'git') {
       drush_set_error('DRUSH_DOG_NO_GIT_BINARY', dt("No valid Git binary could be found on the PATH."), 'error');
@@ -197,10 +191,10 @@ function _dog_git_invoke($command, $cwd = NULL, $repository_path = NULL, $fail_s
   if (!isset($env_source)) {
     $vo = ini_get('variables_order');
     if (strpos($vo, 'E') !== FALSE) {
-      $env_source =& $_ENV;
+      $env_source = & $_ENV;
     }
     else if (strpos($vo, 'S') !== FALSE) {
-      $env_source =& $_SERVER;
+      $env_source = & $_SERVER;
     }
     else {
       $env_source = FALSE;
@@ -234,7 +228,7 @@ function _dog_git_invoke($command, $cwd = NULL, $repository_path = NULL, $fail_s
 
   // proc_open() and $pipes var preclude using drush_op() so we simulate it.
   if (drush_get_context('DRUSH_VERBOSE') || drush_get_context('DRUSH_SIMULATE')) {
-     drush_print("Calling proc_open($git_path $command)");
+    drush_print("Calling proc_open($git_path $command)");
   }
 
   if (!drush_get_context('DRUSH_SIMULATE')) {
@@ -249,7 +243,7 @@ function _dog_git_invoke($command, $cwd = NULL, $repository_path = NULL, $fail_s
     $return_code = proc_close($process);
 
     if ($return_code != 0 && !$fail_safe) {
-      throw new \Exception("Invocation of Git command '" . $command . "' failed with return code " . $return_code .": \n" . $stdout . $stderr);
+      throw new \Exception("Invocation of Git command '" . $command . "' failed with return code " . $return_code . ": \n" . $stdout . $stderr);
     }
 
     return $stdout;
@@ -301,8 +295,7 @@ function _dog_normal_command() {
 
   try {
     $face = new Dog\Face(DRUPAL_ROOT);
-  }
-  catch (Dog\Exception\BadDog $e) {
+  } catch (Dog\Exception\BadDog $e) {
     // Running a dog command, but this isn't a dog instance. Whoops.
     drush_set_error('DRUSH_DOG_NOT_A_DOG_INSTANCE', $e->getMessage());
     return;
@@ -310,7 +303,6 @@ function _dog_normal_command() {
 
   drush_set_context('dogface', $dog);
   // call_user_func_array('drush_invoke', array_merge(array($command['command-hook'], $dog), $args));
-
 }
 
 /**
@@ -375,48 +367,93 @@ function dog_autoload($class) {
 }
 
 /*
-* Dog command callbacks
-*/
-
-function drush_dog_init($target = '') {
-  // Initialize the RepositoryConfig object we'll use to control the new repo.
-  $config = new Dog\Config\RepositoryConfig();
-
-  $config['git']['remote.upstream.url'] = drush_get_option('upstream', 'drupal');
+ * Dog command callbacks
+ */
 
-  // Force inclusion of download.pm.inc. Ugh.
-  drush_include(DRUSH_BASE_PATH . '/commands/pm', 'download.pm');
+function drush_dog_init_validate($target = '') {
+  // First check for interactive mode
+  $interactive = !drush_get_option('y', FALSE);
 
-  if (strpos($config['git']['remote.upstream.url'], 'drupal') === 0) {
-    $version = pm_parse_project_version(array($config['git']['remote.upstream.url']));
-    $version = reset($version);
-    $xml = _drush_pm_get_release_history_xml($version);
-    $release = _pm_download_parse_release($version, $xml);
-    $config['upstream_ref'] = drush_get_option('upstream-ref', $release['tag']);
-    $config['git']['remote.upstream.url'] = 'git://git.drupal.org/project/drupal.git';
+  // Check for remote upstream url
+  $upstream = drush_get_option('upstream', '');
+  if ($interactive && empty($upstream)) {
+    drush_print('Please select the remote upstream url');
+    $upstream = drush_prompt('upstream', 'drupal', TRUE);
   }
-  else {
-    $config['upstream_ref'] = drush_get_option('upstream-ref');
-    if (!is_string($config['upstream_ref'])) {
-      return drush_set_error('DRUSH_DOG_INVALID_UPSTREAM', dt('An upstream ref must be specified as the starting point for the dog instance.'));
+  else if (empty($upstream)) {
+    $upstream = 'drupal';
+  }
+  // Need a loop here when proper option validation is in place.
+  
+  $upstream_ref = drush_get_option('upstream-ref', '');
+  if (empty($upstream_ref)) {
+    if (strpos($upstream, 'drupal') === 0) {
+      // Force inclusion of download.pm.inc. Ugh.
+      drush_include(DRUSH_BASE_PATH . '/commands/pm', 'download.pm');
+      $version = pm_parse_project_version(array($upstream));
+      $version = reset($version);
+      $xml = _drush_pm_get_release_history_xml($version);
+      $release = _pm_download_parse_release($version, $xml);
+      $upstream_ref = $release['tag'];
+    }
+    if ($interactive) {
+      drush_print('Please select the remote upstream ref');
+      $upstream_ref = drush_prompt('upstream-ref', $upstream_ref, TRUE);
     }
+    drush_set_option('upstream-ref', $upstream_ref);
+  }  
+  
+  if (strpos($upstream, 'drupal') === 0) {
+    $upstream = 'git://git.drupal.org/project/drupal.git';
   }
+  drush_set_option('upstream', $upstream);
 
-  $config['git']['remote.collab.url'] = drush_get_option('collab', $config['git']['remote.upstream.url']);
-
-  $target = $target ?: preg_replace('/.*\/([\w]*)(\.git)?$/', '\1', $config['git']['remote.upstream.url']);
-
+  $target = $target ? : preg_replace('/.*\/([\w]*)(\.git)?$/', '\1', $upstream);
   // Normalize target to absolute path
-  $realtarget = $target[0] != '/' ? getcwd() . '/' . $target : $target;
-  $config['worktree'] = $realtarget;
-  $config['repopath'] = "$realtarget/.git";
-
+  $target = $target[0] != '/' ? getcwd() . '/' . $target : $target;
   if (file_exists($realtarget)) {
     return drush_set_error('DRUSH_DOG_TARGET_DIRECTORY_EXISTS', dt("Target directory '$realtarget' already exists."));
   }
+  drush_set_option('target', $target);
+
+  // Check for remote collab url
+  $collab = drush_get_option('collab', '');
+  if ($interactive && empty($collab)) {
+    drush_print('Please select the remote collab url');
+    $collab = drush_prompt('collab', 'upstream', TRUE);
+  }
+  if (empty($collab) || $collab == 'upstream') {
+    $collab = $upstream;
+  }
+  // Need a loop here when proper option validation is in place.
+  drush_set_option('collab', $collab);
+
+  // Check for branch name
+  $branch = drush_get_option('branch', '');
+  if ($interactive && empty($branch)) {
+    drush_print('Please select the local branch');
+    $branch = drush_prompt('branch', 'master', TRUE);
+  }
+  else if (empty($branch)) {
+    $branch = 'master';
+  }
+  // Need a loop here when proper option validation is in place.
+  drush_set_option('branch', $branch);
+}
+
+function drush_dog_init($target = '') {
+  // Initialize the RepositoryConfig object we'll use to control the new repo.
+  $config = new Dog\Config\RepositoryConfig();
+
+  $config['git']['remote.upstream.url'] = drush_get_option('upstream');
+  $config['upstream_ref'] = drush_get_option('upstream-ref');
+  $config['git']['remote.collab.url'] = drush_get_option('collab');
+  $config['localbranch'] = drush_get_option('branch');
+  $config['worktree'] = drush_get_option('target');
+  $config['repopath'] = $config['worktree'] . "/.git";
 
   // Pop out a new face
-  $face = new Dog\Face($realtarget, TRUE);
+  $face = new Dog\Face($config['worktree'], TRUE);
   $core = new Dog\Repository\Core($config, $face);
   $core->create();
   // Leave init mode, now that we've got something to write/check to.
diff --git a/lib/Dog/Repository/Core.php b/lib/Dog/Repository/Core.php
index ef4415a..e054830 100644
--- a/lib/Dog/Repository/Core.php
+++ b/lib/Dog/Repository/Core.php
@@ -52,12 +52,9 @@ class Core extends Base {
 
     drush_log(dt("Successfully initialized a new dog instance in %worktree", array('%worktree' => $this->config['worktree']), 'success'));
 
-    // Create the appropriate local branch based on the requested start point
-    $localbranch = drush_get_option('branch', 'master');
-
     $old = substr(trim($this->gitPassthru('symbolic-ref HEAD', $this->config['worktree'])), 11);
 
-    $this->gitPassthru('checkout -b ' . drush_escapeshellarg($localbranch) . ' ' . drush_escapeshellarg($this->config['upstream_ref']), $this->config['worktree']);
+    $this->gitPassthru('checkout -b ' . drush_escapeshellarg($this->config['localbranch']) . ' ' . drush_escapeshellarg($this->config['upstream_ref']), $this->config['worktree']);
     $this->gitPassthru('branch -D ' . $old, $this->config['worktree']);
 
     // Set up the collab remote, if the URI for it exists
-- 
1.7.6

