git.py
531 lines
| 17.3 KiB
| text/x-python
|
PythonLexer
|
r8250 | # git.py - git support for the convert extension | ||
# | ||||
|
r47575 | # Copyright 2005-2009 Olivia Mackall <olivia@selenic.com> and others | ||
|
r8250 | # | ||
# This software may be used and distributed according to the terms of the | ||||
|
r10263 | # GNU General Public License version 2 or any later version. | ||
|
r28365 | from __future__ import absolute_import | ||
|
r4536 | |||
import os | ||||
|
r29205 | |||
from mercurial.i18n import _ | ||||
|
r47771 | from mercurial.node import sha1nodeconstants | ||
|
r28365 | from mercurial import ( | ||
config, | ||||
error, | ||||
|
r41619 | pycompat, | ||
|
r28365 | ) | ||
|
r4536 | |||
|
r43346 | from . import common | ||
|
r4536 | |||
|
r17929 | class submodule(object): | ||
def __init__(self, path, node, url): | ||||
self.path = path | ||||
self.node = node | ||||
self.url = url | ||||
def hgsub(self): | ||||
|
r43347 | return b"%s = [git]%s" % (self.path, self.url) | ||
|
r17929 | |||
def hgsubstate(self): | ||||
|
r43347 | return b"%s %s" % (self.node, self.path) | ||
|
r17929 | |||
|
r43346 | |||
|
r30660 | # Keys in extra fields that should not be copied if the user requests. | ||
|
r32291 | bannedextrakeys = { | ||
|
r30660 | # Git commit object built-ins. | ||
|
r43347 | b'tree', | ||
b'parent', | ||||
b'author', | ||||
b'committer', | ||||
|
r30660 | # Mercurial built-ins. | ||
|
r43347 | b'branch', | ||
b'close', | ||||
|
r32291 | } | ||
|
r30660 | |||
|
r43346 | |||
|
r28670 | class convert_git(common.converter_source, common.commandline): | ||
|
r5217 | # Windows does not support GIT_DIR= construct while other systems | ||
# cannot remove environment variable. Just assume none have | ||||
# both issues. | ||||
|
r21630 | |||
|
r28659 | def _gitcmd(self, cmd, *args, **kwargs): | ||
|
r43347 | return cmd(b'--git-dir=%s' % self.path, *args, **kwargs) | ||
|
r28659 | |||
def gitrun0(self, *args, **kwargs): | ||||
return self._gitcmd(self.run0, *args, **kwargs) | ||||
def gitrun(self, *args, **kwargs): | ||||
return self._gitcmd(self.run, *args, **kwargs) | ||||
def gitrunlines0(self, *args, **kwargs): | ||||
return self._gitcmd(self.runlines0, *args, **kwargs) | ||||
def gitrunlines(self, *args, **kwargs): | ||||
return self._gitcmd(self.runlines, *args, **kwargs) | ||||
|
r28662 | def gitpipe(self, *args, **kwargs): | ||
return self._gitcmd(self._run3, *args, **kwargs) | ||||
|
r35168 | def __init__(self, ui, repotype, path, revs=None): | ||
super(convert_git, self).__init__(ui, repotype, path, revs=revs) | ||||
|
r43347 | common.commandline.__init__(self, ui, b'git') | ||
|
r25748 | |||
|
r29051 | # Pass an absolute path to git to prevent from ever being interpreted | ||
# as a URL | ||||
path = os.path.abspath(path) | ||||
|
r43347 | if os.path.isdir(path + b"/.git"): | ||
path += b"/.git" | ||||
if not os.path.exists(path + b"/objects"): | ||||
|
r43346 | raise common.NoRepo( | ||
|
r43347 | _(b"%s does not look like a Git repository") % path | ||
|
r43346 | ) | ||
|
r5497 | |||
|
r22512 | # The default value (50) is based on the default for 'git diff'. | ||
|
r43347 | similarity = ui.configint(b'convert', b'git.similarity') | ||
|
r22470 | if similarity < 0 or similarity > 100: | ||
|
r43347 | raise error.Abort(_(b'similarity must be between 0 and 100')) | ||
|
r22470 | if similarity > 0: | ||
|
r43347 | self.simopt = [b'-C%d%%' % similarity] | ||
findcopiesharder = ui.configbool( | ||||
b'convert', b'git.findcopiesharder' | ||||
) | ||||
|
r22471 | if findcopiesharder: | ||
|
r43347 | self.simopt.append(b'--find-copies-harder') | ||
|
r30646 | |||
|
r43347 | renamelimit = ui.configint(b'convert', b'git.renamelimit') | ||
self.simopt.append(b'-l%d' % renamelimit) | ||||
|
r22470 | else: | ||
|
r28660 | self.simopt = [] | ||
|
r22470 | |||
|
r43347 | common.checktool(b'git', b'git') | ||
|
r5497 | |||
|
r4536 | self.path = path | ||
|
r17929 | self.submodules = [] | ||
|
r4536 | |||
|
r43347 | self.catfilepipe = self.gitpipe(b'cat-file', b'--batch') | ||
|
r21630 | |||
|
r43347 | self.copyextrakeys = self.ui.configlist(b'convert', b'git.extrakeys') | ||
|
r30660 | banned = set(self.copyextrakeys) & bannedextrakeys | ||
if banned: | ||||
|
r43346 | raise error.Abort( | ||
|
r43347 | _(b'copying of extra key is forbidden: %s') | ||
% _(b', ').join(sorted(banned)) | ||||
|
r43346 | ) | ||
|
r30660 | |||
|
r43347 | committeractions = self.ui.configlist( | ||
b'convert', b'git.committeractions' | ||||
) | ||||
|
r30813 | |||
messagedifferent = None | ||||
messagealways = None | ||||
for a in committeractions: | ||||
|
r43347 | if a.startswith((b'messagedifferent', b'messagealways')): | ||
|
r30813 | k = a | ||
v = None | ||||
|
r43347 | if b'=' in a: | ||
k, v = a.split(b'=', 1) | ||||
|
r30813 | |||
|
r43347 | if k == b'messagedifferent': | ||
messagedifferent = v or b'committer:' | ||||
elif k == b'messagealways': | ||||
messagealways = v or b'committer:' | ||||
|
r30813 | |||
if messagedifferent and messagealways: | ||||
|
r43346 | raise error.Abort( | ||
_( | ||||
|
r43347 | b'committeractions cannot define both ' | ||
b'messagedifferent and messagealways' | ||||
|
r43346 | ) | ||
) | ||||
|
r30813 | |||
|
r43347 | dropcommitter = b'dropcommitter' in committeractions | ||
replaceauthor = b'replaceauthor' in committeractions | ||||
|
r30813 | |||
|
r30815 | if dropcommitter and replaceauthor: | ||
|
r43346 | raise error.Abort( | ||
_( | ||||
|
r43347 | b'committeractions cannot define both ' | ||
b'dropcommitter and replaceauthor' | ||||
|
r43346 | ) | ||
) | ||||
|
r30813 | |||
if dropcommitter and messagealways: | ||||
|
r43346 | raise error.Abort( | ||
_( | ||||
|
r43347 | b'committeractions cannot define both ' | ||
b'dropcommitter and messagealways' | ||||
|
r43346 | ) | ||
) | ||||
|
r30813 | |||
if not messagedifferent and not messagealways: | ||||
|
r43347 | messagedifferent = b'committer:' | ||
|
r30813 | |||
self.committeractions = { | ||||
|
r43347 | b'dropcommitter': dropcommitter, | ||
b'replaceauthor': replaceauthor, | ||||
b'messagedifferent': messagedifferent, | ||||
b'messagealways': messagealways, | ||||
|
r30813 | } | ||
|
r21630 | def after(self): | ||
for f in self.catfilepipe: | ||||
f.close() | ||||
|
r4536 | def getheads(self): | ||
|
r25748 | if not self.revs: | ||
|
r43347 | output, status = self.gitrun( | ||
b'rev-parse', b'--branches', b'--remotes' | ||||
) | ||||
|
r28660 | heads = output.splitlines() | ||
if status: | ||||
|
r43347 | raise error.Abort(_(b'cannot retrieve git heads')) | ||
|
r4768 | else: | ||
|
r25749 | heads = [] | ||
for rev in self.revs: | ||||
|
r43347 | rawhead, ret = self.gitrun(b'rev-parse', b'--verify', rev) | ||
|
r25749 | heads.append(rawhead[:-1]) | ||
if ret: | ||||
|
r43347 | raise error.Abort(_(b'cannot retrieve git head "%s"') % rev) | ||
|
r10986 | return heads | ||
|
r4536 | |||
|
r36350 | def catfile(self, rev, ftype): | ||
|
r47771 | if rev == sha1nodeconstants.nullhex: | ||
|
r16687 | raise IOError | ||
|
r43347 | self.catfilepipe[0].write(rev + b'\n') | ||
|
r21630 | self.catfilepipe[0].flush() | ||
info = self.catfilepipe[1].readline().split() | ||||
|
r36350 | if info[1] != ftype: | ||
|
r43346 | raise error.Abort( | ||
|
r43347 | _(b'cannot read %r object at %s') | ||
|
r43346 | % (pycompat.bytestr(ftype), rev) | ||
) | ||||
|
r21630 | size = int(info[2]) | ||
data = self.catfilepipe[1].read(size) | ||||
if len(data) < size: | ||||
|
r43346 | raise error.Abort( | ||
|
r43347 | _(b'cannot read %r object at %s: unexpected size') | ||
% (ftype, rev) | ||||
|
r43346 | ) | ||
|
r21630 | # read the trailing newline | ||
self.catfilepipe[1].read(1) | ||||
|
r10986 | return data | ||
|
r4536 | |||
def getfile(self, name, rev): | ||||
|
r47771 | if rev == sha1nodeconstants.nullhex: | ||
|
r22296 | return None, None | ||
|
r43347 | if name == b'.hgsub': | ||
data = b'\n'.join([m.hgsub() for m in self.submoditer()]) | ||||
mode = b'' | ||||
elif name == b'.hgsubstate': | ||||
data = b'\n'.join([m.hgsubstate() for m in self.submoditer()]) | ||||
mode = b'' | ||||
|
r17929 | else: | ||
|
r43347 | data = self.catfile(rev, b"blob") | ||
|
r17929 | mode = self.modecache[(name, rev)] | ||
|
r11134 | return data, mode | ||
|
r4536 | |||
|
r17929 | def submoditer(self): | ||
|
r47771 | null = sha1nodeconstants.nullhex | ||
|
r17929 | for m in sorted(self.submodules, key=lambda p: p.path): | ||
if m.node != null: | ||||
yield m | ||||
def parsegitmodules(self, content): | ||||
"""Parse the formatted .gitmodules file, example file format: | ||||
[submodule "sub"]\n | ||||
\tpath = sub\n | ||||
\turl = git://giturl\n | ||||
""" | ||||
self.submodules = [] | ||||
c = config.config() | ||||
|
r25698 | # Each item in .gitmodules starts with whitespace that cant be parsed | ||
|
r43346 | c.parse( | ||
|
r43347 | b'.gitmodules', | ||
b'\n'.join(line.strip() for line in content.split(b'\n')), | ||||
|
r43346 | ) | ||
|
r17929 | for sec in c.sections(): | ||
r47153 | # turn the config object into a real dict | |||
s = dict(c.items(sec)) | ||||
|
r43347 | if b'url' in s and b'path' in s: | ||
self.submodules.append(submodule(s[b'path'], b'', s[b'url'])) | ||||
|
r17929 | |||
def retrievegitmodules(self, version): | ||||
|
r43347 | modules, ret = self.gitrun( | ||
b'show', b'%s:%s' % (version, b'.gitmodules') | ||||
) | ||||
|
r17929 | if ret: | ||
|
r25699 | # This can happen if a file is in the repo that has permissions | ||
# 160000, but there is no .gitmodules file. | ||||
|
r43346 | self.ui.warn( | ||
|
r43387 | _(b"warning: cannot read submodules config file in %s\n") | ||
|
r43346 | % version | ||
) | ||||
|
r25699 | return | ||
try: | ||||
self.parsegitmodules(modules) | ||||
except error.ParseError: | ||||
|
r43346 | self.ui.warn( | ||
|
r43347 | _(b"warning: unable to parse .gitmodules in %s\n") % version | ||
|
r43346 | ) | ||
|
r25699 | return | ||
|
r17929 | for m in self.submodules: | ||
|
r43347 | node, ret = self.gitrun(b'rev-parse', b'%s:%s' % (version, m.path)) | ||
|
r17929 | if ret: | ||
continue | ||||
m.node = node.strip() | ||||
|
r22300 | def getchanges(self, version, full): | ||
if full: | ||||
|
r43347 | raise error.Abort(_(b"convert from git does not support --full")) | ||
|
r4536 | self.modecache = {} | ||
|
r43346 | cmd = ( | ||
|
r43347 | [b'diff-tree', b'-z', b'--root', b'-m', b'-r'] | ||
+ self.simopt | ||||
+ [version] | ||||
|
r43346 | ) | ||
|
r28660 | output, status = self.gitrun(*cmd) | ||
if status: | ||||
|
r43347 | raise error.Abort(_(b'cannot read changes in %s') % version) | ||
|
r4536 | changes = [] | ||
|
r22470 | copies = {} | ||
|
r8456 | seen = set() | ||
|
r7242 | entry = None | ||
|
r22469 | subexists = [False] | ||
subdeleted = [False] | ||||
|
r43347 | difftree = output.split(b'\x00') | ||
|
r22467 | lcount = len(difftree) | ||
i = 0 | ||||
|
r22469 | |||
|
r43347 | skipsubmodules = self.ui.configbool(b'convert', b'git.skipsubmodules') | ||
|
r43346 | |||
|
r22470 | def add(entry, f, isdest): | ||
|
r22469 | seen.add(f) | ||
h = entry[3] | ||||
|
r43347 | p = entry[1] == b"100755" | ||
s = entry[1] == b"120000" | ||||
renamesource = not isdest and entry[4][0] == b'R' | ||||
|
r22469 | |||
|
r43347 | if f == b'.gitmodules': | ||
|
r26077 | if skipsubmodules: | ||
return | ||||
|
r22469 | subexists[0] = True | ||
|
r43347 | if entry[4] == b'D' or renamesource: | ||
|
r22469 | subdeleted[0] = True | ||
|
r47771 | changes.append((b'.hgsub', sha1nodeconstants.nullhex)) | ||
|
r22469 | else: | ||
|
r43347 | changes.append((b'.hgsub', b'')) | ||
elif entry[1] == b'160000' or entry[0] == b':160000': | ||||
|
r26077 | if not skipsubmodules: | ||
subexists[0] = True | ||||
|
r22469 | else: | ||
|
r22470 | if renamesource: | ||
|
r47771 | h = sha1nodeconstants.nullhex | ||
|
r43347 | self.modecache[(f, h)] = (p and b"x") or (s and b"l") or b"" | ||
|
r22469 | changes.append((f, h)) | ||
|
r22467 | while i < lcount: | ||
l = difftree[i] | ||||
i += 1 | ||||
|
r7242 | if not entry: | ||
|
r43347 | if not l.startswith(b':'): | ||
|
r7242 | continue | ||
|
r41619 | entry = tuple(pycompat.bytestr(p) for p in l.split()) | ||
|
r5335 | continue | ||
|
r7242 | f = l | ||
|
r43347 | if entry[4][0] == b'C': | ||
|
r25997 | copysrc = f | ||
copydest = difftree[i] | ||||
i += 1 | ||||
f = copydest | ||||
copies[copydest] = copysrc | ||||
|
r7242 | if f not in seen: | ||
|
r22470 | add(entry, f, False) | ||
# A file can be copied multiple times, or modified and copied | ||||
# simultaneously. So f can be repeated even if fdest isn't. | ||||
|
r43347 | if entry[4][0] == b'R': | ||
|
r25997 | # rename: next line is the destination | ||
|
r22470 | fdest = difftree[i] | ||
i += 1 | ||||
if fdest not in seen: | ||||
add(entry, fdest, True) | ||||
# .gitmodules isn't imported at all, so it being copied to | ||||
# and fro doesn't really make sense | ||||
|
r43347 | if f != b'.gitmodules' and fdest != b'.gitmodules': | ||
|
r22470 | copies[fdest] = f | ||
|
r7242 | entry = None | ||
|
r17929 | |||
|
r22469 | if subexists[0]: | ||
if subdeleted[0]: | ||||
|
r47771 | changes.append((b'.hgsubstate', sha1nodeconstants.nullhex)) | ||
|
r21868 | else: | ||
self.retrievegitmodules(version) | ||||
|
r43347 | changes.append((b'.hgsubstate', b'')) | ||
|
r24395 | return (changes, copies, set()) | ||
|
r4536 | |||
def getcommit(self, version): | ||||
|
r43347 | c = self.catfile(version, b"commit") # read the commit hash | ||
end = c.find(b"\n\n") | ||||
|
r43346 | message = c[end + 2 :] | ||
|
r4759 | message = self.recode(message) | ||
|
r4536 | l = c[:end].splitlines() | ||
parents = [] | ||||
|
r8271 | author = committer = None | ||
|
r30660 | extra = {} | ||
|
r4536 | for e in l[1:]: | ||
|
r43347 | n, v = e.split(b" ", 1) | ||
if n == b"author": | ||||
|
r4536 | p = v.split() | ||
tm, tz = p[-2:] | ||||
|
r43347 | author = b" ".join(p[:-2]) | ||
if author[0] == b"<": | ||||
|
r35646 | author = author[1:-1] | ||
|
r4759 | author = self.recode(author) | ||
|
r43347 | if n == b"committer": | ||
|
r4536 | p = v.split() | ||
tm, tz = p[-2:] | ||||
|
r43347 | committer = b" ".join(p[:-2]) | ||
if committer[0] == b"<": | ||||
|
r35646 | committer = committer[1:-1] | ||
|
r4759 | committer = self.recode(committer) | ||
|
r43347 | if n == b"parent": | ||
|
r10282 | parents.append(v) | ||
|
r30660 | if n in self.copyextrakeys: | ||
extra[n] = v | ||||
|
r4536 | |||
|
r43347 | if self.committeractions[b'dropcommitter']: | ||
|
r30813 | committer = None | ||
|
r43347 | elif self.committeractions[b'replaceauthor']: | ||
|
r30813 | author = committer | ||
if committer: | ||||
|
r43347 | messagealways = self.committeractions[b'messagealways'] | ||
messagedifferent = self.committeractions[b'messagedifferent'] | ||||
|
r30813 | if messagealways: | ||
|
r43347 | message += b'\n%s %s\n' % (messagealways, committer) | ||
|
r30813 | elif messagedifferent and author != committer: | ||
|
r43347 | message += b'\n%s %s\n' % (messagedifferent, committer) | ||
|
r30813 | |||
|
r43347 | tzs, tzh, tzm = tz[-5:-4] + b"1", tz[-4:-2], tz[-2:] | ||
|
r4536 | tz = -int(tzs) * (int(tzh) * 3600 + int(tzm)) | ||
|
r43347 | date = tm + b" " + (b"%d" % tz) | ||
saverev = self.ui.configbool(b'convert', b'git.saverev') | ||||
|
r4536 | |||
|
r43346 | c = common.commit( | ||
parents=parents, | ||||
date=date, | ||||
author=author, | ||||
desc=message, | ||||
rev=version, | ||||
extra=extra, | ||||
saverev=saverev, | ||||
) | ||||
|
r4536 | return c | ||
|
r22413 | def numcommits(self): | ||
|
r43347 | output, ret = self.gitrunlines(b'rev-list', b'--all') | ||
|
r28660 | if ret: | ||
|
r43346 | raise error.Abort( | ||
|
r43347 | _(b'cannot retrieve number of commits in %s') % self.path | ||
|
r43346 | ) | ||
|
r28660 | return len(output) | ||
|
r22413 | |||
|
r4536 | def gettags(self): | ||
tags = {} | ||||
|
r16259 | alltags = {} | ||
|
r43347 | output, status = self.gitrunlines(b'ls-remote', b'--tags', self.path) | ||
|
r28660 | |||
if status: | ||||
|
r43347 | raise error.Abort(_(b'cannot read tags from %s') % self.path) | ||
prefix = b'refs/tags/' | ||||
|
r16259 | |||
# Build complete list of tags, both annotated and bare ones | ||||
|
r28660 | for line in output: | ||
|
r4536 | line = line.strip() | ||
|
r43347 | if line.startswith(b"error:") or line.startswith(b"fatal:"): | ||
raise error.Abort(_(b'cannot read tags from %s') % self.path) | ||||
|
r4536 | node, tag = line.split(None, 1) | ||
if not tag.startswith(prefix): | ||||
continue | ||||
|
r43346 | alltags[tag[len(prefix) :]] = node | ||
|
r4536 | |||
|
r16259 | # Filter out tag objects for annotated tag refs | ||
for tag in alltags: | ||||
|
r43347 | if tag.endswith(b'^{}'): | ||
|
r16259 | tags[tag[:-3]] = alltags[tag] | ||
else: | ||||
|
r43347 | if tag + b'^{}' in alltags: | ||
|
r16259 | continue | ||
else: | ||||
tags[tag] = alltags[tag] | ||||
|
r4536 | return tags | ||
|
r5380 | |||
def getchangedfiles(self, version, i): | ||||
changes = [] | ||||
if i is None: | ||||
|
r43346 | output, status = self.gitrunlines( | ||
|
r43347 | b'diff-tree', b'--root', b'-m', b'-r', version | ||
|
r43346 | ) | ||
|
r28660 | if status: | ||
|
r43347 | raise error.Abort(_(b'cannot read changes in %s') % version) | ||
|
r28660 | for l in output: | ||
|
r43347 | if b"\t" not in l: | ||
|
r5380 | continue | ||
|
r43347 | m, f = l[:-1].split(b"\t") | ||
|
r5380 | changes.append(f) | ||
else: | ||||
|
r43346 | output, status = self.gitrunlines( | ||
|
r43347 | b'diff-tree', | ||
b'--name-only', | ||||
b'--root', | ||||
b'-r', | ||||
|
r43346 | version, | ||
|
r43347 | b'%s^%d' % (version, i + 1), | ||
b'--', | ||||
|
r43346 | ) | ||
|
r28816 | if status: | ||
|
r43347 | raise error.Abort(_(b'cannot read changes in %s') % version) | ||
changes = [f.rstrip(b'\n') for f in output] | ||||
|
r5380 | |||
return changes | ||||
|
r13756 | |||
def getbookmarks(self): | ||||
bookmarks = {} | ||||
|
r25905 | # Handle local and remote branches | ||
|
r43347 | remoteprefix = self.ui.config(b'convert', b'git.remoteprefix') | ||
|
r25905 | reftypes = [ | ||
# (git prefix, hg prefix) | ||||
|
r43347 | (b'refs/remotes/origin/', remoteprefix + b'/'), | ||
(b'refs/heads/', b''), | ||||
|
r25905 | ] | ||
|
r13756 | |||
|
r32291 | exclude = { | ||
|
r43347 | b'refs/remotes/origin/HEAD', | ||
|
r32291 | } | ||
|
r13756 | |||
|
r25905 | try: | ||
|
r43347 | output, status = self.gitrunlines(b'show-ref') | ||
|
r28660 | for line in output: | ||
|
r25905 | line = line.strip() | ||
rev, name = line.split(None, 1) | ||||
# Process each type of branch | ||||
for gitprefix, hgprefix in reftypes: | ||||
if not name.startswith(gitprefix) or name in exclude: | ||||
|
r13756 | continue | ||
|
r43347 | name = b'%s%s' % (hgprefix, name[len(gitprefix) :]) | ||
|
r13756 | bookmarks[name] = rev | ||
|
r25905 | except Exception: | ||
pass | ||||
|
r13756 | |||
return bookmarks | ||||
|
r19121 | |||
|
r43347 | def checkrevformat(self, revstr, mapname=b'splicemap'): | ||
|
r47856 | """git revision string is a 40 byte hex""" | ||
|
r20373 | self.checkhexformat(revstr, mapname) | ||