2
0
Эх сурвалжийг харах

Add some extra checks to the protocol and enchance dul-daemon

John Carr 16 жил өмнө
parent
commit
d87d95c54f
2 өөрчлөгдсөн 28 нэмэгдсэн , 16 устгасан
  1. 11 7
      bin/dul-daemon
  2. 17 9
      dulwich/server.py

+ 11 - 7
bin/dul-daemon

@@ -17,7 +17,7 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 # MA  02110-1301, USA.
 
-import sys, tempfile
+import os, sys, tempfile
 from dulwich.server import Backend, TCPGitServer
 from dulwich.repo import Repo
 from dulwich.pack import PackData, Pack
@@ -31,20 +31,24 @@ class GitBackend(Backend):
             self.gitdir = tempfile.mkdtemp()
             Repo.create(self.gitdir)
 
-        self.repo = Repo(gitdir)
+        self.repo = Repo(self.gitdir)
 
     def get_refs(self):
-	return [('HEAD', self.repo.head())] +  
-           [tuple(refsha) for refsha in self.repo.heads().items()]
+        refs = []
+        if self.repo.head():
+            refs.append(('HEAD', self.repo.head()))
+        for refsha in self.repo.heads().items():
+            refs.append(refsha)
+        return refs
 
     def has_revision(self, sha):
         return False
 
     def apply_pack(self, refs, read):
         # store the incoming pack in the repository
-        fd, name = tempfile.mkstemp(suffix='pack', prefix='', dir=self.repo.packdir())
-        fd.write(read())
-        fd.close()
+        fd, name = tempfile.mkstemp(suffix='pack', prefix='', dir=self.repo.pack_dir())
+        os.write(fd, read())
+        os.close(fd)
 
         # strip '.pack' off our filename
         basename = name[:-5]

+ 17 - 9
dulwich/server.py

@@ -69,7 +69,10 @@ class Handler(object):
 
         :return: The next string from the stream
         """
-        size = int(self.read(4), 16)
+        sizestr = self.read(4)
+        if not sizestr:
+            return None
+        size = int(sizestr, 16)
         if size == 0:
             return None
         return self.read(size-4)
@@ -108,10 +111,11 @@ class UploadPackHandler(Handler):
     def handle(self):
         refs = self.backend.get_refs()
 
-        self.write_pkt_line("%s %s\x00multi_ack side-band-64k thin-pack ofs-delta\n" % (refs[0][1], refs[0][0]))
-        for i in range(1, len(refs)):
-            ref = refs[i]
-            self.write_pkt_line("%s %s\n" % (ref[1], ref[0]))
+        if refs:
+            self.write_pkt_line("%s %s\x00multi_ack side-band-64k thin-pack ofs-delta\n" % (refs[0][1], refs[0][0]))
+            for i in range(1, len(refs)):
+                ref = refs[i]
+                self.write_pkt_line("%s %s\n" % (ref[1], ref[0]))
 
         # i'm done...
         self.write("0000")
@@ -173,10 +177,11 @@ class ReceivePackHandler(Handler):
     def handle(self):
         refs = self.backend.get_refs()
 
-        self.write_pkt_line("%s %s\x00multi_ack side-band-64k thin-pack ofs-delta\n" % (refs[0][1], refs[0][0]))
-        for i in range(1, len(refs)):
-            ref = refs[i]
-            self.write_pkt_line("%s %s\n" % (ref[1], ref[0]))
+        if refs:
+            self.write_pkt_line("%s %s\x00multi_ack side-band-64k thin-pack ofs-delta\n" % (refs[0][1], refs[0][0]))
+            for i in range(1, len(refs)):
+                ref = refs[i]
+                self.write_pkt_line("%s %s\n" % (ref[1], ref[0]))
 
         self.write("0000")
 
@@ -186,6 +191,9 @@ class ReceivePackHandler(Handler):
             client_refs.append(ref.split())
             ref = self.read_pkt_line()
 
+        if len(client_refs) == 0:
+            return None
+
         self.backend.apply_pack(client_refs, self.read)