##// END OF EJS Templates
cat: add formatter support...
Yuya Nishihara -
r32578:746e12a7 default
parent child Browse files
Show More
@@ -2632,21 +2632,21 b' def remove(ui, repo, m, prefix, after, f'
2632
2632
2633 return ret
2633 return ret
2634
2634
2635 def cat(ui, repo, ctx, matcher, fntemplate, prefix, **opts):
2635 def cat(ui, repo, ctx, matcher, basefm, fntemplate, prefix, **opts):
2636 err = 1
2636 err = 1
2637
2637
2638 def write(path):
2638 def write(path):
2639 filename = None
2639 if fntemplate:
2640 if fntemplate:
2640 filename = makefilename(repo, fntemplate, ctx.node(),
2641 filename = makefilename(repo, fntemplate, ctx.node(),
2641 pathname=os.path.join(prefix, path))
2642 pathname=os.path.join(prefix, path))
2642 fp = open(filename, 'wb')
2643 with formatter.maybereopen(basefm, filename, opts) as fm:
2643 else:
2644 fp = _unclosablefile(ui.fout)
2645 with fp:
2646 data = ctx[path].data()
2644 data = ctx[path].data()
2647 if opts.get('decode'):
2645 if opts.get('decode'):
2648 data = repo.wwritedata(path, data)
2646 data = repo.wwritedata(path, data)
2649 fp.write(data)
2647 fm.startitem()
2648 fm.write('data', '%s', data)
2649 fm.data(abspath=path, path=matcher.rel(path))
2650
2650
2651 # Automation often uses hg cat on single files, so special case it
2651 # Automation often uses hg cat on single files, so special case it
2652 # for performance to avoid the cost of parsing the manifest.
2652 # for performance to avoid the cost of parsing the manifest.
@@ -2670,7 +2670,7 b' def cat(ui, repo, ctx, matcher, fntempla'
2670 try:
2670 try:
2671 submatch = matchmod.subdirmatcher(subpath, matcher)
2671 submatch = matchmod.subdirmatcher(subpath, matcher)
2672
2672
2673 if not sub.cat(submatch, fntemplate,
2673 if not sub.cat(submatch, basefm, fntemplate,
2674 os.path.join(prefix, sub._path), **opts):
2674 os.path.join(prefix, sub._path), **opts):
2675 err = 0
2675 err = 0
2676 except error.RepoLookupError:
2676 except error.RepoLookupError:
@@ -35,6 +35,7 b' from . import ('
35 error,
35 error,
36 exchange,
36 exchange,
37 extensions,
37 extensions,
38 formatter,
38 graphmod,
39 graphmod,
39 hbisect,
40 hbisect,
40 help,
41 help,
@@ -1338,7 +1339,7 b' def bundle(ui, repo, fname, dest=None, *'
1338 _('print output to file with formatted name'), _('FORMAT')),
1339 _('print output to file with formatted name'), _('FORMAT')),
1339 ('r', 'rev', '', _('print the given revision'), _('REV')),
1340 ('r', 'rev', '', _('print the given revision'), _('REV')),
1340 ('', 'decode', None, _('apply any matching decode filter')),
1341 ('', 'decode', None, _('apply any matching decode filter')),
1341 ] + walkopts,
1342 ] + walkopts + formatteropts,
1342 _('[OPTION]... FILE...'),
1343 _('[OPTION]... FILE...'),
1343 inferrepo=True)
1344 inferrepo=True)
1344 def cat(ui, repo, file1, *pats, **opts):
1345 def cat(ui, repo, file1, *pats, **opts):
@@ -1368,9 +1369,13 b' def cat(ui, repo, file1, *pats, **opts):'
1368 if cmdutil.isstdiofilename(fntemplate):
1369 if cmdutil.isstdiofilename(fntemplate):
1369 fntemplate = ''
1370 fntemplate = ''
1370
1371
1371 if not fntemplate:
1372 if fntemplate:
1373 fm = formatter.nullformatter(ui, 'cat')
1374 else:
1372 ui.pager('cat')
1375 ui.pager('cat')
1373 return cmdutil.cat(ui, repo, ctx, m, fntemplate, '', **opts)
1376 fm = ui.formatter('cat', opts)
1377 with fm:
1378 return cmdutil.cat(ui, repo, ctx, m, fm, fntemplate, '', **opts)
1374
1379
1375 @command('^clone',
1380 @command('^clone',
1376 [('U', 'noupdate', None, _('the clone will include an empty working '
1381 [('U', 'noupdate', None, _('the clone will include an empty working '
@@ -538,7 +538,7 b' class abstractsubrepo(object):'
538 self.ui.warn("%s: %s" % (prefix, _("addremove is not supported")))
538 self.ui.warn("%s: %s" % (prefix, _("addremove is not supported")))
539 return 1
539 return 1
540
540
541 def cat(self, match, fntemplate, prefix, **opts):
541 def cat(self, match, fm, fntemplate, prefix, **opts):
542 return 1
542 return 1
543
543
544 def status(self, rev2, **opts):
544 def status(self, rev2, **opts):
@@ -767,11 +767,11 b' class hgsubrepo(abstractsubrepo):'
767 dry_run, similarity)
767 dry_run, similarity)
768
768
769 @annotatesubrepoerror
769 @annotatesubrepoerror
770 def cat(self, match, fntemplate, prefix, **opts):
770 def cat(self, match, fm, fntemplate, prefix, **opts):
771 rev = self._state[1]
771 rev = self._state[1]
772 ctx = self._repo[rev]
772 ctx = self._repo[rev]
773 return cmdutil.cat(self.ui, self._repo, ctx, match, fntemplate, prefix,
773 return cmdutil.cat(self.ui, self._repo, ctx, match, fm, fntemplate,
774 **opts)
774 prefix, **opts)
775
775
776 @annotatesubrepoerror
776 @annotatesubrepoerror
777 def status(self, rev2, **opts):
777 def status(self, rev2, **opts):
@@ -1833,7 +1833,7 b' class gitsubrepo(abstractsubrepo):'
1833
1833
1834
1834
1835 @annotatesubrepoerror
1835 @annotatesubrepoerror
1836 def cat(self, match, fntemplate, prefix, **opts):
1836 def cat(self, match, fm, fntemplate, prefix, **opts):
1837 rev = self._state[1]
1837 rev = self._state[1]
1838 if match.anypats():
1838 if match.anypats():
1839 return 1 #No support for include/exclude yet
1839 return 1 #No support for include/exclude yet
@@ -1841,6 +1841,7 b' class gitsubrepo(abstractsubrepo):'
1841 if not match.files():
1841 if not match.files():
1842 return 1
1842 return 1
1843
1843
1844 # TODO: add support for non-plain formatter (see cmdutil.cat())
1844 for f in match.files():
1845 for f in match.files():
1845 output = self._gitcommand(["show", "%s:%s" % (rev, f)])
1846 output = self._gitcommand(["show", "%s:%s" % (rev, f)])
1846 fp = cmdutil.makefileobj(self._subparent, fntemplate,
1847 fp = cmdutil.makefileobj(self._subparent, fntemplate,
@@ -63,6 +63,46 b' Test fileset'
63 tmp/h_45116003780e
63 tmp/h_45116003780e
64 tmp/r_2
64 tmp/r_2
65
65
66 Test template output
67
68 $ hg --cwd tmp cat ../b ../c -T '== {path} ({abspath}) ==\n{data}'
69 == ../b (b) == (glob)
70 1
71 == ../c (c) == (glob)
72 3
73
74 $ hg cat b c -Tjson --output -
75 [
76 {
77 "abspath": "b",
78 "data": "1\n",
79 "path": "b"
80 },
81 {
82 "abspath": "c",
83 "data": "3\n",
84 "path": "c"
85 }
86 ]
87
88 $ hg cat b c -Tjson --output 'tmp/%p.json'
89 $ cat tmp/b.json
90 [
91 {
92 "abspath": "b",
93 "data": "1\n",
94 "path": "b"
95 }
96 ]
97 $ cat tmp/c.json
98 [
99 {
100 "abspath": "c",
101 "data": "3\n",
102 "path": "c"
103 }
104 ]
105
66 Test working directory
106 Test working directory
67
107
68 $ echo b-wdir > b
108 $ echo b-wdir > b
@@ -241,7 +241,7 b' Show all commands + options'
241 branch: force, clean
241 branch: force, clean
242 branches: active, closed, template
242 branches: active, closed, template
243 bundle: force, rev, branch, base, all, type, ssh, remotecmd, insecure
243 bundle: force, rev, branch, base, all, type, ssh, remotecmd, insecure
244 cat: output, rev, decode, include, exclude
244 cat: output, rev, decode, include, exclude, template
245 config: untrusted, edit, local, global, template
245 config: untrusted, edit, local, global, template
246 copy: after, force, include, exclude, dry-run
246 copy: after, force, include, exclude, dry-run
247 debugancestor:
247 debugancestor:
@@ -99,9 +99,9 b' test generic hooks'
99 abort: pre-identify hook exited with status 1
99 abort: pre-identify hook exited with status 1
100 [255]
100 [255]
101 $ hg cat b
101 $ hg cat b
102 pre-cat hook: HG_ARGS=cat b HG_HOOKNAME=pre-cat HG_HOOKTYPE=pre-cat HG_OPTS={'decode': None, 'exclude': [], 'include': [], 'output': '', 'rev': ''} HG_PATS=['b']
102 pre-cat hook: HG_ARGS=cat b HG_HOOKNAME=pre-cat HG_HOOKTYPE=pre-cat HG_OPTS={'decode': None, 'exclude': [], 'include': [], 'output': '', 'rev': '', 'template': ''} HG_PATS=['b']
103 b
103 b
104 post-cat hook: HG_ARGS=cat b HG_HOOKNAME=post-cat HG_HOOKTYPE=post-cat HG_OPTS={'decode': None, 'exclude': [], 'include': [], 'output': '', 'rev': ''} HG_PATS=['b'] HG_RESULT=0
104 post-cat hook: HG_ARGS=cat b HG_HOOKNAME=post-cat HG_HOOKTYPE=post-cat HG_OPTS={'decode': None, 'exclude': [], 'include': [], 'output': '', 'rev': '', 'template': ''} HG_PATS=['b'] HG_RESULT=0
105
105
106 $ cd ../b
106 $ cd ../b
107 $ hg pull ../a
107 $ hg pull ../a
@@ -1020,6 +1020,14 b' Prepare a repo with subrepo'
1020 $ hg cat sub/repo/foo
1020 $ hg cat sub/repo/foo
1021 test
1021 test
1022 test
1022 test
1023 $ hg cat sub/repo/foo -Tjson
1024 [
1025 {
1026 "abspath": "foo",
1027 "data": "test\ntest\n",
1028 "path": "sub/repo/foo" (glob)
1029 }
1030 ]
1023 $ mkdir -p tmp/sub/repo
1031 $ mkdir -p tmp/sub/repo
1024 $ hg cat -r 0 --output tmp/%p_p sub/repo/foo
1032 $ hg cat -r 0 --output tmp/%p_p sub/repo/foo
1025 $ cat tmp/sub/repo/foo_p
1033 $ cat tmp/sub/repo/foo_p
General Comments 0
You need to be logged in to leave comments. Login now