subrepo.py
2130 lines
| 78.1 KiB
| text/x-python
|
PythonLexer
/ mercurial / subrepo.py
Matt Mackall
|
r8812 | # subrepo.py - sub-repository handling for Mercurial | ||
# | ||||
David Soria Parra
|
r10324 | # Copyright 2009-2010 Matt Mackall <mpm@selenic.com> | ||
Matt Mackall
|
r8812 | # | ||
# This software may be used and distributed according to the terms of the | ||||
Matt Mackall
|
r10263 | # GNU General Public License version 2 or any later version. | ||
Matt Mackall
|
r8812 | |||
Gregory Szorc
|
r25980 | from __future__ import absolute_import | ||
Matt Harbison
|
r23540 | import copy | ||
Gregory Szorc
|
r25980 | import errno | ||
Augie Fackler
|
r29341 | import hashlib | ||
Gregory Szorc
|
r25980 | import os | ||
import posixpath | ||||
import re | ||||
import stat | ||||
import subprocess | ||||
import sys | ||||
import tarfile | ||||
Augie Fackler
|
r19788 | import xml.dom.minidom | ||
Gregory Szorc
|
r25980 | |||
from .i18n import _ | ||||
from . import ( | ||||
cmdutil, | ||||
config, | ||||
Pulkit Goyal
|
r30635 | encoding, | ||
Gregory Szorc
|
r25980 | error, | ||
exchange, | ||||
Simon Farnsworth
|
r30060 | filemerge, | ||
Gregory Szorc
|
r25980 | match as matchmod, | ||
node, | ||||
pathutil, | ||||
phases, | ||||
Pulkit Goyal
|
r30615 | pycompat, | ||
Gregory Szorc
|
r25980 | scmutil, | ||
util, | ||||
Pierre-Yves David
|
r31236 | vfs as vfsmod, | ||
Gregory Szorc
|
r25980 | ) | ||
Abderrahim Kitouni
|
r9092 | hg = None | ||
Patrick Mezard
|
r14050 | propertycache = util.propertycache | ||
Matt Mackall
|
r8813 | |||
Augie Fackler
|
r10177 | nullstate = ('', '', 'empty') | ||
Matt Mackall
|
r8812 | |||
Angel Ezquerra
|
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 | ||||
Angel Ezquerra
|
r18936 | |||
def _getstorehashcachename(remotepath): | ||||
'''get a unique filename for the store hash cache of a remote repository''' | ||||
Pulkit Goyal
|
r35600 | return node.hex(hashlib.sha1(_expandedabspath(remotepath)).digest())[0:12] | ||
Angel Ezquerra
|
r18936 | |||
Angel Ezquerra
|
r18109 | class SubrepoAbort(error.Abort): | ||
"""Exception class used to avoid handling a subrepo error more than once""" | ||||
Angel Ezquerra
|
r18263 | def __init__(self, *args, **kw): | ||
Pulkit Goyal
|
r35372 | self.subrepo = kw.pop(r'subrepo', None) | ||
self.cause = kw.pop(r'cause', None) | ||||
Brendan Cully
|
r18296 | error.Abort.__init__(self, *args, **kw) | ||
Angel Ezquerra
|
r18109 | |||
def annotatesubrepoerror(func): | ||||
def decoratedmethod(self, *args, **kargs): | ||||
try: | ||||
res = func(self, *args, **kargs) | ||||
Gregory Szorc
|
r25660 | except SubrepoAbort as ex: | ||
Angel Ezquerra
|
r18109 | # This exception has already been handled | ||
raise ex | ||||
Gregory Szorc
|
r25660 | except error.Abort as ex: | ||
Angel Ezquerra
|
r18263 | subrepo = subrelpath(self) | ||
Matt Harbison
|
r33365 | errormsg = str(ex) + ' ' + _('(in subrepository "%s")') % subrepo | ||
Angel Ezquerra
|
r18109 | # avoid handling this exception by raising a SubrepoAbort exception | ||
Matt Harbison
|
r18964 | raise SubrepoAbort(errormsg, hint=ex.hint, subrepo=subrepo, | ||
cause=sys.exc_info()) | ||||
Angel Ezquerra
|
r18109 | return res | ||
return decoratedmethod | ||||
Martin Geisler
|
r11775 | def state(ctx, ui): | ||
Mads Kiilerich
|
r11571 | """return a state dict, mapping subrepo paths configured in .hgsub | ||
to tuple: (source from .hgsub, revision from .hgsubstate, kind | ||||
(key in types dict)) | ||||
""" | ||||
Matt Mackall
|
r8812 | p = config.config() | ||
FUJIWARA Katsunori
|
r25768 | repo = ctx.repo() | ||
Matt Mackall
|
r8812 | def read(f, sections=None, remap=None): | ||
if f in ctx: | ||||
Patrick Mezard
|
r13017 | try: | ||
data = ctx[f].data() | ||||
Gregory Szorc
|
r25660 | except IOError as err: | ||
Patrick Mezard
|
r13017 | if err.errno != errno.ENOENT: | ||
raise | ||||
# handle missing subrepo spec files as removed | ||||
Matt Harbison
|
r24645 | ui.warn(_("warning: subrepo spec file \'%s\' not found\n") % | ||
FUJIWARA Katsunori
|
r25769 | repo.pathto(f)) | ||
Patrick Mezard
|
r13017 | return | ||
p.parse(f, data, sections, remap, read) | ||||
Matt Mackall
|
r10174 | else: | ||
Pierre-Yves David
|
r26587 | raise error.Abort(_("subrepo spec file \'%s\' not found") % | ||
FUJIWARA Katsunori
|
r25769 | repo.pathto(f)) | ||
Matt Mackall
|
r10174 | if '.hgsub' in ctx: | ||
read('.hgsub') | ||||
Matt Mackall
|
r8812 | |||
Martin Geisler
|
r11775 | for path, src in ui.configitems('subpaths'): | ||
p.set('subpaths', path, src, ui.configsource('subpaths', path)) | ||||
Matt Mackall
|
r8812 | rev = {} | ||
if '.hgsubstate' in ctx: | ||||
try: | ||||
Patrick Mezard
|
r16596 | for i, l in enumerate(ctx['.hgsubstate'].data().splitlines()): | ||
Patrick Mezard
|
r16595 | l = l.lstrip() | ||
if not l: | ||||
continue | ||||
Patrick Mezard
|
r16596 | try: | ||
revision, path = l.split(" ", 1) | ||||
except ValueError: | ||||
Pierre-Yves David
|
r26587 | raise error.Abort(_("invalid subrepository revision " | ||
Matt Harbison
|
r24645 | "specifier in \'%s\' line %d") | ||
FUJIWARA Katsunori
|
r25769 | % (repo.pathto('.hgsubstate'), (i + 1))) | ||
Matt Mackall
|
r8812 | rev[path] = revision | ||
Gregory Szorc
|
r25660 | except IOError as err: | ||
Matt Mackall
|
r8812 | if err.errno != errno.ENOENT: | ||
raise | ||||
Martin Geisler
|
r15149 | def remap(src): | ||
Martin Geisler
|
r11775 | for pattern, repl in p.items('subpaths'): | ||
Martin Geisler
|
r11961 | # Turn r'C:\foo\bar' into r'C:\\foo\\bar' since re.sub | ||
# does a string decode. | ||||
Yuya Nishihara
|
r31451 | repl = util.escapestr(repl) | ||
Martin Geisler
|
r11961 | # However, we still want to allow back references to go | ||
# through unharmed, so we turn r'\\1' into r'\1'. Again, | ||||
# extra escapes are needed because re.sub string decodes. | ||||
Yuya Nishihara
|
r34070 | repl = re.sub(br'\\\\([0-9]+)', br'\\\1', repl) | ||
Martin Geisler
|
r11775 | try: | ||
src = re.sub(pattern, repl, src, 1) | ||||
Gregory Szorc
|
r25660 | except re.error as e: | ||
Pierre-Yves David
|
r26587 | raise error.Abort(_("bad subrepository pattern in %s: %s") | ||
Martin Geisler
|
r11775 | % (p.source('subpaths', pattern), e)) | ||
Martin Geisler
|
r15149 | return src | ||
Martin Geisler
|
r11775 | |||
Martin Geisler
|
r15149 | state = {} | ||
for path, src in p[''].items(): | ||||
kind = 'hg' | ||||
if src.startswith('['): | ||||
if ']' not in src: | ||||
Matt Harbison
|
r33365 | raise error.Abort(_('missing ] in subrepository source')) | ||
Martin Geisler
|
r15149 | kind, src = src.split(']', 1) | ||
kind = kind[1:] | ||||
Martin Geisler
|
r15150 | src = src.lstrip() # strip any extra whitespace after ']' | ||
if not util.url(src).isabs(): | ||||
FUJIWARA Katsunori
|
r25768 | parent = _abssource(repo, abort=False) | ||
Martin Geisler
|
r15150 | if parent: | ||
parent = util.url(parent) | ||||
parent.path = posixpath.join(parent.path or '', src) | ||||
parent.path = posixpath.normpath(parent.path) | ||||
joined = str(parent) | ||||
# Remap the full joined path and use it if it changes, | ||||
# else remap the original source. | ||||
remapped = remap(joined) | ||||
if remapped == joined: | ||||
src = remap(src) | ||||
else: | ||||
src = remapped | ||||
Martin Geisler
|
r15149 | src = remap(src) | ||
FUJIWARA Katsunori
|
r15723 | state[util.pconvert(path)] = (src.strip(), rev.get(path, ''), kind) | ||
Matt Mackall
|
r8812 | |||
return state | ||||
Matt Mackall
|
r8813 | |||
def writestate(repo, state): | ||||
Mads Kiilerich
|
r11571 | """rewrite .hgsubstate in (outer) repo with these subrepo states""" | ||
Matt Harbison
|
r24858 | lines = ['%s %s\n' % (state[s][1], s) for s in sorted(state) | ||
if state[s][1] != nullstate[1]] | ||||
Martin Geisler
|
r14443 | repo.wwrite('.hgsubstate', ''.join(lines), '') | ||
Matt Mackall
|
r8813 | |||
Simon Farnsworth
|
r30060 | def submerge(repo, wctx, mctx, actx, overwrite, labels=None): | ||
Mads Kiilerich
|
r11571 | """delegated from merge.applyupdates: merging of .hgsubstate file | ||
in working context, merging context and ancestor context""" | ||||
Matt Mackall
|
r8814 | if mctx == actx: # backwards? | ||
actx = wctx.p1() | ||||
s1 = wctx.substate | ||||
s2 = mctx.substate | ||||
sa = actx.substate | ||||
sm = {} | ||||
Matt Mackall
|
r9782 | repo.ui.debug("subrepo merge %s %s %s\n" % (wctx, mctx, actx)) | ||
Matt Mackall
|
r9779 | def debug(s, msg, r=""): | ||
if r: | ||||
Augie Fackler
|
r10177 | r = "%s:%s:%s" % r | ||
Matt Mackall
|
r9782 | repo.ui.debug(" subrepo %s: %s %s\n" % (s, msg, r)) | ||
Matt Mackall
|
r9779 | |||
Simon Farnsworth
|
r31516 | promptssrc = filemerge.partextras(labels) | ||
Mads Kiilerich
|
r18364 | for s, l in sorted(s1.iteritems()): | ||
Simon Farnsworth
|
r31516 | prompts = None | ||
Matt Mackall
|
r11470 | a = sa.get(s, nullstate) | ||
Matt Mackall
|
r11463 | ld = l # local state with possible dirty flag for compares | ||
Matt Mackall
|
r11470 | if wctx.sub(s).dirty(): | ||
Matt Mackall
|
r11463 | ld = (l[0], l[1] + "+") | ||
Matt Mackall
|
r11470 | if wctx == actx: # overwrite | ||
a = ld | ||||
Matt Mackall
|
r11463 | |||
Simon Farnsworth
|
r31516 | prompts = promptssrc.copy() | ||
prompts['s'] = s | ||||
Matt Mackall
|
r8814 | if s in s2: | ||
r = s2[s] | ||||
Matt Mackall
|
r11463 | if ld == r or r == a: # no change or local is newer | ||
Matt Mackall
|
r8814 | sm[s] = l | ||
continue | ||||
Matt Mackall
|
r11463 | elif ld == a: # other side changed | ||
Matt Mackall
|
r9782 | debug(s, "other changed, get", r) | ||
Erik Zielke
|
r13322 | wctx.sub(s).get(r, overwrite) | ||
Matt Mackall
|
r8814 | sm[s] = r | ||
Matt Mackall
|
r11463 | elif ld[0] != r[0]: # sources differ | ||
Simon Farnsworth
|
r30060 | prompts['lo'] = l[0] | ||
prompts['ro'] = r[0] | ||||
Simon Heimberg
|
r9048 | if repo.ui.promptchoice( | ||
Simon Farnsworth
|
r30060 | _(' subrepository sources for %(s)s differ\n' | ||
'use (l)ocal%(l)s source (%(lo)s)' | ||||
' or (r)emote%(o)s source (%(ro)s)?' | ||||
'$$ &Local $$ &Remote') % prompts, 0): | ||||
Matt Mackall
|
r9782 | debug(s, "prompt changed, get", r) | ||
Erik Zielke
|
r13322 | wctx.sub(s).get(r, overwrite) | ||
Matt Mackall
|
r8814 | sm[s] = r | ||
Matt Mackall
|
r11463 | elif ld[1] == a[1]: # local side is unchanged | ||
Matt Mackall
|
r9782 | debug(s, "other side changed, get", r) | ||
Erik Zielke
|
r13322 | wctx.sub(s).get(r, overwrite) | ||
Matt Mackall
|
r8814 | sm[s] = r | ||
else: | ||||
Angel Ezquerra
|
r19811 | debug(s, "both sides changed") | ||
Angel Ezquerra
|
r21401 | srepo = wctx.sub(s) | ||
Simon Farnsworth
|
r30060 | prompts['sl'] = srepo.shortid(l[1]) | ||
prompts['sr'] = srepo.shortid(r[1]) | ||||
Angel Ezquerra
|
r19811 | option = repo.ui.promptchoice( | ||
Simon Farnsworth
|
r30060 | _(' subrepository %(s)s diverged (local revision: %(sl)s, ' | ||
'remote revision: %(sr)s)\n' | ||||
'(M)erge, keep (l)ocal%(l)s or keep (r)emote%(o)s?' | ||||
Angel Ezquerra
|
r19811 | '$$ &Merge $$ &Local $$ &Remote') | ||
Simon Farnsworth
|
r30060 | % prompts, 0) | ||
Angel Ezquerra
|
r19811 | if option == 0: | ||
wctx.sub(s).merge(r) | ||||
sm[s] = l | ||||
debug(s, "merge with", r) | ||||
elif option == 1: | ||||
sm[s] = l | ||||
debug(s, "keep local subrepo revision", l) | ||||
else: | ||||
wctx.sub(s).get(r, overwrite) | ||||
sm[s] = r | ||||
debug(s, "get remote subrepo revision", r) | ||||
Matt Mackall
|
r11463 | elif ld == a: # remote removed, local unchanged | ||
Matt Mackall
|
r9782 | debug(s, "remote removed, remove") | ||
Matt Mackall
|
r8814 | wctx.sub(s).remove() | ||
Matt Mackall
|
r14417 | elif a == nullstate: # not present in remote or ancestor | ||
debug(s, "local added, keep") | ||||
sm[s] = l | ||||
continue | ||||
Matt Mackall
|
r8814 | else: | ||
Simon Heimberg
|
r9048 | if repo.ui.promptchoice( | ||
Simon Farnsworth
|
r30060 | _(' local%(l)s changed subrepository %(s)s' | ||
' which remote%(o)s removed\n' | ||||
Matt Mackall
|
r19226 | 'use (c)hanged version or (d)elete?' | ||
Simon Farnsworth
|
r30060 | '$$ &Changed $$ &Delete') % prompts, 0): | ||
Matt Mackall
|
r9782 | debug(s, "prompt remove") | ||
Matt Mackall
|
r8814 | wctx.sub(s).remove() | ||
Adrian Buehlmann
|
r13857 | for s, r in sorted(s2.items()): | ||
Simon Farnsworth
|
r31516 | prompts = None | ||
Matt Mackall
|
r8814 | if s in s1: | ||
continue | ||||
elif s not in sa: | ||||
Matt Mackall
|
r9782 | debug(s, "remote added, get", r) | ||
Augie Fackler
|
r10175 | mctx.sub(s).get(r) | ||
Matt Mackall
|
r8814 | sm[s] = r | ||
elif r != sa[s]: | ||||
Simon Farnsworth
|
r31516 | prompts = promptssrc.copy() | ||
prompts['s'] = s | ||||
Simon Heimberg
|
r9048 | if repo.ui.promptchoice( | ||
Simon Farnsworth
|
r30060 | _(' remote%(o)s changed subrepository %(s)s' | ||
' which local%(l)s removed\n' | ||||
Matt Mackall
|
r19226 | 'use (c)hanged version or (d)elete?' | ||
Simon Farnsworth
|
r30060 | '$$ &Changed $$ &Delete') % prompts, 0) == 0: | ||
Matt Mackall
|
r9782 | debug(s, "prompt recreate", r) | ||
Martin von Zweigbergk
|
r24110 | mctx.sub(s).get(r) | ||
Matt Mackall
|
r8814 | sm[s] = r | ||
# record merged .hgsubstate | ||||
writestate(repo, sm) | ||||
Angel Ezquerra
|
r19637 | return sm | ||
Matt Mackall
|
r8814 | |||
Yuya Nishihara
|
r35018 | def precommit(ui, wctx, status, match, force=False): | ||
"""Calculate .hgsubstate changes that should be applied before committing | ||||
Returns (subs, commitsubs, newstate) where | ||||
- subs: changed subrepos (including dirty ones) | ||||
- commitsubs: dirty subrepos which the caller needs to commit recursively | ||||
- newstate: new state dict which the caller must write to .hgsubstate | ||||
This also updates the given status argument. | ||||
""" | ||||
subs = [] | ||||
commitsubs = set() | ||||
newstate = wctx.substate.copy() | ||||
# only manage subrepos and .hgsubstate if .hgsub is present | ||||
if '.hgsub' in wctx: | ||||
# we'll decide whether to track this ourselves, thanks | ||||
for c in status.modified, status.added, status.removed: | ||||
if '.hgsubstate' in c: | ||||
c.remove('.hgsubstate') | ||||
# compare current state to last committed state | ||||
# build new substate based on last committed state | ||||
oldstate = wctx.p1().substate | ||||
for s in sorted(newstate.keys()): | ||||
if not match(s): | ||||
# ignore working copy, use old state if present | ||||
if s in oldstate: | ||||
newstate[s] = oldstate[s] | ||||
continue | ||||
if not force: | ||||
raise error.Abort( | ||||
_("commit with new subrepo %s excluded") % s) | ||||
dirtyreason = wctx.sub(s).dirtyreason(True) | ||||
if dirtyreason: | ||||
if not ui.configbool('ui', 'commitsubrepos'): | ||||
raise error.Abort(dirtyreason, | ||||
hint=_("use --subrepos for recursive commit")) | ||||
subs.append(s) | ||||
commitsubs.add(s) | ||||
else: | ||||
bs = wctx.sub(s).basestate() | ||||
newstate[s] = (newstate[s][0], bs, newstate[s][2]) | ||||
if oldstate.get(s, (None, None, None))[1] != bs: | ||||
subs.append(s) | ||||
# check for removed subrepos | ||||
for p in wctx.parents(): | ||||
r = [s for s in p.substate if s not in newstate] | ||||
subs += [s for s in r if match(s)] | ||||
if subs: | ||||
if (not match('.hgsub') and | ||||
'.hgsub' in (wctx.modified() + wctx.added())): | ||||
raise error.Abort(_("can't commit subrepos without .hgsub")) | ||||
status.modified.insert(0, '.hgsubstate') | ||||
elif '.hgsub' in status.removed: | ||||
# clean up .hgsubstate when .hgsub is removed | ||||
if ('.hgsubstate' in wctx and | ||||
'.hgsubstate' not in (status.modified + status.added + | ||||
status.removed)): | ||||
status.removed.insert(0, '.hgsubstate') | ||||
return subs, commitsubs, newstate | ||||
Erik Zielke
|
r13417 | def _updateprompt(ui, sub, dirty, local, remote): | ||
if dirty: | ||||
msg = (_(' subrepository sources for %s differ\n' | ||||
Mads Kiilerich
|
r22590 | 'use (l)ocal source (%s) or (r)emote source (%s)?' | ||
Matt Mackall
|
r19226 | '$$ &Local $$ &Remote') | ||
Erik Zielke
|
r13417 | % (subrelpath(sub), local, remote)) | ||
else: | ||||
Brodie Rao
|
r16683 | msg = (_(' subrepository sources for %s differ (in checked out ' | ||
'version)\n' | ||||
Mads Kiilerich
|
r22590 | 'use (l)ocal source (%s) or (r)emote source (%s)?' | ||
Matt Mackall
|
r19226 | '$$ &Local $$ &Remote') | ||
Erik Zielke
|
r13417 | % (subrelpath(sub), local, remote)) | ||
Matt Mackall
|
r19226 | return ui.promptchoice(msg, 0) | ||
Erik Zielke
|
r13417 | |||
Matt Harbison
|
r24785 | def reporelpath(repo): | ||
"""return path to this (sub)repo as seen from outermost repo""" | ||||
parent = repo | ||||
while util.safehasattr(parent, '_subparent'): | ||||
parent = parent._subparent | ||||
return repo.root[len(pathutil.normasprefix(parent.root)):] | ||||
Mads Kiilerich
|
r12752 | def subrelpath(sub): | ||
Mads Kiilerich
|
r11571 | """return path to this subrepo as seen from outermost repo""" | ||
FUJIWARA Katsunori
|
r24673 | return sub._relpath | ||
Edouard Gomez
|
r11112 | |||
Mads Kiilerich
|
r12753 | def _abssource(repo, push=False, abort=True): | ||
"""return pull/push path of repo - either based on parent repo .hgsub info | ||||
or on the top repo config. Abort or return None if no source found.""" | ||||
Augie Fackler
|
r14963 | if util.safehasattr(repo, '_subparent'): | ||
Brodie Rao
|
r14076 | source = util.url(repo._subsource) | ||
Matt Mackall
|
r14766 | if source.isabs(): | ||
Pulkit Goyal
|
r35631 | return bytes(source) | ||
Brodie Rao
|
r13771 | source.path = posixpath.normpath(source.path) | ||
Mads Kiilerich
|
r12753 | parent = _abssource(repo._subparent, push, abort=False) | ||
if parent: | ||||
Mads Kiilerich
|
r15498 | parent = util.url(util.pconvert(parent)) | ||
Mads Kiilerich
|
r15055 | parent.path = posixpath.join(parent.path or '', source.path) | ||
Brodie Rao
|
r13771 | parent.path = posixpath.normpath(parent.path) | ||
Pulkit Goyal
|
r35631 | return bytes(parent) | ||
Mads Kiilerich
|
r12753 | else: # recursion reached top repo | ||
Augie Fackler
|
r14963 | if util.safehasattr(repo, '_subtoppath'): | ||
Mads Kiilerich
|
r12852 | return repo._subtoppath | ||
Mads Kiilerich
|
r12753 | if push and repo.ui.config('paths', 'default-push'): | ||
return repo.ui.config('paths', 'default-push') | ||||
if repo.ui.config('paths', 'default'): | ||||
return repo.ui.config('paths', 'default') | ||||
Angel Ezquerra
|
r23666 | if repo.shared(): | ||
Matt Harbison
|
r18510 | # chop off the .hg component to get the default path form | ||
return os.path.dirname(repo.sharedpath) | ||||
Mads Kiilerich
|
r12753 | if abort: | ||
Pierre-Yves David
|
r26587 | raise error.Abort(_("default path for subrepository not found")) | ||
Matt Mackall
|
r8814 | |||
FUJIWARA Katsunori
|
r24724 | def _sanitize(ui, vfs, ignore): | ||
FUJIWARA Katsunori
|
r24726 | for dirname, dirs, names in vfs.walk(): | ||
FUJIWARA Katsunori
|
r21567 | for i, d in enumerate(dirs): | ||
if d.lower() == ignore: | ||||
del dirs[i] | ||||
break | ||||
FUJIWARA Katsunori
|
r25771 | if vfs.basename(dirname).lower() != '.hg': | ||
FUJIWARA Katsunori
|
r21564 | continue | ||
Matt Mackall
|
r20104 | for f in names: | ||
if f.lower() == 'hgrc': | ||||
FUJIWARA Katsunori
|
r21564 | ui.warn(_("warning: removing potentially hostile 'hgrc' " | ||
FUJIWARA Katsunori
|
r24726 | "in '%s'\n") % vfs.join(dirname)) | ||
vfs.unlink(vfs.reljoin(dirname, f)) | ||||
Matt Mackall
|
r20104 | |||
Yuya Nishihara
|
r34985 | def _auditsubrepopath(repo, path): | ||
# 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) | ||||
Gregory Szorc
|
r34990 | SUBREPO_ALLOWED_DEFAULTS = { | ||
'hg': True, | ||||
'git': False, | ||||
'svn': False, | ||||
} | ||||
Yuya Nishihara
|
r34986 | def _checktype(ui, kind): | ||
Gregory Szorc
|
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'), | ||||
Yuya Nishihara
|
r34986 | hint=_("see 'hg help config.subrepos' for details")) | ||
Gregory Szorc
|
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")) | ||||
Yuya Nishihara
|
r34986 | if kind not in types: | ||
raise error.Abort(_('unknown subrepo type %s') % kind) | ||||
Matt Harbison
|
r29021 | def subrepo(ctx, path, allowwdir=False, allowcreate=True): | ||
Mads Kiilerich
|
r11571 | """return instance of the right subrepo class for subrepo in path""" | ||
Matt Mackall
|
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 | ||||
Abderrahim Kitouni
|
r9092 | global hg | ||
Gregory Szorc
|
r25980 | from . import hg as h | ||
Matt Mackall
|
r8814 | hg = h | ||
Matt Mackall
|
r8813 | |||
Yuya Nishihara
|
r34986 | repo = ctx.repo() | ||
_auditsubrepopath(repo, path) | ||||
Dov Feldstern
|
r16756 | state = ctx.substate[path] | ||
Yuya Nishihara
|
r34986 | _checktype(repo.ui, state[2]) | ||
Matt Harbison
|
r25600 | if allowwdir: | ||
state = (state[0], ctx.subrev(path), state[2]) | ||||
Matt Harbison
|
r29021 | return types[state[2]](ctx, path, state[:2], allowcreate) | ||
Matt Mackall
|
r8813 | |||
Matt Harbison
|
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 | ||||
Gregory Szorc
|
r25980 | from . import hg as h | ||
Matt Harbison
|
r25416 | hg = h | ||
Yuya Nishihara
|
r34986 | repo = ctx.repo() | ||
_auditsubrepopath(repo, path) | ||||
Matt Harbison
|
r25416 | state = ctx.substate[path] | ||
Yuya Nishihara
|
r34986 | _checktype(repo.ui, state[2]) | ||
Matt Harbison
|
r25416 | subrev = '' | ||
if state[2] == 'hg': | ||||
subrev = "0" * 40 | ||||
Matt Harbison
|
r29021 | return types[state[2]](pctx, path, (state[0], subrev), True) | ||
Matt Harbison
|
r25416 | |||
FUJIWARA Katsunori
|
r20176 | def newcommitphase(ui, ctx): | ||
commitphase = phases.newcommitphase(ui) | ||||
substate = getattr(ctx, "substate", None) | ||||
if not substate: | ||||
return commitphase | ||||
Jun Wu
|
r33499 | check = ui.config('phases', 'checksubrepos') | ||
FUJIWARA Katsunori
|
r20176 | if check not in ('ignore', 'follow', 'abort'): | ||
Pierre-Yves David
|
r26587 | raise error.Abort(_('invalid phases.checksubrepos configuration: %s') | ||
FUJIWARA Katsunori
|
r20176 | % (check)) | ||
if check == 'ignore': | ||||
return commitphase | ||||
maxphase = phases.public | ||||
maxsub = None | ||||
for s in sorted(substate): | ||||
sub = ctx.sub(s) | ||||
subphase = sub.phase(substate[s][1]) | ||||
if maxphase < subphase: | ||||
maxphase = subphase | ||||
maxsub = s | ||||
if commitphase < maxphase: | ||||
if check == 'abort': | ||||
Pierre-Yves David
|
r26587 | raise error.Abort(_("can't commit in %s phase" | ||
FUJIWARA Katsunori
|
r20176 | " conflicting %s from subrepository %s") % | ||
(phases.phasenames[commitphase], | ||||
phases.phasenames[maxphase], maxsub)) | ||||
ui.warn(_("warning: changes are committed in" | ||||
" %s phase from subrepository %s\n") % | ||||
(phases.phasenames[maxphase], maxsub)) | ||||
return maxphase | ||||
return commitphase | ||||
Martin Geisler
|
r11559 | # subrepo classes need to implement the following abstract class: | ||
class abstractsubrepo(object): | ||||
FUJIWARA Katsunori
|
r24671 | def __init__(self, ctx, path): | ||
"""Initialize abstractsubrepo part | ||||
``ctx`` is the context referring this subrepository in the | ||||
parent repository. | ||||
Mads Kiilerich
|
r26781 | ``path`` is the path to this subrepository as seen from | ||
FUJIWARA Katsunori
|
r24671 | innermost repository. | ||
""" | ||||
self.ui = ctx.repo().ui | ||||
self._ctx = ctx | ||||
self._path = path | ||||
Matt Harbison
|
r23536 | |||
Matt Harbison
|
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 | ||||
Angel Ezquerra
|
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 | ||||
Matt Harbison
|
r33364 | def dirty(self, ignoreupdate=False, missing=False): | ||
Kevin Bullock
|
r13174 | """returns true if the dirstate of the subrepo is dirty or does not | ||
match current stored state. If ignoreupdate is true, only check | ||||
Matt Harbison
|
r33364 | whether the subrepo has uncommitted changes in its dirstate. If missing | ||
is true, check for deleted files. | ||||
Martin Geisler
|
r11559 | """ | ||
raise NotImplementedError | ||||
Matt Harbison
|
r33364 | def dirtyreason(self, ignoreupdate=False, missing=False): | ||
FUJIWARA Katsunori
|
r24470 | """return reason string if it is ``dirty()`` | ||
Returned string should have enough information for the message | ||||
of exception. | ||||
This returns None, otherwise. | ||||
""" | ||||
Matt Harbison
|
r33364 | if self.dirty(ignoreupdate=ignoreupdate, missing=missing): | ||
Matt Harbison
|
r33365 | return _('uncommitted changes in subrepository "%s"' | ||
FUJIWARA Katsunori
|
r24470 | ) % subrelpath(self) | ||
Valters Vingolds
|
r30755 | def bailifchanged(self, ignoreupdate=False, hint=None): | ||
FUJIWARA Katsunori
|
r24471 | """raise Abort if subrepository is ``dirty()`` | ||
""" | ||||
Matt Harbison
|
r33364 | dirtyreason = self.dirtyreason(ignoreupdate=ignoreupdate, | ||
missing=True) | ||||
FUJIWARA Katsunori
|
r24471 | if dirtyreason: | ||
Valters Vingolds
|
r30755 | raise error.Abort(dirtyreason, hint=hint) | ||
FUJIWARA Katsunori
|
r24471 | |||
Matt Mackall
|
r16072 | def basestate(self): | ||
"""current working directory base state, disregarding .hgsubstate | ||||
state and working directory modifications""" | ||||
raise NotImplementedError | ||||
Brodie Rao
|
r12506 | def checknested(self, path): | ||
Martin Geisler
|
r12162 | """check if path is a subrepository within this repository""" | ||
return False | ||||
Martin Geisler
|
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 | ||||
FUJIWARA Katsunori
|
r20176 | def phase(self, state): | ||
"""returns phase of specified state in the subrepository. | ||||
""" | ||||
return phases.public | ||||
Martin Geisler
|
r11559 | def remove(self): | ||
"""remove the subrepo | ||||
Matt Mackall
|
r8813 | |||
Martin Geisler
|
r11559 | (should verify the dirstate is not dirty first) | ||
""" | ||||
raise NotImplementedError | ||||
Erik Zielke
|
r13322 | def get(self, state, overwrite=False): | ||
Martin Geisler
|
r11559 | """run whatever commands are needed to put the subrepo into | ||
this state | ||||
""" | ||||
raise NotImplementedError | ||||
Erik Zielke
|
r13413 | def merge(self, state): | ||
Martin Geisler
|
r11559 | """merge currently-saved state with the new state.""" | ||
raise NotImplementedError | ||||
Angel Ezquerra
|
r15708 | def push(self, opts): | ||
Martin Geisler
|
r11572 | """perform whatever action is analogous to 'hg push' | ||
Martin Geisler
|
r11559 | |||
This may be a no-op on some systems. | ||||
""" | ||||
raise NotImplementedError | ||||
Matt Harbison
|
r23963 | def add(self, ui, match, prefix, explicitonly, **opts): | ||
Martin Geisler
|
r12270 | return [] | ||
Martin Geisler
|
r11559 | |||
Matt Harbison
|
r23537 | def addremove(self, matcher, prefix, opts, dry_run, similarity): | ||
Matt Harbison
|
r23572 | self.ui.warn("%s: %s" % (prefix, _("addremove is not supported"))) | ||
Matt Harbison
|
r23537 | return 1 | ||
Yuya Nishihara
|
r32578 | def cat(self, match, fm, fntemplate, prefix, **opts): | ||
Matt Harbison
|
r21041 | return 1 | ||
Martin Geisler
|
r12166 | def status(self, rev2, **opts): | ||
Martin von Zweigbergk
|
r22914 | return scmutil.status([], [], [], [], [], [], []) | ||
Martin Geisler
|
r12166 | |||
FUJIWARA Katsunori
|
r18006 | def diff(self, ui, diffopts, node2, match, prefix, **opts): | ||
Martin Geisler
|
r12167 | pass | ||
Martin Geisler
|
r12272 | def outgoing(self, ui, dest, opts): | ||
return 1 | ||||
Martin Geisler
|
r12274 | def incoming(self, ui, source, opts): | ||
return 1 | ||||
Martin Geisler
|
r12322 | def files(self): | ||
"""return filename iterator""" | ||||
raise NotImplementedError | ||||
Matt Harbison
|
r31099 | def filedata(self, name, decode): | ||
"""return file data, optionally passed through repo decoders""" | ||||
Martin Geisler
|
r12322 | raise NotImplementedError | ||
def fileflags(self, name): | ||||
"""return file flags""" | ||||
return '' | ||||
Matt Harbison
|
r25121 | def getfileset(self, expr): | ||
"""Resolve the fileset expression for this repo""" | ||||
return set() | ||||
Matt Harbison
|
r25228 | def printfiles(self, ui, m, fm, fmt, subrepos): | ||
Matt Harbison
|
r24413 | """handle the files command for this subrepo""" | ||
return 1 | ||||
Matt Harbison
|
r31099 | def archive(self, archiver, prefix, match=None, decode=True): | ||
Matt Harbison
|
r17108 | if match is not None: | ||
files = [f for f in self.files() if match(f)] | ||||
else: | ||||
files = self.files() | ||||
Martin Geisler
|
r13144 | total = len(files) | ||
relpath = subrelpath(self) | ||||
Matt Harbison
|
r23575 | self.ui.progress(_('archiving (%s)') % relpath, 0, | ||
unit=_('files'), total=total) | ||||
Martin Geisler
|
r13144 | for i, name in enumerate(files): | ||
Martin Geisler
|
r12323 | flags = self.fileflags(name) | ||
Gregory Szorc
|
r25658 | mode = 'x' in flags and 0o755 or 0o644 | ||
Martin Geisler
|
r12323 | symlink = 'l' in flags | ||
Matt Harbison
|
r24924 | archiver.addfile(prefix + self._path + '/' + name, | ||
Matt Harbison
|
r31099 | mode, symlink, self.filedata(name, decode)) | ||
Matt Harbison
|
r23575 | self.ui.progress(_('archiving (%s)') % relpath, i + 1, | ||
unit=_('files'), total=total) | ||||
self.ui.progress(_('archiving (%s)') % relpath, None) | ||||
Angel Ezquerra
|
r18967 | return total | ||
Martin Geisler
|
r12323 | |||
David M. Carr
|
r15410 | def walk(self, match): | ||
''' | ||||
walk recursively through the directory tree, finding all files | ||||
matched by the match function | ||||
''' | ||||
Martin Geisler
|
r12323 | |||
Matt Harbison
|
r23577 | def forget(self, match, prefix): | ||
Patrick Mezard
|
r16527 | return ([], []) | ||
Martin Geisler
|
r12323 | |||
timeless
|
r28607 | def removefiles(self, matcher, prefix, after, force, subrepos, warnings): | ||
Matt Harbison
|
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. | ||||
""" | ||||
timeless
|
r28607 | warnings.append(_("warning: removefiles not implemented (%s)") | ||
% self._path) | ||||
Matt Harbison
|
r23325 | return 1 | ||
Matt Harbison
|
r23579 | def revert(self, substate, *pats, **opts): | ||
FUJIWARA Katsunori
|
r29243 | self.ui.warn(_('%s: reverting %s subrepos is unsupported\n') \ | ||
Angel Ezquerra
|
r16468 | % (substate[0], substate[2])) | ||
Angel Ezquerra
|
r16429 | return [] | ||
Angel Ezquerra
|
r21400 | def shortid(self, revid): | ||
return revid | ||||
Matt Harbison
|
r34880 | def unshare(self): | ||
''' | ||||
convert this repository from shared to normal storage. | ||||
''' | ||||
Matt Harbison
|
r25591 | def verify(self): | ||
'''verify the integrity of the repository. Return 0 on success or | ||||
warning, 1 on any error. | ||||
''' | ||||
return 0 | ||||
FUJIWARA Katsunori
|
r24672 | @propertycache | ||
def wvfs(self): | ||||
"""return vfs to access the working directory of this subrepository | ||||
""" | ||||
Pierre-Yves David
|
r31236 | return vfsmod.vfs(self._ctx.repo().wvfs.join(self._path)) | ||
FUJIWARA Katsunori
|
r24672 | |||
FUJIWARA Katsunori
|
r24673 | @propertycache | ||
def _relpath(self): | ||||
"""return path to this subrepository as seen from outermost repository | ||||
""" | ||||
Matt Harbison
|
r24785 | return self.wvfs.reljoin(reporelpath(self._ctx.repo()), self._path) | ||
FUJIWARA Katsunori
|
r24673 | |||
Martin Geisler
|
r11559 | class hgsubrepo(abstractsubrepo): | ||
Matt Harbison
|
r29021 | def __init__(self, ctx, path, state, allowcreate): | ||
FUJIWARA Katsunori
|
r24671 | super(hgsubrepo, self).__init__(ctx, path) | ||
Matt Mackall
|
r8813 | self._state = state | ||
Matt Harbison
|
r24302 | r = ctx.repo() | ||
Matt Mackall
|
r8813 | root = r.wjoin(path) | ||
Matt Harbison
|
r29021 | create = allowcreate and not r.wvfs.exists('%s/.hg' % path) | ||
FUJIWARA Katsunori
|
r17873 | self._repo = hg.repository(r.baseui, root, create=create) | ||
Matt Harbison
|
r24877 | |||
# Propagate the parent's --hidden option | ||||
if r is r.unfiltered(): | ||||
self._repo = self._repo.unfiltered() | ||||
Matt Harbison
|
r23573 | self.ui = self._repo.ui | ||
FUJIWARA Katsunori
|
r17873 | for s, k in [('ui', 'commitsubrepos')]: | ||
v = r.ui.config(s, k) | ||||
if v: | ||||
Matt Harbison
|
r23574 | self.ui.setconfig(s, k, v, 'subrepo') | ||
Matt Mackall
|
r25848 | # internal config: ui._usedassubrepo | ||
Matt Harbison
|
r23574 | self.ui.setconfig('ui', '_usedassubrepo', 'True', 'subrepo') | ||
Martin Geisler
|
r14281 | self._initrepo(r, state[0], create) | ||
Matt Harbison
|
r32005 | @annotatesubrepoerror | ||
def addwebdirpath(self, serverpath, webconf): | ||||
cmdutil.addwebdirpath(self._repo, subrelpath(self), webconf) | ||||
Angel Ezquerra
|
r18939 | def storeclean(self, path): | ||
Bryan O'Sullivan
|
r27844 | with self._repo.lock(): | ||
FUJIWARA Katsunori
|
r21885 | return self._storeclean(path) | ||
def _storeclean(self, path): | ||||
Angel Ezquerra
|
r18939 | clean = True | ||
itercache = self._calcstorehash(path) | ||||
Pierre-Yves David
|
r25172 | for filehash in self._readstorehashcache(path): | ||
if filehash != next(itercache, None): | ||||
clean = False | ||||
break | ||||
if clean: | ||||
# if not empty: | ||||
Angel Ezquerra
|
r18939 | # the cached and current pull states have a different size | ||
Pierre-Yves David
|
r25172 | clean = next(itercache, None) is None | ||
Angel Ezquerra
|
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') | ||||
Angel Ezquerra
|
r18940 | yield '# %s\n' % _expandedabspath(remotepath) | ||
FUJIWARA Katsunori
|
r23365 | vfs = self._repo.vfs | ||
Angel Ezquerra
|
r18939 | for relname in filelist: | ||
Pulkit Goyal
|
r35600 | filehash = node.hex(hashlib.sha1(vfs.tryread(relname)).digest()) | ||
FUJIWARA Katsunori
|
r23365 | yield '%s = %s\n' % (relname, filehash) | ||
Angel Ezquerra
|
r18939 | |||
FUJIWARA Katsunori
|
r23367 | @propertycache | ||
def _cachestorehashvfs(self): | ||||
Pierre-Yves David
|
r31325 | return vfsmod.vfs(self._repo.vfs.join('cache/storehash')) | ||
FUJIWARA Katsunori
|
r23367 | |||
Angel Ezquerra
|
r18939 | def _readstorehashcache(self, remotepath): | ||
'''read the store hash cache for a given remote repository''' | ||||
FUJIWARA Katsunori
|
r23369 | cachefile = _getstorehashcachename(remotepath) | ||
return self._cachestorehashvfs.tryreadlines(cachefile, 'r') | ||||
Angel Ezquerra
|
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 | ||||
''' | ||||
FUJIWARA Katsunori
|
r23372 | cachefile = _getstorehashcachename(remotepath) | ||
Bryan O'Sullivan
|
r27843 | with self._repo.lock(): | ||
FUJIWARA Katsunori
|
r21886 | storehash = list(self._calcstorehash(remotepath)) | ||
FUJIWARA Katsunori
|
r23372 | vfs = self._cachestorehashvfs | ||
Pulkit Goyal
|
r35605 | vfs.writelines(cachefile, storehash, mode='wb', notindexed=True) | ||
Angel Ezquerra
|
r18939 | |||
Matt Harbison
|
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] | ||||
Angel Ezquerra
|
r18109 | @annotatesubrepoerror | ||
Martin Geisler
|
r14281 | def _initrepo(self, parentrepo, source, create): | ||
self._repo._subparent = parentrepo | ||||
self._repo._subsource = source | ||||
Matt Mackall
|
r8813 | |||
Benoit Boissinot
|
r10666 | if create: | ||
FUJIWARA Katsunori
|
r21891 | lines = ['[paths]\n'] | ||
Benoit Boissinot
|
r10666 | |||
def addpathconfig(key, value): | ||||
Mads Kiilerich
|
r12753 | if value: | ||
FUJIWARA Katsunori
|
r21891 | lines.append('%s = %s\n' % (key, value)) | ||
Matt Harbison
|
r23574 | self.ui.setconfig('paths', key, value, 'subrepo') | ||
Benoit Boissinot
|
r10666 | |||
Mads Kiilerich
|
r12753 | defpath = _abssource(self._repo, abort=False) | ||
defpushpath = _abssource(self._repo, True, abort=False) | ||||
Benoit Boissinot
|
r10666 | addpathconfig('default', defpath) | ||
Edouard Gomez
|
r10697 | if defpath != defpushpath: | ||
addpathconfig('default-push', defpushpath) | ||||
FUJIWARA Katsunori
|
r21891 | |||
Yuya Nishihara
|
r35639 | self._repo.vfs.write('hgrc', util.tonativeeol(''.join(lines))) | ||
Benoit Boissinot
|
r10666 | |||
Angel Ezquerra
|
r18109 | @annotatesubrepoerror | ||
Matt Harbison
|
r23885 | def add(self, ui, match, prefix, explicitonly, **opts): | ||
return cmdutil.add(ui, self._repo, match, | ||||
FUJIWARA Katsunori
|
r24675 | self.wvfs.reljoin(prefix, self._path), | ||
explicitonly, **opts) | ||||
Martin Geisler
|
r12270 | |||
Matt Harbison
|
r24132 | @annotatesubrepoerror | ||
Matt Harbison
|
r23537 | def addremove(self, m, prefix, opts, dry_run, similarity): | ||
Matt Harbison
|
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 | ||||
Matt Harbison
|
r23537 | return scmutil.addremove(self._repo, m, | ||
FUJIWARA Katsunori
|
r24675 | self.wvfs.reljoin(prefix, self._path), opts, | ||
Matt Harbison
|
r23537 | dry_run, similarity) | ||
Angel Ezquerra
|
r18109 | @annotatesubrepoerror | ||
Yuya Nishihara
|
r32578 | def cat(self, match, fm, fntemplate, prefix, **opts): | ||
Matt Harbison
|
r21041 | rev = self._state[1] | ||
ctx = self._repo[rev] | ||||
Yuya Nishihara
|
r32578 | return cmdutil.cat(self.ui, self._repo, ctx, match, fm, fntemplate, | ||
prefix, **opts) | ||||
Matt Harbison
|
r21041 | |||
@annotatesubrepoerror | ||||
Martin Geisler
|
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) | ||||
Gregory Szorc
|
r25660 | except error.RepoLookupError as inst: | ||
Matt Harbison
|
r23574 | self.ui.warn(_('warning: error "%s" in subrepository "%s"\n') | ||
% (inst, subrelpath(self))) | ||||
Martin von Zweigbergk
|
r22914 | return scmutil.status([], [], [], [], [], [], []) | ||
Martin Geisler
|
r12166 | |||
Angel Ezquerra
|
r18109 | @annotatesubrepoerror | ||
FUJIWARA Katsunori
|
r18006 | def diff(self, ui, diffopts, node2, match, prefix, **opts): | ||
Martin Geisler
|
r12167 | try: | ||
node1 = node.bin(self._state[1]) | ||||
Patrick Mezard
|
r12209 | # We currently expect node2 to come from substate and be | ||
# in hex format | ||||
Martin Geisler
|
r12210 | if node2 is not None: | ||
node2 = node.bin(node2) | ||||
FUJIWARA Katsunori
|
r18006 | cmdutil.diffordiffstat(ui, self._repo, diffopts, | ||
Martin Geisler
|
r12167 | node1, node2, match, | ||
Bryan O'Sullivan
|
r17968 | prefix=posixpath.join(prefix, self._path), | ||
Martin Geisler
|
r12167 | listsubrepos=True, **opts) | ||
Gregory Szorc
|
r25660 | except error.RepoLookupError as inst: | ||
Matt Harbison
|
r23574 | self.ui.warn(_('warning: error "%s" in subrepository "%s"\n') | ||
% (inst, subrelpath(self))) | ||||
Martin Geisler
|
r12167 | |||
Angel Ezquerra
|
r18109 | @annotatesubrepoerror | ||
Matt Harbison
|
r31099 | def archive(self, archiver, prefix, match=None, decode=True): | ||
Martin Geisler
|
r15286 | self._get(self._state + ('hg',)) | ||
Matt Harbison
|
r23575 | total = abstractsubrepo.archive(self, archiver, prefix, match) | ||
Martin Geisler
|
r12323 | rev = self._state[1] | ||
ctx = self._repo[rev] | ||||
for subpath in ctx.substate: | ||||
Matt Harbison
|
r25601 | s = subrepo(ctx, subpath, True) | ||
Martin von Zweigbergk
|
r28017 | submatch = matchmod.subdirmatcher(subpath, match) | ||
Matt Harbison
|
r31099 | total += s.archive(archiver, prefix + self._path + '/', submatch, | ||
decode) | ||||
Angel Ezquerra
|
r18967 | return total | ||
Martin Geisler
|
r12323 | |||
Angel Ezquerra
|
r18109 | @annotatesubrepoerror | ||
Matt Harbison
|
r33364 | def dirty(self, ignoreupdate=False, missing=False): | ||
Matt Mackall
|
r8813 | r = self._state[1] | ||
Kevin Bullock
|
r13174 | if r == '' and not ignoreupdate: # no state recorded | ||
Matt Mackall
|
r8813 | return True | ||
w = self._repo[None] | ||||
Matt Mackall
|
r14316 | if r != w.p1().hex() and not ignoreupdate: | ||
Kevin Bullock
|
r13325 | # different version checked out | ||
Matt Mackall
|
r8813 | return True | ||
Matt Harbison
|
r33364 | return w.dirty(missing=missing) # working directory changed | ||
Matt Mackall
|
r8813 | |||
Matt Mackall
|
r16072 | def basestate(self): | ||
return self._repo['.'].hex() | ||||
Martin Geisler
|
r12162 | def checknested(self, path): | ||
return self._repo._checknested(self._repo.wjoin(path)) | ||||
Angel Ezquerra
|
r18109 | @annotatesubrepoerror | ||
Matt Mackall
|
r8813 | def commit(self, text, user, date): | ||
Kevin Bullock
|
r14898 | # don't bother committing in the subrepo if it's only been | ||
# updated | ||||
if not self.dirty(True): | ||||
return self._repo['.'].hex() | ||||
Matt Harbison
|
r23574 | self.ui.debug("committing subrepo %s\n" % subrelpath(self)) | ||
Matt Mackall
|
r8813 | n = self._repo.commit(text, user, date) | ||
if not n: | ||||
return self._repo['.'].hex() # different version checked out | ||||
return node.hex(n) | ||||
Matt Mackall
|
r8814 | |||
Angel Ezquerra
|
r18109 | @annotatesubrepoerror | ||
FUJIWARA Katsunori
|
r20176 | def phase(self, state): | ||
return self._repo[state].phase() | ||||
@annotatesubrepoerror | ||||
Matt Mackall
|
r8814 | def remove(self): | ||
# we can't fully delete the repository as it may contain | ||||
# local-only history | ||||
Matt Harbison
|
r23574 | self.ui.note(_('removing subrepo %s\n') % subrelpath(self)) | ||
Matt Mackall
|
r8814 | hg.clean(self._repo, node.nullid, False) | ||
Matt Mackall
|
r9507 | def _get(self, state): | ||
Augie Fackler
|
r10177 | source, revision, kind = state | ||
Matt Harbison
|
r34816 | parentrepo = self._repo._subparent | ||
Angel Ezquerra
|
r20317 | if revision in self._repo.unfiltered(): | ||
Matt Harbison
|
r34816 | # Allow shared subrepos tracked at null to setup the sharedpath | ||
if len(self._repo) != 0 or not parentrepo.shared(): | ||||
return True | ||||
Angel Ezquerra
|
r20318 | self._repo._subsource = source | ||
srcurl = _abssource(self._repo) | ||||
other = hg.peer(self._repo, {}, srcurl) | ||||
if len(self._repo) == 0: | ||||
FUJIWARA Katsunori
|
r24690 | # use self._repo.vfs instead of self.wvfs to remove .hg only | ||
self._repo.vfs.rmtree() | ||||
Matt Harbison
|
r34816 | if parentrepo.shared(): | ||
self.ui.status(_('sharing subrepo %s from %s\n') | ||||
% (subrelpath(self), srcurl)) | ||||
shared = hg.share(self._repo._subparent.baseui, | ||||
other, self._repo.root, | ||||
update=False, bookmarks=False) | ||||
self._repo = shared.local() | ||||
else: | ||||
self.ui.status(_('cloning subrepo %s from %s\n') | ||||
% (subrelpath(self), srcurl)) | ||||
other, cloned = hg.clone(self._repo._subparent.baseui, {}, | ||||
other, self._repo.root, | ||||
update=False) | ||||
self._repo = cloned.local() | ||||
Angel Ezquerra
|
r20318 | self._initrepo(parentrepo, source, create=True) | ||
self._cachestorehash(srcurl) | ||||
Angel Ezquerra
|
r20317 | else: | ||
Matt Harbison
|
r23574 | self.ui.status(_('pulling subrepo %s from %s\n') | ||
% (subrelpath(self), srcurl)) | ||||
Angel Ezquerra
|
r20318 | cleansub = self.storeclean(srcurl) | ||
Pierre-Yves David
|
r22695 | exchange.pull(self._repo, other) | ||
Angel Ezquerra
|
r20318 | if cleansub: | ||
# keep the repo clean after pull | ||||
Angel Ezquerra
|
r18939 | self._cachestorehash(srcurl) | ||
Angel Ezquerra
|
r20319 | return False | ||
Matt Mackall
|
r8814 | |||
Angel Ezquerra
|
r18109 | @annotatesubrepoerror | ||
Erik Zielke
|
r13322 | def get(self, state, overwrite=False): | ||
Angel Ezquerra
|
r20319 | inrepo = self._get(state) | ||
Augie Fackler
|
r10177 | source, revision, kind = state | ||
Angel Ezquerra
|
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( | ||||
Matt Harbison
|
r33365 | _('revision %s in subrepository "%s" is hidden\n') \ | ||
Angel Ezquerra
|
r20319 | % (revision[0:12], self._path)) | ||
repo = urepo | ||||
hg.updaterepo(repo, revision, overwrite) | ||||
Matt Mackall
|
r8814 | |||
Angel Ezquerra
|
r18109 | @annotatesubrepoerror | ||
Matt Mackall
|
r8814 | def merge(self, state): | ||
Matt Mackall
|
r9507 | self._get(state) | ||
Matt Mackall
|
r9781 | cur = self._repo['.'] | ||
dst = self._repo[state[1]] | ||||
Benoit Boissinot
|
r10251 | anc = dst.ancestor(cur) | ||
Erik Zielke
|
r13417 | |||
def mergefunc(): | ||||
Friedrich Kastner-Masilko
|
r16196 | if anc == cur and dst.branch() == cur.branch(): | ||
Matt Harbison
|
r33365 | self.ui.debug('updating subrepository "%s"\n' | ||
% subrelpath(self)) | ||||
Erik Zielke
|
r13417 | hg.update(self._repo, state[1]) | ||
elif anc == dst: | ||||
Matt Harbison
|
r33365 | self.ui.debug('skipping subrepository "%s"\n' | ||
% subrelpath(self)) | ||||
Erik Zielke
|
r13417 | else: | ||
Matt Harbison
|
r33365 | self.ui.debug('merging subrepository "%s"\n' % subrelpath(self)) | ||
Erik Zielke
|
r13417 | hg.merge(self._repo, state[1], remind=False) | ||
wctx = self._repo[None] | ||||
if self.dirty(): | ||||
if anc != dst: | ||||
Matt Harbison
|
r23574 | if _updateprompt(self.ui, self, wctx.dirty(), cur, dst): | ||
Erik Zielke
|
r13417 | mergefunc() | ||
else: | ||||
mergefunc() | ||||
Matt Mackall
|
r9781 | else: | ||
Erik Zielke
|
r13417 | mergefunc() | ||
Matt Mackall
|
r8815 | |||
Angel Ezquerra
|
r18109 | @annotatesubrepoerror | ||
Angel Ezquerra
|
r15708 | def push(self, opts): | ||
force = opts.get('force') | ||||
newbranch = opts.get('new_branch') | ||||
ssh = opts.get('ssh') | ||||
Matt Mackall
|
r8815 | # push subrepos depth-first for coherent ordering | ||
c = self._repo[''] | ||||
subs = c.substate # only repos that are committed | ||||
for s in sorted(subs): | ||||
Matt Mackall
|
r16022 | if c.sub(s).push(opts) == 0: | ||
Matt Mackall
|
r11067 | return False | ||
Matt Mackall
|
r8815 | |||
dsturl = _abssource(self._repo, True) | ||||
Angel Ezquerra
|
r18940 | if not force: | ||
if self.storeclean(dsturl): | ||||
Matt Harbison
|
r23574 | self.ui.status( | ||
Angel Ezquerra
|
r18940 | _('no changes made to subrepo %s since last push to %s\n') | ||
% (subrelpath(self), dsturl)) | ||||
return None | ||||
Matt Harbison
|
r23574 | self.ui.status(_('pushing subrepo %s to %s\n') % | ||
Mads Kiilerich
|
r12752 | (subrelpath(self), dsturl)) | ||
Simon Heimberg
|
r17874 | other = hg.peer(self._repo, {'ssh': ssh}, dsturl) | ||
Pierre-Yves David
|
r22619 | res = exchange.push(self._repo, other, force, newbranch=newbranch) | ||
Angel Ezquerra
|
r18939 | |||
# the repo is now clean | ||||
self._cachestorehash(dsturl) | ||||
Pierre-Yves David
|
r22619 | return res.cgresult | ||
Augie Fackler
|
r10177 | |||
Angel Ezquerra
|
r18109 | @annotatesubrepoerror | ||
Martin Geisler
|
r12272 | def outgoing(self, ui, dest, opts): | ||
Matt Harbison
|
r24875 | if 'rev' in opts or 'branch' in opts: | ||
opts = copy.copy(opts) | ||||
opts.pop('rev', None) | ||||
opts.pop('branch', None) | ||||
Martin Geisler
|
r12272 | return hg.outgoing(ui, self._repo, _abssource(self._repo, True), opts) | ||
Angel Ezquerra
|
r18109 | @annotatesubrepoerror | ||
Martin Geisler
|
r12274 | def incoming(self, ui, source, opts): | ||
Matt Harbison
|
r24876 | if 'rev' in opts or 'branch' in opts: | ||
opts = copy.copy(opts) | ||||
opts.pop('rev', None) | ||||
opts.pop('branch', None) | ||||
Martin Geisler
|
r12274 | return hg.incoming(ui, self._repo, _abssource(self._repo, False), opts) | ||
Angel Ezquerra
|
r18109 | @annotatesubrepoerror | ||
Martin Geisler
|
r12322 | def files(self): | ||
rev = self._state[1] | ||||
ctx = self._repo[rev] | ||||
Matt Harbison
|
r24173 | return ctx.manifest().keys() | ||
Martin Geisler
|
r12322 | |||
Matt Harbison
|
r31099 | def filedata(self, name, decode): | ||
Martin Geisler
|
r12322 | rev = self._state[1] | ||
Matt Harbison
|
r31099 | data = self._repo[rev][name].data() | ||
if decode: | ||||
data = self._repo.wwritedata(name, data) | ||||
return data | ||||
Martin Geisler
|
r12322 | |||
def fileflags(self, name): | ||||
rev = self._state[1] | ||||
ctx = self._repo[rev] | ||||
return ctx.flags(name) | ||||
Matt Harbison
|
r24413 | @annotatesubrepoerror | ||
Matt Harbison
|
r25228 | def printfiles(self, ui, m, fm, fmt, subrepos): | ||
Matt Harbison
|
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] | ||||
Matt Harbison
|
r25228 | return cmdutil.files(ui, ctx, m, fm, fmt, subrepos) | ||
Matt Harbison
|
r24413 | |||
Matt Harbison
|
r25121 | @annotatesubrepoerror | ||
def getfileset(self, expr): | ||||
if self._ctx.rev() is None: | ||||
ctx = self._repo[None] | ||||
else: | ||||
rev = self._state[1] | ||||
ctx = self._repo[rev] | ||||
files = ctx.getfileset(expr) | ||||
for subpath in ctx.substate: | ||||
sub = ctx.sub(subpath) | ||||
try: | ||||
files.extend(subpath + '/' + f for f in sub.getfileset(expr)) | ||||
except error.LookupError: | ||||
self.ui.status(_("skipping missing subrepository: %s\n") | ||||
% self.wvfs.reljoin(reporelpath(self), subpath)) | ||||
return files | ||||
David M. Carr
|
r15410 | def walk(self, match): | ||
ctx = self._repo[None] | ||||
return ctx.walk(match) | ||||
Martin Geisler
|
r12322 | |||
Angel Ezquerra
|
r18109 | @annotatesubrepoerror | ||
Matt Harbison
|
r23577 | def forget(self, match, prefix): | ||
return cmdutil.forget(self.ui, self._repo, match, | ||||
FUJIWARA Katsunori
|
r24675 | self.wvfs.reljoin(prefix, self._path), True) | ||
Martin Geisler
|
r12322 | |||
Angel Ezquerra
|
r18109 | @annotatesubrepoerror | ||
timeless
|
r28607 | def removefiles(self, matcher, prefix, after, force, subrepos, warnings): | ||
Matt Harbison
|
r23578 | return cmdutil.remove(self.ui, self._repo, matcher, | ||
FUJIWARA Katsunori
|
r24675 | self.wvfs.reljoin(prefix, self._path), | ||
after, force, subrepos) | ||||
Matt Harbison
|
r23325 | |||
@annotatesubrepoerror | ||||
Matt Harbison
|
r23579 | def revert(self, substate, *pats, **opts): | ||
Angel Ezquerra
|
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 | ||||
Matt Harbison
|
r23579 | self.ui.status(_('reverting subrepo %s\n') % substate[0]) | ||
Pulkit Goyal
|
r35148 | if not opts.get(r'no_backup'): | ||
Angel Ezquerra
|
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() | ||||
Pulkit Goyal
|
r35148 | opts[r'date'] = None | ||
opts[r'rev'] = substate[1] | ||||
Angel Ezquerra
|
r16430 | |||
Matt Harbison
|
r23579 | self.filerevert(*pats, **opts) | ||
Angel Ezquerra
|
r16429 | |||
# Update the repo to the revision specified in the given substate | ||||
Pulkit Goyal
|
r35148 | if not opts.get(r'dry_run'): | ||
Matt Harbison
|
r24134 | self.get(substate, overwrite=True) | ||
Angel Ezquerra
|
r16429 | |||
Matt Harbison
|
r23579 | def filerevert(self, *pats, **opts): | ||
Pulkit Goyal
|
r35148 | ctx = self._repo[opts[r'rev']] | ||
Angel Ezquerra
|
r16430 | parents = self._repo.dirstate.parents() | ||
Pulkit Goyal
|
r35148 | if opts.get(r'all'): | ||
Angel Ezquerra
|
r16430 | pats = ['set:modified()'] | ||
else: | ||||
pats = [] | ||||
Matt Harbison
|
r23579 | cmdutil.revert(self.ui, self._repo, ctx, parents, *pats, **opts) | ||
Angel Ezquerra
|
r16430 | |||
Angel Ezquerra
|
r21400 | def shortid(self, revid): | ||
return revid[:12] | ||||
Matt Harbison
|
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) | ||||
Matt Harbison
|
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 | ||||
FUJIWARA Katsunori
|
r24672 | @propertycache | ||
def wvfs(self): | ||||
Mads Kiilerich
|
r26781 | """return own wvfs for efficiency and consistency | ||
FUJIWARA Katsunori
|
r24672 | """ | ||
return self._repo.wvfs | ||||
Matt Harbison
|
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) | ||||
Martin Geisler
|
r11559 | class svnsubrepo(abstractsubrepo): | ||
Matt Harbison
|
r29021 | def __init__(self, ctx, path, state, allowcreate): | ||
FUJIWARA Katsunori
|
r24671 | super(svnsubrepo, self).__init__(ctx, path) | ||
Augie Fackler
|
r10178 | self._state = state | ||
Matt Mackall
|
r15190 | self._exe = util.findexe('svn') | ||
if not self._exe: | ||||
Pierre-Yves David
|
r26587 | raise error.Abort(_("'svn' executable not found for subrepo '%s'") | ||
Matt Mackall
|
r15190 | % self._path) | ||
Augie Fackler
|
r10178 | |||
Augie Fackler
|
r14664 | def _svncommand(self, commands, filename='', failok=False): | ||
Matt Mackall
|
r15190 | cmd = [self._exe] | ||
Augie Fackler
|
r14506 | extrakw = {} | ||
Matt Harbison
|
r23572 | if not self.ui.interactive(): | ||
Augie Fackler
|
r14506 | # Making stdin be a pipe should prevent svn from behaving | ||
# interactively even if we can't pass --non-interactive. | ||||
Pulkit Goyal
|
r35372 | extrakw[r'stdin'] = subprocess.PIPE | ||
Augie Fackler
|
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') | ||||
Augie Fackler
|
r14025 | cmd.extend(commands) | ||
Patrick Mezard
|
r14050 | if filename is not None: | ||
FUJIWARA Katsunori
|
r24675 | path = self.wvfs.reljoin(self._ctx.repo().origroot, | ||
self._path, filename) | ||||
Patrick Mezard
|
r14050 | cmd.append(path) | ||
Pulkit Goyal
|
r30635 | env = dict(encoding.environ) | ||
Patrick Mezard
|
r10271 | # Avoid localized output, preserve current locale for everything else. | ||
Thomas Arendsen Hein
|
r17705 | lc_all = env.get('LC_ALL') | ||
if lc_all: | ||||
env['LANG'] = lc_all | ||||
del env['LC_ALL'] | ||||
Patrick Mezard
|
r10271 | env['LC_MESSAGES'] = 'C' | ||
Eric Eisner
|
r13108 | p = subprocess.Popen(cmd, bufsize=-1, close_fds=util.closefds, | ||
Patrick Mezard
|
r13014 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, | ||
Augie Fackler
|
r14506 | universal_newlines=True, env=env, **extrakw) | ||
Patrick Mezard
|
r13014 | stdout, stderr = p.communicate() | ||
stderr = stderr.strip() | ||||
Augie Fackler
|
r14664 | if not failok: | ||
if p.returncode: | ||||
Pierre-Yves David
|
r26587 | raise error.Abort(stderr or 'exited with code %d' | ||
% p.returncode) | ||||
Augie Fackler
|
r14664 | if stderr: | ||
Matt Harbison
|
r23572 | self.ui.warn(stderr + '\n') | ||
Augie Fackler
|
r14664 | return stdout, stderr | ||
Augie Fackler
|
r10178 | |||
Patrick Mezard
|
r14050 | @propertycache | ||
def _svnversion(self): | ||||
Thomas Arendsen Hein
|
r17707 | output, err = self._svncommand(['--version', '--quiet'], filename=None) | ||
Yuya Nishihara
|
r34070 | m = re.search(br'^(\d+)\.(\d+)', output) | ||
Patrick Mezard
|
r14050 | if not m: | ||
Pierre-Yves David
|
r26587 | raise error.Abort(_('cannot retrieve svn tool version')) | ||
Patrick Mezard
|
r14050 | return (int(m.group(1)), int(m.group(2))) | ||
Jordi Gutiérrez Hermoso
|
r35679 | def _svnmissing(self): | ||
return not self.wvfs.exists('.svn') | ||||
Patrick Mezard
|
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. | ||||
Augie Fackler
|
r14664 | output, err = self._svncommand(['info', '--xml']) | ||
Patrick Mezard
|
r10272 | doc = xml.dom.minidom.parseString(output) | ||
entries = doc.getElementsByTagName('entry') | ||||
Patrick Mezard
|
r13287 | lastrev, rev = '0', '0' | ||
if entries: | ||||
rev = str(entries[0].getAttribute('revision')) or '0' | ||||
commits = entries[0].getElementsByTagName('commit') | ||||
if commits: | ||||
lastrev = str(commits[0].getAttribute('revision')) or '0' | ||||
return (lastrev, rev) | ||||
def _wcrev(self): | ||||
return self._wcrevs()[0] | ||||
Augie Fackler
|
r10178 | |||
Patrick Mezard
|
r10273 | def _wcchanged(self): | ||
Patrick Mezard
|
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. | ||||
Patrick Mezard
|
r10273 | """ | ||
Augie Fackler
|
r14664 | output, err = self._svncommand(['status', '--xml']) | ||
Patrick Mezard
|
r16530 | externals, changes, missing = [], [], [] | ||
Patrick Mezard
|
r10272 | doc = xml.dom.minidom.parseString(output) | ||
Patrick Mezard
|
r10273 | for e in doc.getElementsByTagName('entry'): | ||
s = e.getElementsByTagName('wc-status') | ||||
if not s: | ||||
continue | ||||
item = s[0].getAttribute('item') | ||||
props = s[0].getAttribute('props') | ||||
path = e.getAttribute('path') | ||||
if item == 'external': | ||||
externals.append(path) | ||||
Patrick Mezard
|
r16530 | elif item == 'missing': | ||
missing.append(path) | ||||
Patrick Mezard
|
r10273 | if (item not in ('', 'normal', 'unversioned', 'external') | ||
Vasily Titskiy
|
r14994 | or props not in ('', 'none', 'normal')): | ||
Patrick Mezard
|
r10273 | changes.append(path) | ||
for path in changes: | ||||
for ext in externals: | ||||
Pulkit Goyal
|
r30615 | if path == ext or path.startswith(ext + pycompat.ossep): | ||
Patrick Mezard
|
r16530 | return True, True, bool(missing) | ||
return bool(changes), False, bool(missing) | ||||
Augie Fackler
|
r10178 | |||
Jordi Gutiérrez Hermoso
|
r35678 | @annotatesubrepoerror | ||
Matt Harbison
|
r33364 | def dirty(self, ignoreupdate=False, missing=False): | ||
Jordi Gutiérrez Hermoso
|
r35694 | if self._svnmissing(): | ||
return self._state[1] != '' | ||||
Matt Harbison
|
r33364 | wcchanged = self._wcchanged() | ||
changed = wcchanged[0] or (missing and wcchanged[2]) | ||||
if not changed: | ||||
Patrick Mezard
|
r13288 | if self._state[1] in self._wcrevs() or ignoreupdate: | ||
Kevin Bullock
|
r13174 | return False | ||
Augie Fackler
|
r10178 | return True | ||
Matt Mackall
|
r16072 | def basestate(self): | ||
Patrick Mezard
|
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: | ||||
Thomas Arendsen Hein
|
r17035 | self._svncommand(['list', '%s@%s' % (self._state[0], lastrev)]) | ||
Patrick Mezard
|
r16554 | return lastrev | ||
except error.Abort: | ||||
pass | ||||
return rev | ||||
Matt Mackall
|
r16072 | |||
Angel Ezquerra
|
r18109 | @annotatesubrepoerror | ||
Augie Fackler
|
r10178 | def commit(self, text, user, date): | ||
# user and date are out of our hands since svn is centralized | ||||
Patrick Mezard
|
r16530 | changed, extchanged, missing = self._wcchanged() | ||
Patrick Mezard
|
r10273 | if not changed: | ||
Patrick Mezard
|
r16554 | return self.basestate() | ||
Patrick Mezard
|
r10273 | if extchanged: | ||
# Do not try to commit externals | ||||
Pierre-Yves David
|
r26587 | raise error.Abort(_('cannot commit svn externals')) | ||
Patrick Mezard
|
r16530 | if missing: | ||
# svn can commit with missing entries but aborting like hg | ||||
# seems a better approach. | ||||
Pierre-Yves David
|
r26587 | raise error.Abort(_('cannot commit missing svn entries')) | ||
Augie Fackler
|
r14664 | commitinfo, err = self._svncommand(['commit', '-m', text]) | ||
Matt Harbison
|
r23572 | self.ui.status(commitinfo) | ||
Martin Geisler
|
r12060 | newrev = re.search('Committed revision ([0-9]+).', commitinfo) | ||
Augie Fackler
|
r10178 | if not newrev: | ||
Patrick Mezard
|
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. | ||||
Pierre-Yves David
|
r26587 | raise error.Abort(_('failed to commit svn changes')) | ||
raise error.Abort(commitinfo.splitlines()[-1]) | ||||
Augie Fackler
|
r10178 | newrev = newrev.groups()[0] | ||
Matt Harbison
|
r23572 | self.ui.status(self._svncommand(['update', '-r', newrev])[0]) | ||
Augie Fackler
|
r10178 | return newrev | ||
Angel Ezquerra
|
r18109 | @annotatesubrepoerror | ||
Augie Fackler
|
r10178 | def remove(self): | ||
if self.dirty(): | ||||
Matt Harbison
|
r23572 | self.ui.warn(_('not removing repo %s because ' | ||
'it has changes.\n') % self._path) | ||||
Augie Fackler
|
r10178 | return | ||
Matt Harbison
|
r23572 | self.ui.note(_('removing subrepo %s\n') % self._path) | ||
Patrick Mezard
|
r13013 | |||
FUJIWARA Katsunori
|
r24690 | self.wvfs.rmtree(forcibly=True) | ||
Patrick Mezard
|
r13015 | try: | ||
FUJIWARA Katsunori
|
r25773 | pwvfs = self._ctx.repo().wvfs | ||
pwvfs.removedirs(pwvfs.dirname(self._path)) | ||||
Patrick Mezard
|
r13015 | except OSError: | ||
pass | ||||
Augie Fackler
|
r10178 | |||
Angel Ezquerra
|
r18109 | @annotatesubrepoerror | ||
Erik Zielke
|
r13322 | def get(self, state, overwrite=False): | ||
if overwrite: | ||||
Patrick Mezard
|
r13332 | self._svncommand(['revert', '--recursive']) | ||
Patrick Mezard
|
r14050 | args = ['checkout'] | ||
if self._svnversion >= (1, 5): | ||||
args.append('--force') | ||||
Eli Carter
|
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])) | ||||
Sean Farley
|
r33730 | |||
# SEC: check that the ssh url is safe | ||||
util.checksafessh(state[0]) | ||||
Augie Fackler
|
r14664 | status, err = self._svncommand(args, failok=True) | ||
FUJIWARA Katsunori
|
r24724 | _sanitize(self.ui, self.wvfs, '.svn') | ||
Martin Geisler
|
r12060 | if not re.search('Checked out revision [0-9]+.', status): | ||
Augie Fackler
|
r14664 | if ('is already a working copy for a different URL' in err | ||
Patrick Mezard
|
r16530 | and (self._wcchanged()[:2] == (False, False))): | ||
Augie Fackler
|
r14664 | # obstructed but clean working copy, so just blow it away. | ||
self.remove() | ||||
self.get(state, overwrite=False) | ||||
return | ||||
Pierre-Yves David
|
r26587 | raise error.Abort((status or err).splitlines()[-1]) | ||
Matt Harbison
|
r23572 | self.ui.status(status) | ||
Augie Fackler
|
r10178 | |||
Angel Ezquerra
|
r18109 | @annotatesubrepoerror | ||
Augie Fackler
|
r10178 | def merge(self, state): | ||
Erik Zielke
|
r13417 | old = self._state[1] | ||
new = state[1] | ||||
Patrick Mezard
|
r16555 | wcrev = self._wcrev() | ||
if new != wcrev: | ||||
dirty = old == wcrev or self._wcchanged()[0] | ||||
Matt Harbison
|
r23572 | if _updateprompt(self.ui, self, dirty, wcrev, new): | ||
Erik Zielke
|
r13417 | self.get(state, False) | ||
Augie Fackler
|
r10178 | |||
Angel Ezquerra
|
r15708 | def push(self, opts): | ||
Matt Mackall
|
r11455 | # push is a no-op for SVN | ||
return True | ||||
Augie Fackler
|
r10178 | |||
Angel Ezquerra
|
r18109 | @annotatesubrepoerror | ||
Martin Geisler
|
r12322 | def files(self): | ||
Patrick Mezard
|
r16450 | output = self._svncommand(['list', '--recursive', '--xml'])[0] | ||
doc = xml.dom.minidom.parseString(output) | ||||
paths = [] | ||||
for e in doc.getElementsByTagName('entry'): | ||||
kind = str(e.getAttribute('kind')) | ||||
if kind != 'file': | ||||
continue | ||||
name = ''.join(c.data for c | ||||
in e.getElementsByTagName('name')[0].childNodes | ||||
if c.nodeType == c.TEXT_NODE) | ||||
Bryan O'Sullivan
|
r17441 | paths.append(name.encode('utf-8')) | ||
Patrick Mezard
|
r16450 | return paths | ||
Martin Geisler
|
r12322 | |||
Matt Harbison
|
r31099 | def filedata(self, name, decode): | ||
Patrick Mezard
|
r16450 | return self._svncommand(['cat'], name)[0] | ||
Martin Geisler
|
r12322 | |||
Eric Eisner
|
r13106 | class gitsubrepo(abstractsubrepo): | ||
Matt Harbison
|
r29021 | def __init__(self, ctx, path, state, allowcreate): | ||
FUJIWARA Katsunori
|
r24671 | super(gitsubrepo, self).__init__(ctx, path) | ||
Eric Eisner
|
r12992 | self._state = state | ||
Matt Harbison
|
r24302 | self._abspath = ctx.repo().wjoin(path) | ||
self._subparent = ctx.repo() | ||||
Benjamin Pollack
|
r17024 | self._ensuregit() | ||
def _ensuregit(self): | ||||
Benjamin Pollack
|
r17025 | try: | ||
self._gitexecutable = 'git' | ||||
out, err = self._gitnodir(['--version']) | ||||
Gregory Szorc
|
r25660 | except OSError as e: | ||
Mason Malone
|
r27935 | genericerror = _("error executing git for subrepo '%s': %s") | ||
notfoundhint = _("check git is installed and in your PATH") | ||||
if e.errno != errno.ENOENT: | ||||
Augie Fackler
|
r34024 | raise error.Abort(genericerror % ( | ||
self._path, encoding.strtolocal(e.strerror))) | ||||
Jun Wu
|
r34646 | elif pycompat.iswindows: | ||
Mason Malone
|
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, | ||||
Augie Fackler
|
r34024 | encoding.strtolocal(e2.strerror))) | ||
Mason Malone
|
r27935 | else: | ||
raise error.Abort(_("couldn't find git for subrepo '%s'") | ||||
% self._path, hint=notfoundhint) | ||||
Siddharth Agarwal
|
r20840 | versionstatus = self._checkversion(out) | ||
if versionstatus == 'unknown': | ||||
Matt Harbison
|
r23572 | self.ui.warn(_('cannot retrieve git version\n')) | ||
Siddharth Agarwal
|
r20840 | elif versionstatus == 'abort': | ||
Pierre-Yves David
|
r26587 | raise error.Abort(_('git subrepo requires at least 1.6.0 or later')) | ||
Siddharth Agarwal
|
r20840 | elif versionstatus == 'warning': | ||
Matt Harbison
|
r23572 | self.ui.warn(_('git subrepo requires at least 1.6.0 or later\n')) | ||
Siddharth Agarwal
|
r20840 | |||
@staticmethod | ||||
Mathias De Maré
|
r23521 | def _gitversion(out): | ||
Yuya Nishihara
|
r34070 | m = re.search(br'^git version (\d+)\.(\d+)\.(\d+)', out) | ||
Mathias De Maré
|
r23522 | if m: | ||
return (int(m.group(1)), int(m.group(2)), int(m.group(3))) | ||||
Yuya Nishihara
|
r34070 | m = re.search(br'^git version (\d+)\.(\d+)', out) | ||
Mathias De Maré
|
r23521 | if m: | ||
Mathias De Maré
|
r23522 | return (int(m.group(1)), int(m.group(2)), 0) | ||
Mathias De Maré
|
r23521 | |||
return -1 | ||||
@staticmethod | ||||
Siddharth Agarwal
|
r20840 | def _checkversion(out): | ||
'''ensure git version is new enough | ||||
>>> _checkversion = gitsubrepo._checkversion | ||||
Yuya Nishihara
|
r34133 | >>> _checkversion(b'git version 1.6.0') | ||
Siddharth Agarwal
|
r20840 | 'ok' | ||
Yuya Nishihara
|
r34133 | >>> _checkversion(b'git version 1.8.5') | ||
Siddharth Agarwal
|
r20840 | 'ok' | ||
Yuya Nishihara
|
r34133 | >>> _checkversion(b'git version 1.4.0') | ||
Siddharth Agarwal
|
r20840 | 'abort' | ||
Yuya Nishihara
|
r34133 | >>> _checkversion(b'git version 1.5.0') | ||
Siddharth Agarwal
|
r20840 | 'warning' | ||
Yuya Nishihara
|
r34133 | >>> _checkversion(b'git version 1.9-rc0') | ||
Siddharth Agarwal
|
r20840 | 'ok' | ||
Yuya Nishihara
|
r34133 | >>> _checkversion(b'git version 1.9.0.265.g81cdec2') | ||
Siddharth Agarwal
|
r20840 | 'ok' | ||
Yuya Nishihara
|
r34133 | >>> _checkversion(b'git version 1.9.0.GIT') | ||
Siddharth Agarwal
|
r20840 | 'ok' | ||
Yuya Nishihara
|
r34133 | >>> _checkversion(b'git version 12345') | ||
Siddharth Agarwal
|
r20840 | 'unknown' | ||
Yuya Nishihara
|
r34133 | >>> _checkversion(b'no') | ||
Siddharth Agarwal
|
r20840 | 'unknown' | ||
''' | ||||
Mathias De Maré
|
r23521 | version = gitsubrepo._gitversion(out) | ||
Benjamin Pollack
|
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. | ||||
Mathias De Maré
|
r23521 | if version == -1: | ||
return 'unknown' | ||||
Mathias De Maré
|
r23522 | if version < (1, 5, 0): | ||
Siddharth Agarwal
|
r20840 | return 'abort' | ||
Mathias De Maré
|
r23522 | elif version < (1, 6, 0): | ||
Siddharth Agarwal
|
r20840 | return 'warning' | ||
return 'ok' | ||||
Eric Eisner
|
r12992 | |||
Eric Eisner
|
r13095 | def _gitcommand(self, commands, env=None, stream=False): | ||
return self._gitdir(commands, env=env, stream=stream)[0] | ||||
Eric Eisner
|
r12992 | |||
Eric Eisner
|
r13095 | def _gitdir(self, commands, env=None, stream=False): | ||
Eric Eisner
|
r13181 | return self._gitnodir(commands, env=env, stream=stream, | ||
cwd=self._abspath) | ||||
Eric Eisner
|
r12992 | |||
Eric Eisner
|
r13095 | def _gitnodir(self, commands, env=None, stream=False, cwd=None): | ||
Eric Eisner
|
r12992 | """Calls the git command | ||
Mads Kiilerich
|
r17424 | The methods tries to call the git command. versions prior to 1.6.0 | ||
Eric Eisner
|
r12992 | are not supported and very probably fail. | ||
""" | ||||
Matt Harbison
|
r23572 | self.ui.debug('%s: git %s\n' % (self._relpath, ' '.join(commands))) | ||
Mateusz Kwapich
|
r28658 | if env is None: | ||
Pulkit Goyal
|
r30635 | env = encoding.environ.copy() | ||
Matt Mackall
|
r28949 | # disable localization for Git output (issue5176) | ||
env['LC_ALL'] = 'C' | ||||
Mateusz Kwapich
|
r28658 | # fix for Git CVE-2015-7545 | ||
if 'GIT_ALLOW_PROTOCOL' not in env: | ||||
env['GIT_ALLOW_PROTOCOL'] = 'file:git:http:https:ssh' | ||||
Eric Eisner
|
r13111 | # unless ui.quiet is set, print git's stderr, | ||
# which is mostly progress and useful info | ||||
errpipe = None | ||||
Matt Harbison
|
r23572 | if self.ui.quiet: | ||
Eric Eisner
|
r13111 | errpipe = open(os.devnull, 'w') | ||
Pierre-Yves David
|
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') | ||||
Benjamin Pollack
|
r17025 | p = subprocess.Popen([self._gitexecutable] + commands, bufsize=-1, | ||
cwd=cwd, env=env, close_fds=util.closefds, | ||||
Eric Eisner
|
r13111 | stdout=subprocess.PIPE, stderr=errpipe) | ||
Eric Eisner
|
r13027 | if stream: | ||
return p.stdout, None | ||||
Eric Eisner
|
r13085 | retdata = p.stdout.read().strip() | ||
Eric Eisner
|
r12992 | # wait for the child to exit to avoid race condition. | ||
p.wait() | ||||
Eric Eisner
|
r13107 | if p.returncode != 0 and p.returncode != 1: | ||
Eric Eisner
|
r12992 | # there are certain error codes that are ok | ||
Eric Eisner
|
r13110 | command = commands[0] | ||
Eric Eisner
|
r13150 | if command in ('cat-file', 'symbolic-ref'): | ||
Eric Eisner
|
r12992 | return retdata, p.returncode | ||
# for all others, abort | ||||
liscju
|
r29389 | raise error.Abort(_('git %s error %d in %s') % | ||
Eric Eisner
|
r13110 | (command, p.returncode, self._relpath)) | ||
Eric Eisner
|
r12992 | |||
return retdata, p.returncode | ||||
Eric Eisner
|
r13553 | def _gitmissing(self): | ||
Matt Harbison
|
r24695 | return not self.wvfs.exists('.git') | ||
Eric Eisner
|
r13553 | |||
Eric Eisner
|
r12992 | def _gitstate(self): | ||
Eric Eisner
|
r13085 | return self._gitcommand(['rev-parse', 'HEAD']) | ||
Eric Eisner
|
r12992 | |||
Eric Eisner
|
r13152 | def _gitcurrentbranch(self): | ||
current, err = self._gitdir(['symbolic-ref', 'HEAD', '--quiet']) | ||||
if err: | ||||
current = None | ||||
return current | ||||
Eric Eisner
|
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:] | ||||
Eric Eisner
|
r12992 | def _githavelocally(self, revision): | ||
out, code = self._gitdir(['cat-file', '-e', revision]) | ||||
return code == 0 | ||||
Eric Eisner
|
r13029 | def _gitisancestor(self, r1, r2): | ||
Eric Eisner
|
r13085 | base = self._gitcommand(['merge-base', r1, r2]) | ||
Eric Eisner
|
r13029 | return base == r1 | ||
Paul Molodowitch
|
r14440 | def _gitisbare(self): | ||
return self._gitcommand(['config', '--bool', 'core.bare']) == 'true' | ||||
Eric Roshan Eisner
|
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']) | ||||
Eric Eisner
|
r12995 | def _gitbranchmap(self): | ||
Eric Eisner
|
r13178 | '''returns 2 things: | ||
Eric Eisner
|
r13086 | a map from git branch to revision | ||
Eric Eisner
|
r13178 | a map from revision to branches''' | ||
Eric Eisner
|
r13086 | branch2rev = {} | ||
rev2branch = {} | ||||
Eric Eisner
|
r13178 | |||
Eric Eisner
|
r13150 | out = self._gitcommand(['for-each-ref', '--format', | ||
Eric Eisner
|
r13178 | '%(objectname) %(refname)']) | ||
Eric Eisner
|
r12995 | for line in out.split('\n'): | ||
Eric Eisner
|
r13178 | revision, ref = line.split(' ') | ||
Eric Eisner
|
r13465 | if (not ref.startswith('refs/heads/') and | ||
not ref.startswith('refs/remotes/')): | ||||
Eric Eisner
|
r12995 | continue | ||
Eric Eisner
|
r13150 | if ref.startswith('refs/remotes/') and ref.endswith('/HEAD'): | ||
Eric Eisner
|
r12995 | continue # ignore remote/HEAD redirects | ||
Eric Eisner
|
r13150 | branch2rev[ref] = revision | ||
rev2branch.setdefault(revision, []).append(ref) | ||||
Eric Eisner
|
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 | ||||
Eric Roshan Eisner
|
r15234 | bname = b.split('/', 2)[2] | ||
remote = self._gitcommand(['config', 'branch.%s.remote' % bname]) | ||||
Eric Eisner
|
r13178 | if remote: | ||
Eric Roshan Eisner
|
r15234 | ref = self._gitcommand(['config', 'branch.%s.merge' % bname]) | ||
Eric Eisner
|
r13178 | tracking['refs/remotes/%s/%s' % | ||
(remote, ref.split('/', 2)[2])] = b | ||||
return tracking | ||||
Eric Eisner
|
r13087 | |||
Eric Eisner
|
r13460 | def _abssource(self, source): | ||
Eric Eisner
|
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 | ||||
Eric Eisner
|
r13460 | self._subsource = source | ||
return _abssource(self) | ||||
Eric Eisner
|
r12993 | def _fetch(self, source, revision): | ||
Eric Eisner
|
r13553 | if self._gitmissing(): | ||
Sean Farley
|
r33731 | # SEC: check for safe ssh url | ||
util.checksafessh(source) | ||||
Eric Eisner
|
r13525 | source = self._abssource(source) | ||
Matt Harbison
|
r23572 | self.ui.status(_('cloning subrepo %s from %s\n') % | ||
Eric Eisner
|
r13525 | (self._relpath, source)) | ||
self._gitnodir(['clone', source, self._abspath]) | ||||
Eric Eisner
|
r12993 | if self._githavelocally(revision): | ||
return | ||||
Matt Harbison
|
r23572 | self.ui.status(_('pulling subrepo %s from %s\n') % | ||
Eric Eisner
|
r13569 | (self._relpath, self._gitremote('origin'))) | ||
Eric Eisner
|
r13466 | # try only origin: the originally cloned repo | ||
Eric Eisner
|
r13097 | self._gitcommand(['fetch']) | ||
Eric Eisner
|
r12993 | if not self._githavelocally(revision): | ||
Matt Harbison
|
r33365 | raise error.Abort(_('revision %s does not exist in subrepository ' | ||
'"%s"\n') % (revision, self._relpath)) | ||||
Eric Eisner
|
r12993 | |||
Angel Ezquerra
|
r18109 | @annotatesubrepoerror | ||
Matt Harbison
|
r33364 | def dirty(self, ignoreupdate=False, missing=False): | ||
Eric Eisner
|
r13553 | if self._gitmissing(): | ||
Eric Eisner
|
r14469 | return self._state[1] != '' | ||
Paul Molodowitch
|
r14440 | if self._gitisbare(): | ||
return True | ||||
Eric Eisner
|
r13179 | if not ignoreupdate and self._state[1] != self._gitstate(): | ||
Kevin Bullock
|
r13325 | # different version checked out | ||
Eric Eisner
|
r12992 | return True | ||
# check for staged changes or modified files; ignore untracked files | ||||
Eric Roshan Eisner
|
r15531 | self._gitupdatestat() | ||
Eric Eisner
|
r13153 | out, code = self._gitdir(['diff-index', '--quiet', 'HEAD']) | ||
return code == 1 | ||||
Eric Eisner
|
r12992 | |||
Matt Mackall
|
r16072 | def basestate(self): | ||
return self._gitstate() | ||||
Angel Ezquerra
|
r18109 | @annotatesubrepoerror | ||
Martin Geisler
|
r13323 | def get(self, state, overwrite=False): | ||
Eric Eisner
|
r12993 | source, revision, kind = state | ||
Eric Eisner
|
r14469 | if not revision: | ||
self.remove() | ||||
return | ||||
Eric Eisner
|
r12993 | self._fetch(source, revision) | ||
Eric Eisner
|
r12996 | # if the repo was set to be bare, unbare it | ||
Paul Molodowitch
|
r14440 | if self._gitisbare(): | ||
Eric Eisner
|
r12996 | self._gitcommand(['config', 'core.bare', 'false']) | ||
if self._gitstate() == revision: | ||||
self._gitcommand(['reset', '--hard', 'HEAD']) | ||||
return | ||||
elif self._gitstate() == revision: | ||||
Erik Zielke
|
r13324 | if overwrite: | ||
Augie Fackler
|
r13927 | # first reset the index to unmark new files for commit, because | ||
Erik Zielke
|
r13324 | # reset --hard will otherwise throw away files added for commit, | ||
# not just unmark them. | ||||
self._gitcommand(['reset', 'HEAD']) | ||||
self._gitcommand(['reset', '--hard', 'HEAD']) | ||||
Eric Eisner
|
r12995 | return | ||
Eric Eisner
|
r13178 | branch2rev, rev2branch = self._gitbranchmap() | ||
Eric Eisner
|
r13087 | |||
Erik Zielke
|
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) | ||||
FUJIWARA Katsunori
|
r24724 | _sanitize(self.ui, self.wvfs, '.git') | ||
Erik Zielke
|
r13324 | |||
Eric Eisner
|
r13087 | def rawcheckout(): | ||
Eric Eisner
|
r12995 | # no branch to checkout, check it out with no branch | ||
Matt Harbison
|
r33365 | self.ui.warn(_('checking out detached HEAD in ' | ||
'subrepository "%s"\n') % self._relpath) | ||||
Matt Harbison
|
r23572 | self.ui.warn(_('check out a git branch if you intend ' | ||
Eric Eisner
|
r12993 | 'to make changes\n')) | ||
Erik Zielke
|
r13324 | checkout(['-q', revision]) | ||
Eric Eisner
|
r13087 | |||
if revision not in rev2branch: | ||||
rawcheckout() | ||||
Eric Eisner
|
r12995 | return | ||
Eric Eisner
|
r13086 | branches = rev2branch[revision] | ||
Eric Eisner
|
r12995 | firstlocalbranch = None | ||
for b in branches: | ||||
Eric Eisner
|
r13150 | if b == 'refs/heads/master': | ||
Eric Eisner
|
r12995 | # master trumps all other branches | ||
Erik Zielke
|
r13324 | checkout(['refs/heads/master']) | ||
Eric Eisner
|
r12995 | return | ||
Eric Eisner
|
r13150 | if not firstlocalbranch and not b.startswith('refs/remotes/'): | ||
Eric Eisner
|
r12995 | firstlocalbranch = b | ||
if firstlocalbranch: | ||||
Erik Zielke
|
r13324 | checkout([firstlocalbranch]) | ||
Eric Eisner
|
r13087 | return | ||
Eric Eisner
|
r13178 | tracking = self._gittracking(branch2rev.keys()) | ||
Eric Eisner
|
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 | ||||
pozheg
|
r19012 | local = remote.split('/', 3)[3] | ||
Erik Zielke
|
r13324 | checkout(['-b', local, remote]) | ||
Eric Eisner
|
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. | ||||
Eric Eisner
|
r13152 | if tracking[remote] != self._gitcurrentbranch(): | ||
Erik Zielke
|
r13324 | checkout([tracking[remote]]) | ||
Eric Eisner
|
r13087 | self._gitcommand(['merge', '--ff', remote]) | ||
FUJIWARA Katsunori
|
r24724 | _sanitize(self.ui, self.wvfs, '.git') | ||
Eric Eisner
|
r13087 | else: | ||
# a real merge would be required, just checkout the revision | ||||
rawcheckout() | ||||
Eric Eisner
|
r12993 | |||
Angel Ezquerra
|
r18109 | @annotatesubrepoerror | ||
Eric Eisner
|
r12992 | def commit(self, text, user, date): | ||
Eric Eisner
|
r13553 | if self._gitmissing(): | ||
Pierre-Yves David
|
r26587 | raise error.Abort(_("subrepo %s is missing") % self._relpath) | ||
Eric Eisner
|
r12992 | cmd = ['commit', '-a', '-m', text] | ||
Pulkit Goyal
|
r30635 | env = encoding.environ.copy() | ||
Eric Eisner
|
r12992 | if user: | ||
cmd += ['--author', user] | ||||
if date: | ||||
# git's date parser silently ignores when seconds < 1e9 | ||||
# convert to ISO8601 | ||||
Eric Eisner
|
r13095 | env['GIT_AUTHOR_DATE'] = util.datestr(date, | ||
'%Y-%m-%dT%H:%M:%S %1%2') | ||||
self._gitcommand(cmd, env=env) | ||||
Eric Eisner
|
r12992 | # make sure commit works otherwise HEAD might not exist under certain | ||
# circumstances | ||||
return self._gitstate() | ||||
Angel Ezquerra
|
r18109 | @annotatesubrepoerror | ||
Eric Eisner
|
r12994 | def merge(self, state): | ||
source, revision, kind = state | ||||
self._fetch(source, revision) | ||||
Eric Eisner
|
r13085 | base = self._gitcommand(['merge-base', revision, self._state[1]]) | ||
Eric Roshan Eisner
|
r15531 | self._gitupdatestat() | ||
Erik Zielke
|
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]) | ||||
FUJIWARA Katsunori
|
r24724 | _sanitize(self.ui, self.wvfs, '.git') | ||
Erik Zielke
|
r13417 | |||
if self.dirty(): | ||||
if self._gitstate() != revision: | ||||
dirty = self._gitstate() == self._state[1] or code != 0 | ||||
Matt Harbison
|
r23572 | if _updateprompt(self.ui, self, dirty, | ||
Martin Geisler
|
r13432 | self._state[1][:7], revision[:7]): | ||
Erik Zielke
|
r13417 | mergefunc() | ||
else: | ||||
mergefunc() | ||||
Eric Eisner
|
r12994 | |||
Angel Ezquerra
|
r18109 | @annotatesubrepoerror | ||
Angel Ezquerra
|
r15708 | def push(self, opts): | ||
force = opts.get('force') | ||||
Eric Eisner
|
r14469 | if not self._state[1]: | ||
return True | ||||
Eric Eisner
|
r13553 | if self._gitmissing(): | ||
Pierre-Yves David
|
r26587 | raise error.Abort(_("subrepo %s is missing") % self._relpath) | ||
Eric Eisner
|
r13029 | # if a branch in origin contains the revision, nothing to do | ||
Eric Eisner
|
r13178 | branch2rev, rev2branch = self._gitbranchmap() | ||
Eric Eisner
|
r13109 | if self._state[1] in rev2branch: | ||
for b in rev2branch[self._state[1]]: | ||||
Eric Eisner
|
r13150 | if b.startswith('refs/remotes/origin/'): | ||
Eric Eisner
|
r13109 | return True | ||
Eric Eisner
|
r13086 | for b, revision in branch2rev.iteritems(): | ||
Eric Eisner
|
r13150 | if b.startswith('refs/remotes/origin/'): | ||
Eric Eisner
|
r13086 | if self._gitisancestor(self._state[1], revision): | ||
return True | ||||
Eric Eisner
|
r13029 | # otherwise, try to push the currently checked out branch | ||
Eric Eisner
|
r12994 | cmd = ['push'] | ||
if force: | ||||
cmd.append('--force') | ||||
Eric Eisner
|
r13152 | |||
current = self._gitcurrentbranch() | ||||
Eric Eisner
|
r12995 | if current: | ||
Eric Eisner
|
r13029 | # determine if the current branch is even useful | ||
if not self._gitisancestor(self._state[1], current): | ||||
Matt Harbison
|
r23572 | self.ui.warn(_('unrelated git branch checked out ' | ||
Matt Harbison
|
r33365 | 'in subrepository "%s"\n') % self._relpath) | ||
Eric Eisner
|
r13029 | return False | ||
Matt Harbison
|
r33365 | self.ui.status(_('pushing branch %s of subrepository "%s"\n') % | ||
Matt Harbison
|
r23572 | (current.split('/', 2)[2], self._relpath)) | ||
Matt Mackall
|
r20970 | ret = self._gitdir(cmd + ['origin', current]) | ||
return ret[1] == 0 | ||||
Eric Eisner
|
r12995 | else: | ||
Matt Harbison
|
r33365 | self.ui.warn(_('no branch checked out in subrepository "%s"\n' | ||
Matt Harbison
|
r23572 | 'cannot push revision %s\n') % | ||
Eric Eisner
|
r13029 | (self._relpath, self._state[1])) | ||
Eric Eisner
|
r12995 | return False | ||
Eric Eisner
|
r12994 | |||
Angel Ezquerra
|
r18109 | @annotatesubrepoerror | ||
Mathias De Maré
|
r24174 | def add(self, ui, match, prefix, explicitonly, **opts): | ||
if self._gitmissing(): | ||||
return [] | ||||
Matt Harbison
|
r24182 | |||
(modified, added, removed, | ||||
Matt Harbison
|
r24209 | deleted, unknown, ignored, clean) = self.status(None, unknown=True, | ||
clean=True) | ||||
Mathias De Maré
|
r24174 | |||
Matt Harbison
|
r24183 | tracked = set() | ||
# dirstates 'amn' warn, 'r' is added again | ||||
for l in (modified, added, deleted, clean): | ||||
tracked.update(l) | ||||
Matt Harbison
|
r24182 | # Unknown files not of interest will be rejected by the matcher | ||
files = unknown | ||||
files.extend(match.files()) | ||||
Matt Harbison
|
r24183 | rejected = [] | ||
Matt Harbison
|
r24182 | files = [f for f in sorted(set(files)) if match(f)] | ||
Mathias De Maré
|
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: | ||||
ui.status(_('adding %s\n') % match.rel(f)) | ||||
Matt Harbison
|
r24183 | |||
if f in tracked: # hg prints 'adding' even if already tracked | ||||
if exact: | ||||
rejected.append(f) | ||||
continue | ||||
Pulkit Goyal
|
r32147 | if not opts.get(r'dry_run'): | ||
Mathias De Maré
|
r24174 | self._gitcommand(command + [f]) | ||
Matt Harbison
|
r24183 | |||
for f in rejected: | ||||
ui.warn(_("%s already tracked!\n") % match.abs(f)) | ||||
return rejected | ||||
Mathias De Maré
|
r24174 | |||
@annotatesubrepoerror | ||||
Eric Eisner
|
r12996 | def remove(self): | ||
Eric Eisner
|
r13553 | if self._gitmissing(): | ||
return | ||||
Eric Eisner
|
r12996 | if self.dirty(): | ||
Matt Harbison
|
r23572 | self.ui.warn(_('not removing repo %s because ' | ||
'it has changes.\n') % self._relpath) | ||||
Eric Eisner
|
r12996 | return | ||
# we can't fully delete the repository as it may contain | ||||
# local-only history | ||||
Matt Harbison
|
r23572 | self.ui.note(_('removing subrepo %s\n') % self._relpath) | ||
Eric Eisner
|
r12996 | self._gitcommand(['config', 'core.bare', 'true']) | ||
FUJIWARA Katsunori
|
r24688 | for f, kind in self.wvfs.readdir(): | ||
Eric Eisner
|
r12996 | if f == '.git': | ||
continue | ||||
FUJIWARA Katsunori
|
r24688 | if kind == stat.S_IFDIR: | ||
FUJIWARA Katsunori
|
r24690 | self.wvfs.rmtree(f) | ||
Eric Eisner
|
r12996 | else: | ||
FUJIWARA Katsunori
|
r24691 | self.wvfs.unlink(f) | ||
Eric Eisner
|
r12996 | |||
Matt Harbison
|
r31099 | def archive(self, archiver, prefix, match=None, decode=True): | ||
Angel Ezquerra
|
r18967 | total = 0 | ||
Eric Eisner
|
r13027 | source, revision = self._state | ||
Eric Eisner
|
r14469 | if not revision: | ||
Angel Ezquerra
|
r18967 | return total | ||
Eric Eisner
|
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) | ||||
tar = tarfile.open(fileobj=tarstream, mode='r|') | ||||
Martin Geisler
|
r13144 | relpath = subrelpath(self) | ||
Matt Harbison
|
r23575 | self.ui.progress(_('archiving (%s)') % relpath, 0, unit=_('files')) | ||
Martin Geisler
|
r13144 | for i, info in enumerate(tar): | ||
Eric Eisner
|
r13180 | if info.isdir(): | ||
continue | ||||
Matt Harbison
|
r17108 | if match and not match(info.name): | ||
continue | ||||
Eric Eisner
|
r13180 | if info.issym(): | ||
data = info.linkname | ||||
else: | ||||
data = tar.extractfile(info).read() | ||||
Matt Harbison
|
r24924 | archiver.addfile(prefix + self._path + '/' + info.name, | ||
Eric Eisner
|
r13180 | info.mode, info.issym(), data) | ||
Angel Ezquerra
|
r18967 | total += 1 | ||
Matt Harbison
|
r23575 | self.ui.progress(_('archiving (%s)') % relpath, i + 1, | ||
unit=_('files')) | ||||
self.ui.progress(_('archiving (%s)') % relpath, None) | ||||
Angel Ezquerra
|
r18967 | return total | ||
Martin Geisler
|
r13144 | |||
Eric Eisner
|
r13027 | |||
Angel Ezquerra
|
r18109 | @annotatesubrepoerror | ||
Yuya Nishihara
|
r32578 | def cat(self, match, fm, fntemplate, prefix, **opts): | ||
Mathias De Maré
|
r23991 | rev = self._state[1] | ||
if match.anypats(): | ||||
return 1 #No support for include/exclude yet | ||||
if not match.files(): | ||||
return 1 | ||||
Yuya Nishihara
|
r32578 | # TODO: add support for non-plain formatter (see cmdutil.cat()) | ||
Mathias De Maré
|
r23991 | for f in match.files(): | ||
output = self._gitcommand(["show", "%s:%s" % (rev, f)]) | ||||
Yuya Nishihara
|
r32540 | fp = cmdutil.makefileobj(self._subparent, fntemplate, | ||
Mathias De Maré
|
r23991 | self._ctx.node(), | ||
FUJIWARA Katsunori
|
r24675 | pathname=self.wvfs.reljoin(prefix, f)) | ||
Mathias De Maré
|
r23991 | fp.write(output) | ||
fp.close() | ||||
return 0 | ||||
@annotatesubrepoerror | ||||
Eric Eisner
|
r13182 | def status(self, rev2, **opts): | ||
Eric Eisner
|
r14469 | rev1 = self._state[1] | ||
if self._gitmissing() or not rev1: | ||||
Eric Eisner
|
r13553 | # if the repo is missing, return no results | ||
Matt Harbison
|
r24210 | return scmutil.status([], [], [], [], [], [], []) | ||
Eric Eisner
|
r13182 | modified, added, removed = [], [], [] | ||
Eric Roshan Eisner
|
r15531 | self._gitupdatestat() | ||
Eric Eisner
|
r13182 | if rev2: | ||
Martin von Zweigbergk
|
r28618 | command = ['diff-tree', '--no-renames', '-r', rev1, rev2] | ||
Eric Eisner
|
r13182 | else: | ||
Martin von Zweigbergk
|
r28618 | command = ['diff-index', '--no-renames', rev1] | ||
Eric Eisner
|
r13182 | out = self._gitcommand(command) | ||
for line in out.split('\n'): | ||||
tab = line.find('\t') | ||||
if tab == -1: | ||||
continue | ||||
status, f = line[tab - 1], line[tab + 1:] | ||||
if status == 'M': | ||||
modified.append(f) | ||||
elif status == 'A': | ||||
added.append(f) | ||||
elif status == 'D': | ||||
removed.append(f) | ||||
Martin von Zweigbergk
|
r22927 | deleted, unknown, ignored, clean = [], [], [], [] | ||
Mathias De Maré
|
r23411 | |||
Mathias De Maré
|
r24256 | command = ['status', '--porcelain', '-z'] | ||
Pulkit Goyal
|
r32157 | if opts.get(r'unknown'): | ||
Mathias De Maré
|
r24256 | command += ['--untracked-files=all'] | ||
Pulkit Goyal
|
r32157 | if opts.get(r'ignored'): | ||
Mathias De Maré
|
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) | ||||
Pulkit Goyal
|
r32157 | if opts.get(r'clean'): | ||
Mathias De Maré
|
r24256 | out = self._gitcommand(['ls-files']) | ||
for f in out.split('\n'): | ||||
if not f in changedfiles: | ||||
clean.append(f) | ||||
Mathias De Maré
|
r23411 | |||
Martin von Zweigbergk
|
r22914 | return scmutil.status(modified, added, removed, deleted, | ||
unknown, ignored, clean) | ||||
Eric Eisner
|
r13182 | |||
Mathias De Maré
|
r23523 | @annotatesubrepoerror | ||
def diff(self, ui, diffopts, node2, match, prefix, **opts): | ||||
node1 = self._state[1] | ||||
Martin von Zweigbergk
|
r28618 | cmd = ['diff', '--no-renames'] | ||
Pulkit Goyal
|
r32157 | if opts[r'stat']: | ||
Mathias De Maré
|
r23523 | cmd.append('--stat') | ||
else: | ||||
# for Git, this also implies '-p' | ||||
cmd.append('-U%d' % diffopts.context) | ||||
FUJIWARA Katsunori
|
r24675 | gitprefix = self.wvfs.reljoin(prefix, self._path) | ||
Mathias De Maré
|
r23523 | |||
if diffopts.noprefix: | ||||
cmd.extend(['--src-prefix=%s/' % gitprefix, | ||||
'--dst-prefix=%s/' % gitprefix]) | ||||
else: | ||||
cmd.extend(['--src-prefix=a/%s/' % gitprefix, | ||||
'--dst-prefix=b/%s/' % gitprefix]) | ||||
if diffopts.ignorews: | ||||
cmd.append('--ignore-all-space') | ||||
if diffopts.ignorewsamount: | ||||
cmd.append('--ignore-space-change') | ||||
if self._gitversion(self._gitcommand(['--version'])) >= (1, 8, 4) \ | ||||
and diffopts.ignoreblanklines: | ||||
cmd.append('--ignore-blank-lines') | ||||
cmd.append(node1) | ||||
if node2: | ||||
cmd.append(node2) | ||||
Mathias De Maré
|
r23938 | output = "" | ||
Mathias De Maré
|
r23523 | if match.always(): | ||
Mathias De Maré
|
r23938 | output += self._gitcommand(cmd) + '\n' | ||
Mathias De Maré
|
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' | ||||
Mathias De Maré
|
r23938 | |||
if output.strip(): | ||||
ui.write(output) | ||||
Mathias De Maré
|
r23523 | |||
Mathias De Maré
|
r23679 | @annotatesubrepoerror | ||
Matt Mackall
|
r23580 | def revert(self, substate, *pats, **opts): | ||
self.ui.status(_('reverting subrepo %s\n') % substate[0]) | ||||
Pulkit Goyal
|
r32157 | if not opts.get(r'no_backup'): | ||
Mathias De Maré
|
r23678 | status = self.status(None) | ||
names = status.modified | ||||
for name in names: | ||||
Siddharth Agarwal
|
r27651 | bakname = scmutil.origpath(self.ui, self._subparent, name) | ||
Mathias De Maré
|
r23678 | self.ui.note(_('saving current version of %s as %s\n') % | ||
(name, bakname)) | ||||
Matt Harbison
|
r24695 | self.wvfs.rename(name, bakname) | ||
Mathias De Maré
|
r23550 | |||
Pulkit Goyal
|
r32157 | if not opts.get(r'dry_run'): | ||
Matt Harbison
|
r24134 | self.get(substate, overwrite=True) | ||
Mathias De Maré
|
r23550 | return [] | ||
Angel Ezquerra
|
r21400 | def shortid(self, revid): | ||
return revid[:7] | ||||
Augie Fackler
|
r10177 | types = { | ||
'hg': hgsubrepo, | ||||
Augie Fackler
|
r10178 | 'svn': svnsubrepo, | ||
Eric Eisner
|
r12992 | 'git': gitsubrepo, | ||
Augie Fackler
|
r10177 | } | ||