allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brond...@apache.org
Subject [8/9] git commit: [#2809] Copy existing git hooks when cloning a local repo
Date Wed, 26 Sep 2012 16:19:46 GMT
[#2809] Copy existing git hooks when cloning a local repo

Signed-off-by: Cory Johns <johnsca@geek.net>


Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/3f2cb4de
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/3f2cb4de
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/3f2cb4de

Branch: refs/heads/master
Commit: 3f2cb4de63aa5a8375e18e8478bedcc80bc35f5d
Parents: a87bfb4
Author: Cory Johns <johnsca@geek.net>
Authored: Thu Sep 13 19:42:48 2012 +0000
Committer: Dave Brondsema <dbrondsema@geek.net>
Committed: Wed Sep 26 16:18:55 2012 +0000

----------------------------------------------------------------------
 Allura/allura/model/repository.py                  |    8 ++--
 ForgeGit/forgegit/model/git_repo.py                |   24 ++++++++++++--
 .../tests/data/testgit.git/hooks/post-receive      |    1 +
 .../forgegit/tests/data/testgit.git/hooks/update   |    1 +
 ForgeGit/forgegit/tests/model/test_repository.py   |   10 ++++++
 ForgeHg/forgehg/model/hg.py                        |    2 +-
 ForgeSVN/forgesvn/model/svn.py                     |    2 +-
 7 files changed, 38 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/3f2cb4de/Allura/allura/model/repository.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/repository.py b/Allura/allura/model/repository.py
index 3139b3c..b137879 100644
--- a/Allura/allura/model/repository.py
+++ b/Allura/allura/model/repository.py
@@ -76,9 +76,9 @@ class RepositoryImplementation(object):
         '''Refresh the data in the commit with id oid'''
         raise NotImplementedError, 'refresh_commit_info'
 
-    def _setup_hooks(self): # pragma no cover
+    def _setup_hooks(self, source_path=None): # pragma no cover
         '''Install a hook in the repository that will ping the refresh url for
-        the repo'''
+        the repo.  Optionally provide a path from which to copy existing hooks.'''
         raise NotImplementedError, '_setup_hooks'
 
     def log(self, object_id, skip, count): # pragma no cover
@@ -133,12 +133,12 @@ class RepositoryImplementation(object):
                 log.warn('setup_paths error %s' % path, exc_info=True)
         return fullname
 
-    def _setup_special_files(self):
+    def _setup_special_files(self, source_path=None):
         magic_file = os.path.join(self._repo.fs_path, self._repo.name, '.SOURCEFORGE-REPOSITORY')
         with open(magic_file, 'w') as f:
             f.write(self._repo.repo_id)
         os.chmod(magic_file, stat.S_IRUSR|stat.S_IRGRP|stat.S_IROTH)
-        self._setup_hooks()
+        self._setup_hooks(source_path)
 
 class Repository(Artifact, ActivityObject):
     BATCH_SIZE=100

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/3f2cb4de/ForgeGit/forgegit/model/git_repo.py
----------------------------------------------------------------------
diff --git a/ForgeGit/forgegit/model/git_repo.py b/ForgeGit/forgegit/model/git_repo.py
index 5bf0265..5ca4740 100644
--- a/ForgeGit/forgegit/model/git_repo.py
+++ b/ForgeGit/forgegit/model/git_repo.py
@@ -5,6 +5,7 @@ import logging
 import random
 from collections import namedtuple
 from datetime import datetime
+from glob import glob
 
 import tg
 import git
@@ -53,8 +54,14 @@ class Repository(M.Repository):
 class GitImplementation(M.RepositoryImplementation):
     post_receive_template = string.Template(
         '#!/bin/bash\n'
-        '# The following line is required for site integration, do not remove/modify\n'
-        'curl -s $url\n')
+        '# The following is required for site integration, do not remove/modify.\n'
+        '# Place user hook code in post-receive-user and it will be called from here.\n'
+        'curl -s $url\n'
+        '\n'
+        'DIR="$$(dirname "$${BASH_SOURCE[0]}")"\n'
+        'if [ -x $$DIR/post-receive-user ]; then'
+        '  exec $$DIR/post-receive-user\n'
+        'fi')
 
     def __init__(self, repo):
         self._repo = repo
@@ -97,7 +104,7 @@ class GitImplementation(M.RepositoryImplementation):
                 to_path=fullname,
                 bare=True)
             self.__dict__['_git'] = repo
-            self._setup_special_files()
+            self._setup_special_files(source_url)
         except:
             self._repo.status = 'ready'
             session(self._repo).flush(self._repo)
@@ -255,7 +262,7 @@ class GitImplementation(M.RepositoryImplementation):
     def blob_size(self, blob):
         return self._object(blob._id).data_stream.size
 
-    def _setup_hooks(self):
+    def _setup_hooks(self, source_path=None):
         'Set up the git post-commit hook'
         text = self.post_receive_template.substitute(
             url=tg.config.get('base_url', 'http://localhost:8080')
@@ -264,6 +271,15 @@ class GitImplementation(M.RepositoryImplementation):
         with open(fn, 'w') as fp:
             fp.write(text)
         os.chmod(fn, 0755)
+        # copy existing hooks if source path is given and exists
+        if source_path is not None and os.path.exists(source_path):
+            for hook in glob(os.path.join(source_path, 'hooks/*')):
+                filename = os.path.basename(hook)
+                target_filename = filename
+                if filename == 'post-receive':
+                    target_filename = 'post-receive-user'
+                target = os.path.join(self._repo.full_fs_path, 'hooks', target_filename)
+                shutil.copyfile(hook, target)
 
     def _object(self, oid):
         evens = oid[::2]

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/3f2cb4de/ForgeGit/forgegit/tests/data/testgit.git/hooks/post-receive
----------------------------------------------------------------------
diff --git a/ForgeGit/forgegit/tests/data/testgit.git/hooks/post-receive b/ForgeGit/forgegit/tests/data/testgit.git/hooks/post-receive
new file mode 100644
index 0000000..0f7a148
--- /dev/null
+++ b/ForgeGit/forgegit/tests/data/testgit.git/hooks/post-receive
@@ -0,0 +1 @@
+post-receive

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/3f2cb4de/ForgeGit/forgegit/tests/data/testgit.git/hooks/update
----------------------------------------------------------------------
diff --git a/ForgeGit/forgegit/tests/data/testgit.git/hooks/update b/ForgeGit/forgegit/tests/data/testgit.git/hooks/update
new file mode 100644
index 0000000..4ea5e4d
--- /dev/null
+++ b/ForgeGit/forgegit/tests/data/testgit.git/hooks/update
@@ -0,0 +1 @@
+update

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/3f2cb4de/ForgeGit/forgegit/tests/model/test_repository.py
----------------------------------------------------------------------
diff --git a/ForgeGit/forgegit/tests/model/test_repository.py b/ForgeGit/forgegit/tests/model/test_repository.py
index e6d2753..9c42aad 100644
--- a/ForgeGit/forgegit/tests/model/test_repository.py
+++ b/ForgeGit/forgegit/tests/model/test_repository.py
@@ -144,6 +144,16 @@ class TestGitRepo(unittest.TestCase, RepoImplTestBase):
         repo.init()
         repo._impl.clone_from(repo_path)
         assert len(repo.log())
+        assert os.path.exists('/tmp/testgit.git/hooks/update')
+        with open('/tmp/testgit.git/hooks/update') as f: c = f.read()
+        self.assertEqual(c, 'update\n')
+        assert os.path.exists('/tmp/testgit.git/hooks/post-receive-user')
+        with open('/tmp/testgit.git/hooks/post-receive-user') as f: c = f.read()
+        self.assertEqual(c, 'post-receive\n')
+        assert os.path.exists('/tmp/testgit.git/hooks/post-receive')
+        with open('/tmp/testgit.git/hooks/post-receive') as f: c = f.read()
+        self.assertIn('curl -s http://localhost//auth/refresh_repo/p/test/src-git/\n', c)
+        self.assertIn('exec $DIR/post-receive-user\n', c)
         shutil.rmtree(dirname)
 
     def test_index(self):

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/3f2cb4de/ForgeHg/forgehg/model/hg.py
----------------------------------------------------------------------
diff --git a/ForgeHg/forgehg/model/hg.py b/ForgeHg/forgehg/model/hg.py
index be69c49..229bff0 100644
--- a/ForgeHg/forgehg/model/hg.py
+++ b/ForgeHg/forgehg/model/hg.py
@@ -250,7 +250,7 @@ class HgImplementation(M.RepositoryImplementation):
         fctx = self._hg[blob.commit._id][h.really_unicode(blob.path()).encode('utf-8')[1:]]
         return fctx.size()
 
-    def _setup_hooks(self):
+    def _setup_hooks(self, source_path=None):
         'Set up the hg changegroup hook'
         cp = ConfigParser()
         fn = os.path.join(self._repo.fs_path, self._repo.name, '.hg', 'hgrc')

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/3f2cb4de/ForgeSVN/forgesvn/model/svn.py
----------------------------------------------------------------------
diff --git a/ForgeSVN/forgesvn/model/svn.py b/ForgeSVN/forgesvn/model/svn.py
index 0037bd0..2d530bb 100644
--- a/ForgeSVN/forgesvn/model/svn.py
+++ b/ForgeSVN/forgesvn/model/svn.py
@@ -413,7 +413,7 @@ class SVNImplementation(M.RepositoryImplementation):
 
         return size
 
-    def _setup_hooks(self):
+    def _setup_hooks(self, source_path=None):
         'Set up the post-commit and pre-revprop-change hooks'
         text = self.post_receive_template.substitute(
             url=tg.config.get('base_url', 'http://localhost:8080')


Mime
View raw message