From b2faa6010416c784782b268bdbbd60ce4027da28 Mon Sep 17 00:00:00 2001
From: rfay <randy@randyfay.com>
Date: Thu, 17 Feb 2011 21:30:08 -0700
Subject: [PATCH 1/8] First try at patch forgiveness

---
 client/review/vcs/git.inc          |   24 ++++++++++++++++++++++--
 client/review/vcs/vcs.inc          |   10 ++++++++++
 server/pifr_server.environment.inc |    2 +-
 3 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/client/review/vcs/git.inc b/client/review/vcs/git.inc
index 027451b..bfe2182 100644
--- a/client/review/vcs/git.inc
+++ b/client/review/vcs/git.inc
@@ -23,8 +23,24 @@ class PIFRClientReviewVCS_git implements PIFRClientReviewVCS {
     return pifr_client_review::exec($cmd);
   }
 
+  /**
+   * Apply a patch.
+   *
+   * This will try patching with the -p possibilities in
+   * $this->patch_levels_to_try.
+   *
+   * @param $patch
+   *   The patch file to apply.
+   */
   public function apply($patch) {
-    return pifr_client_review::exec("patch -p1 -i $patch");
+    foreach($this->patch_levels_to_try as $patch_level) {
+      $result = pifr_client_review::exec("patch -p$patch_level -i $patch");
+      if ($result) {
+        $this->patch_level = $patch_level;
+        return TRUE;
+      }
+    }
+    return FALSE;
   }
 
   public function get_changed_files($patch) {
@@ -33,7 +49,11 @@ class PIFRClientReviewVCS_git implements PIFRClientReviewVCS {
     // +++ b/install.php
     // and removes the '+++ b/' from them, returning the filenames that have
     // had content added.
-    preg_match_all('%^[\+-]{3}\s+[^/]\/(.*?)\s%m', $contents, $matches, PREG_SET_ORDER);
+    $patch_level = $this->patch_level;
+    $regex = "%^[\+-]{3}\s+([^/]\/){$patch_level}(.*?)\s%m";
+    watchdog('pifr', 'get_changed_files; regex=%regex', array('%regex' => $regex));
+    preg_match_all($regex, $contents, $matches, PREG_SET_ORDER);
+    watchdog('pifr', 'get_changed_files matches=%matches', array('%matches'=> print_r($matches, TRUE)));
 
     $files = array();
     foreach ($matches as $match) {
diff --git a/client/review/vcs/vcs.inc b/client/review/vcs/vcs.inc
index b580542..3f506ab 100644
--- a/client/review/vcs/vcs.inc
+++ b/client/review/vcs/vcs.inc
@@ -31,6 +31,16 @@ interface PIFRClientReviewVCS {
   public function apply($patch);
 
   /**
+   * Array of patch levels to try when patching.
+   */
+  protected $patch_levels_to_try = array(1, 0);
+
+  /**
+   * The patch level (-p0 or -p1) detected.
+   */
+  protected $patch_level = 0;
+
+  /**
    * Get a list of the files changed by a patch.
    *
    * @param string Location of patch file.
diff --git a/server/pifr_server.environment.inc b/server/pifr_server.environment.inc
index 7a587dc..9d35c81 100644
--- a/server/pifr_server.environment.inc
+++ b/server/pifr_server.environment.inc
@@ -3,7 +3,7 @@
 
 /**
  * @file
- * Provide environment management and convience functions.
+ * Provide environment management and convenience functions.
  *
  * @author Jimmy Berry ("boombatower", http://drupal.org/user/214218)
  */
-- 
1.7.0.4


From f8a8b06091c6b1f60967760d420ac3725d1edc6e Mon Sep 17 00:00:00 2001
From: rfay <randy@randyfay.com>
Date: Thu, 17 Feb 2011 21:49:29 -0700
Subject: [PATCH 2/8] Ooops member var in interface

---
 client/review/vcs/git.inc |   11 +++++++++++
 client/review/vcs/vcs.inc |   10 ----------
 2 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/client/review/vcs/git.inc b/client/review/vcs/git.inc
index bfe2182..579774b 100644
--- a/client/review/vcs/git.inc
+++ b/client/review/vcs/git.inc
@@ -13,6 +13,17 @@
  */
 class PIFRClientReviewVCS_git implements PIFRClientReviewVCS {
 
+  /**
+   * Array of patch levels to try when patching.
+   */
+  protected $patch_levels_to_try = array(1, 0);
+
+  /**
+   * The patch level (-p0 or -p1) detected.
+   */
+  protected $patch_level = 0;
+
+
   public function checkout($directory, $url, $branch) {
     $url = escapeshellarg($url);
     $branch = escapeshellarg($branch);
diff --git a/client/review/vcs/vcs.inc b/client/review/vcs/vcs.inc
index 3f506ab..b580542 100644
--- a/client/review/vcs/vcs.inc
+++ b/client/review/vcs/vcs.inc
@@ -31,16 +31,6 @@ interface PIFRClientReviewVCS {
   public function apply($patch);
 
   /**
-   * Array of patch levels to try when patching.
-   */
-  protected $patch_levels_to_try = array(1, 0);
-
-  /**
-   * The patch level (-p0 or -p1) detected.
-   */
-  protected $patch_level = 0;
-
-  /**
    * Get a list of the files changed by a patch.
    *
    * @param string Location of patch file.
-- 
1.7.0.4


From 571d42e5f6420517370464b1ac694ee854fb808a Mon Sep 17 00:00:00 2001
From: rfay <randy@randyfay.com>
Date: Thu, 17 Feb 2011 22:21:46 -0700
Subject: [PATCH 3/8] Add logging for command success

---
 review/client.inc                            |    4 ++--
 review/simpletest/pifr_simpletest.client.inc |    2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/review/client.inc b/review/client.inc
index eb9d185..45e5267 100644
--- a/review/client.inc
+++ b/review/client.inc
@@ -664,10 +664,10 @@ abstract class pifr_client_review {
 
     if ($status != 0) {
       $output = trim(implode("\n", $output));
-      self::$instance->log("Command [$command] failed in directory [$cwd] with status [$status] and " .
-        ($output ? "output:\n$output" : 'no output') . '.');
+      self::$instance->log("Command [$command] failed in directory [$cwd] with status [$status] and " . ($output ? "output:\n$output" : 'no output') . '.');
       return FALSE;
     }
+    self::$instance->log("Command [$command] succeeded in directory [$cwd] with status [$status] and " . ($output ? "output:\n$output" : 'no output') . '.');
     return TRUE;
   }
 }
diff --git a/review/simpletest/pifr_simpletest.client.inc b/review/simpletest/pifr_simpletest.client.inc
index 48ad092..7054192 100644
--- a/review/simpletest/pifr_simpletest.client.inc
+++ b/review/simpletest/pifr_simpletest.client.inc
@@ -93,7 +93,7 @@ class pifr_client_review_pifr_simpletest extends pifr_client_review_pifr_drupal
 
     $b = new PIFRBrowser();
 
-    watchdog('pifr_client', 'Test in execution: ' . print_r($test, TRUE));
+    watchdog('pifr_client', 'Test in execution: %test', array('%test' => print_r($test, TRUE)));
     // Login as admin.
     $user = new stdClass();
     $user->name = $this->admin_username;
-- 
1.7.0.4


From be555c7090060d05d8545cd11a72ec9c77c17c7d Mon Sep 17 00:00:00 2001
From: rfay <randy@randyfay.com>
Date: Thu, 17 Feb 2011 22:28:25 -0700
Subject: [PATCH 4/8] Build the regex another way

---
 client/review/vcs/git.inc |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/client/review/vcs/git.inc b/client/review/vcs/git.inc
index 579774b..62b63f3 100644
--- a/client/review/vcs/git.inc
+++ b/client/review/vcs/git.inc
@@ -61,7 +61,7 @@ class PIFRClientReviewVCS_git implements PIFRClientReviewVCS {
     // and removes the '+++ b/' from them, returning the filenames that have
     // had content added.
     $patch_level = $this->patch_level;
-    $regex = "%^[\+-]{3}\s+([^/]\/){$patch_level}(.*?)\s%m";
+    $regex = '%^[\+-]{3}\s+([^/]\/){' . $this->patch_level . '(.*?)\s%m';
     watchdog('pifr', 'get_changed_files; regex=%regex', array('%regex' => $regex));
     preg_match_all($regex, $contents, $matches, PREG_SET_ORDER);
     watchdog('pifr', 'get_changed_files matches=%matches', array('%matches'=> print_r($matches, TRUE)));
-- 
1.7.0.4


From ec52458238226614df775925745f8567f5fd9a1b Mon Sep 17 00:00:00 2001
From: rfay <randy@randyfay.com>
Date: Thu, 17 Feb 2011 22:31:26 -0700
Subject: [PATCH 5/8] lost a brace

---
 client/review/vcs/git.inc |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/client/review/vcs/git.inc b/client/review/vcs/git.inc
index 62b63f3..db4a6d1 100644
--- a/client/review/vcs/git.inc
+++ b/client/review/vcs/git.inc
@@ -61,7 +61,7 @@ class PIFRClientReviewVCS_git implements PIFRClientReviewVCS {
     // and removes the '+++ b/' from them, returning the filenames that have
     // had content added.
     $patch_level = $this->patch_level;
-    $regex = '%^[\+-]{3}\s+([^/]\/){' . $this->patch_level . '(.*?)\s%m';
+    $regex = '%^[\+-]{3}\s+([^/]\/){' . $this->patch_level . '}(.*?)\s%m';
     watchdog('pifr', 'get_changed_files; regex=%regex', array('%regex' => $regex));
     preg_match_all($regex, $contents, $matches, PREG_SET_ORDER);
     watchdog('pifr', 'get_changed_files matches=%matches', array('%matches'=> print_r($matches, TRUE)));
-- 
1.7.0.4


From 1eda1209f03c6c48e0d4886996a930d9dcffda50 Mon Sep 17 00:00:00 2001
From: rfay <randy@randyfay.com>
Date: Thu, 17 Feb 2011 22:34:28 -0700
Subject: [PATCH 6/8] report output

---
 review/client.inc |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/review/client.inc b/review/client.inc
index 45e5267..410d6b9 100644
--- a/review/client.inc
+++ b/review/client.inc
@@ -662,8 +662,8 @@ abstract class pifr_client_review {
     exec($command, $output, $status);
     self::$instance->output = $output;
 
+    $output = trim(implode("\n", $output));
     if ($status != 0) {
-      $output = trim(implode("\n", $output));
       self::$instance->log("Command [$command] failed in directory [$cwd] with status [$status] and " . ($output ? "output:\n$output" : 'no output') . '.');
       return FALSE;
     }
-- 
1.7.0.4


From 3c0c79996d18675423f64f2cd10f25ed4323627d Mon Sep 17 00:00:00 2001
From: rfay <randy@randyfay.com>
Date: Thu, 17 Feb 2011 22:50:10 -0700
Subject: [PATCH 7/8] fiddle with which match to match

---
 client/review/vcs/git.inc |    9 +++++----
 1 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/client/review/vcs/git.inc b/client/review/vcs/git.inc
index db4a6d1..b4ec387 100644
--- a/client/review/vcs/git.inc
+++ b/client/review/vcs/git.inc
@@ -61,15 +61,16 @@ class PIFRClientReviewVCS_git implements PIFRClientReviewVCS {
     // and removes the '+++ b/' from them, returning the filenames that have
     // had content added.
     $patch_level = $this->patch_level;
-    $regex = '%^[\+-]{3}\s+([^/]\/){' . $this->patch_level . '}(.*?)\s%m';
+    $regex = '%^[\+-]{3}\s+([^/]\/){' . $patch_level . '}(.*?)\s%m';
     watchdog('pifr', 'get_changed_files; regex=%regex', array('%regex' => $regex));
     preg_match_all($regex, $contents, $matches, PREG_SET_ORDER);
-    watchdog('pifr', 'get_changed_files matches=%matches', array('%matches'=> print_r($matches, TRUE)));
+    watchdog('pifr', 'get_changed_files matches=<pre>%matches</pre>', array('%matches'=> print_r($matches, TRUE)));
 
     $files = array();
     foreach ($matches as $match) {
-      if ($match[1] != '/dev/null') {
-        $files[] = $match[1];
+      // $match[2] is the file. $match[1] is the prefixes.
+      if ($match[2] != '/dev/null') {
+        $files[] = $match[2];
       }
     }
     return array_unique($files);
-- 
1.7.0.4


From 2e24bdecbe05f4b25cf525043f109f8441c54bd4 Mon Sep 17 00:00:00 2001
From: rfay <randy@randyfay.com>
Date: Thu, 17 Feb 2011 22:56:41 -0700
Subject: [PATCH 8/8] Improve comments

---
 client/review/vcs/git.inc |   13 +++++++++++--
 1 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/client/review/vcs/git.inc b/client/review/vcs/git.inc
index b4ec387..5559e0a 100644
--- a/client/review/vcs/git.inc
+++ b/client/review/vcs/git.inc
@@ -54,6 +54,17 @@ class PIFRClientReviewVCS_git implements PIFRClientReviewVCS {
     return FALSE;
   }
 
+  /**
+   * Inspect the patch to determine which files should have changed.
+   *
+   * $this->patch_level has already been initialized by $this->apply(), and
+   * is the number that was successfully used in "patch -p<number>".
+   * We use this to determine how much to strip out of the +++ line.
+   *
+   * @param $patch
+   *   The patch which was applied. (It has to have already been applied to
+   *   initialize $this->patch_level.
+   */
   public function get_changed_files($patch) {
     $contents = file_get_contents($patch);
     // This regex looks through the patch file for lines of the format
@@ -62,9 +73,7 @@ class PIFRClientReviewVCS_git implements PIFRClientReviewVCS {
     // had content added.
     $patch_level = $this->patch_level;
     $regex = '%^[\+-]{3}\s+([^/]\/){' . $patch_level . '}(.*?)\s%m';
-    watchdog('pifr', 'get_changed_files; regex=%regex', array('%regex' => $regex));
     preg_match_all($regex, $contents, $matches, PREG_SET_ORDER);
-    watchdog('pifr', 'get_changed_files matches=<pre>%matches</pre>', array('%matches'=> print_r($matches, TRUE)));
 
     $files = array();
     foreach ($matches as $match) {
-- 
1.7.0.4

