# HG changeset patch # User Boris Feld # Date 1513326616 -3600 # Node ID 6580cf7514186f0ac866a4b0e5b25dd6b313d15e # Parent 4c3a4bb31c0e3d9b8920b4c9b64ae930b1fe52ce debug: add a 'debugdownload' command This command resolve and fetch and URL through the Mercurial logic. Mercurial logic add various headers (including authentication) while resolving an URL so the commands helps with building the same request Mercurial would be doing. A new test file is created because we'll add more logic regarding Mercurial download logic and it will grow to a reasonable size. diff -r 4c3a4bb31c0e -r 6580cf751418 mercurial/debugcommands.py --- a/mercurial/debugcommands.py Sat Apr 01 17:12:48 2017 +0900 +++ b/mercurial/debugcommands.py Fri Dec 15 09:30:16 2017 +0100 @@ -69,6 +69,7 @@ templater, treediscovery, upgrade, + url as urlmod, util, vfs as vfsmod, ) @@ -786,6 +787,30 @@ localrevs = opts['rev'] doit(localrevs, remoterevs) +_chunksize = 4 << 10 + +@command('debugdownload', + [ + ('o', 'output', '', _('path')), + ], + norepo=True) +def debugdownload(ui, url, output=None, **opts): + """download a resource using Mercurial logic and config + """ + fh = urlmod.open(ui, url, output) + + dest = ui + if output: + dest = open(output, "wb", _chunksize) + try: + data = fh.read(_chunksize) + while data: + dest.write(data) + data = fh.read(_chunksize) + finally: + if output: + dest.close() + @command('debugextensions', cmdutil.formatteropts, [], norepo=True) def debugextensions(ui, **opts): '''show information about active extensions''' diff -r 4c3a4bb31c0e -r 6580cf751418 tests/test-completion.t --- a/tests/test-completion.t Sat Apr 01 17:12:48 2017 +0900 +++ b/tests/test-completion.t Fri Dec 15 09:30:16 2017 +0100 @@ -85,6 +85,7 @@ debugdeltachain debugdirstate debugdiscovery + debugdownload debugextensions debugfileset debugformat @@ -263,6 +264,7 @@ debugdeltachain: changelog, manifest, dir, template debugdirstate: nodates, datesort debugdiscovery: old, nonheads, rev, ssh, remotecmd, insecure + debugdownload: output debugextensions: template debugfileset: rev debugformat: template diff -r 4c3a4bb31c0e -r 6580cf751418 tests/test-help.t --- a/tests/test-help.t Sat Apr 01 17:12:48 2017 +0900 +++ b/tests/test-help.t Fri Dec 15 09:30:16 2017 +0100 @@ -919,6 +919,8 @@ show the contents of the current dirstate debugdiscovery runs the changeset discovery protocol in isolation + debugdownload + download a resource using Mercurial logic and config debugextensions show information about active extensions debugfileset parse and apply a fileset specification diff -r 4c3a4bb31c0e -r 6580cf751418 tests/test-url-download.t --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test-url-download.t Fri Dec 15 09:30:16 2017 +0100 @@ -0,0 +1,36 @@ +#require serve + + $ hg init server + $ hg serve -R server -p $HGPORT -d --pid-file=hg1.pid -E ../error.log + $ cat hg1.pid >> $DAEMON_PIDS + +Check basic fetching + + $ hg debugdownload "http://localhost:$HGPORT/?cmd=lookup&key=tip" + 1 0000000000000000000000000000000000000000 + $ hg debugdownload -o null.txt "http://localhost:$HGPORT/?cmd=lookup&key=null" + $ cat null.txt + 1 0000000000000000000000000000000000000000 + +Check the request is made from the usual Mercurial logic +(rev details, give different content if the request has a Mercurial user agent) + + $ get-with-headers.py --headeronly "localhost:$HGPORT" "rev/tip" content-type + 200 Script output follows + content-type: text/html; charset=ascii + $ hg debugdownload "http://localhost:$HGPORT/rev/tip" + + # HG changeset patch + # User + # Date 0 0 + # Node ID 0000000000000000000000000000000000000000 + + + + + +Check other kind of compatible url + + $ hg debugdownload ./null.txt + 1 0000000000000000000000000000000000000000 +