From 24dd355543092c83848e106983bc157d3704b9dd Mon Sep 17 00:00:00 2001
From: Darren Oh <darrenoh@30772.no-reply.drupal.org>
Date: Sun, 24 Jun 2018 02:09:32 -0400
Subject: [PATCH 1/2] Issue #2395149 by Darren Oh: Fix missing version error

---
 git_deploy.module | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/git_deploy.module b/git_deploy.module
index d9b3a08..74d3342 100644
--- a/git_deploy.module
+++ b/git_deploy.module
@@ -53,7 +53,7 @@ function git_deploy_system_info_alter(array &$info, Extension $file, $type) {
       if ($branch) {
         $branch = $branch[0];
         // Any Drupal-formatted branch.
-        $branch_preg =  '\d+\.x-\d+\.';
+        $branch_preg = $is_core ? '\d+.\d+\.' : '\d+\.x-\d+\.';
         if (preg_match('/^' . $branch_preg . 'x$/', $branch)) {
           $info['version'] = $branch . '-dev';
           // Nail down the core and the major version now that we know
-- 
2.17.2 (Apple Git-113)


From 45e2d1073a7353d718dba0a7919173c160334250 Mon Sep 17 00:00:00 2001
From: Darren Oh <darrenoh@30772.no-reply.drupal.org>
Date: Sun, 21 Apr 2019 12:07:18 -0400
Subject: [PATCH 2/2] Issue #1254200 by Darren Oh: Fix dev release datestamp

---
 git_deploy.module | 109 +++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 98 insertions(+), 11 deletions(-)

diff --git a/git_deploy.module b/git_deploy.module
index 74d3342..c72745d 100644
--- a/git_deploy.module
+++ b/git_deploy.module
@@ -6,6 +6,8 @@
  */
 
 use Drupal\Core\Extension\Extension;
+use Drupal\update\UpdateFetcherInterface;
+use Drupal\update\UpdateManagerInterface;
 
 /**
  * Implements hook_system_info_alter().
@@ -48,10 +50,9 @@ function git_deploy_system_info_alter(array &$info, Extension $file, $type) {
         $info['project'] = $project_name;
       }
       // Try to fill in branch and tag.
-      exec("$git rev-parse --abbrev-ref HEAD 2>&1", $branch);
+      $branch = exec("$git rev-parse --abbrev-ref HEAD 2>&1");
       $tag_found = FALSE;
       if ($branch) {
-        $branch = $branch[0];
         // Any Drupal-formatted branch.
         $branch_preg = $is_core ? '\d+.\d+\.' : '\d+\.x-\d+\.';
         if (preg_match('/^' . $branch_preg . 'x$/', $branch)) {
@@ -61,23 +62,70 @@ function git_deploy_system_info_alter(array &$info, Extension $file, $type) {
           $branch_preg = preg_quote(substr($branch, 0, -1));
         }
         // Now try to find a tag.
-        exec("$git rev-list --topo-order --max-count=1 HEAD 2>&1", $last_tag_hash);
+        $last_tag_hash = exec("$git rev-list --topo-order --max-count=1 HEAD 2>&1");
         if ($last_tag_hash) {
-          exec("$git describe  --tags $last_tag_hash[0] 2>&1", $last_tag);
+          $last_tag = exec("$git describe --tags $last_tag_hash 2>&1");
           if ($last_tag) {
-            $last_tag = $last_tag[0];
             // Make sure the tag starts as Drupal formatted (for eg.
             // 7.x-1.0-alpha1) and if we are on a proper branch (ie. not
             // master) then it's on that branch.
             if (!$is_core) {
-              $tag_preg = '/^(' . $branch_preg . '\d+(?:-[^-]+)?)(-(\d+-)g[0-9a-f]{7})?$/';
+              $tag_preg = '/^(' . $branch_preg . '\d+(?:-[^-]+)?)(-(\d+-)g[0-9a-f]{7,})?$/';
             }
             else {
-              $tag_preg = '/^(\d\.\d\.\d+(?:-[^-]+)?)(-(\d+-)g[0-9a-f]{7})?$/';
+              $tag_preg = '/^(\d+\.\d+\.\d+(?:-[^-]+)?)(-(\d+-)g[0-9a-f]{7,})?$/';
             }
             if (preg_match($tag_preg, $last_tag, $matches)) {
               $tag_found = TRUE;
-              $info['version'] = isset($matches[2]) ? $matches[1] . '+' . $matches[3] . 'dev' : $last_tag;
+              if (isset($matches[2])) {
+                // Retrieve all remote branches that contain this tag.
+                exec("$git branch -r --contains $matches[1] --format='%(refname:lstrip=3)'", $branches);
+                // Filter branches by core compatibility.
+                if ($is_core) {
+                  $branches = preg_grep('/^\d+\.\d+\.x$/', $branches);
+                }
+                else {
+                  $branches = preg_filter('/^' . preg_quote($info['core']) . '-/', '', $branches);
+                }
+                if (!empty($branches)) {
+                  // Select branch that has the fewest differences.
+                  usort($branches, 'version_compare');
+                  foreach ($branches as $key => $branch) {
+                    if (!$is_core) {
+                      $branch = "$info[core]-$branch";
+                    }
+                    // Get sum of local and remote differences.
+                    $difference = exec("$git rev-list --count HEAD ^origin/$branch");
+                    $difference += exec("$git rev-list --count ^HEAD origin/$branch");
+                    // If the number of differences increased, stop looking.
+                    if (isset($last_difference) && $difference > $last_difference) {
+                      $branch = $last_branch;
+                      break;
+                    }
+                    $last_difference = $difference;
+                    $last_branch = $branch;
+                  }
+                  // Find most recent common commit between local and remote
+                  // branch.
+                  $base = exec("$git merge-base HEAD origin/$branch");
+                  if ($base == $last_tag_hash) {
+                    // Our commits are based on a tag.
+                    $last_tag = $matches[1];
+                    $info['version'] = $last_tag;
+                  }
+                  else {
+                    // Our commits are based on a branch.
+                    $last_tag = $base;
+                    $info['version'] = "$branch-dev";
+                  }
+                }
+                else {
+                  $info['version'] = $matches[1] . '+' . $matches[3] . 'dev';
+                }
+              }
+              else {
+                $info['version'] = $last_tag;
+              }
             }
           }
         }
@@ -88,9 +136,9 @@ function git_deploy_system_info_alter(array &$info, Extension $file, $type) {
       // The git log -1 command always succeeds and if we are not on a
       // tag this will happen to return the time of the last commit which
       // is exactly what we wanted.
-      exec("$git log -1 --pretty=format:%at $last_tag 2>&1", $datestamp);
-      if ($datestamp && is_numeric($datestamp[0])) {
-        $info['datestamp'] = $datestamp[0];
+      $datestamp = exec("$git log -1 --pretty=format:%at $last_tag 2>&1");
+      if (is_numeric($datestamp)) {
+        $info['datestamp'] = $datestamp;
       }
 
       if ($is_core) {
@@ -100,3 +148,42 @@ function git_deploy_system_info_alter(array &$info, Extension $file, $type) {
     }
   }
 }
+
+/**
+ * Implements hook_update_status_alter().
+ */
+function git_deploy_update_status_alter(&$projects) {
+  foreach ($projects as $project => &$project_info) {
+    // Override the update status on dev branches.
+    if ($project_info['install_type'] == 'dev' && $project_info['status'] != UpdateFetcherInterface::NOT_CHECKED) {
+      $directory = drupal_get_path($project_info['project_type'], key($project_info['includes']));
+      while ($directory && !file_exists("$directory/.git")) {
+        $directory = substr($directory, 0,  strrpos($directory, '/'));
+      }
+      $git_dir = DRUPAL_ROOT . (empty($directory) ? '' : '/') . "$directory/.git";
+      $version = $project_info['existing_version'];
+      if (file_exists($git_dir) && isset($project_info['releases'][$version])) {
+        $release = $project_info['releases'][$version];
+        if (isset($release['tag'])) {
+          $git = "git --git-dir $git_dir";
+          // Fetch latest commit from origin.
+          exec("$git fetch origin 2>&1");
+          $last_commit = exec("$git log -1 --pretty=format:%H origin/$release[tag] 2>&1");
+          // See if local branch contains latest commit.
+          $branch = exec("$git branch --contains $last_commit --format='%(refname:short)' 2>&1");
+          // We still need to compare commit time to release time because this
+          // repository may have been cloned from a copy of the upstream
+          // repository. Allow a 12 hour time difference between release and
+          // last commit, because dev releases are packaged only twice a day.
+          // Add a 100-second buffer to account for packaging time.
+          if (!empty($branch) && $project_info['datestamp'] + 43200 + 100 > $release['date']) {
+            $project_info['status'] = UpdateManagerInterface::CURRENT;
+          }
+          else {
+            $project_data['status'] = UpdateManagerInterface::NOT_CURRENT;
+          }
+        }
+      }
+    }
+  }
+}
-- 
2.17.2 (Apple Git-113)

