Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/cmdutil.py @ 35662:91f0979f16c0
cat: factor out a function that populates the formatter
This will allow extensions to add data to the templater.
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Tue, 16 Jan 2018 19:56:00 -0500 |
parents | b6b7855c79aa |
children | 8273c1a47282 |
comparison
equal
deleted
inserted
replaced
35661:1c0ee0befba0 | 35662:91f0979f16c0 |
---|---|
2960 for warning in warnings: | 2960 for warning in warnings: |
2961 ui.warn(warning) | 2961 ui.warn(warning) |
2962 | 2962 |
2963 return ret | 2963 return ret |
2964 | 2964 |
2965 def _updatecatformatter(fm, ctx, matcher, path, decode): | |
2966 """Hook for adding data to the formatter used by ``hg cat``. | |
2967 | |
2968 Extensions (e.g., lfs) can wrap this to inject keywords/data, but must call | |
2969 this method first.""" | |
2970 data = ctx[path].data() | |
2971 if decode: | |
2972 data = ctx.repo().wwritedata(path, data) | |
2973 fm.startitem() | |
2974 fm.write('data', '%s', data) | |
2975 fm.data(abspath=path, path=matcher.rel(path)) | |
2976 | |
2965 def cat(ui, repo, ctx, matcher, basefm, fntemplate, prefix, **opts): | 2977 def cat(ui, repo, ctx, matcher, basefm, fntemplate, prefix, **opts): |
2966 err = 1 | 2978 err = 1 |
2967 opts = pycompat.byteskwargs(opts) | 2979 opts = pycompat.byteskwargs(opts) |
2968 | 2980 |
2969 def write(path): | 2981 def write(path): |
2975 try: | 2987 try: |
2976 os.makedirs(os.path.dirname(filename)) | 2988 os.makedirs(os.path.dirname(filename)) |
2977 except OSError: | 2989 except OSError: |
2978 pass | 2990 pass |
2979 with formatter.maybereopen(basefm, filename, opts) as fm: | 2991 with formatter.maybereopen(basefm, filename, opts) as fm: |
2980 data = ctx[path].data() | 2992 _updatecatformatter(fm, ctx, matcher, path, opts.get('decode')) |
2981 if opts.get('decode'): | |
2982 data = repo.wwritedata(path, data) | |
2983 fm.startitem() | |
2984 fm.write('data', '%s', data) | |
2985 fm.data(abspath=path, path=matcher.rel(path)) | |
2986 | 2993 |
2987 # Automation often uses hg cat on single files, so special case it | 2994 # Automation often uses hg cat on single files, so special case it |
2988 # for performance to avoid the cost of parsing the manifest. | 2995 # for performance to avoid the cost of parsing the manifest. |
2989 if len(matcher.files()) == 1 and not matcher.anypats(): | 2996 if len(matcher.files()) == 1 and not matcher.anypats(): |
2990 file = matcher.files()[0] | 2997 file = matcher.files()[0] |