108 } |
108 } |
109 |
109 |
110 def __init__(self, ui, repo, expand, path='', node=None): |
110 def __init__(self, ui, repo, expand, path='', node=None): |
111 self.ui = ui |
111 self.ui = ui |
112 self.repo = repo |
112 self.repo = repo |
113 self.t = expand or None |
113 self.ct = expand or None |
114 self.path = path |
114 self.path = path |
115 self.node = node |
115 self.node = node |
116 |
116 |
117 kwmaps = self.ui.configitems('keywordmaps') |
117 kwmaps = self.ui.configitems('keywordmaps') |
118 if kwmaps: # override default templates |
118 if kwmaps: # override default templates |
120 for (k, v) in kwmaps] |
120 for (k, v) in kwmaps] |
121 self.templates = dict(kwmaps) |
121 self.templates = dict(kwmaps) |
122 escaped = map(re.escape, self.templates.keys()) |
122 escaped = map(re.escape, self.templates.keys()) |
123 kwpat = r'\$(%s)(: [^$\n\r]*? )??\$' % '|'.join(escaped) |
123 kwpat = r'\$(%s)(: [^$\n\r]*? )??\$' % '|'.join(escaped) |
124 self.re_kw = re.compile(kwpat) |
124 self.re_kw = re.compile(kwpat) |
125 if self.t: |
125 if self.ct: |
126 templater.common_filters['utcdate'] = utcdate |
126 templater.common_filters['utcdate'] = utcdate |
127 self.t = cmdutil.changeset_templater(self.ui, self.repo, |
127 self.ct = cmdutil.changeset_templater(self.ui, self.repo, |
128 False, '', False) |
128 False, '', False) |
129 |
129 |
130 def substitute(self, node, data, subfunc): |
130 def substitute(self, node, data, subfunc): |
131 '''Obtains node if missing, and calls given substitution function.''' |
131 '''Obtains node if missing, and calls given substitution function.''' |
132 if not self.node: |
132 if not self.node: |
133 c = context.filectx(self.repo, self.path, fileid=node) |
133 c = context.filectx(self.repo, self.path, fileid=node) |
134 self.node = c.node() |
134 self.node = c.node() |
135 |
135 |
136 def kwsub(mobj): |
136 def kwsub(mobj): |
137 '''Substitutes keyword using corresponding template.''' |
137 '''Substitutes keyword using corresponding template.''' |
138 kw = mobj.group(1) |
138 kw = mobj.group(1) |
139 self.t.use_template(self.templates[kw]) |
139 self.ct.use_template(self.templates[kw]) |
140 self.ui.pushbuffer() |
140 self.ui.pushbuffer() |
141 self.t.show(changenode=self.node, |
141 self.ct.show(changenode=self.node, |
142 root=self.repo.root, file=self.path) |
142 root=self.repo.root, file=self.path) |
143 return '$%s: %s $' % (kw, templater.firstline(self.ui.popbuffer())) |
143 return '$%s: %s $' % (kw, templater.firstline(self.ui.popbuffer())) |
144 |
144 |
145 return subfunc(kwsub, data) |
145 return subfunc(kwsub, data) |
146 |
146 |
147 def expand(self, node, data): |
147 def expand(self, node, data): |
155 Count is number of keywords/keyword substitutions, indicates |
155 Count is number of keywords/keyword substitutions, indicates |
156 to caller whether to act on file containing data. |
156 to caller whether to act on file containing data. |
157 Keywords in data are expanded, if templater was initialized.''' |
157 Keywords in data are expanded, if templater was initialized.''' |
158 if util.binary(data): |
158 if util.binary(data): |
159 return data, None |
159 return data, None |
160 if self.t: |
160 if self.ct: |
161 return self.substitute(node, data, self.re_kw.subn) |
161 return self.substitute(node, data, self.re_kw.subn) |
162 return data, self.re_kw.search(data) |
162 return data, self.re_kw.search(data) |
163 |
163 |
164 def shrink(self, text): |
164 def shrink(self, text): |
165 '''Returns text with all keyword substitutions removed.''' |
165 '''Returns text with all keyword substitutions removed.''' |
169 |
169 |
170 def overwrite(self, candidates, man, commit): |
170 def overwrite(self, candidates, man, commit): |
171 '''Overwrites files in working directory if keywords are detected. |
171 '''Overwrites files in working directory if keywords are detected. |
172 Keywords are expanded if keyword templater is initialized, |
172 Keywords are expanded if keyword templater is initialized, |
173 otherwise their substitution is removed.''' |
173 otherwise their substitution is removed.''' |
174 expand = self.t is not None |
174 expand = self.ct is not None |
175 action = ('shrinking', 'expanding')[expand] |
175 action = ('shrinking', 'expanding')[expand] |
176 notify = (self.ui.note, self.ui.debug)[commit] |
176 notify = (self.ui.note, self.ui.debug)[commit] |
177 for f in candidates: |
177 for f in candidates: |
178 fp = self.repo.file(f, kwexp=expand, kwmatch=True) |
178 fp = self.repo.file(f, kwexp=expand, kwmatch=True) |
179 data, kwfound = fp.kwctread(man[f]) |
179 data, kwfound = fp.kwctread(man[f]) |