comparison mercurial/cmdutil.py @ 32433:7feaf5550a9e

cmdutil: extract closure that performs the actual export formatting This simplifies things a little by making the actual act of turning a revision into patch data a single function. After this, adding formatter support to `hg export` should be much simpler.
author Augie Fackler <augie@google.com>
date Mon, 22 May 2017 13:34:03 -0400
parents 75544176bc28
children 69c864149d69
comparison
equal deleted inserted replaced
32432:75544176bc28 32433:7feaf5550a9e
1139 # mapping from identifier to actual export function 1139 # mapping from identifier to actual export function
1140 # function as to return a string to be added to the header or None 1140 # function as to return a string to be added to the header or None
1141 # it is given two arguments (sequencenumber, changectx) 1141 # it is given two arguments (sequencenumber, changectx)
1142 extraexportmap = {} 1142 extraexportmap = {}
1143 1143
1144 def _exportsingle(repo, ctx, match, switch_parent, rev, seqno, write, diffopts):
1145 node = ctx.node()
1146 parents = [p.node() for p in ctx.parents() if p]
1147 branch = ctx.branch()
1148 if switch_parent:
1149 parents.reverse()
1150
1151 if parents:
1152 prev = parents[0]
1153 else:
1154 prev = nullid
1155
1156 write("# HG changeset patch\n")
1157 write("# User %s\n" % ctx.user())
1158 write("# Date %d %d\n" % ctx.date())
1159 write("# %s\n" % util.datestr(ctx.date()))
1160 if branch and branch != 'default':
1161 write("# Branch %s\n" % branch)
1162 write("# Node ID %s\n" % hex(node))
1163 write("# Parent %s\n" % hex(prev))
1164 if len(parents) > 1:
1165 write("# Parent %s\n" % hex(parents[1]))
1166
1167 for headerid in extraexport:
1168 header = extraexportmap[headerid](seqno, ctx)
1169 if header is not None:
1170 write('# %s\n' % header)
1171 write(ctx.description().rstrip())
1172 write("\n\n")
1173
1174 for chunk, label in patch.diffui(repo, prev, node, match, opts=diffopts):
1175 write(chunk, label=label)
1176
1144 def export(repo, revs, fntemplate='hg-%h.patch', fp=None, switch_parent=False, 1177 def export(repo, revs, fntemplate='hg-%h.patch', fp=None, switch_parent=False,
1145 opts=None, match=None): 1178 opts=None, match=None):
1146 '''export changesets as hg patches 1179 '''export changesets as hg patches
1147 1180
1148 Args: 1181 Args:
1170 1203
1171 total = len(revs) 1204 total = len(revs)
1172 revwidth = max(len(str(rev)) for rev in revs) 1205 revwidth = max(len(str(rev)) for rev in revs)
1173 filemode = {} 1206 filemode = {}
1174 1207
1175 def single(rev, seqno, fp): 1208 for seqno, rev in enumerate(revs, 1):
1176 ctx = repo[rev] 1209 ctx = repo[rev]
1177 node = ctx.node() 1210 fo = None
1178 parents = [p.node() for p in ctx.parents() if p] 1211 dest = '<unnamed>'
1179 branch = ctx.branch()
1180 if switch_parent:
1181 parents.reverse()
1182
1183 if parents:
1184 prev = parents[0]
1185 else:
1186 prev = nullid
1187
1188 shouldclose = False
1189 if not fp and len(fntemplate) > 0: 1212 if not fp and len(fntemplate) > 0:
1190 desc_lines = ctx.description().rstrip().split('\n') 1213 desc_lines = ctx.description().rstrip().split('\n')
1191 desc = desc_lines[0] #Commit always has a first line. 1214 desc = desc_lines[0] #Commit always has a first line.
1192 fp = makefileobj(repo, fntemplate, node, desc=desc, total=total, 1215 fo = makefileobj(repo, fntemplate, ctx.node(), desc=desc,
1193 seqno=seqno, revwidth=revwidth, mode='wb', 1216 total=total, seqno=seqno, revwidth=revwidth,
1194 modemap=filemode) 1217 mode='wb', modemap=filemode)
1195 shouldclose = True 1218 dest = fo.name
1196 if fp and not getattr(fp, 'name', '<unnamed>').startswith('<'): 1219 def write(s, **kw):
1197 repo.ui.note("%s\n" % fp.name) 1220 fo.write(s)
1198 1221 elif fp:
1199 if not fp: 1222 dest = getattr(fp, 'name', dest)
1200 write = repo.ui.write
1201 else:
1202 def write(s, **kw): 1223 def write(s, **kw):
1203 fp.write(s) 1224 fp.write(s)
1204 1225 else:
1205 write("# HG changeset patch\n") 1226 write = repo.ui.write
1206 write("# User %s\n" % ctx.user()) 1227 if not dest.startswith('<'):
1207 write("# Date %d %d\n" % ctx.date()) 1228 repo.ui.note("%s\n" % dest)
1208 write("# %s\n" % util.datestr(ctx.date())) 1229 _exportsingle(
1209 if branch and branch != 'default': 1230 repo, ctx, match, switch_parent, rev, seqno, write, opts)
1210 write("# Branch %s\n" % branch) 1231 if fo is not None:
1211 write("# Node ID %s\n" % hex(node)) 1232 fo.close()
1212 write("# Parent %s\n" % hex(prev))
1213 if len(parents) > 1:
1214 write("# Parent %s\n" % hex(parents[1]))
1215
1216 for headerid in extraexport:
1217 header = extraexportmap[headerid](seqno, ctx)
1218 if header is not None:
1219 write('# %s\n' % header)
1220 write(ctx.description().rstrip())
1221 write("\n\n")
1222
1223 for chunk, label in patch.diffui(repo, prev, node, match, opts=opts):
1224 write(chunk, label=label)
1225
1226 if shouldclose:
1227 fp.close()
1228
1229 for seqno, rev in enumerate(revs):
1230 single(rev, seqno + 1, fp)
1231 1233
1232 def diffordiffstat(ui, repo, diffopts, node1, node2, match, 1234 def diffordiffstat(ui, repo, diffopts, node1, node2, match,
1233 changes=None, stat=False, fp=None, prefix='', 1235 changes=None, stat=False, fp=None, prefix='',
1234 root='', listsubrepos=False): 1236 root='', listsubrepos=False):
1235 '''show diff or diffstat.''' 1237 '''show diff or diffstat.'''