From 934028253e4587ea4b0cd565a899f9b1c984853c Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Thu, 28 Apr 2016 09:54:06 -0500
Subject: [PATCH] cli: deploy: Automatically deploy rewritten files

With the advent of the MANIFEST file, files which are rewritten when
deployed, such as those which have the GIT hash in the query string to force
browser reloading, were no longer deployed unless the content of those files
changed.

This patch adds a flag to the MANIFEST file to indicate that the file was
rewritten when it was deployed. This allows the file to be deployed and
rewritten again when the deployment is run, but also adds an indication
to the console output to distinguish deploying changes as opposed to
deploying rewrites.
---
 include/cli/modules/deploy.php | 26 ++++++++++++++++++++------
 include/cli/modules/unpack.php | 10 +++++++---
 2 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/include/cli/modules/deploy.php b/include/cli/modules/deploy.php
index d57a27b3c..6ca6f1d87 100644
--- a/include/cli/modules/deploy.php
+++ b/include/cli/modules/deploy.php
@@ -126,6 +126,7 @@ class Deployment extends Unpacker {
             return false;
 
         $source = file_get_contents($src);
+        $original = crc32($source);
         $source = preg_replace(':<script(.*) src="([^"]+)\.js"></script>:',
             '<script$1 src="$2.js?'.$short.'"></script>',
             $source);
@@ -145,11 +146,20 @@ class Deployment extends Unpacker {
             "$1ini_set('$2', '0'); // Set by installer",
             $source);
 
-        return $source;
+        // return FALSE if the edited contents do not differ from the
+        // original contents
+        return $original != crc32($source) ? $source : false;
     }
 
-    function copyFile($source, $dest, $hash=false, $mode=0644) {
-        $contents = $this->getEditedContents($source);
+    function isChanged($source, $hash=false) {
+        $local = str_replace($this->source.'/', '', $source);
+        $hash = $hash ?: $this->hashFile($source);
+        list($shash, $flag) = explode(':', $this->readManifest($local));
+        return ($flag === 'rewrite') ? $flag : $shash != $hash;
+    }
+
+    function copyFile($source, $dest, $hash=false, $mode=0644, $contents=false) {
+        $contents = $contents ?: $this->getEditedContents($source);
         if ($contents === false)
             // Regular file
             return parent::copyFile($source, $dest, $hash, $mode);
@@ -157,7 +167,7 @@ class Deployment extends Unpacker {
         if (!file_put_contents($dest, $contents))
             $this->fail($dest.": Unable to apply rewrite rules");
 
-        $this->updateManifest($source, $hash);
+        $this->updateManifest($source, "$hash:rewrite");
         return chmod($dest, $mode);
     }
 
@@ -200,8 +210,12 @@ class Deployment extends Unpacker {
             if (!$force && false === ($flag = $this->isChanged($src, $hash)))
                 continue;
             $dst = $destination.$path;
-            if ($verbose)
-                $this->stdout->write($dst."\n");
+            if ($verbose) {
+                $msg = $dst;
+                if (is_string($flag))
+                    $msg = "$msg ({$flag})";
+                $this->stdout->write("$msg\n");
+            }
             if ($dryrun)
                 continue;
             if (!is_dir(dirname($dst)))
diff --git a/include/cli/modules/unpack.php b/include/cli/modules/unpack.php
index 71973aafe..75fa1090f 100644
--- a/include/cli/modules/unpack.php
+++ b/include/cli/modules/unpack.php
@@ -99,7 +99,7 @@ class Unpacker extends Module {
         if (!is_file($path))
             return null;
 
-        if (!preg_match_all('/^(\w+) (.+)$/mu', file_get_contents($path),
+        if (!preg_match_all('/^([\w:,]+) (.+)$/mu', file_get_contents($path),
             $lines, PREG_PATTERN_ORDER)
         ) {
             return null;
@@ -168,8 +168,12 @@ class Unpacker extends Module {
                 if (!$force && is_file($target)
                         && false === ($flag = $this->isChanged($file, $hash)))
                     continue;
-                if ($verbose)
-                    $this->stdout->write($target."\n");
+                if ($verbose) {
+                    $msg = $target;
+                    if (is_string($flag))
+                        $msg = "$msg ({$flag})";
+                    $this->stdout->write("$msg\n");
+                }
                 if ($dryrun)
                     continue;
                 if (!is_dir($destination))
-- 
GitLab