Răsfoiți Sursa

Execute pre-receive hook

Stéphane Klein 10 ani în urmă
părinte
comite
bc13d79c2c
2 a modificat fișierele cu 37 adăugiri și 2 ștergeri
  1. 22 0
      dulwich/hooks.py
  2. 15 2
      dulwich/server.py

+ 22 - 0
dulwich/hooks.py

@@ -142,3 +142,25 @@ class CommitMsgShellHook(ShellHook):
 
         ShellHook.__init__(self, 'commit-msg', filepath, 1,
                            prepare_msg, clean_msg)
+
+
+class PreReceiveShellHook(ShellHook):
+    """pre-receive shell hook"""
+
+    def __init__(self, controldir):
+        filepath = os.path.join(controldir, '.git', 'hooks', 'pre-receive')
+        ShellHook.__init__(self, 'pre-receive', filepath, 0)
+
+    def exists(self):
+        print(self.filepath)
+        return os.path.exists(self.filepath)
+
+    def execute(self, stdin):
+        p = subprocess.Popen(
+            self.filepath,
+            stdin=subprocess.PIPE,
+            stdout=subprocess.PIPE,
+            stderr=subprocess.PIPE
+        )
+        self.stdout, self.stderr = p.communicate(stdin)
+        return p.returncode

+ 15 - 2
dulwich/server.py

@@ -82,6 +82,7 @@ from dulwich.refs import (
 from dulwich.repo import (
     Repo,
     )
+from hooks import PreReceiveShellHook
 
 
 logger = log_utils.getLogger(__name__)
@@ -821,8 +822,20 @@ class ReceivePackHandler(Handler):
             client_refs.append(ref.split())
             ref = self.proto.read_pkt_line()
 
-        # backend can now deal with this refs and read a pack using self.read
-        status = self._apply_pack(client_refs)
+        hook = PreReceiveShellHook(self.repo.path)
+        ret = 0
+        if hook.exists():
+            ret = hook.execute(stdin=' '.join(client_refs[0]))
+            self.proto.write_sideband(2, hook.stdout)
+
+        if ret == 0:
+            # backend can now deal with this refs and read a pack using self.read
+            status = self._apply_pack(client_refs)
+        else:
+            status = [
+                ('unpack', 'ok'),
+                (client_refs[0][-1], 'pre-receive hook declined')
+            ]
 
         # when we have read all the pack from the client, send a status report
         # if the client asked for it