subrepo.py
1840 lines
| 67.6 KiB
| text/x-python
|
PythonLexer
/ mercurial / subrepo.py
|
r36026 | # subrepo.py - sub-repository classes and factory | ||
|
r8812 | # | ||
|
r10324 | # Copyright 2009-2010 Matt Mackall <mpm@selenic.com> | ||
|
r8812 | # | ||
# This software may be used and distributed according to the terms of the | ||||
|
r10263 | # GNU General Public License version 2 or any later version. | ||
|
r8812 | |||
|
r25980 | from __future__ import absolute_import | ||
|
r23540 | import copy | ||
|
r25980 | import errno | ||
|
r29341 | import hashlib | ||
|
r25980 | import os | ||
import re | ||||
import stat | ||||
import subprocess | ||||
import sys | ||||
import tarfile | ||||
|
r19788 | import xml.dom.minidom | ||
|
r25980 | |||
from .i18n import _ | ||||
from . import ( | ||||
cmdutil, | ||||
|
r30635 | encoding, | ||
|
r25980 | error, | ||
exchange, | ||||
|
r35906 | logcmdutil, | ||
|
r25980 | match as matchmod, | ||
node, | ||||
pathutil, | ||||
phases, | ||||
|
r30615 | pycompat, | ||
|
r25980 | scmutil, | ||
|
r36026 | subrepoutil, | ||
|
r25980 | util, | ||
|
r31236 | vfs as vfsmod, | ||
|
r25980 | ) | ||
|
r37102 | from .utils import ( | ||
dateutil, | ||||
|
r37138 | procutil, | ||
|
r37102 | stringutil, | ||
) | ||||
|
r25980 | |||
|
r9092 | hg = None | ||
|
r36026 | reporelpath = subrepoutil.reporelpath | ||
subrelpath = subrepoutil.subrelpath | ||||
_abssource = subrepoutil._abssource | ||||
|
r14050 | propertycache = util.propertycache | ||
|
r8813 | |||
|
r18940 | def _expandedabspath(path): | ||
''' | ||||
get a path or url and if it is a path expand it and return an absolute path | ||||
''' | ||||
expandedpath = util.urllocalpath(util.expandpath(path)) | ||||
u = util.url(expandedpath) | ||||
if not u.scheme: | ||||
path = util.normpath(os.path.abspath(u.path)) | ||||
return path | ||||
|
r18936 | |||
def _getstorehashcachename(remotepath): | ||||
'''get a unique filename for the store hash cache of a remote repository''' | ||||
|
r35600 | return node.hex(hashlib.sha1(_expandedabspath(remotepath)).digest())[0:12] | ||
|
r18936 | |||
|
r18109 | class SubrepoAbort(error.Abort): | ||
"""Exception class used to avoid handling a subrepo error more than once""" | ||||
|
r18263 | def __init__(self, *args, **kw): | ||
|
r35372 | self.subrepo = kw.pop(r'subrepo', None) | ||
self.cause = kw.pop(r'cause', None) | ||||
|
r18296 | error.Abort.__init__(self, *args, **kw) | ||
|
r18109 | |||
def annotatesubrepoerror(func): | ||||
def decoratedmethod(self, *args, **kargs): | ||||
try: | ||||
res = func(self, *args, **kargs) | ||||
|
r25660 | except SubrepoAbort as ex: | ||
|
r18109 | # This exception has already been handled | ||
raise ex | ||||
|
r25660 | except error.Abort as ex: | ||
|
r18263 | subrepo = subrelpath(self) | ||
|
r37102 | errormsg = (stringutil.forcebytestr(ex) + ' ' | ||
|
r36439 | + _('(in subrepository "%s")') % subrepo) | ||
|
r18109 | # avoid handling this exception by raising a SubrepoAbort exception | ||
|
r18964 | raise SubrepoAbort(errormsg, hint=ex.hint, subrepo=subrepo, | ||
cause=sys.exc_info()) | ||||
|
r18109 | return res | ||
return decoratedmethod | ||||
|
r13417 | def _updateprompt(ui, sub, dirty, local, remote): | ||
if dirty: | ||||
msg = (_(' subrepository sources for %s differ\n' | ||||
|
r42791 | 'you can use (l)ocal source (%s) or (r)emote source (%s).\n' | ||
'what do you want to do?' | ||||
|
r19226 | '$$ &Local $$ &Remote') | ||
|
r13417 | % (subrelpath(sub), local, remote)) | ||
else: | ||||
|
r16683 | msg = (_(' subrepository sources for %s differ (in checked out ' | ||
'version)\n' | ||||
|
r42791 | 'you can use (l)ocal source (%s) or (r)emote source (%s).\n' | ||
'what do you want to do?' | ||||
|
r19226 | '$$ &Local $$ &Remote') | ||
|
r13417 | % (subrelpath(sub), local, remote)) | ||
|
r19226 | return ui.promptchoice(msg, 0) | ||
|
r13417 | |||
|
r24724 | def _sanitize(ui, vfs, ignore): | ||
|
r24726 | for dirname, dirs, names in vfs.walk(): | ||
|
r21567 | for i, d in enumerate(dirs): | ||
if d.lower() == ignore: | ||||
del dirs[i] | ||||
break | ||||
|
r25771 | if vfs.basename(dirname).lower() != '.hg': | ||
|
r21564 | continue | ||
|
r20104 | for f in names: | ||
if f.lower() == 'hgrc': | ||||
|
r21564 | ui.warn(_("warning: removing potentially hostile 'hgrc' " | ||
|
r24726 | "in '%s'\n") % vfs.join(dirname)) | ||
vfs.unlink(vfs.reljoin(dirname, f)) | ||||
|
r20104 | |||
|
r34985 | def _auditsubrepopath(repo, path): | ||
|
r41551 | # sanity check for potentially unsafe paths such as '~' and '$FOO' | ||
if path.startswith('~') or '$' in path or util.expandpath(path) != path: | ||||
raise error.Abort(_('subrepo path contains illegal component: %s') | ||||
% path) | ||||
|
r34985 | # auditor doesn't check if the path itself is a symlink | ||
pathutil.pathauditor(repo.root)(path) | ||||
if repo.wvfs.islink(path): | ||||
raise error.Abort(_("subrepo '%s' traverses symbolic link") % path) | ||||
|
r34990 | SUBREPO_ALLOWED_DEFAULTS = { | ||
'hg': True, | ||||
'git': False, | ||||
'svn': False, | ||||
} | ||||
|
r34986 | def _checktype(ui, kind): | ||
|
r34990 | # subrepos.allowed is a master kill switch. If disabled, subrepos are | ||
# disabled period. | ||||
if not ui.configbool('subrepos', 'allowed', True): | ||||
raise error.Abort(_('subrepos not enabled'), | ||||
|
r34986 | hint=_("see 'hg help config.subrepos' for details")) | ||
|
r34990 | |||
default = SUBREPO_ALLOWED_DEFAULTS.get(kind, False) | ||||
if not ui.configbool('subrepos', '%s:allowed' % kind, default): | ||||
raise error.Abort(_('%s subrepos not allowed') % kind, | ||||
hint=_("see 'hg help config.subrepos' for details")) | ||||
|
r34986 | if kind not in types: | ||
raise error.Abort(_('unknown subrepo type %s') % kind) | ||||
|
r29021 | def subrepo(ctx, path, allowwdir=False, allowcreate=True): | ||
|
r11571 | """return instance of the right subrepo class for subrepo in path""" | ||
|
r8813 | # subrepo inherently violates our import layering rules | ||
# because it wants to make repo objects from deep inside the stack | ||||
# so we manually delay the circular imports to not break | ||||
# scripts that don't use our demand-loading | ||||
|
r9092 | global hg | ||
|
r25980 | from . import hg as h | ||
|
r8814 | hg = h | ||
|
r8813 | |||
|
r34986 | repo = ctx.repo() | ||
_auditsubrepopath(repo, path) | ||||
|
r16756 | state = ctx.substate[path] | ||
|
r34986 | _checktype(repo.ui, state[2]) | ||
|
r25600 | if allowwdir: | ||
state = (state[0], ctx.subrev(path), state[2]) | ||||
|
r29021 | return types[state[2]](ctx, path, state[:2], allowcreate) | ||
|
r8813 | |||
|
r25416 | def nullsubrepo(ctx, path, pctx): | ||
"""return an empty subrepo in pctx for the extant subrepo in ctx""" | ||||
# subrepo inherently violates our import layering rules | ||||
# because it wants to make repo objects from deep inside the stack | ||||
# so we manually delay the circular imports to not break | ||||
# scripts that don't use our demand-loading | ||||
global hg | ||||
|
r25980 | from . import hg as h | ||
|
r25416 | hg = h | ||
|
r34986 | repo = ctx.repo() | ||
_auditsubrepopath(repo, path) | ||||
|
r25416 | state = ctx.substate[path] | ||
|
r34986 | _checktype(repo.ui, state[2]) | ||
|
r25416 | subrev = '' | ||
if state[2] == 'hg': | ||||
subrev = "0" * 40 | ||||
|
r29021 | return types[state[2]](pctx, path, (state[0], subrev), True) | ||
|
r25416 | |||
|
r11559 | # subrepo classes need to implement the following abstract class: | ||
class abstractsubrepo(object): | ||||
|
r24671 | def __init__(self, ctx, path): | ||
"""Initialize abstractsubrepo part | ||||
``ctx`` is the context referring this subrepository in the | ||||
parent repository. | ||||
|
r26781 | ``path`` is the path to this subrepository as seen from | ||
|
r24671 | innermost repository. | ||
""" | ||||
self.ui = ctx.repo().ui | ||||
self._ctx = ctx | ||||
self._path = path | ||||
|
r23536 | |||
|
r32005 | def addwebdirpath(self, serverpath, webconf): | ||
"""Add the hgwebdir entries for this subrepo, and any of its subrepos. | ||||
``serverpath`` is the path component of the URL for this repo. | ||||
``webconf`` is the dictionary of hgwebdir entries. | ||||
""" | ||||
pass | ||||
|
r18937 | def storeclean(self, path): | ||
""" | ||||
returns true if the repository has not changed since it was last | ||||
cloned from or pushed to a given repository. | ||||
""" | ||||
return False | ||||
|
r33364 | def dirty(self, ignoreupdate=False, missing=False): | ||
|
r13174 | """returns true if the dirstate of the subrepo is dirty or does not | ||
match current stored state. If ignoreupdate is true, only check | ||||
|
r33364 | whether the subrepo has uncommitted changes in its dirstate. If missing | ||
is true, check for deleted files. | ||||
|
r11559 | """ | ||
raise NotImplementedError | ||||
|
r33364 | def dirtyreason(self, ignoreupdate=False, missing=False): | ||
|
r24470 | """return reason string if it is ``dirty()`` | ||
Returned string should have enough information for the message | ||||
of exception. | ||||
This returns None, otherwise. | ||||
""" | ||||
|
r33364 | if self.dirty(ignoreupdate=ignoreupdate, missing=missing): | ||
|
r33365 | return _('uncommitted changes in subrepository "%s"' | ||
|
r24470 | ) % subrelpath(self) | ||
|
r30755 | def bailifchanged(self, ignoreupdate=False, hint=None): | ||
|
r24471 | """raise Abort if subrepository is ``dirty()`` | ||
""" | ||||
|
r33364 | dirtyreason = self.dirtyreason(ignoreupdate=ignoreupdate, | ||
missing=True) | ||||
|
r24471 | if dirtyreason: | ||
|
r30755 | raise error.Abort(dirtyreason, hint=hint) | ||
|
r24471 | |||
|
r16072 | def basestate(self): | ||
"""current working directory base state, disregarding .hgsubstate | ||||
state and working directory modifications""" | ||||
raise NotImplementedError | ||||
|
r12506 | def checknested(self, path): | ||
|
r12162 | """check if path is a subrepository within this repository""" | ||
return False | ||||
|
r11559 | def commit(self, text, user, date): | ||
"""commit the current changes to the subrepo with the given | ||||
log message. Use given user and date if possible. Return the | ||||
new state of the subrepo. | ||||
""" | ||||
raise NotImplementedError | ||||
|
r20176 | def phase(self, state): | ||
"""returns phase of specified state in the subrepository. | ||||
""" | ||||
return phases.public | ||||
|
r11559 | def remove(self): | ||
"""remove the subrepo | ||||
|
r8813 | |||
|
r11559 | (should verify the dirstate is not dirty first) | ||
""" | ||||
raise NotImplementedError | ||||
|
r13322 | def get(self, state, overwrite=False): | ||
|
r11559 | """run whatever commands are needed to put the subrepo into | ||
this state | ||||
""" | ||||
raise NotImplementedError | ||||
|
r13413 | def merge(self, state): | ||
|
r11559 | """merge currently-saved state with the new state.""" | ||
raise NotImplementedError | ||||
|
r15708 | def push(self, opts): | ||
|
r11572 | """perform whatever action is analogous to 'hg push' | ||
|
r11559 | |||
This may be a no-op on some systems. | ||||
""" | ||||
raise NotImplementedError | ||||
|
r41799 | def add(self, ui, match, prefix, uipathfn, explicitonly, **opts): | ||
|
r12270 | return [] | ||
|
r11559 | |||
|
r41801 | def addremove(self, matcher, prefix, uipathfn, opts): | ||
|
r23572 | self.ui.warn("%s: %s" % (prefix, _("addremove is not supported"))) | ||
|
r23537 | return 1 | ||
|
r32578 | def cat(self, match, fm, fntemplate, prefix, **opts): | ||
|
r21041 | return 1 | ||
|
r12166 | def status(self, rev2, **opts): | ||
|
r22914 | return scmutil.status([], [], [], [], [], [], []) | ||
|
r12166 | |||
|
r18006 | def diff(self, ui, diffopts, node2, match, prefix, **opts): | ||
|
r12167 | pass | ||
|
r12272 | def outgoing(self, ui, dest, opts): | ||
return 1 | ||||
|
r12274 | def incoming(self, ui, source, opts): | ||
return 1 | ||||
|
r12322 | def files(self): | ||
"""return filename iterator""" | ||||
raise NotImplementedError | ||||
|
r31099 | def filedata(self, name, decode): | ||
"""return file data, optionally passed through repo decoders""" | ||||
|
r12322 | raise NotImplementedError | ||
def fileflags(self, name): | ||||
"""return file flags""" | ||||
return '' | ||||
|
r38631 | def matchfileset(self, expr, badfn=None): | ||
|
r25121 | """Resolve the fileset expression for this repo""" | ||
|
r41825 | return matchmod.never(badfn=badfn) | ||
|
r25121 | |||
|
r41914 | def printfiles(self, ui, m, uipathfn, fm, fmt, subrepos): | ||
|
r24413 | """handle the files command for this subrepo""" | ||
return 1 | ||||
|
r31099 | def archive(self, archiver, prefix, match=None, decode=True): | ||
|
r17108 | if match is not None: | ||
files = [f for f in self.files() if match(f)] | ||||
else: | ||||
files = self.files() | ||||
|
r13144 | total = len(files) | ||
relpath = subrelpath(self) | ||||
|
r38398 | progress = self.ui.makeprogress(_('archiving (%s)') % relpath, | ||
unit=_('files'), total=total) | ||||
progress.update(0) | ||||
for name in files: | ||||
|
r12323 | flags = self.fileflags(name) | ||
|
r25658 | mode = 'x' in flags and 0o755 or 0o644 | ||
|
r12323 | symlink = 'l' in flags | ||
|
r41780 | archiver.addfile(prefix + name, mode, symlink, | ||
self.filedata(name, decode)) | ||||
|
r38398 | progress.increment() | ||
progress.complete() | ||||
|
r18967 | return total | ||
|
r12323 | |||
|
r15410 | def walk(self, match): | ||
''' | ||||
walk recursively through the directory tree, finding all files | ||||
matched by the match function | ||||
''' | ||||
|
r12323 | |||
|
r41802 | def forget(self, match, prefix, uipathfn, dryrun, interactive): | ||
|
r16527 | return ([], []) | ||
|
r12323 | |||
|
r41800 | def removefiles(self, matcher, prefix, uipathfn, after, force, subrepos, | ||
|
r37168 | dryrun, warnings): | ||
|
r23325 | """remove the matched files from the subrepository and the filesystem, | ||
possibly by force and/or after the file has been removed from the | ||||
filesystem. Return 0 on success, 1 on any warning. | ||||
""" | ||||
|
r28607 | warnings.append(_("warning: removefiles not implemented (%s)") | ||
% self._path) | ||||
|
r23325 | return 1 | ||
|
r23579 | def revert(self, substate, *pats, **opts): | ||
|
r41925 | self.ui.warn(_('%s: reverting %s subrepos is unsupported\n') | ||
% (substate[0], substate[2])) | ||||
|
r16429 | return [] | ||
|
r21400 | def shortid(self, revid): | ||
return revid | ||||
|
r34880 | def unshare(self): | ||
''' | ||||
convert this repository from shared to normal storage. | ||||
''' | ||||
|
r25591 | def verify(self): | ||
'''verify the integrity of the repository. Return 0 on success or | ||||
warning, 1 on any error. | ||||
''' | ||||
return 0 | ||||
|
r24672 | @propertycache | ||
def wvfs(self): | ||||
"""return vfs to access the working directory of this subrepository | ||||
""" | ||||
|
r31236 | return vfsmod.vfs(self._ctx.repo().wvfs.join(self._path)) | ||
|
r24672 | |||
|
r24673 | @propertycache | ||
def _relpath(self): | ||||
"""return path to this subrepository as seen from outermost repository | ||||
""" | ||||
|
r24785 | return self.wvfs.reljoin(reporelpath(self._ctx.repo()), self._path) | ||
|
r24673 | |||
|
r11559 | class hgsubrepo(abstractsubrepo): | ||
|
r29021 | def __init__(self, ctx, path, state, allowcreate): | ||
|
r24671 | super(hgsubrepo, self).__init__(ctx, path) | ||
|
r8813 | self._state = state | ||
|
r24302 | r = ctx.repo() | ||
|
r41747 | root = r.wjoin(util.localpath(path)) | ||
|
r29021 | create = allowcreate and not r.wvfs.exists('%s/.hg' % path) | ||
|
r41550 | # repository constructor does expand variables in path, which is | ||
# unsafe since subrepo path might come from untrusted source. | ||||
if os.path.realpath(util.expandpath(root)) != root: | ||||
raise error.Abort(_('subrepo path contains illegal component: %s') | ||||
% path) | ||||
|
r17873 | self._repo = hg.repository(r.baseui, root, create=create) | ||
|
r41550 | if self._repo.root != root: | ||
raise error.ProgrammingError('failed to reject unsafe subrepo ' | ||||
'path: %s (expanded to %s)' | ||||
% (root, self._repo.root)) | ||||
|
r24877 | |||
# Propagate the parent's --hidden option | ||||
if r is r.unfiltered(): | ||||
self._repo = self._repo.unfiltered() | ||||
|
r23573 | self.ui = self._repo.ui | ||
|
r17873 | for s, k in [('ui', 'commitsubrepos')]: | ||
v = r.ui.config(s, k) | ||||
if v: | ||||
|
r23574 | self.ui.setconfig(s, k, v, 'subrepo') | ||
|
r25848 | # internal config: ui._usedassubrepo | ||
|
r23574 | self.ui.setconfig('ui', '_usedassubrepo', 'True', 'subrepo') | ||
|
r14281 | self._initrepo(r, state[0], create) | ||
|
r32005 | @annotatesubrepoerror | ||
def addwebdirpath(self, serverpath, webconf): | ||||
cmdutil.addwebdirpath(self._repo, subrelpath(self), webconf) | ||||
|
r18939 | def storeclean(self, path): | ||
|
r27844 | with self._repo.lock(): | ||
|
r21885 | return self._storeclean(path) | ||
def _storeclean(self, path): | ||||
|
r18939 | clean = True | ||
itercache = self._calcstorehash(path) | ||||
|
r25172 | for filehash in self._readstorehashcache(path): | ||
if filehash != next(itercache, None): | ||||
clean = False | ||||
break | ||||
if clean: | ||||
# if not empty: | ||||
|
r18939 | # the cached and current pull states have a different size | ||
|
r25172 | clean = next(itercache, None) is None | ||
|
r18939 | return clean | ||
def _calcstorehash(self, remotepath): | ||||
'''calculate a unique "store hash" | ||||
This method is used to to detect when there are changes that may | ||||
require a push to a given remote path.''' | ||||
# sort the files that will be hashed in increasing (likely) file size | ||||
filelist = ('bookmarks', 'store/phaseroots', 'store/00changelog.i') | ||||
|
r18940 | yield '# %s\n' % _expandedabspath(remotepath) | ||
|
r23365 | vfs = self._repo.vfs | ||
|
r18939 | for relname in filelist: | ||
|
r35600 | filehash = node.hex(hashlib.sha1(vfs.tryread(relname)).digest()) | ||
|
r23365 | yield '%s = %s\n' % (relname, filehash) | ||
|
r18939 | |||
|
r23367 | @propertycache | ||
def _cachestorehashvfs(self): | ||||
|
r31325 | return vfsmod.vfs(self._repo.vfs.join('cache/storehash')) | ||
|
r23367 | |||
|
r18939 | def _readstorehashcache(self, remotepath): | ||
'''read the store hash cache for a given remote repository''' | ||||
|
r23369 | cachefile = _getstorehashcachename(remotepath) | ||
return self._cachestorehashvfs.tryreadlines(cachefile, 'r') | ||||
|
r18939 | |||
def _cachestorehash(self, remotepath): | ||||
'''cache the current store hash | ||||
Each remote repo requires its own store hash cache, because a subrepo | ||||
store may be "clean" versus a given remote repo, but not versus another | ||||
''' | ||||
|
r23372 | cachefile = _getstorehashcachename(remotepath) | ||
|
r27843 | with self._repo.lock(): | ||
|
r21886 | storehash = list(self._calcstorehash(remotepath)) | ||
|
r23372 | vfs = self._cachestorehashvfs | ||
|
r35605 | vfs.writelines(cachefile, storehash, mode='wb', notindexed=True) | ||
|
r18939 | |||
|
r25572 | def _getctx(self): | ||
'''fetch the context for this subrepo revision, possibly a workingctx | ||||
''' | ||||
if self._ctx.rev() is None: | ||||
return self._repo[None] # workingctx if parent is workingctx | ||||
else: | ||||
rev = self._state[1] | ||||
return self._repo[rev] | ||||
|
r18109 | @annotatesubrepoerror | ||
|
r14281 | def _initrepo(self, parentrepo, source, create): | ||
self._repo._subparent = parentrepo | ||||
self._repo._subsource = source | ||||
|
r8813 | |||
|
r10666 | if create: | ||
|
r21891 | lines = ['[paths]\n'] | ||
|
r10666 | |||
def addpathconfig(key, value): | ||||
|
r12753 | if value: | ||
|
r21891 | lines.append('%s = %s\n' % (key, value)) | ||
|
r23574 | self.ui.setconfig('paths', key, value, 'subrepo') | ||
|
r10666 | |||
|
r12753 | defpath = _abssource(self._repo, abort=False) | ||
defpushpath = _abssource(self._repo, True, abort=False) | ||||
|
r10666 | addpathconfig('default', defpath) | ||
|
r10697 | if defpath != defpushpath: | ||
addpathconfig('default-push', defpushpath) | ||||
|
r21891 | |||
|
r35639 | self._repo.vfs.write('hgrc', util.tonativeeol(''.join(lines))) | ||
|
r10666 | |||
|
r18109 | @annotatesubrepoerror | ||
|
r41799 | def add(self, ui, match, prefix, uipathfn, explicitonly, **opts): | ||
return cmdutil.add(ui, self._repo, match, prefix, uipathfn, | ||||
|
r24675 | explicitonly, **opts) | ||
|
r12270 | |||
|
r24132 | @annotatesubrepoerror | ||
|
r41801 | def addremove(self, m, prefix, uipathfn, opts): | ||
|
r23540 | # In the same way as sub directories are processed, once in a subrepo, | ||
# always entry any of its subrepos. Don't corrupt the options that will | ||||
# be used to process sibling subrepos however. | ||||
opts = copy.copy(opts) | ||||
opts['subrepos'] = True | ||||
|
r41801 | return scmutil.addremove(self._repo, m, prefix, uipathfn, opts) | ||
|
r23537 | |||
|
r18109 | @annotatesubrepoerror | ||
|
r32578 | def cat(self, match, fm, fntemplate, prefix, **opts): | ||
|
r21041 | rev = self._state[1] | ||
ctx = self._repo[rev] | ||||
|
r32578 | return cmdutil.cat(self.ui, self._repo, ctx, match, fm, fntemplate, | ||
prefix, **opts) | ||||
|
r21041 | |||
@annotatesubrepoerror | ||||
|
r12166 | def status(self, rev2, **opts): | ||
try: | ||||
rev1 = self._state[1] | ||||
ctx1 = self._repo[rev1] | ||||
ctx2 = self._repo[rev2] | ||||
return self._repo.status(ctx1, ctx2, **opts) | ||||
|
r25660 | except error.RepoLookupError as inst: | ||
|
r23574 | self.ui.warn(_('warning: error "%s" in subrepository "%s"\n') | ||
% (inst, subrelpath(self))) | ||||
|
r22914 | return scmutil.status([], [], [], [], [], [], []) | ||
|
r12166 | |||
|
r18109 | @annotatesubrepoerror | ||
|
r18006 | def diff(self, ui, diffopts, node2, match, prefix, **opts): | ||
|
r12167 | try: | ||
node1 = node.bin(self._state[1]) | ||||
|
r12209 | # We currently expect node2 to come from substate and be | ||
# in hex format | ||||
|
r12210 | if node2 is not None: | ||
node2 = node.bin(node2) | ||||
|
r41779 | logcmdutil.diffordiffstat(ui, self._repo, diffopts, node1, node2, | ||
match, prefix=prefix, listsubrepos=True, | ||||
**opts) | ||||
|
r25660 | except error.RepoLookupError as inst: | ||
|
r23574 | self.ui.warn(_('warning: error "%s" in subrepository "%s"\n') | ||
% (inst, subrelpath(self))) | ||||
|
r12167 | |||
|
r18109 | @annotatesubrepoerror | ||
|
r31099 | def archive(self, archiver, prefix, match=None, decode=True): | ||
|
r15286 | self._get(self._state + ('hg',)) | ||
|
r35943 | files = self.files() | ||
if match: | ||||
files = [f for f in files if match(f)] | ||||
|
r12323 | rev = self._state[1] | ||
ctx = self._repo[rev] | ||||
|
r37780 | scmutil.prefetchfiles(self._repo, [ctx.rev()], | ||
scmutil.matchfiles(self._repo, files)) | ||||
|
r35943 | total = abstractsubrepo.archive(self, archiver, prefix, match) | ||
|
r12323 | for subpath in ctx.substate: | ||
|
r25601 | s = subrepo(ctx, subpath, True) | ||
|
r28017 | submatch = matchmod.subdirmatcher(subpath, match) | ||
|
r41780 | subprefix = prefix + subpath + '/' | ||
total += s.archive(archiver, subprefix, submatch, | ||||
|
r31099 | decode) | ||
|
r18967 | return total | ||
|
r12323 | |||
|
r18109 | @annotatesubrepoerror | ||
|
r33364 | def dirty(self, ignoreupdate=False, missing=False): | ||
|
r8813 | r = self._state[1] | ||
|
r13174 | if r == '' and not ignoreupdate: # no state recorded | ||
|
r8813 | return True | ||
w = self._repo[None] | ||||
|
r14316 | if r != w.p1().hex() and not ignoreupdate: | ||
|
r13325 | # different version checked out | ||
|
r8813 | return True | ||
|
r33364 | return w.dirty(missing=missing) # working directory changed | ||
|
r8813 | |||
|
r16072 | def basestate(self): | ||
return self._repo['.'].hex() | ||||
|
r12162 | def checknested(self, path): | ||
return self._repo._checknested(self._repo.wjoin(path)) | ||||
|
r18109 | @annotatesubrepoerror | ||
|
r8813 | def commit(self, text, user, date): | ||
|
r14898 | # don't bother committing in the subrepo if it's only been | ||
# updated | ||||
if not self.dirty(True): | ||||
return self._repo['.'].hex() | ||||
|
r23574 | self.ui.debug("committing subrepo %s\n" % subrelpath(self)) | ||
|
r8813 | n = self._repo.commit(text, user, date) | ||
if not n: | ||||
return self._repo['.'].hex() # different version checked out | ||||
return node.hex(n) | ||||
|
r8814 | |||
|
r18109 | @annotatesubrepoerror | ||
|
r20176 | def phase(self, state): | ||
|
r37283 | return self._repo[state or '.'].phase() | ||
|
r20176 | |||
@annotatesubrepoerror | ||||
|
r8814 | def remove(self): | ||
# we can't fully delete the repository as it may contain | ||||
# local-only history | ||||
|
r23574 | self.ui.note(_('removing subrepo %s\n') % subrelpath(self)) | ||
|
r8814 | hg.clean(self._repo, node.nullid, False) | ||
|
r9507 | def _get(self, state): | ||
|
r10177 | source, revision, kind = state | ||
|
r34816 | parentrepo = self._repo._subparent | ||
|
r20317 | if revision in self._repo.unfiltered(): | ||
|
r34816 | # Allow shared subrepos tracked at null to setup the sharedpath | ||
if len(self._repo) != 0 or not parentrepo.shared(): | ||||
return True | ||||
|
r20318 | self._repo._subsource = source | ||
srcurl = _abssource(self._repo) | ||||
|
r40691 | |||
# Defer creating the peer until after the status message is logged, in | ||||
# case there are network problems. | ||||
getpeer = lambda: hg.peer(self._repo, {}, srcurl) | ||||
|
r20318 | if len(self._repo) == 0: | ||
|
r24690 | # use self._repo.vfs instead of self.wvfs to remove .hg only | ||
self._repo.vfs.rmtree() | ||||
|
r36705 | |||
# A remote subrepo could be shared if there is a local copy | ||||
# relative to the parent's share source. But clone pooling doesn't | ||||
# assemble the repos in a tree, so that can't be consistently done. | ||||
# A simpler option is for the user to configure clone pooling, and | ||||
# work with that. | ||||
if parentrepo.shared() and hg.islocal(srcurl): | ||||
|
r34816 | self.ui.status(_('sharing subrepo %s from %s\n') | ||
% (subrelpath(self), srcurl)) | ||||
shared = hg.share(self._repo._subparent.baseui, | ||||
|
r40691 | getpeer(), self._repo.root, | ||
|
r34816 | update=False, bookmarks=False) | ||
self._repo = shared.local() | ||||
else: | ||||
|
r36706 | # TODO: find a common place for this and this code in the | ||
# share.py wrap of the clone command. | ||||
if parentrepo.shared(): | ||||
pool = self.ui.config('share', 'pool') | ||||
if pool: | ||||
pool = util.expandpath(pool) | ||||
shareopts = { | ||||
'pool': pool, | ||||
'mode': self.ui.config('share', 'poolnaming'), | ||||
} | ||||
else: | ||||
shareopts = {} | ||||
|
r34816 | self.ui.status(_('cloning subrepo %s from %s\n') | ||
|
r39579 | % (subrelpath(self), util.hidepassword(srcurl))) | ||
|
r34816 | other, cloned = hg.clone(self._repo._subparent.baseui, {}, | ||
|
r40691 | getpeer(), self._repo.root, | ||
|
r36706 | update=False, shareopts=shareopts) | ||
|
r34816 | self._repo = cloned.local() | ||
|
r20318 | self._initrepo(parentrepo, source, create=True) | ||
self._cachestorehash(srcurl) | ||||
|
r20317 | else: | ||
|
r23574 | self.ui.status(_('pulling subrepo %s from %s\n') | ||
|
r39579 | % (subrelpath(self), util.hidepassword(srcurl))) | ||
|
r20318 | cleansub = self.storeclean(srcurl) | ||
|
r40691 | exchange.pull(self._repo, getpeer()) | ||
|
r20318 | if cleansub: | ||
# keep the repo clean after pull | ||||
|
r18939 | self._cachestorehash(srcurl) | ||
|
r20319 | return False | ||
|
r8814 | |||
|
r18109 | @annotatesubrepoerror | ||
|
r13322 | def get(self, state, overwrite=False): | ||
|
r20319 | inrepo = self._get(state) | ||
|
r10177 | source, revision, kind = state | ||
|
r20319 | repo = self._repo | ||
repo.ui.debug("getting subrepo %s\n" % self._path) | ||||
if inrepo: | ||||
urepo = repo.unfiltered() | ||||
ctx = urepo[revision] | ||||
if ctx.hidden(): | ||||
urepo.ui.warn( | ||||
|
r41925 | _('revision %s in subrepository "%s" is hidden\n') | ||
|
r20319 | % (revision[0:12], self._path)) | ||
repo = urepo | ||||
hg.updaterepo(repo, revision, overwrite) | ||||
|
r8814 | |||
|
r18109 | @annotatesubrepoerror | ||
|
r8814 | def merge(self, state): | ||
|
r9507 | self._get(state) | ||
|
r9781 | cur = self._repo['.'] | ||
dst = self._repo[state[1]] | ||||
|
r10251 | anc = dst.ancestor(cur) | ||
|
r13417 | |||
def mergefunc(): | ||||
|
r16196 | if anc == cur and dst.branch() == cur.branch(): | ||
|
r33365 | self.ui.debug('updating subrepository "%s"\n' | ||
% subrelpath(self)) | ||||
|
r13417 | hg.update(self._repo, state[1]) | ||
elif anc == dst: | ||||
|
r33365 | self.ui.debug('skipping subrepository "%s"\n' | ||
% subrelpath(self)) | ||||
|
r13417 | else: | ||
|
r33365 | self.ui.debug('merging subrepository "%s"\n' % subrelpath(self)) | ||
|
r13417 | hg.merge(self._repo, state[1], remind=False) | ||
wctx = self._repo[None] | ||||
if self.dirty(): | ||||
if anc != dst: | ||||
|
r23574 | if _updateprompt(self.ui, self, wctx.dirty(), cur, dst): | ||
|
r13417 | mergefunc() | ||
else: | ||||
mergefunc() | ||||
|
r9781 | else: | ||
|
r13417 | mergefunc() | ||
|
r8815 | |||
|
r18109 | @annotatesubrepoerror | ||
|
r15708 | def push(self, opts): | ||
force = opts.get('force') | ||||
newbranch = opts.get('new_branch') | ||||
ssh = opts.get('ssh') | ||||
|
r8815 | # push subrepos depth-first for coherent ordering | ||
|
r37170 | c = self._repo['.'] | ||
|
r8815 | subs = c.substate # only repos that are committed | ||
for s in sorted(subs): | ||||
|
r16022 | if c.sub(s).push(opts) == 0: | ||
|
r11067 | return False | ||
|
r8815 | |||
dsturl = _abssource(self._repo, True) | ||||
|
r18940 | if not force: | ||
if self.storeclean(dsturl): | ||||
|
r23574 | self.ui.status( | ||
|
r18940 | _('no changes made to subrepo %s since last push to %s\n') | ||
|
r39579 | % (subrelpath(self), util.hidepassword(dsturl))) | ||
|
r18940 | return None | ||
|
r23574 | self.ui.status(_('pushing subrepo %s to %s\n') % | ||
|
r39579 | (subrelpath(self), util.hidepassword(dsturl))) | ||
|
r17874 | other = hg.peer(self._repo, {'ssh': ssh}, dsturl) | ||
|
r22619 | res = exchange.push(self._repo, other, force, newbranch=newbranch) | ||
|
r18939 | |||
# the repo is now clean | ||||
self._cachestorehash(dsturl) | ||||
|
r22619 | return res.cgresult | ||
|
r10177 | |||
|
r18109 | @annotatesubrepoerror | ||
|
r12272 | def outgoing(self, ui, dest, opts): | ||
|
r24875 | if 'rev' in opts or 'branch' in opts: | ||
opts = copy.copy(opts) | ||||
opts.pop('rev', None) | ||||
opts.pop('branch', None) | ||||
|
r12272 | return hg.outgoing(ui, self._repo, _abssource(self._repo, True), opts) | ||
|
r18109 | @annotatesubrepoerror | ||
|
r12274 | def incoming(self, ui, source, opts): | ||
|
r24876 | if 'rev' in opts or 'branch' in opts: | ||
opts = copy.copy(opts) | ||||
opts.pop('rev', None) | ||||
opts.pop('branch', None) | ||||
|
r12274 | return hg.incoming(ui, self._repo, _abssource(self._repo, False), opts) | ||
|
r18109 | @annotatesubrepoerror | ||
|
r12322 | def files(self): | ||
rev = self._state[1] | ||||
ctx = self._repo[rev] | ||||
|
r24173 | return ctx.manifest().keys() | ||
|
r12322 | |||
|
r31099 | def filedata(self, name, decode): | ||
|
r12322 | rev = self._state[1] | ||
|
r31099 | data = self._repo[rev][name].data() | ||
if decode: | ||||
data = self._repo.wwritedata(name, data) | ||||
return data | ||||
|
r12322 | |||
def fileflags(self, name): | ||||
rev = self._state[1] | ||||
ctx = self._repo[rev] | ||||
return ctx.flags(name) | ||||
|
r24413 | @annotatesubrepoerror | ||
|
r41914 | def printfiles(self, ui, m, uipathfn, fm, fmt, subrepos): | ||
|
r24413 | # If the parent context is a workingctx, use the workingctx here for | ||
# consistency. | ||||
if self._ctx.rev() is None: | ||||
ctx = self._repo[None] | ||||
else: | ||||
rev = self._state[1] | ||||
ctx = self._repo[rev] | ||||
|
r41914 | return cmdutil.files(ui, ctx, m, uipathfn, fm, fmt, subrepos) | ||
|
r24413 | |||
|
r25121 | @annotatesubrepoerror | ||
|
r38631 | def matchfileset(self, expr, badfn=None): | ||
|
r25121 | if self._ctx.rev() is None: | ||
|
r41825 | ctx = self._repo[None] | ||
|
r25121 | else: | ||
rev = self._state[1] | ||||
|
r41825 | ctx = self._repo[rev] | ||
|
r25121 | |||
|
r38631 | matchers = [ctx.matchfileset(expr, badfn=badfn)] | ||
|
r25121 | |||
for subpath in ctx.substate: | ||||
sub = ctx.sub(subpath) | ||||
try: | ||||
|
r38631 | sm = sub.matchfileset(expr, badfn=badfn) | ||
|
r41824 | pm = matchmod.prefixdirmatcher(subpath, sm, badfn=badfn) | ||
|
r38631 | matchers.append(pm) | ||
|
r25121 | except error.LookupError: | ||
self.ui.status(_("skipping missing subrepository: %s\n") | ||||
% self.wvfs.reljoin(reporelpath(self), subpath)) | ||||
|
r38631 | if len(matchers) == 1: | ||
return matchers[0] | ||||
return matchmod.unionmatcher(matchers) | ||||
|
r25121 | |||
|
r15410 | def walk(self, match): | ||
ctx = self._repo[None] | ||||
return ctx.walk(match) | ||||
|
r12322 | |||
|
r18109 | @annotatesubrepoerror | ||
|
r41802 | def forget(self, match, prefix, uipathfn, dryrun, interactive): | ||
return cmdutil.forget(self.ui, self._repo, match, prefix, uipathfn, | ||||
|
r37796 | True, dryrun=dryrun, interactive=interactive) | ||
|
r12322 | |||
|
r18109 | @annotatesubrepoerror | ||
|
r41800 | def removefiles(self, matcher, prefix, uipathfn, after, force, subrepos, | ||
|
r37168 | dryrun, warnings): | ||
|
r41800 | return cmdutil.remove(self.ui, self._repo, matcher, prefix, uipathfn, | ||
|
r37168 | after, force, subrepos, dryrun) | ||
|
r23325 | |||
@annotatesubrepoerror | ||||
|
r23579 | def revert(self, substate, *pats, **opts): | ||
|
r16430 | # reverting a subrepo is a 2 step process: | ||
# 1. if the no_backup is not set, revert all modified | ||||
# files inside the subrepo | ||||
# 2. update the subrepo to the revision specified in | ||||
# the corresponding substate dictionary | ||||
|
r23579 | self.ui.status(_('reverting subrepo %s\n') % substate[0]) | ||
|
r35148 | if not opts.get(r'no_backup'): | ||
|
r16430 | # Revert all files on the subrepo, creating backups | ||
# Note that this will not recursively revert subrepos | ||||
# We could do it if there was a set:subrepos() predicate | ||||
opts = opts.copy() | ||||
|
r35148 | opts[r'date'] = None | ||
opts[r'rev'] = substate[1] | ||||
|
r16430 | |||
|
r23579 | self.filerevert(*pats, **opts) | ||
|
r16429 | |||
# Update the repo to the revision specified in the given substate | ||||
|
r35148 | if not opts.get(r'dry_run'): | ||
|
r24134 | self.get(substate, overwrite=True) | ||
|
r16429 | |||
|
r23579 | def filerevert(self, *pats, **opts): | ||
|
r35148 | ctx = self._repo[opts[r'rev']] | ||
|
r16430 | parents = self._repo.dirstate.parents() | ||
|
r35148 | if opts.get(r'all'): | ||
|
r16430 | pats = ['set:modified()'] | ||
else: | ||||
pats = [] | ||||
|
r23579 | cmdutil.revert(self.ui, self._repo, ctx, parents, *pats, **opts) | ||
|
r16430 | |||
|
r21400 | def shortid(self, revid): | ||
return revid[:12] | ||||
|
r34880 | @annotatesubrepoerror | ||
def unshare(self): | ||||
# subrepo inherently violates our import layering rules | ||||
# because it wants to make repo objects from deep inside the stack | ||||
# so we manually delay the circular imports to not break | ||||
# scripts that don't use our demand-loading | ||||
global hg | ||||
from . import hg as h | ||||
hg = h | ||||
# Nothing prevents a user from sharing in a repo, and then making that a | ||||
# subrepo. Alternately, the previous unshare attempt may have failed | ||||
# part way through. So recurse whether or not this layer is shared. | ||||
if self._repo.shared(): | ||||
self.ui.status(_("unsharing subrepo '%s'\n") % self._relpath) | ||||
hg.unshare(self.ui, self._repo) | ||||
|
r25591 | def verify(self): | ||
try: | ||||
rev = self._state[1] | ||||
ctx = self._repo.unfiltered()[rev] | ||||
if ctx.hidden(): | ||||
# Since hidden revisions aren't pushed/pulled, it seems worth an | ||||
# explicit warning. | ||||
ui = self._repo.ui | ||||
ui.warn(_("subrepo '%s' is hidden in revision %s\n") % | ||||
(self._relpath, node.short(self._ctx.node()))) | ||||
return 0 | ||||
except error.RepoLookupError: | ||||
# A missing subrepo revision may be a case of needing to pull it, so | ||||
# don't treat this as an error. | ||||
self._repo.ui.warn(_("subrepo '%s' not found in revision %s\n") % | ||||
(self._relpath, node.short(self._ctx.node()))) | ||||
return 0 | ||||
|
r24672 | @propertycache | ||
def wvfs(self): | ||||
|
r26781 | """return own wvfs for efficiency and consistency | ||
|
r24672 | """ | ||
return self._repo.wvfs | ||||
|
r24786 | @propertycache | ||
def _relpath(self): | ||||
"""return path to this subrepository as seen from outermost repository | ||||
""" | ||||
# Keep consistent dir separators by avoiding vfs.join(self._path) | ||||
return reporelpath(self._repo) | ||||
|
r11559 | class svnsubrepo(abstractsubrepo): | ||
|
r29021 | def __init__(self, ctx, path, state, allowcreate): | ||
|
r24671 | super(svnsubrepo, self).__init__(ctx, path) | ||
|
r10178 | self._state = state | ||
|
r37138 | self._exe = procutil.findexe('svn') | ||
|
r15190 | if not self._exe: | ||
|
r26587 | raise error.Abort(_("'svn' executable not found for subrepo '%s'") | ||
|
r15190 | % self._path) | ||
|
r10178 | |||
|
r14664 | def _svncommand(self, commands, filename='', failok=False): | ||
|
r15190 | cmd = [self._exe] | ||
|
r14506 | extrakw = {} | ||
|
r23572 | if not self.ui.interactive(): | ||
|
r14506 | # Making stdin be a pipe should prevent svn from behaving | ||
# interactively even if we can't pass --non-interactive. | ||||
|
r35372 | extrakw[r'stdin'] = subprocess.PIPE | ||
|
r14506 | # Starting in svn 1.5 --non-interactive is a global flag | ||
# instead of being per-command, but we need to support 1.4 so | ||||
# we have to be intelligent about what commands take | ||||
# --non-interactive. | ||||
if commands[0] in ('update', 'checkout', 'commit'): | ||||
cmd.append('--non-interactive') | ||||
|
r14025 | cmd.extend(commands) | ||
|
r14050 | if filename is not None: | ||
|
r24675 | path = self.wvfs.reljoin(self._ctx.repo().origroot, | ||
self._path, filename) | ||||
|
r14050 | cmd.append(path) | ||
|
r30635 | env = dict(encoding.environ) | ||
|
r10271 | # Avoid localized output, preserve current locale for everything else. | ||
|
r17705 | lc_all = env.get('LC_ALL') | ||
if lc_all: | ||||
env['LANG'] = lc_all | ||||
del env['LC_ALL'] | ||||
|
r10271 | env['LC_MESSAGES'] = 'C' | ||
|
r39851 | p = subprocess.Popen(pycompat.rapply(procutil.tonativestr, cmd), | ||
bufsize=-1, close_fds=procutil.closefds, | ||||
|
r13014 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, | ||
|
r39851 | env=procutil.tonativeenv(env), **extrakw) | ||
|
r41658 | stdout, stderr = map(util.fromnativeeol, p.communicate()) | ||
|
r13014 | stderr = stderr.strip() | ||
|
r14664 | if not failok: | ||
if p.returncode: | ||||
|
r26587 | raise error.Abort(stderr or 'exited with code %d' | ||
% p.returncode) | ||||
|
r14664 | if stderr: | ||
|
r23572 | self.ui.warn(stderr + '\n') | ||
|
r14664 | return stdout, stderr | ||
|
r10178 | |||
|
r14050 | @propertycache | ||
def _svnversion(self): | ||||
|
r17707 | output, err = self._svncommand(['--version', '--quiet'], filename=None) | ||
|
r34070 | m = re.search(br'^(\d+)\.(\d+)', output) | ||
|
r14050 | if not m: | ||
|
r26587 | raise error.Abort(_('cannot retrieve svn tool version')) | ||
|
r14050 | return (int(m.group(1)), int(m.group(2))) | ||
|
r35679 | def _svnmissing(self): | ||
return not self.wvfs.exists('.svn') | ||||
|
r13287 | def _wcrevs(self): | ||
# Get the working directory revision as well as the last | ||||
# commit revision so we can compare the subrepo state with | ||||
# both. We used to store the working directory one. | ||||
|
r14664 | output, err = self._svncommand(['info', '--xml']) | ||
|
r10272 | doc = xml.dom.minidom.parseString(output) | ||
|
r41635 | entries = doc.getElementsByTagName(r'entry') | ||
|
r13287 | lastrev, rev = '0', '0' | ||
if entries: | ||||
|
r41635 | rev = pycompat.bytestr(entries[0].getAttribute(r'revision')) or '0' | ||
commits = entries[0].getElementsByTagName(r'commit') | ||||
|
r13287 | if commits: | ||
|
r41635 | lastrev = pycompat.bytestr( | ||
commits[0].getAttribute(r'revision')) or '0' | ||||
|
r13287 | return (lastrev, rev) | ||
def _wcrev(self): | ||||
return self._wcrevs()[0] | ||||
|
r10178 | |||
|
r10273 | def _wcchanged(self): | ||
|
r16530 | """Return (changes, extchanges, missing) where changes is True | ||
if the working directory was changed, extchanges is | ||||
True if any of these changes concern an external entry and missing | ||||
is True if any change is a missing entry. | ||||
|
r10273 | """ | ||
|
r14664 | output, err = self._svncommand(['status', '--xml']) | ||
|
r16530 | externals, changes, missing = [], [], [] | ||
|
r10272 | doc = xml.dom.minidom.parseString(output) | ||
|
r41635 | for e in doc.getElementsByTagName(r'entry'): | ||
s = e.getElementsByTagName(r'wc-status') | ||||
|
r10273 | if not s: | ||
continue | ||||
|
r41635 | item = s[0].getAttribute(r'item') | ||
props = s[0].getAttribute(r'props') | ||||
path = e.getAttribute(r'path').encode('utf8') | ||||
if item == r'external': | ||||
|
r10273 | externals.append(path) | ||
|
r41635 | elif item == r'missing': | ||
|
r16530 | missing.append(path) | ||
|
r41635 | if (item not in (r'', r'normal', r'unversioned', r'external') | ||
or props not in (r'', r'none', r'normal')): | ||||
|
r10273 | changes.append(path) | ||
for path in changes: | ||||
for ext in externals: | ||||
|
r30615 | if path == ext or path.startswith(ext + pycompat.ossep): | ||
|
r16530 | return True, True, bool(missing) | ||
return bool(changes), False, bool(missing) | ||||
|
r10178 | |||
|
r35678 | @annotatesubrepoerror | ||
|
r33364 | def dirty(self, ignoreupdate=False, missing=False): | ||
|
r35694 | if self._svnmissing(): | ||
return self._state[1] != '' | ||||
|
r33364 | wcchanged = self._wcchanged() | ||
changed = wcchanged[0] or (missing and wcchanged[2]) | ||||
if not changed: | ||||
|
r13288 | if self._state[1] in self._wcrevs() or ignoreupdate: | ||
|
r13174 | return False | ||
|
r10178 | return True | ||
|
r16072 | def basestate(self): | ||
|
r16554 | lastrev, rev = self._wcrevs() | ||
if lastrev != rev: | ||||
# Last committed rev is not the same than rev. We would | ||||
# like to take lastrev but we do not know if the subrepo | ||||
# URL exists at lastrev. Test it and fallback to rev it | ||||
# is not there. | ||||
try: | ||||
|
r17035 | self._svncommand(['list', '%s@%s' % (self._state[0], lastrev)]) | ||
|
r16554 | return lastrev | ||
except error.Abort: | ||||
pass | ||||
return rev | ||||
|
r16072 | |||
|
r18109 | @annotatesubrepoerror | ||
|
r10178 | def commit(self, text, user, date): | ||
# user and date are out of our hands since svn is centralized | ||||
|
r16530 | changed, extchanged, missing = self._wcchanged() | ||
|
r10273 | if not changed: | ||
|
r16554 | return self.basestate() | ||
|
r10273 | if extchanged: | ||
# Do not try to commit externals | ||||
|
r26587 | raise error.Abort(_('cannot commit svn externals')) | ||
|
r16530 | if missing: | ||
# svn can commit with missing entries but aborting like hg | ||||
# seems a better approach. | ||||
|
r26587 | raise error.Abort(_('cannot commit missing svn entries')) | ||
|
r14664 | commitinfo, err = self._svncommand(['commit', '-m', text]) | ||
|
r23572 | self.ui.status(commitinfo) | ||
|
r12060 | newrev = re.search('Committed revision ([0-9]+).', commitinfo) | ||
|
r10178 | if not newrev: | ||
|
r16529 | if not commitinfo.strip(): | ||
# Sometimes, our definition of "changed" differs from | ||||
# svn one. For instance, svn ignores missing files | ||||
# when committing. If there are only missing files, no | ||||
# commit is made, no output and no error code. | ||||
|
r26587 | raise error.Abort(_('failed to commit svn changes')) | ||
raise error.Abort(commitinfo.splitlines()[-1]) | ||||
|
r10178 | newrev = newrev.groups()[0] | ||
|
r23572 | self.ui.status(self._svncommand(['update', '-r', newrev])[0]) | ||
|
r10178 | return newrev | ||
|
r18109 | @annotatesubrepoerror | ||
|
r10178 | def remove(self): | ||
if self.dirty(): | ||||
|
r23572 | self.ui.warn(_('not removing repo %s because ' | ||
'it has changes.\n') % self._path) | ||||
|
r10178 | return | ||
|
r23572 | self.ui.note(_('removing subrepo %s\n') % self._path) | ||
|
r13013 | |||
|
r24690 | self.wvfs.rmtree(forcibly=True) | ||
|
r13015 | try: | ||
|
r25773 | pwvfs = self._ctx.repo().wvfs | ||
pwvfs.removedirs(pwvfs.dirname(self._path)) | ||||
|
r13015 | except OSError: | ||
pass | ||||
|
r10178 | |||
|
r18109 | @annotatesubrepoerror | ||
|
r13322 | def get(self, state, overwrite=False): | ||
if overwrite: | ||||
|
r13332 | self._svncommand(['revert', '--recursive']) | ||
|
r14050 | args = ['checkout'] | ||
if self._svnversion >= (1, 5): | ||||
args.append('--force') | ||||
|
r14820 | # The revision must be specified at the end of the URL to properly | ||
# update to a directory which has since been deleted and recreated. | ||||
args.append('%s@%s' % (state[0], state[1])) | ||||
|
r33730 | |||
# SEC: check that the ssh url is safe | ||||
util.checksafessh(state[0]) | ||||
|
r14664 | status, err = self._svncommand(args, failok=True) | ||
|
r24724 | _sanitize(self.ui, self.wvfs, '.svn') | ||
|
r12060 | if not re.search('Checked out revision [0-9]+.', status): | ||
|
r14664 | if ('is already a working copy for a different URL' in err | ||
|
r16530 | and (self._wcchanged()[:2] == (False, False))): | ||
|
r14664 | # obstructed but clean working copy, so just blow it away. | ||
self.remove() | ||||
self.get(state, overwrite=False) | ||||
return | ||||
|
r26587 | raise error.Abort((status or err).splitlines()[-1]) | ||
|
r23572 | self.ui.status(status) | ||
|
r10178 | |||
|
r18109 | @annotatesubrepoerror | ||
|
r10178 | def merge(self, state): | ||
|
r13417 | old = self._state[1] | ||
new = state[1] | ||||
|
r16555 | wcrev = self._wcrev() | ||
if new != wcrev: | ||||
dirty = old == wcrev or self._wcchanged()[0] | ||||
|
r23572 | if _updateprompt(self.ui, self, dirty, wcrev, new): | ||
|
r13417 | self.get(state, False) | ||
|
r10178 | |||
|
r15708 | def push(self, opts): | ||
|
r11455 | # push is a no-op for SVN | ||
return True | ||||
|
r10178 | |||
|
r18109 | @annotatesubrepoerror | ||
|
r12322 | def files(self): | ||
|
r16450 | output = self._svncommand(['list', '--recursive', '--xml'])[0] | ||
doc = xml.dom.minidom.parseString(output) | ||||
paths = [] | ||||
|
r41635 | for e in doc.getElementsByTagName(r'entry'): | ||
kind = pycompat.bytestr(e.getAttribute(r'kind')) | ||||
|
r16450 | if kind != 'file': | ||
continue | ||||
|
r41635 | name = r''.join(c.data for c | ||
in e.getElementsByTagName(r'name')[0].childNodes | ||||
if c.nodeType == c.TEXT_NODE) | ||||
paths.append(name.encode('utf8')) | ||||
|
r16450 | return paths | ||
|
r12322 | |||
|
r31099 | def filedata(self, name, decode): | ||
|
r16450 | return self._svncommand(['cat'], name)[0] | ||
|
r12322 | |||
|
r13106 | class gitsubrepo(abstractsubrepo): | ||
|
r29021 | def __init__(self, ctx, path, state, allowcreate): | ||
|
r24671 | super(gitsubrepo, self).__init__(ctx, path) | ||
|
r12992 | self._state = state | ||
|
r24302 | self._abspath = ctx.repo().wjoin(path) | ||
self._subparent = ctx.repo() | ||||
|
r17024 | self._ensuregit() | ||
def _ensuregit(self): | ||||
|
r17025 | try: | ||
self._gitexecutable = 'git' | ||||
out, err = self._gitnodir(['--version']) | ||||
|
r25660 | except OSError as e: | ||
|
r27935 | genericerror = _("error executing git for subrepo '%s': %s") | ||
notfoundhint = _("check git is installed and in your PATH") | ||||
if e.errno != errno.ENOENT: | ||||
|
r34024 | raise error.Abort(genericerror % ( | ||
self._path, encoding.strtolocal(e.strerror))) | ||||
|
r34646 | elif pycompat.iswindows: | ||
|
r27935 | try: | ||
self._gitexecutable = 'git.cmd' | ||||
out, err = self._gitnodir(['--version']) | ||||
except OSError as e2: | ||||
if e2.errno == errno.ENOENT: | ||||
raise error.Abort(_("couldn't find 'git' or 'git.cmd'" | ||||
" for subrepo '%s'") % self._path, | ||||
hint=notfoundhint) | ||||
else: | ||||
raise error.Abort(genericerror % (self._path, | ||||
|
r34024 | encoding.strtolocal(e2.strerror))) | ||
|
r27935 | else: | ||
raise error.Abort(_("couldn't find git for subrepo '%s'") | ||||
% self._path, hint=notfoundhint) | ||||
|
r20840 | versionstatus = self._checkversion(out) | ||
if versionstatus == 'unknown': | ||||
|
r23572 | self.ui.warn(_('cannot retrieve git version\n')) | ||
|
r20840 | elif versionstatus == 'abort': | ||
|
r26587 | raise error.Abort(_('git subrepo requires at least 1.6.0 or later')) | ||
|
r20840 | elif versionstatus == 'warning': | ||
|
r23572 | self.ui.warn(_('git subrepo requires at least 1.6.0 or later\n')) | ||
|
r20840 | |||
@staticmethod | ||||
|
r23521 | def _gitversion(out): | ||
|
r34070 | m = re.search(br'^git version (\d+)\.(\d+)\.(\d+)', out) | ||
|
r23522 | if m: | ||
return (int(m.group(1)), int(m.group(2)), int(m.group(3))) | ||||
|
r34070 | m = re.search(br'^git version (\d+)\.(\d+)', out) | ||
|
r23521 | if m: | ||
|
r23522 | return (int(m.group(1)), int(m.group(2)), 0) | ||
|
r23521 | |||
return -1 | ||||
@staticmethod | ||||
|
r20840 | def _checkversion(out): | ||
'''ensure git version is new enough | ||||
>>> _checkversion = gitsubrepo._checkversion | ||||
|
r34133 | >>> _checkversion(b'git version 1.6.0') | ||
|
r20840 | 'ok' | ||
|
r34133 | >>> _checkversion(b'git version 1.8.5') | ||
|
r20840 | 'ok' | ||
|
r34133 | >>> _checkversion(b'git version 1.4.0') | ||
|
r20840 | 'abort' | ||
|
r34133 | >>> _checkversion(b'git version 1.5.0') | ||
|
r20840 | 'warning' | ||
|
r34133 | >>> _checkversion(b'git version 1.9-rc0') | ||
|
r20840 | 'ok' | ||
|
r34133 | >>> _checkversion(b'git version 1.9.0.265.g81cdec2') | ||
|
r20840 | 'ok' | ||
|
r34133 | >>> _checkversion(b'git version 1.9.0.GIT') | ||
|
r20840 | 'ok' | ||
|
r34133 | >>> _checkversion(b'git version 12345') | ||
|
r20840 | 'unknown' | ||
|
r34133 | >>> _checkversion(b'no') | ||
|
r20840 | 'unknown' | ||
''' | ||||
|
r23521 | version = gitsubrepo._gitversion(out) | ||
|
r17024 | # git 1.4.0 can't work at all, but 1.5.X can in at least some cases, | ||
# despite the docstring comment. For now, error on 1.4.0, warn on | ||||
# 1.5.0 but attempt to continue. | ||||
|
r23521 | if version == -1: | ||
return 'unknown' | ||||
|
r23522 | if version < (1, 5, 0): | ||
|
r20840 | return 'abort' | ||
|
r23522 | elif version < (1, 6, 0): | ||
|
r20840 | return 'warning' | ||
return 'ok' | ||||
|
r12992 | |||
|
r13095 | def _gitcommand(self, commands, env=None, stream=False): | ||
return self._gitdir(commands, env=env, stream=stream)[0] | ||||
|
r12992 | |||
|
r13095 | def _gitdir(self, commands, env=None, stream=False): | ||
|
r13181 | return self._gitnodir(commands, env=env, stream=stream, | ||
cwd=self._abspath) | ||||
|
r12992 | |||
|
r13095 | def _gitnodir(self, commands, env=None, stream=False, cwd=None): | ||
|
r12992 | """Calls the git command | ||
|
r17424 | The methods tries to call the git command. versions prior to 1.6.0 | ||
|
r12992 | are not supported and very probably fail. | ||
""" | ||||
|
r23572 | self.ui.debug('%s: git %s\n' % (self._relpath, ' '.join(commands))) | ||
|
r28658 | if env is None: | ||
|
r30635 | env = encoding.environ.copy() | ||
|
r28949 | # disable localization for Git output (issue5176) | ||
env['LC_ALL'] = 'C' | ||||
|
r28658 | # fix for Git CVE-2015-7545 | ||
if 'GIT_ALLOW_PROTOCOL' not in env: | ||||
env['GIT_ALLOW_PROTOCOL'] = 'file:git:http:https:ssh' | ||||
|
r13111 | # unless ui.quiet is set, print git's stderr, | ||
# which is mostly progress and useful info | ||||
errpipe = None | ||||
|
r23572 | if self.ui.quiet: | ||
|
r13111 | errpipe = open(os.devnull, 'w') | ||
|
r31102 | if self.ui._colormode and len(commands) and commands[0] == "diff": | ||
# insert the argument in the front, | ||||
# the end of git diff arguments is used for paths | ||||
commands.insert(1, '--color') | ||||
|
r39851 | p = subprocess.Popen(pycompat.rapply(procutil.tonativestr, | ||
[self._gitexecutable] + commands), | ||||
bufsize=-1, | ||||
cwd=pycompat.rapply(procutil.tonativestr, cwd), | ||||
env=procutil.tonativeenv(env), | ||||
close_fds=procutil.closefds, | ||||
|
r13111 | stdout=subprocess.PIPE, stderr=errpipe) | ||
|
r13027 | if stream: | ||
return p.stdout, None | ||||
|
r13085 | retdata = p.stdout.read().strip() | ||
|
r12992 | # wait for the child to exit to avoid race condition. | ||
p.wait() | ||||
|
r13107 | if p.returncode != 0 and p.returncode != 1: | ||
|
r12992 | # there are certain error codes that are ok | ||
|
r13110 | command = commands[0] | ||
|
r13150 | if command in ('cat-file', 'symbolic-ref'): | ||
|
r12992 | return retdata, p.returncode | ||
# for all others, abort | ||||
|
r29389 | raise error.Abort(_('git %s error %d in %s') % | ||
|
r13110 | (command, p.returncode, self._relpath)) | ||
|
r12992 | |||
return retdata, p.returncode | ||||
|
r13553 | def _gitmissing(self): | ||
|
r24695 | return not self.wvfs.exists('.git') | ||
|
r13553 | |||
|
r12992 | def _gitstate(self): | ||
|
r13085 | return self._gitcommand(['rev-parse', 'HEAD']) | ||
|
r12992 | |||
|
r13152 | def _gitcurrentbranch(self): | ||
current, err = self._gitdir(['symbolic-ref', 'HEAD', '--quiet']) | ||||
if err: | ||||
current = None | ||||
return current | ||||
|
r13569 | def _gitremote(self, remote): | ||
out = self._gitcommand(['remote', 'show', '-n', remote]) | ||||
line = out.split('\n')[1] | ||||
i = line.index('URL: ') + len('URL: ') | ||||
return line[i:] | ||||
|
r12992 | def _githavelocally(self, revision): | ||
out, code = self._gitdir(['cat-file', '-e', revision]) | ||||
return code == 0 | ||||
|
r13029 | def _gitisancestor(self, r1, r2): | ||
|
r13085 | base = self._gitcommand(['merge-base', r1, r2]) | ||
|
r13029 | return base == r1 | ||
|
r14440 | def _gitisbare(self): | ||
return self._gitcommand(['config', '--bool', 'core.bare']) == 'true' | ||||
|
r15531 | def _gitupdatestat(self): | ||
"""This must be run before git diff-index. | ||||
diff-index only looks at changes to file stat; | ||||
this command looks at file contents and updates the stat.""" | ||||
self._gitcommand(['update-index', '-q', '--refresh']) | ||||
|
r12995 | def _gitbranchmap(self): | ||
|
r13178 | '''returns 2 things: | ||
|
r13086 | a map from git branch to revision | ||
|
r13178 | a map from revision to branches''' | ||
|
r13086 | branch2rev = {} | ||
rev2branch = {} | ||||
|
r13178 | |||
|
r13150 | out = self._gitcommand(['for-each-ref', '--format', | ||
|
r13178 | '%(objectname) %(refname)']) | ||
|
r12995 | for line in out.split('\n'): | ||
|
r13178 | revision, ref = line.split(' ') | ||
|
r13465 | if (not ref.startswith('refs/heads/') and | ||
not ref.startswith('refs/remotes/')): | ||||
|
r12995 | continue | ||
|
r13150 | if ref.startswith('refs/remotes/') and ref.endswith('/HEAD'): | ||
|
r12995 | continue # ignore remote/HEAD redirects | ||
|
r13150 | branch2rev[ref] = revision | ||
rev2branch.setdefault(revision, []).append(ref) | ||||
|
r13178 | return branch2rev, rev2branch | ||
def _gittracking(self, branches): | ||||
'return map of remote branch to local tracking branch' | ||||
# assumes no more than one local tracking branch for each remote | ||||
tracking = {} | ||||
for b in branches: | ||||
if b.startswith('refs/remotes/'): | ||||
continue | ||||
|
r15234 | bname = b.split('/', 2)[2] | ||
remote = self._gitcommand(['config', 'branch.%s.remote' % bname]) | ||||
|
r13178 | if remote: | ||
|
r15234 | ref = self._gitcommand(['config', 'branch.%s.merge' % bname]) | ||
|
r13178 | tracking['refs/remotes/%s/%s' % | ||
(remote, ref.split('/', 2)[2])] = b | ||||
return tracking | ||||
|
r13087 | |||
|
r13460 | def _abssource(self, source): | ||
|
r13692 | if '://' not in source: | ||
# recognize the scp syntax as an absolute source | ||||
colon = source.find(':') | ||||
if colon != -1 and '/' not in source[:colon]: | ||||
return source | ||||
|
r13460 | self._subsource = source | ||
return _abssource(self) | ||||
|
r12993 | def _fetch(self, source, revision): | ||
|
r13553 | if self._gitmissing(): | ||
|
r33731 | # SEC: check for safe ssh url | ||
util.checksafessh(source) | ||||
|
r13525 | source = self._abssource(source) | ||
|
r23572 | self.ui.status(_('cloning subrepo %s from %s\n') % | ||
|
r13525 | (self._relpath, source)) | ||
self._gitnodir(['clone', source, self._abspath]) | ||||
|
r12993 | if self._githavelocally(revision): | ||
return | ||||
|
r23572 | self.ui.status(_('pulling subrepo %s from %s\n') % | ||
|
r13569 | (self._relpath, self._gitremote('origin'))) | ||
|
r13466 | # try only origin: the originally cloned repo | ||
|
r13097 | self._gitcommand(['fetch']) | ||
|
r12993 | if not self._githavelocally(revision): | ||
|
r33365 | raise error.Abort(_('revision %s does not exist in subrepository ' | ||
'"%s"\n') % (revision, self._relpath)) | ||||
|
r12993 | |||
|
r18109 | @annotatesubrepoerror | ||
|
r33364 | def dirty(self, ignoreupdate=False, missing=False): | ||
|
r13553 | if self._gitmissing(): | ||
|
r14469 | return self._state[1] != '' | ||
|
r14440 | if self._gitisbare(): | ||
return True | ||||
|
r13179 | if not ignoreupdate and self._state[1] != self._gitstate(): | ||
|
r13325 | # different version checked out | ||
|
r12992 | return True | ||
# check for staged changes or modified files; ignore untracked files | ||||
|
r15531 | self._gitupdatestat() | ||
|
r13153 | out, code = self._gitdir(['diff-index', '--quiet', 'HEAD']) | ||
return code == 1 | ||||
|
r12992 | |||
|
r16072 | def basestate(self): | ||
return self._gitstate() | ||||
|
r18109 | @annotatesubrepoerror | ||
|
r13323 | def get(self, state, overwrite=False): | ||
|
r12993 | source, revision, kind = state | ||
|
r14469 | if not revision: | ||
self.remove() | ||||
return | ||||
|
r12993 | self._fetch(source, revision) | ||
|
r12996 | # if the repo was set to be bare, unbare it | ||
|
r14440 | if self._gitisbare(): | ||
|
r12996 | self._gitcommand(['config', 'core.bare', 'false']) | ||
if self._gitstate() == revision: | ||||
self._gitcommand(['reset', '--hard', 'HEAD']) | ||||
return | ||||
elif self._gitstate() == revision: | ||||
|
r13324 | if overwrite: | ||
|
r13927 | # first reset the index to unmark new files for commit, because | ||
|
r13324 | # reset --hard will otherwise throw away files added for commit, | ||
# not just unmark them. | ||||
self._gitcommand(['reset', 'HEAD']) | ||||
self._gitcommand(['reset', '--hard', 'HEAD']) | ||||
|
r12995 | return | ||
|
r13178 | branch2rev, rev2branch = self._gitbranchmap() | ||
|
r13087 | |||
|
r13324 | def checkout(args): | ||
cmd = ['checkout'] | ||||
if overwrite: | ||||
# first reset the index to unmark new files for commit, because | ||||
# the -f option will otherwise throw away files added for | ||||
# commit, not just unmark them. | ||||
self._gitcommand(['reset', 'HEAD']) | ||||
cmd.append('-f') | ||||
self._gitcommand(cmd + args) | ||||
|
r24724 | _sanitize(self.ui, self.wvfs, '.git') | ||
|
r13324 | |||
|
r13087 | def rawcheckout(): | ||
|
r12995 | # no branch to checkout, check it out with no branch | ||
|
r33365 | self.ui.warn(_('checking out detached HEAD in ' | ||
'subrepository "%s"\n') % self._relpath) | ||||
|
r23572 | self.ui.warn(_('check out a git branch if you intend ' | ||
|
r12993 | 'to make changes\n')) | ||
|
r13324 | checkout(['-q', revision]) | ||
|
r13087 | |||
if revision not in rev2branch: | ||||
rawcheckout() | ||||
|
r12995 | return | ||
|
r13086 | branches = rev2branch[revision] | ||
|
r12995 | firstlocalbranch = None | ||
for b in branches: | ||||
|
r13150 | if b == 'refs/heads/master': | ||
|
r12995 | # master trumps all other branches | ||
|
r13324 | checkout(['refs/heads/master']) | ||
|
r12995 | return | ||
|
r13150 | if not firstlocalbranch and not b.startswith('refs/remotes/'): | ||
|
r12995 | firstlocalbranch = b | ||
if firstlocalbranch: | ||||
|
r13324 | checkout([firstlocalbranch]) | ||
|
r13087 | return | ||
|
r13178 | tracking = self._gittracking(branch2rev.keys()) | ||
|
r13087 | # choose a remote branch already tracked if possible | ||
remote = branches[0] | ||||
if remote not in tracking: | ||||
for b in branches: | ||||
if b in tracking: | ||||
remote = b | ||||
break | ||||
if remote not in tracking: | ||||
# create a new local tracking branch | ||||
|
r19012 | local = remote.split('/', 3)[3] | ||
|
r13324 | checkout(['-b', local, remote]) | ||
|
r13087 | elif self._gitisancestor(branch2rev[tracking[remote]], remote): | ||
# When updating to a tracked remote branch, | ||||
# if the local tracking branch is downstream of it, | ||||
# a normal `git pull` would have performed a "fast-forward merge" | ||||
# which is equivalent to updating the local branch to the remote. | ||||
# Since we are only looking at branching at update, we need to | ||||
# detect this situation and perform this action lazily. | ||||
|
r13152 | if tracking[remote] != self._gitcurrentbranch(): | ||
|
r13324 | checkout([tracking[remote]]) | ||
|
r13087 | self._gitcommand(['merge', '--ff', remote]) | ||
|
r24724 | _sanitize(self.ui, self.wvfs, '.git') | ||
|
r13087 | else: | ||
# a real merge would be required, just checkout the revision | ||||
rawcheckout() | ||||
|
r12993 | |||
|
r18109 | @annotatesubrepoerror | ||
|
r12992 | def commit(self, text, user, date): | ||
|
r13553 | if self._gitmissing(): | ||
|
r26587 | raise error.Abort(_("subrepo %s is missing") % self._relpath) | ||
|
r12992 | cmd = ['commit', '-a', '-m', text] | ||
|
r30635 | env = encoding.environ.copy() | ||
|
r12992 | if user: | ||
cmd += ['--author', user] | ||||
if date: | ||||
# git's date parser silently ignores when seconds < 1e9 | ||||
# convert to ISO8601 | ||||
|
r36625 | env['GIT_AUTHOR_DATE'] = dateutil.datestr(date, | ||
|
r13095 | '%Y-%m-%dT%H:%M:%S %1%2') | ||
self._gitcommand(cmd, env=env) | ||||
|
r12992 | # make sure commit works otherwise HEAD might not exist under certain | ||
# circumstances | ||||
return self._gitstate() | ||||
|
r18109 | @annotatesubrepoerror | ||
|
r12994 | def merge(self, state): | ||
source, revision, kind = state | ||||
self._fetch(source, revision) | ||||
|
r13085 | base = self._gitcommand(['merge-base', revision, self._state[1]]) | ||
|
r15531 | self._gitupdatestat() | ||
|
r13417 | out, code = self._gitdir(['diff-index', '--quiet', 'HEAD']) | ||
def mergefunc(): | ||||
if base == revision: | ||||
self.get(state) # fast forward merge | ||||
elif base != self._state[1]: | ||||
self._gitcommand(['merge', '--no-commit', revision]) | ||||
|
r24724 | _sanitize(self.ui, self.wvfs, '.git') | ||
|
r13417 | |||
if self.dirty(): | ||||
if self._gitstate() != revision: | ||||
dirty = self._gitstate() == self._state[1] or code != 0 | ||||
|
r23572 | if _updateprompt(self.ui, self, dirty, | ||
|
r13432 | self._state[1][:7], revision[:7]): | ||
|
r13417 | mergefunc() | ||
else: | ||||
mergefunc() | ||||
|
r12994 | |||
|
r18109 | @annotatesubrepoerror | ||
|
r15708 | def push(self, opts): | ||
force = opts.get('force') | ||||
|
r14469 | if not self._state[1]: | ||
return True | ||||
|
r13553 | if self._gitmissing(): | ||
|
r26587 | raise error.Abort(_("subrepo %s is missing") % self._relpath) | ||
|
r13029 | # if a branch in origin contains the revision, nothing to do | ||
|
r13178 | branch2rev, rev2branch = self._gitbranchmap() | ||
|
r13109 | if self._state[1] in rev2branch: | ||
for b in rev2branch[self._state[1]]: | ||||
|
r13150 | if b.startswith('refs/remotes/origin/'): | ||
|
r13109 | return True | ||
|
r13086 | for b, revision in branch2rev.iteritems(): | ||
|
r13150 | if b.startswith('refs/remotes/origin/'): | ||
|
r13086 | if self._gitisancestor(self._state[1], revision): | ||
return True | ||||
|
r13029 | # otherwise, try to push the currently checked out branch | ||
|
r12994 | cmd = ['push'] | ||
if force: | ||||
cmd.append('--force') | ||||
|
r13152 | |||
current = self._gitcurrentbranch() | ||||
|
r12995 | if current: | ||
|
r13029 | # determine if the current branch is even useful | ||
if not self._gitisancestor(self._state[1], current): | ||||
|
r23572 | self.ui.warn(_('unrelated git branch checked out ' | ||
|
r33365 | 'in subrepository "%s"\n') % self._relpath) | ||
|
r13029 | return False | ||
|
r33365 | self.ui.status(_('pushing branch %s of subrepository "%s"\n') % | ||
|
r23572 | (current.split('/', 2)[2], self._relpath)) | ||
|
r20970 | ret = self._gitdir(cmd + ['origin', current]) | ||
return ret[1] == 0 | ||||
|
r12995 | else: | ||
|
r33365 | self.ui.warn(_('no branch checked out in subrepository "%s"\n' | ||
|
r23572 | 'cannot push revision %s\n') % | ||
|
r13029 | (self._relpath, self._state[1])) | ||
|
r12995 | return False | ||
|
r12994 | |||
|
r18109 | @annotatesubrepoerror | ||
|
r41799 | def add(self, ui, match, prefix, uipathfn, explicitonly, **opts): | ||
|
r24174 | if self._gitmissing(): | ||
return [] | ||||
|
r24182 | |||
|
r40378 | s = self.status(None, unknown=True, clean=True) | ||
|
r24174 | |||
|
r24183 | tracked = set() | ||
# dirstates 'amn' warn, 'r' is added again | ||||
|
r40378 | for l in (s.modified, s.added, s.deleted, s.clean): | ||
|
r24183 | tracked.update(l) | ||
|
r24182 | # Unknown files not of interest will be rejected by the matcher | ||
|
r40378 | files = s.unknown | ||
|
r24182 | files.extend(match.files()) | ||
|
r24183 | rejected = [] | ||
|
r24182 | files = [f for f in sorted(set(files)) if match(f)] | ||
|
r24174 | for f in files: | ||
exact = match.exact(f) | ||||
command = ["add"] | ||||
if exact: | ||||
command.append("-f") #should be added, even if ignored | ||||
if ui.verbose or not exact: | ||||
|
r41799 | ui.status(_('adding %s\n') % uipathfn(f)) | ||
|
r24183 | |||
if f in tracked: # hg prints 'adding' even if already tracked | ||||
if exact: | ||||
rejected.append(f) | ||||
continue | ||||
|
r32147 | if not opts.get(r'dry_run'): | ||
|
r24174 | self._gitcommand(command + [f]) | ||
|
r24183 | |||
for f in rejected: | ||||
|
r41799 | ui.warn(_("%s already tracked!\n") % uipathfn(f)) | ||
|
r24183 | |||
return rejected | ||||
|
r24174 | |||
@annotatesubrepoerror | ||||
|
r12996 | def remove(self): | ||
|
r13553 | if self._gitmissing(): | ||
return | ||||
|
r12996 | if self.dirty(): | ||
|
r23572 | self.ui.warn(_('not removing repo %s because ' | ||
'it has changes.\n') % self._relpath) | ||||
|
r12996 | return | ||
# we can't fully delete the repository as it may contain | ||||
# local-only history | ||||
|
r23572 | self.ui.note(_('removing subrepo %s\n') % self._relpath) | ||
|
r12996 | self._gitcommand(['config', 'core.bare', 'true']) | ||
|
r24688 | for f, kind in self.wvfs.readdir(): | ||
|
r12996 | if f == '.git': | ||
continue | ||||
|
r24688 | if kind == stat.S_IFDIR: | ||
|
r24690 | self.wvfs.rmtree(f) | ||
|
r12996 | else: | ||
|
r24691 | self.wvfs.unlink(f) | ||
|
r12996 | |||
|
r31099 | def archive(self, archiver, prefix, match=None, decode=True): | ||
|
r18967 | total = 0 | ||
|
r13027 | source, revision = self._state | ||
|
r14469 | if not revision: | ||
|
r18967 | return total | ||
|
r13027 | self._fetch(source, revision) | ||
# Parse git's native archive command. | ||||
# This should be much faster than manually traversing the trees | ||||
# and objects with many subprocess calls. | ||||
tarstream = self._gitcommand(['archive', revision], stream=True) | ||||
|
r37596 | tar = tarfile.open(fileobj=tarstream, mode=r'r|') | ||
|
r13144 | relpath = subrelpath(self) | ||
|
r38398 | progress = self.ui.makeprogress(_('archiving (%s)') % relpath, | ||
unit=_('files')) | ||||
progress.update(0) | ||||
for info in tar: | ||||
|
r13180 | if info.isdir(): | ||
continue | ||||
|
r41624 | bname = pycompat.fsencode(info.name) | ||
if match and not match(bname): | ||||
|
r17108 | continue | ||
|
r13180 | if info.issym(): | ||
data = info.linkname | ||||
else: | ||||
data = tar.extractfile(info).read() | ||||
|
r41780 | archiver.addfile(prefix + bname, info.mode, info.issym(), data) | ||
|
r18967 | total += 1 | ||
|
r38398 | progress.increment() | ||
progress.complete() | ||||
|
r18967 | return total | ||
|
r13144 | |||
|
r13027 | |||
|
r18109 | @annotatesubrepoerror | ||
|
r32578 | def cat(self, match, fm, fntemplate, prefix, **opts): | ||
|
r23991 | rev = self._state[1] | ||
if match.anypats(): | ||||
return 1 #No support for include/exclude yet | ||||
if not match.files(): | ||||
return 1 | ||||
|
r32578 | # TODO: add support for non-plain formatter (see cmdutil.cat()) | ||
|
r23991 | for f in match.files(): | ||
output = self._gitcommand(["show", "%s:%s" % (rev, f)]) | ||||
|
r36223 | fp = cmdutil.makefileobj(self._ctx, fntemplate, | ||
|
r24675 | pathname=self.wvfs.reljoin(prefix, f)) | ||
|
r23991 | fp.write(output) | ||
fp.close() | ||||
return 0 | ||||
@annotatesubrepoerror | ||||
|
r13182 | def status(self, rev2, **opts): | ||
|
r14469 | rev1 = self._state[1] | ||
if self._gitmissing() or not rev1: | ||||
|
r13553 | # if the repo is missing, return no results | ||
|
r24210 | return scmutil.status([], [], [], [], [], [], []) | ||
|
r13182 | modified, added, removed = [], [], [] | ||
|
r15531 | self._gitupdatestat() | ||
|
r13182 | if rev2: | ||
|
r28618 | command = ['diff-tree', '--no-renames', '-r', rev1, rev2] | ||
|
r13182 | else: | ||
|
r28618 | command = ['diff-index', '--no-renames', rev1] | ||
|
r13182 | out = self._gitcommand(command) | ||
for line in out.split('\n'): | ||||
tab = line.find('\t') | ||||
if tab == -1: | ||||
continue | ||||
|
r38389 | status, f = line[tab - 1:tab], line[tab + 1:] | ||
|
r13182 | if status == 'M': | ||
modified.append(f) | ||||
elif status == 'A': | ||||
added.append(f) | ||||
elif status == 'D': | ||||
removed.append(f) | ||||
|
r22927 | deleted, unknown, ignored, clean = [], [], [], [] | ||
|
r23411 | |||
|
r24256 | command = ['status', '--porcelain', '-z'] | ||
|
r32157 | if opts.get(r'unknown'): | ||
|
r24256 | command += ['--untracked-files=all'] | ||
|
r32157 | if opts.get(r'ignored'): | ||
|
r24256 | command += ['--ignored'] | ||
out = self._gitcommand(command) | ||||
changedfiles = set() | ||||
changedfiles.update(modified) | ||||
changedfiles.update(added) | ||||
changedfiles.update(removed) | ||||
for line in out.split('\0'): | ||||
if not line: | ||||
continue | ||||
st = line[0:2] | ||||
#moves and copies show 2 files on one line | ||||
if line.find('\0') >= 0: | ||||
filename1, filename2 = line[3:].split('\0') | ||||
else: | ||||
filename1 = line[3:] | ||||
filename2 = None | ||||
changedfiles.add(filename1) | ||||
if filename2: | ||||
changedfiles.add(filename2) | ||||
if st == '??': | ||||
unknown.append(filename1) | ||||
elif st == '!!': | ||||
ignored.append(filename1) | ||||
|
r32157 | if opts.get(r'clean'): | ||
|
r24256 | out = self._gitcommand(['ls-files']) | ||
for f in out.split('\n'): | ||||
if not f in changedfiles: | ||||
clean.append(f) | ||||
|
r23411 | |||
|
r22914 | return scmutil.status(modified, added, removed, deleted, | ||
unknown, ignored, clean) | ||||
|
r13182 | |||
|
r23523 | @annotatesubrepoerror | ||
def diff(self, ui, diffopts, node2, match, prefix, **opts): | ||||
node1 = self._state[1] | ||||
|
r28618 | cmd = ['diff', '--no-renames'] | ||
|
r32157 | if opts[r'stat']: | ||
|
r23523 | cmd.append('--stat') | ||
else: | ||||
# for Git, this also implies '-p' | ||||
cmd.append('-U%d' % diffopts.context) | ||||
if diffopts.noprefix: | ||||
|
r41779 | cmd.extend(['--src-prefix=%s/' % prefix, | ||
'--dst-prefix=%s/' % prefix]) | ||||
|
r23523 | else: | ||
|
r41779 | cmd.extend(['--src-prefix=a/%s/' % prefix, | ||
'--dst-prefix=b/%s/' % prefix]) | ||||
|
r23523 | |||
if diffopts.ignorews: | ||||
cmd.append('--ignore-all-space') | ||||
if diffopts.ignorewsamount: | ||||
cmd.append('--ignore-space-change') | ||||
|
r41925 | if (self._gitversion(self._gitcommand(['--version'])) >= (1, 8, 4) | ||
and diffopts.ignoreblanklines): | ||||
|
r23523 | cmd.append('--ignore-blank-lines') | ||
cmd.append(node1) | ||||
if node2: | ||||
cmd.append(node2) | ||||
|
r23938 | output = "" | ||
|
r23523 | if match.always(): | ||
|
r23938 | output += self._gitcommand(cmd) + '\n' | ||
|
r24778 | else: | ||
st = self.status(node2)[:3] | ||||
files = [f for sublist in st for f in sublist] | ||||
for f in files: | ||||
if match(f): | ||||
output += self._gitcommand(cmd + ['--', f]) + '\n' | ||||
|
r23938 | |||
if output.strip(): | ||||
ui.write(output) | ||||
|
r23523 | |||
|
r23679 | @annotatesubrepoerror | ||
|
r23580 | def revert(self, substate, *pats, **opts): | ||
self.ui.status(_('reverting subrepo %s\n') % substate[0]) | ||||
|
r32157 | if not opts.get(r'no_backup'): | ||
|
r23678 | status = self.status(None) | ||
names = status.modified | ||||
for name in names: | ||||
|
r41752 | # backuppath() expects a path relative to the parent repo (the | ||
# repo that ui.origbackuppath is relative to) | ||||
parentname = os.path.join(self._path, name) | ||||
bakname = scmutil.backuppath(self.ui, self._subparent, | ||||
parentname) | ||||
|
r23678 | self.ui.note(_('saving current version of %s as %s\n') % | ||
|
r41733 | (name, os.path.relpath(bakname))) | ||
|
r41752 | util.rename(self.wvfs.join(name), bakname) | ||
|
r23550 | |||
|
r32157 | if not opts.get(r'dry_run'): | ||
|
r24134 | self.get(substate, overwrite=True) | ||
|
r23550 | return [] | ||
|
r21400 | def shortid(self, revid): | ||
return revid[:7] | ||||
|
r10177 | types = { | ||
'hg': hgsubrepo, | ||||
|
r10178 | 'svn': svnsubrepo, | ||
|
r12992 | 'git': gitsubrepo, | ||
|
r10177 | } | ||