##// END OF EJS Templates
fix: add suboption for configuring execution order of tools...
Danny Hooper -
r40599:b9557567 default
parent child Browse files
Show More
@@ -54,6 +54,24 b' will also cause :hg:`fix` to exit with a'
54 [fix]
54 [fix]
55 failure = abort
55 failure = abort
56
56
57 When multiple tools are configured to affect a file, they execute in an order
58 defined by the :priority suboption. The priority suboption has a default value
59 of zero for each tool. Tools are executed in order of descending priority. The
60 execution order of tools with equal priority is unspecified. For example, you
61 could use the 'sort' and 'head' utilities to keep only the 10 smallest numbers
62 in a text file by ensuring that 'sort' runs before 'head'::
63
64 [fix]
65 sort:command = sort --numeric-sort
66 head:command = head --lines=10
67 sort:pattern = numbers.txt
68 head:pattern = numbers.txt
69 sort:priority = 2
70 head:priority = 1
71
72 To account for changes made by each tool, the line numbers used for incremental
73 formatting are recomputed before executing the next tool. So, each tool may see
74 different values for the arguments added by the :linerange suboption.
57 """
75 """
58
76
59 from __future__ import absolute_import
77 from __future__ import absolute_import
@@ -100,10 +118,16 b' configtable = {}'
100 configitem = registrar.configitem(configtable)
118 configitem = registrar.configitem(configtable)
101
119
102 # Register the suboptions allowed for each configured fixer.
120 # Register the suboptions allowed for each configured fixer.
103 FIXER_ATTRS = ('command', 'linerange', 'fileset', 'pattern')
121 FIXER_ATTRS = {
122 'command': None,
123 'linerange': None,
124 'fileset': None,
125 'pattern': None,
126 'priority': 0,
127 }
104
128
105 for key in FIXER_ATTRS:
129 for key, default in FIXER_ATTRS.items():
106 configitem('fix', '.*(:%s)?' % key, default=None, generic=True)
130 configitem('fix', '.*(:%s)?' % key, default=default, generic=True)
107
131
108 # A good default size allows most source code files to be fixed, but avoids
132 # A good default size allows most source code files to be fixed, but avoids
109 # letting fixer tools choke on huge inputs, which could be surprising to the
133 # letting fixer tools choke on huge inputs, which could be surprising to the
@@ -602,18 +626,21 b' def getfixers(ui):'
602 Each value is a Fixer object with methods that implement the behavior of the
626 Each value is a Fixer object with methods that implement the behavior of the
603 fixer's config suboptions. Does not validate the config values.
627 fixer's config suboptions. Does not validate the config values.
604 """
628 """
605 result = {}
629 fixers = {}
606 for name in fixernames(ui):
630 for name in fixernames(ui):
607 result[name] = Fixer()
631 fixers[name] = Fixer()
608 attrs = ui.configsuboptions('fix', name)[1]
632 attrs = ui.configsuboptions('fix', name)[1]
609 if 'fileset' in attrs and 'pattern' not in attrs:
633 if 'fileset' in attrs and 'pattern' not in attrs:
610 ui.warn(_('the fix.tool:fileset config name is deprecated; '
634 ui.warn(_('the fix.tool:fileset config name is deprecated; '
611 'please rename it to fix.tool:pattern\n'))
635 'please rename it to fix.tool:pattern\n'))
612 attrs['pattern'] = attrs['fileset']
636 attrs['pattern'] = attrs['fileset']
613 for key in FIXER_ATTRS:
637 for key, default in FIXER_ATTRS.items():
614 setattr(result[name], pycompat.sysstr('_' + key),
638 setattr(fixers[name], pycompat.sysstr('_' + key),
615 attrs.get(key, ''))
639 attrs.get(key, default))
616 return result
640 fixers[name]._priority = int(fixers[name]._priority)
641 return collections.OrderedDict(
642 sorted(fixers.items(), key=lambda item: item[1]._priority,
643 reverse=True))
617
644
618 def fixernames(ui):
645 def fixernames(ui):
619 """Returns the names of [fix] config options that have suboptions"""
646 """Returns the names of [fix] config options that have suboptions"""
@@ -165,6 +165,26 b' Help text for fix.'
165 [fix]
165 [fix]
166 failure = abort
166 failure = abort
167
167
168 When multiple tools are configured to affect a file, they execute in an order
169 defined by the :priority suboption. The priority suboption has a default value
170 of zero for each tool. Tools are executed in order of descending priority. The
171 execution order of tools with equal priority is unspecified. For example, you
172 could use the 'sort' and 'head' utilities to keep only the 10 smallest numbers
173 in a text file by ensuring that 'sort' runs before 'head':
174
175 [fix]
176 sort:command = sort --numeric-sort
177 head:command = head --lines=10
178 sort:pattern = numbers.txt
179 head:pattern = numbers.txt
180 sort:priority = 2
181 head:priority = 1
182
183 To account for changes made by each tool, the line numbers used for
184 incremental formatting are recomputed before executing the next tool. So, each
185 tool may see different values for the arguments added by the :linerange
186 suboption.
187
168 list of commands:
188 list of commands:
169
189
170 fix rewrite file content in changesets or working directory
190 fix rewrite file content in changesets or working directory
@@ -1127,3 +1147,51 b' warning.'
1127 first
1147 first
1128
1148
1129 $ cd ..
1149 $ cd ..
1150
1151 The execution order of tools can be controlled. This example doesn't work if
1152 you sort after truncating, but the config defines the correct order while the
1153 definitions are out of order (which might imply the incorrect order given the
1154 implementation of fix). The goal is to use multiple tools to select the lowest
1155 5 numbers in the file.
1156
1157 $ hg init priorityexample
1158 $ cd priorityexample
1159
1160 $ cat >> .hg/hgrc <<EOF
1161 > [fix]
1162 > head:command = head --lines=5
1163 > head:pattern = numbers.txt
1164 > head:priority = 1
1165 > sort:command = sort --numeric-sort
1166 > sort:pattern = numbers.txt
1167 > sort:priority = 2
1168 > EOF
1169
1170 $ printf "8\n2\n3\n6\n7\n4\n9\n5\n1\n0\n" > numbers.txt
1171 $ hg add -q
1172 $ hg fix -w
1173 $ cat numbers.txt
1174 0
1175 1
1176 2
1177 3
1178 4
1179
1180 And of course we should be able to break this by reversing the execution order.
1181 Test negative priorities while we're at it.
1182
1183 $ cat >> .hg/hgrc <<EOF
1184 > [fix]
1185 > head:priority = -1
1186 > sort:priority = -2
1187 > EOF
1188 $ printf "8\n2\n3\n6\n7\n4\n9\n5\n1\n0\n" > numbers.txt
1189 $ hg fix -w
1190 $ cat numbers.txt
1191 2
1192 3
1193 6
1194 7
1195 8
1196
1197 $ cd ..
General Comments 0
You need to be logged in to leave comments. Login now