You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2012/10/02 23:30:28 UTC

[14/34] git commit: [#5022] Added flag to explicitly indicate whether hooks should be copied

[#5022] Added flag to explicitly indicate whether hooks should be copied

Signed-off-by: Cory Johns <jo...@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/7b836aa5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/7b836aa5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/7b836aa5

Branch: refs/heads/db/4968
Commit: 7b836aa58bbafbbac88bd38062e4a7d728b77860
Parents: a9a8cb2
Author: Cory Johns <jo...@geek.net>
Authored: Mon Oct 1 16:01:53 2012 +0000
Committer: Dave Brondsema <db...@geek.net>
Committed: Mon Oct 1 20:25:54 2012 +0000

----------------------------------------------------------------------
 Allura/allura/model/repository.py                |   12 +++---
 ForgeGit/forgegit/model/git_repo.py              |   11 +++---
 ForgeGit/forgegit/tests/model/test_repository.py |   21 +++++++++++-
 ForgeHg/forgehg/model/hg.py                      |    9 +++--
 ForgeHg/forgehg/tests/model/test_repository.py   |   30 ++++++++++++++++-
 ForgeSVN/forgesvn/model/svn.py                   |    9 +++--
 ForgeSVN/forgesvn/tests/model/test_repository.py |   30 ++++++++++++++++-
 7 files changed, 100 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/7b836aa5/Allura/allura/model/repository.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/repository.py b/Allura/allura/model/repository.py
index b137879..3635cbe 100644
--- a/Allura/allura/model/repository.py
+++ b/Allura/allura/model/repository.py
@@ -46,7 +46,7 @@ class RepositoryImplementation(object):
     def init(self): # pragma no cover
         raise NotImplementedError, 'init'
 
-    def clone_from(self, source_url): # pragma no cover
+    def clone_from(self, source_url, copy_hooks=False): # pragma no cover
         raise NotImplementedError, 'clone_from'
 
     def commit(self, revision): # pragma no cover
@@ -76,7 +76,7 @@ class RepositoryImplementation(object):
         '''Refresh the data in the commit with id oid'''
         raise NotImplementedError, 'refresh_commit_info'
 
-    def _setup_hooks(self, source_path=None): # pragma no cover
+    def _setup_hooks(self, source_path=None, copy_hooks=False): # pragma no cover
         '''Install a hook in the repository that will ping the refresh url for
         the repo.  Optionally provide a path from which to copy existing hooks.'''
         raise NotImplementedError, '_setup_hooks'
@@ -133,12 +133,12 @@ class RepositoryImplementation(object):
                 log.warn('setup_paths error %s' % path, exc_info=True)
         return fullname
 
-    def _setup_special_files(self, source_path=None):
+    def _setup_special_files(self, source_path=None, copy_hooks=False):
         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(source_path)
+        self._setup_hooks(source_path, copy_hooks)
 
 class Repository(Artifact, ActivityObject):
     BATCH_SIZE=100
@@ -214,12 +214,12 @@ class Repository(Artifact, ActivityObject):
         if ci is None: return []
         return ci.log(int(skip), int(max_count))
 
-    def init_as_clone(self, source_path, source_name, source_url):
+    def init_as_clone(self, source_path, source_name, source_url, copy_hooks=False):
         self.upstream_repo.name = source_name
         self.upstream_repo.url = source_url
         session(self).flush(self)
         source = source_path if source_path else source_url
-        self._impl.clone_from(source)
+        self._impl.clone_from(source, copy_hooks)
 
     def log(self, branch='master', offset=0, limit=10):
         return list(self._log(rev=branch, skip=offset, max_count=limit))

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/7b836aa5/ForgeGit/forgegit/model/git_repo.py
----------------------------------------------------------------------
diff --git a/ForgeGit/forgegit/model/git_repo.py b/ForgeGit/forgegit/model/git_repo.py
index 70ecbb7..68887af 100644
--- a/ForgeGit/forgegit/model/git_repo.py
+++ b/ForgeGit/forgegit/model/git_repo.py
@@ -59,7 +59,7 @@ class GitImplementation(M.RepositoryImplementation):
         'curl -s $url\n'
         '\n'
         'DIR="$$(dirname "$${BASH_SOURCE[0]}")"\n'
-        'if [ -x $$DIR/post-receive-user ]; then'
+        'if [ -x $$DIR/post-receive-user ]; then\n'
         '  exec $$DIR/post-receive-user\n'
         'fi')
 
@@ -89,7 +89,7 @@ class GitImplementation(M.RepositoryImplementation):
         self._setup_special_files()
         self._repo.status = 'ready'
 
-    def clone_from(self, source_url):
+    def clone_from(self, source_url, copy_hooks=False):
         '''Initialize a repo as a clone of another'''
         self._repo.status = 'cloning'
         session(self._repo).flush(self._repo)
@@ -104,7 +104,7 @@ class GitImplementation(M.RepositoryImplementation):
                 to_path=fullname,
                 bare=True)
             self.__dict__['_git'] = repo
-            self._setup_special_files(source_url)
+            self._setup_special_files(source_url, copy_hooks)
         except:
             self._repo.status = 'ready'
             session(self._repo).flush(self._repo)
@@ -272,8 +272,10 @@ class GitImplementation(M.RepositoryImplementation):
             target = os.path.join(self._repo.full_fs_path, 'hooks', target_filename)
             shutil.copy2(hook, target)
 
-    def _setup_hooks(self, source_path=None):
+    def _setup_hooks(self, source_path=None, copy_hooks=False):
         'Set up the git post-commit hook'
+        if copy_hooks:
+            self._copy_hooks(source_path)
         text = self.post_receive_template.substitute(
             url=tg.config.get('base_url', 'http://localhost:8080')
             + '/auth/refresh_repo' + self._repo.url())
@@ -281,7 +283,6 @@ class GitImplementation(M.RepositoryImplementation):
         with open(fn, 'w') as fp:
             fp.write(text)
         os.chmod(fn, 0755)
-        self._copy_hooks(source_path)
 
     def _object(self, oid):
         evens = oid[::2]

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/7b836aa5/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 d365199..fe878dc 100644
--- a/ForgeGit/forgegit/tests/model/test_repository.py
+++ b/ForgeGit/forgegit/tests/model/test_repository.py
@@ -129,6 +129,25 @@ class TestGitRepo(unittest.TestCase, RepoImplTestBase):
         repo.init()
         shutil.rmtree(dirname)
 
+    def test_fork(self):
+        repo = GM.Repository(
+            name='testgit.git',
+            fs_path='/tmp/',
+            url_path = '/test/',
+            tool = 'git',
+            status = 'creating')
+        repo_path = pkg_resources.resource_filename(
+            'forgegit', 'tests/data/testgit.git')
+        dirname = os.path.join(repo.fs_path, repo.name)
+        if os.path.exists(dirname):
+            shutil.rmtree(dirname)
+        repo.init()
+        repo._impl.clone_from(repo_path, copy_hooks=False)
+        assert not os.path.exists('/tmp/testgit.git/hooks/update')
+        assert not os.path.exists('/tmp/testgit.git/hooks/post-receive-user')
+        assert os.path.exists('/tmp/testgit.git/hooks/post-receive')
+        assert os.access('/tmp/testgit.git/hooks/post-receive', os.X_OK)
+
     def test_clone(self):
         repo = GM.Repository(
             name='testgit.git',
@@ -142,7 +161,7 @@ class TestGitRepo(unittest.TestCase, RepoImplTestBase):
         if os.path.exists(dirname):
             shutil.rmtree(dirname)
         repo.init()
-        repo._impl.clone_from(repo_path)
+        repo._impl.clone_from(repo_path, copy_hooks=True)
         assert len(repo.log())
         assert os.path.exists('/tmp/testgit.git/hooks/update')
         assert os.access('/tmp/testgit.git/hooks/update', os.X_OK)

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/7b836aa5/ForgeHg/forgehg/model/hg.py
----------------------------------------------------------------------
diff --git a/ForgeHg/forgehg/model/hg.py b/ForgeHg/forgehg/model/hg.py
index a99ea85..ebed574 100644
--- a/ForgeHg/forgehg/model/hg.py
+++ b/ForgeHg/forgehg/model/hg.py
@@ -97,7 +97,7 @@ class HgImplementation(M.RepositoryImplementation):
         self._setup_special_files()
         self._repo.status = 'ready'
 
-    def clone_from(self, source_url):
+    def clone_from(self, source_url, copy_hooks=False):
         '''Initialize a repo as a clone of another'''
         self._repo.status = 'cloning'
         session(self._repo).flush(self._repo)
@@ -114,7 +114,7 @@ class HgImplementation(M.RepositoryImplementation):
                 self._repo.full_fs_path.encode('utf-8'),
                 update=False)
             self.__dict__['_hg'] = repo
-            self._setup_special_files(source_url)
+            self._setup_special_files(source_url, copy_hooks)
         except:
             self._repo.status = 'raise'
             session(self._repo).flush(self._repo)
@@ -300,9 +300,10 @@ class HgImplementation(M.RepositoryImplementation):
             else:
                 shutil.copy2(source, target)
 
-    def _setup_hooks(self, source_path=None):
+    def _setup_hooks(self, source_path=None, copy_hooks=False):
         'Set up the hg changegroup hook'
-        self._copy_hooks(source_path)
+        if copy_hooks:
+            self._copy_hooks(source_path)
         hgrc = os.path.join(self._repo.fs_path, self._repo.name, '.hg', 'hgrc')
         cp = ConfigParser()
         cp.read(hgrc)

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/7b836aa5/ForgeHg/forgehg/tests/model/test_repository.py
----------------------------------------------------------------------
diff --git a/ForgeHg/forgehg/tests/model/test_repository.py b/ForgeHg/forgehg/tests/model/test_repository.py
index e6339a2..488c142 100644
--- a/ForgeHg/forgehg/tests/model/test_repository.py
+++ b/ForgeHg/forgehg/tests/model/test_repository.py
@@ -115,6 +115,34 @@ class TestHgRepo(unittest.TestCase, RepoImplTestBase):
         repo.init()
         shutil.rmtree(dirname)
 
+    def test_fork(self):
+        repo = HM.Repository(
+            name='testrepo.hg',
+            fs_path='/tmp/',
+            url_path = '/test/',
+            tool = 'hg',
+            status = 'creating')
+        repo_path = pkg_resources.resource_filename(
+            'forgehg', 'tests/data/testrepo.hg')
+        dirname = os.path.join(repo.fs_path, repo.name)
+        if os.path.exists(dirname):
+            shutil.rmtree(dirname)
+        repo.init()
+        repo._impl.clone_from(repo_path, copy_hooks=False)
+        assert len(repo.log())
+        assert not os.path.exists('/tmp/testrepo.hg/.hg/external-changegroup')
+        assert not os.path.exists('/tmp/testrepo.hg/.hg/nested/nested-file')
+        assert os.path.exists('/tmp/testrepo.hg/.hg/hgrc')
+        cp = ConfigParser()
+        cp.read('/tmp/testrepo.hg/.hg/hgrc')
+        assert not cp.has_section('other')
+        assert cp.has_section('hooks')
+        assert not cp.has_option('hooks', 'changegroup.external')
+        assert not cp.has_option('hooks', 'commit')
+        self.assertEquals(cp.get('hooks', 'changegroup.sourceforge'), 'curl -s http://localhost//auth/refresh_repo/p/test/src-hg/')
+        assert not os.path.exists('/tmp/testrepo.hg/.hg/undo.branch')
+        shutil.rmtree(dirname)
+
     def test_clone(self):
         repo = HM.Repository(
             name='testrepo.hg',
@@ -128,7 +156,7 @@ class TestHgRepo(unittest.TestCase, RepoImplTestBase):
         if os.path.exists(dirname):
             shutil.rmtree(dirname)
         repo.init()
-        repo._impl.clone_from(repo_path)
+        repo._impl.clone_from(repo_path, copy_hooks=True)
         assert len(repo.log())
         assert os.path.exists('/tmp/testrepo.hg/.hg/external-changegroup')
         assert os.access('/tmp/testrepo.hg/.hg/external-changegroup', os.X_OK)

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/7b836aa5/ForgeSVN/forgesvn/model/svn.py
----------------------------------------------------------------------
diff --git a/ForgeSVN/forgesvn/model/svn.py b/ForgeSVN/forgesvn/model/svn.py
index 0a59dea..5e6cb81 100644
--- a/ForgeSVN/forgesvn/model/svn.py
+++ b/ForgeSVN/forgesvn/model/svn.py
@@ -144,7 +144,7 @@ class SVNImplementation(M.RepositoryImplementation):
             self._repo._impl._svn.checkin([fullname+'/tmp/trunk',fullname+'/tmp/tags',fullname+'/tmp/branches'],'Initial commit')
             shutil.rmtree(fullname+'/tmp')
 
-    def clone_from(self, source_url):
+    def clone_from(self, source_url, copy_hooks=False):
         '''Initialize a repo as a clone of another using svnsync'''
         self.init(default_dirs=False, skip_special_files=True)
         # Need a pre-revprop-change hook for cloning
@@ -175,7 +175,7 @@ class SVNImplementation(M.RepositoryImplementation):
                           c.app.config.options['checkout_url'])):
             c.app.config.options['checkout_url'] = ""
         self._repo.refresh(notify=False)
-        self._setup_special_files(source_url)
+        self._setup_special_files(source_url, copy_hooks)
 
     def refresh_heads(self):
         info = self._svn.info2(
@@ -434,9 +434,10 @@ class SVNImplementation(M.RepositoryImplementation):
             target = os.path.join(self._repo.full_fs_path, 'hooks', target_filename)
             shutil.copy2(hook, target)
 
-    def _setup_hooks(self, source_path=None):
+    def _setup_hooks(self, source_path=None, copy_hooks=False):
         'Set up the post-commit and pre-revprop-change hooks'
-        self._copy_hooks(source_path)
+        if copy_hooks:
+            self._copy_hooks(source_path)
         # setup a post-commit hook to notify Allura of changes to the repo
         # the hook should also call the user-defined post-commit-user hook
         text = self.post_receive_template.substitute(

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/7b836aa5/ForgeSVN/forgesvn/tests/model/test_repository.py
----------------------------------------------------------------------
diff --git a/ForgeSVN/forgesvn/tests/model/test_repository.py b/ForgeSVN/forgesvn/tests/model/test_repository.py
index e93ec8c..44b18b9 100644
--- a/ForgeSVN/forgesvn/tests/model/test_repository.py
+++ b/ForgeSVN/forgesvn/tests/model/test_repository.py
@@ -107,6 +107,34 @@ class TestSVNRepo(unittest.TestCase, RepoImplTestBase):
         repo.init()
         shutil.rmtree(dirname)
 
+    def test_fork(self):
+        repo = SM.Repository(
+            name='testsvn',
+            fs_path='/tmp/',
+            url_path = '/test/',
+            tool = 'svn',
+            status = 'creating')
+        repo_path = pkg_resources.resource_filename(
+            'forgesvn', 'tests/data/testsvn')
+        dirname = os.path.join(repo.fs_path, repo.name)
+        if os.path.exists(dirname):
+            shutil.rmtree(dirname)
+        repo.init()
+        repo._impl.clone_from('file://' + repo_path, copy_hooks=False)
+        assert len(repo.log())
+        assert os.path.exists('/tmp/testsvn/hooks/pre-revprop-change')
+        assert os.access('/tmp/testsvn/hooks/pre-revprop-change', os.X_OK)
+        with open('/tmp/testsvn/hooks/pre-revprop-change') as f: c = f.read()
+        self.assertEqual(c, '#!/bin/sh\n')
+        assert not os.path.exists('/tmp/testsvn/hooks/post-revprop-change')
+        assert not os.path.exists('/tmp/testsvn/hooks/post-commit-user')
+        assert os.path.exists('/tmp/testsvn/hooks/post-commit')
+        assert os.access('/tmp/testsvn/hooks/post-commit', os.X_OK)
+        with open('/tmp/testsvn/hooks/post-commit') as f: c = f.read()
+        self.assertIn('curl -s http://localhost//auth/refresh_repo/p/test/src/\n', c)
+        self.assertIn('exec $DIR/post-commit-user "$@"\n', c)
+        shutil.rmtree(dirname)
+
     def test_clone(self):
         repo = SM.Repository(
             name='testsvn',
@@ -120,7 +148,7 @@ class TestSVNRepo(unittest.TestCase, RepoImplTestBase):
         if os.path.exists(dirname):
             shutil.rmtree(dirname)
         repo.init()
-        repo._impl.clone_from('file://' + repo_path)
+        repo._impl.clone_from('file://' + repo_path, copy_hooks=True)
         assert len(repo.log())
         assert os.path.exists('/tmp/testsvn/hooks/pre-revprop-change')
         assert os.access('/tmp/testsvn/hooks/pre-revprop-change', os.X_OK)