From e9643f8bbf341ca79e6c3dce31d2433db1ae76c5 Mon Sep 17 00:00:00 2001
From: Mark Sonnabaum <mark@sonnabaum.com>
Date: Sat, 26 Feb 2011 20:43:55 -0600
Subject: [PATCH 1/2] First attempt at bringing local git caching to drush pm via: http://randyfay.com/node/93.

---
 commands/pm/package_handler/git_drupalorg.inc |   47 +++++++++++++++++++++++++
 1 files changed, 47 insertions(+), 0 deletions(-)

diff --git commands/pm/package_handler/git_drupalorg.inc commands/pm/package_handler/git_drupalorg.inc
index ca96390..6cd8eb4 100644
--- commands/pm/package_handler/git_drupalorg.inc
+++ commands/pm/package_handler/git_drupalorg.inc
@@ -34,9 +34,18 @@ function package_handler_download_project(&$request, $release) {
   $repository = 'git://git.drupal.org/project/' . $request['name'] . '.git';
   $tag = $release['tag'];
 
+  if (drush_get_option('cache')) {
+    $gitcache = drush_server_home() . '/.drush/gitcache';
+    drush_gitcache_check($gitcache);
+    drush_gitcache_add_project($gitcache, $request, $repository);
+  }
+
   // Clone the repo into its appropriate target location.
   $command  = 'git clone';
   $command .= ' ' . drush_get_option('gitcloneparams');
+  if (drush_get_option('cache')) {
+    $command .= ' --reference ' . $gitcache;
+  }
   $command .= ' ' . drush_escapeshellarg($repository);
   $command .= ' ' . drush_escapeshellarg($request['full_project_path']);
   if (!drush_shell_exec($command)) {
@@ -144,3 +153,41 @@ function package_handler_post_download($project) {
   }
 }
 
+
+function drush_gitcache_check($gitcache) {
+  if (is_dir(drush_server_home() . '/.drush')) {
+    drush_mkdir($gitcache);
+    if (!drush_shell_cd_and_exec($gitcache, 'test $(git rev-parse --is-bare-repository) == "true"')) {
+      drush_shell_cd_and_exec($gitcache, 'git init --bare');
+      drush_shell_cd_and_exec($gitcache, 'git config core.compression 1');
+    }
+  }
+}
+
+function drush_gitcache_add_project($gitcache, $request, $repository) {
+  $tmpdir = drush_tempdir();
+  if (drush_shell_cd_and_exec($tmpdir, 'git clone --bare ' . $repository)) {
+    // Then fetch from the temporary dir into our main repo.
+    $commands = array();
+    $commands[] = 'git remote add';
+    $commands[] = $request['name'];
+    $commands[] = $tmpdir;
+    $commands[] = '&&';
+    $commands[] = 'git fetch';
+    $commands[] = $request['name'];
+    $commands[] = '--tags';
+    drush_print_r($commands);
+    drush_shell_cd_and_exec($gitcache, implode(' ', $commands));
+
+    // Then change the remote URL and fetch normally.
+    $commands = array();
+    $commands[] = 'git remote set-url';
+    $commands[] = $request['name'];
+    $commands[] = $repository;
+    $commands[] = '&&';
+    $commands[] = 'git fetch';
+    $commands[] = $request['name'];
+    $commands[] = '--tags';
+    drush_shell_cd_and_exec($gitcache, implode(' ', $commands));
+  }
+}
-- 
1.7.4.1


From 7d869ff8fba3dc0e51017d540836270fd2507c5a Mon Sep 17 00:00:00 2001
From: Mark Sonnabaum <mark@sonnabaum.com>
Date: Mon, 28 Feb 2011 11:23:03 -0600
Subject: [PATCH 2/2] Updated git package handler to only add a project to the cache if it isn't already there.

---
 commands/pm/package_handler/git_drupalorg.inc |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)

diff --git commands/pm/package_handler/git_drupalorg.inc commands/pm/package_handler/git_drupalorg.inc
index 6cd8eb4..b6269d7 100644
--- commands/pm/package_handler/git_drupalorg.inc
+++ commands/pm/package_handler/git_drupalorg.inc
@@ -37,7 +37,13 @@ function package_handler_download_project(&$request, $release) {
   if (drush_get_option('cache')) {
     $gitcache = drush_server_home() . '/.drush/gitcache';
     drush_gitcache_check($gitcache);
-    drush_gitcache_add_project($gitcache, $request, $repository);
+
+    drush_shell_cd_and_exec($gitcache, 'git remote show'); 
+    $cached_modules = drush_shell_exec_output();
+
+    if (!in_array($request['name'], $cached_modules)) {
+      drush_gitcache_add_project($gitcache, $request, $repository);
+    }
   }
 
   // Clone the repo into its appropriate target location.
@@ -176,7 +182,6 @@ function drush_gitcache_add_project($gitcache, $request, $repository) {
     $commands[] = 'git fetch';
     $commands[] = $request['name'];
     $commands[] = '--tags';
-    drush_print_r($commands);
     drush_shell_cd_and_exec($gitcache, implode(' ', $commands));
 
     // Then change the remote URL and fetch normally.
-- 
1.7.4.1

