comparison mercurial/patch.py @ 35062:1706eae096e2

patch: accept prefix argument to changedfiles() helper I'd like to call the function from an extension, passing both "strip" and "prefix", but it currently only accepts "strip". The only in-tree caller seems to be mq.py, which doesn't even pass "strip". Differential Revision: https://phab.mercurial-scm.org/D1413
author Martin von Zweigbergk <martinvonz@google.com>
date Tue, 14 Nov 2017 10:26:36 -0800
parents 3649c3f2cd90
children a1d2fc32bb99
comparison
equal deleted inserted replaced
35061:e9a8a941950a 35062:1706eae096e2
1988 patching then normalized according to 'eolmode'. 1988 patching then normalized according to 'eolmode'.
1989 """ 1989 """
1990 return _applydiff(ui, fp, patchfile, backend, store, strip=strip, 1990 return _applydiff(ui, fp, patchfile, backend, store, strip=strip,
1991 prefix=prefix, eolmode=eolmode) 1991 prefix=prefix, eolmode=eolmode)
1992 1992
1993 def _canonprefix(repo, prefix):
1994 if prefix:
1995 prefix = pathutil.canonpath(repo.root, repo.getcwd(), prefix)
1996 if prefix != '':
1997 prefix += '/'
1998 return prefix
1999
1993 def _applydiff(ui, fp, patcher, backend, store, strip=1, prefix='', 2000 def _applydiff(ui, fp, patcher, backend, store, strip=1, prefix='',
1994 eolmode='strict'): 2001 eolmode='strict'):
1995 2002 prefix = _canonprefix(backend.repo, prefix)
1996 if prefix:
1997 prefix = pathutil.canonpath(backend.repo.root, backend.repo.getcwd(),
1998 prefix)
1999 if prefix != '':
2000 prefix += '/'
2001 def pstrip(p): 2003 def pstrip(p):
2002 return pathtransform(p, strip - 1, prefix)[1] 2004 return pathtransform(p, strip - 1, prefix)[1]
2003 2005
2004 rejects = 0 2006 rejects = 0
2005 err = 0 2007 err = 0
2181 return _externalpatch(ui, repo, patcher, patchname, strip, 2183 return _externalpatch(ui, repo, patcher, patchname, strip,
2182 files, similarity) 2184 files, similarity)
2183 return internalpatch(ui, repo, patchname, strip, prefix, files, eolmode, 2185 return internalpatch(ui, repo, patchname, strip, prefix, files, eolmode,
2184 similarity) 2186 similarity)
2185 2187
2186 def changedfiles(ui, repo, patchpath, strip=1): 2188 def changedfiles(ui, repo, patchpath, strip=1, prefix=''):
2187 backend = fsbackend(ui, repo.root) 2189 backend = fsbackend(ui, repo.root)
2190 prefix = _canonprefix(repo, prefix)
2188 with open(patchpath, 'rb') as fp: 2191 with open(patchpath, 'rb') as fp:
2189 changed = set() 2192 changed = set()
2190 for state, values in iterhunks(fp): 2193 for state, values in iterhunks(fp):
2191 if state == 'file': 2194 if state == 'file':
2192 afile, bfile, first_hunk, gp = values 2195 afile, bfile, first_hunk, gp = values
2193 if gp: 2196 if gp:
2194 gp.path = pathtransform(gp.path, strip - 1, '')[1] 2197 gp.path = pathtransform(gp.path, strip - 1, prefix)[1]
2195 if gp.oldpath: 2198 if gp.oldpath:
2196 gp.oldpath = pathtransform(gp.oldpath, strip - 1, '')[1] 2199 gp.oldpath = pathtransform(gp.oldpath, strip - 1,
2200 prefix)[1]
2197 else: 2201 else:
2198 gp = makepatchmeta(backend, afile, bfile, first_hunk, strip, 2202 gp = makepatchmeta(backend, afile, bfile, first_hunk, strip,
2199 '') 2203 prefix)
2200 changed.add(gp.path) 2204 changed.add(gp.path)
2201 if gp.op == 'RENAME': 2205 if gp.op == 'RENAME':
2202 changed.add(gp.oldpath) 2206 changed.add(gp.oldpath)
2203 elif state not in ('hunk', 'git'): 2207 elif state not in ('hunk', 'git'):
2204 raise error.Abort(_('unsupported parser state: %s') % state) 2208 raise error.Abort(_('unsupported parser state: %s') % state)