8 |
8 |
9 import os |
9 import os |
10 import os.path |
10 import os.path |
11 import mimetypes |
11 import mimetypes |
12 from mercurial.demandload import demandload |
12 from mercurial.demandload import demandload |
13 demandload(globals(), "re zlib ConfigParser cStringIO") |
13 demandload(globals(), "re zlib ConfigParser cStringIO sys tempfile") |
14 demandload(globals(), "mercurial:mdiff,ui,hg,util,archival,templater") |
14 demandload(globals(), "mercurial:mdiff,ui,hg,util,archival,templater") |
15 demandload(globals(), "mercurial.hgweb.request:hgrequest") |
15 demandload(globals(), "mercurial.hgweb.request:hgrequest") |
16 demandload(globals(), "mercurial.hgweb.common:get_mtime,staticfile") |
16 demandload(globals(), "mercurial.hgweb.common:get_mtime,staticfile") |
17 from mercurial.node import * |
17 from mercurial.node import * |
18 from mercurial.i18n import gettext as _ |
18 from mercurial.i18n import gettext as _ |
833 "static")) |
833 "static")) |
834 req.write(staticfile(static, fname) |
834 req.write(staticfile(static, fname) |
835 or self.t("error", error="%r not found" % fname)) |
835 or self.t("error", error="%r not found" % fname)) |
836 |
836 |
837 def do_capabilities(self, req): |
837 def do_capabilities(self, req): |
838 resp = '' |
838 resp = 'unbundle' |
839 req.httphdr("application/mercurial-0.1", length=len(resp)) |
839 req.httphdr("application/mercurial-0.1", length=len(resp)) |
840 req.write(resp) |
840 req.write(resp) |
841 |
841 |
|
842 def do_unbundle(self, req): |
|
843 their_heads = req.form['heads'][0].split(' ') |
|
844 |
|
845 def check_heads(): |
|
846 heads = map(hex, self.repo.heads()) |
|
847 return their_heads == [hex('force')] or their_heads == heads |
|
848 |
|
849 req.httphdr("application/mercurial-0.1") |
|
850 |
|
851 # fail early if possible |
|
852 if not check_heads(): |
|
853 req.write('0\n') |
|
854 req.write(_('unsynced changes\n')) |
|
855 return |
|
856 |
|
857 # do not lock repo until all changegroup data is |
|
858 # streamed. save to temporary file. |
|
859 |
|
860 fd, tempname = tempfile.mkstemp(prefix='hg-unbundle-') |
|
861 fp = os.fdopen(fd, 'wb+') |
|
862 try: |
|
863 length = int(req.env['CONTENT_LENGTH']) |
|
864 for s in util.filechunkiter(req, limit=length): |
|
865 fp.write(s) |
|
866 |
|
867 lock = self.repo.lock() |
|
868 try: |
|
869 if not check_heads(): |
|
870 req.write('0\n') |
|
871 req.write(_('unsynced changes\n')) |
|
872 return |
|
873 |
|
874 fp.seek(0) |
|
875 |
|
876 # send addchangegroup output to client |
|
877 |
|
878 old_stdout = sys.stdout |
|
879 sys.stdout = cStringIO.StringIO() |
|
880 |
|
881 try: |
|
882 ret = self.repo.addchangegroup(fp, 'serve') |
|
883 req.write('%d\n' % ret) |
|
884 req.write(sys.stdout.getvalue()) |
|
885 finally: |
|
886 sys.stdout = old_stdout |
|
887 finally: |
|
888 lock.release() |
|
889 finally: |
|
890 fp.close() |
|
891 os.unlink(tempname) |