comparison hgext/mq.py @ 2873:4ec58b157265

refactor text diff/patch code. rename commands.dodiff to patch.diff. rename commands.doexport to patch.export. move some functions from commands to new mercurial.cmdutil module. turn list of diff options into mdiff.diffopts class. patch.diff and patch.export now has clean api for call from 3rd party python code.
author Vadim Gelfer <vadim.gelfer@gmail.com>
date Sat, 12 Aug 2006 16:13:27 -0700
parents 5dd6631c8238
children 3d6efcbbd1c9
comparison
equal deleted inserted replaced
2872:5dd6631c8238 2873:4ec58b157265
30 ''' 30 '''
31 31
32 from mercurial.demandload import * 32 from mercurial.demandload import *
33 from mercurial.i18n import gettext as _ 33 from mercurial.i18n import gettext as _
34 demandload(globals(), "os sys re struct traceback errno bz2") 34 demandload(globals(), "os sys re struct traceback errno bz2")
35 demandload(globals(), "mercurial:commands,hg,revlog,ui,util") 35 demandload(globals(), "mercurial:commands,hg,patch,revlog,ui,util")
36 36
37 commands.norepo += " qclone qversion" 37 commands.norepo += " qclone qversion"
38 38
39 class statusentry: 39 class statusentry:
40 def __init__(self, rev, name=None): 40 def __init__(self, rev, name=None):
63 self.series_path = "series" 63 self.series_path = "series"
64 self.status_path = "status" 64 self.status_path = "status"
65 self.guards_path = "guards" 65 self.guards_path = "guards"
66 self.active_guards = None 66 self.active_guards = None
67 self.guards_dirty = False 67 self.guards_dirty = False
68 self._diffopts = None
68 69
69 if os.path.exists(self.join(self.series_path)): 70 if os.path.exists(self.join(self.series_path)):
70 self.full_series = self.opener(self.series_path).read().splitlines() 71 self.full_series = self.opener(self.series_path).read().splitlines()
71 self.parse_series() 72 self.parse_series()
72 73
73 if os.path.exists(self.join(self.status_path)): 74 if os.path.exists(self.join(self.status_path)):
74 lines = self.opener(self.status_path).read().splitlines() 75 lines = self.opener(self.status_path).read().splitlines()
75 self.applied = [statusentry(l) for l in lines] 76 self.applied = [statusentry(l) for l in lines]
77
78 def diffopts(self):
79 if self._diffopts is None:
80 self._diffopts = self.ui.diffopts()
81 return self._diffopts
76 82
77 def join(self, *p): 83 def join(self, *p):
78 return os.path.join(self.path, *p) 84 return os.path.join(self.path, *p)
79 85
80 def find_series(self, patch): 86 def find_series(self, patch):
289 if format and format.startswith("tag") and subject: 295 if format and format.startswith("tag") and subject:
290 message.insert(0, "") 296 message.insert(0, "")
291 message.insert(0, subject) 297 message.insert(0, subject)
292 return (message, comments, user, date, diffstart > 1) 298 return (message, comments, user, date, diffstart > 1)
293 299
300 def printdiff(self, repo, node1, node2=None, files=None,
301 fp=None, changes=None, opts=None):
302 patch.diff(repo, node1, node2, files,
303 fp=fp, changes=changes, opts=self.diffopts())
304
294 def mergeone(self, repo, mergeq, head, patch, rev, wlock): 305 def mergeone(self, repo, mergeq, head, patch, rev, wlock):
295 # first try just applying the patch 306 # first try just applying the patch
296 (err, n) = self.apply(repo, [ patch ], update_status=False, 307 (err, n) = self.apply(repo, [ patch ], update_status=False,
297 strict=True, merge=rev, wlock=wlock) 308 strict=True, merge=rev, wlock=wlock)
298 309
322 333
323 patchf = self.opener(patch, "w") 334 patchf = self.opener(patch, "w")
324 if comments: 335 if comments:
325 comments = "\n".join(comments) + '\n\n' 336 comments = "\n".join(comments) + '\n\n'
326 patchf.write(comments) 337 patchf.write(comments)
327 commands.dodiff(patchf, self.ui, repo, head, n) 338 self.printdiff(repo, head, n, fp=patchf)
328 patchf.close() 339 patchf.close()
329 return (0, n) 340 return (0, n)
330 341
331 def qparents(self, repo, rev=None): 342 def qparents(self, repo, rev=None):
332 if rev is None: 343 if rev is None:
916 top = self.check_toppatch(repo) 927 top = self.check_toppatch(repo)
917 if not top: 928 if not top:
918 self.ui.write("No patches applied\n") 929 self.ui.write("No patches applied\n")
919 return 930 return
920 qp = self.qparents(repo, top) 931 qp = self.qparents(repo, top)
921 commands.dodiff(sys.stdout, self.ui, repo, qp, None, files) 932 self.printdiff(repo, qp, files=files)
922 933
923 def refresh(self, repo, msg='', short=False): 934 def refresh(self, repo, msg='', short=False):
924 if len(self.applied) == 0: 935 if len(self.applied) == 0:
925 self.ui.write("No patches applied\n") 936 self.ui.write("No patches applied\n")
926 return 937 return
999 1010
1000 c = list(util.unique(cc)) 1011 c = list(util.unique(cc))
1001 r = list(util.unique(dd)) 1012 r = list(util.unique(dd))
1002 a = list(util.unique(aa)) 1013 a = list(util.unique(aa))
1003 filelist = list(util.unique(c + r + a )) 1014 filelist = list(util.unique(c + r + a ))
1004 commands.dodiff(patchf, self.ui, repo, patchparent, None, 1015 self.printdiff(repo, patchparent, files=filelist,
1005 filelist, changes=(c, a, r, [], u)) 1016 changes=(c, a, r, [], u), fp=patchf)
1006 patchf.close() 1017 patchf.close()
1007 1018
1008 changes = repo.changelog.read(tip) 1019 changes = repo.changelog.read(tip)
1009 repo.dirstate.setparents(*cparents) 1020 repo.dirstate.setparents(*cparents)
1010 repo.dirstate.update(a, 'a') 1021 repo.dirstate.update(a, 'a')
1023 self.strip(repo, top, update=False, backup='strip', wlock=wlock) 1034 self.strip(repo, top, update=False, backup='strip', wlock=wlock)
1024 n = repo.commit(filelist, message, changes[1], force=1, wlock=wlock) 1035 n = repo.commit(filelist, message, changes[1], force=1, wlock=wlock)
1025 self.applied[-1] = statusentry(revlog.hex(n), patch) 1036 self.applied[-1] = statusentry(revlog.hex(n), patch)
1026 self.applied_dirty = 1 1037 self.applied_dirty = 1
1027 else: 1038 else:
1028 commands.dodiff(patchf, self.ui, repo, patchparent, None) 1039 self.printdiff(repo, patchparent, fp=patchf)
1029 patchf.close() 1040 patchf.close()
1030 self.pop(repo, force=True, wlock=wlock) 1041 self.pop(repo, force=True, wlock=wlock)
1031 self.push(repo, force=True, wlock=wlock) 1042 self.push(repo, force=True, wlock=wlock)
1032 1043
1033 def init(self, repo, create=False): 1044 def init(self, repo, create=False):