[PATCH] verify pulling from a proper hg repository

Muli Ben-Yehuda mulix at mulix.org
Thu Jul 21 15:26:47 CDT 2005


Hi,

When pulling from a non hg repository URL (e.g. http://www.kernel.org/hg)
you geta pretty obscure error (zlib: uknown compression type). The
attached patch modifies hgweb.py and hg.py to supply and check a
'Content-type: application/hg-0.1' HTTP header for the branches,
between and changegroup commands, so that we know it's a proper hg
repo before snarfing the input. Comments appreciated!

diff -r fed842bb84b2 mercurial/hg.py
--- a/mercurial/hg.py	Tue Jul 19 20:09:04 2005
+++ b/mercurial/hg.py	Thu Jul 21 16:13:17 2005
@@ -1706,10 +1706,20 @@
             self.ui.warn("unexpected response:\n" + d[:400] + "\n...\n")
             raise
 
+    def verify_hg_repo(self, resp):
+        if (resp.headers['content-type'] == 'application/hg-0.1'):
+            pass
+        else:
+            msg = """'%s' does not appear to be a valid hg repository -
+missing a 'Content-type: application/hg-0.1' HTTP header""" % (self.url,)
+            raise RepoError(msg)
+
     def branches(self, nodes):
         n = " ".join(map(hex, nodes))
-        d = self.do_cmd("branches", nodes=n).read()
+        resp = self.do_cmd("branches", nodes=n);
+        self.verify_hg_repo(resp);
         try:
+            d = resp.read()
             br = [ tuple(map(bin, b.split(" "))) for b in d.splitlines() ]
             return br
         except:
@@ -1718,8 +1728,10 @@
 
     def between(self, pairs):
         n = "\n".join(["-".join(map(hex, p)) for p in pairs])
-        d = self.do_cmd("between", pairs=n).read()
+        resp = self.do_cmd("between", pairs=n)
+        self.verify_hg_repo(resp) 
         try:
+            d = resp.read()
             p = [ l and map(bin, l.split(" ")) or [] for l in d.splitlines() ]
             return p
         except:
@@ -1728,7 +1740,8 @@
 
     def changegroup(self, nodes):
         n = " ".join(map(hex, nodes))
-        f = self.do_cmd("changegroup", roots=n)
+        resp = self.do_cmd("changegroup", roots=n)
+        self.verify_hg_repo(resp)
         bytes = 0
 
         class zread:
@@ -1738,7 +1751,7 @@
                 self.buf = ""
             def read(self, l):
                 while l > len(self.buf):
-                    r = f.read(4096)
+                    r = self.f.read(4096)
                     if r:
                         self.buf += self.zd.decompress(r)
                     else:
@@ -1747,7 +1760,7 @@
                 d, self.buf = self.buf[:l], self.buf[l:]
                 return d
 
-        return zread(f)
+        return zread(resp)
 
 class remotelock:
     def __init__(self, repo):
diff -r fed842bb84b2 mercurial/hgweb.py
--- a/mercurial/hgweb.py	Tue Jul 19 20:09:04 2005
+++ b/mercurial/hgweb.py	Thu Jul 21 16:13:17 2005
@@ -662,7 +662,7 @@
             sys.stdout.write(" ".join(map(hex, h)) + "\n")
 
         elif args['cmd'][0] == 'branches':
-            httphdr("text/plain")
+            httphdr("application/hg-0.1")
             nodes = []
             if args.has_key('nodes'):
                 nodes = map(bin, args['nodes'][0].split(" "))
@@ -670,7 +670,7 @@
                 sys.stdout.write(" ".join(map(hex, b)) + "\n")
 
         elif args['cmd'][0] == 'between':
-            httphdr("text/plain")
+            httphdr("application/hg-0.1")
             nodes = []
             if args.has_key('pairs'):
                 pairs = [ map(bin, p.split("-"))
@@ -679,7 +679,7 @@
                 sys.stdout.write(" ".join(map(hex, b)) + "\n")
 
         elif args['cmd'][0] == 'changegroup':
-            httphdr("application/hg-changegroup")
+            httphdr("application/hg-0.1")
             nodes = []
             if self.viewonly:
                 return

-- 
Muli Ben-Yehuda
http://www.mulix.org | http://mulix.livejournal.com/



More information about the Mercurial mailing list