git.py
532 lines
| 17.3 KiB
| text/x-python
|
PythonLexer
Martin Geisler
|
r8250 | # git.py - git support for the convert extension | ||
# | ||||
Raphaël Gomès
|
r47575 | # Copyright 2005-2009 Olivia Mackall <olivia@selenic.com> and others | ||
Martin Geisler
|
r8250 | # | ||
# 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. | ||
timeless
|
r28365 | from __future__ import absolute_import | ||
Brendan Cully
|
r4536 | |||
import os | ||||
Yuya Nishihara
|
r29205 | |||
from mercurial.i18n import _ | ||||
Joerg Sonnenberger
|
r47771 | from mercurial.node import sha1nodeconstants | ||
timeless
|
r28365 | from mercurial import ( | ||
config, | ||||
error, | ||||
Augie Fackler
|
r41619 | pycompat, | ||
r48433 | util, | |||
timeless
|
r28365 | ) | ||
Brendan Cully
|
r4536 | |||
Augie Fackler
|
r43346 | from . import common | ||
Brendan Cully
|
r4536 | |||
YaNan Xu
|
r17929 | class submodule(object): | ||
def __init__(self, path, node, url): | ||||
self.path = path | ||||
self.node = node | ||||
self.url = url | ||||
def hgsub(self): | ||||
Augie Fackler
|
r43347 | return b"%s = [git]%s" % (self.path, self.url) | ||
YaNan Xu
|
r17929 | |||
def hgsubstate(self): | ||||
Augie Fackler
|
r43347 | return b"%s %s" % (self.node, self.path) | ||
YaNan Xu
|
r17929 | |||
Augie Fackler
|
r43346 | |||
Gregory Szorc
|
r30660 | # Keys in extra fields that should not be copied if the user requests. | ||
Martin von Zweigbergk
|
r32291 | bannedextrakeys = { | ||
Gregory Szorc
|
r30660 | # Git commit object built-ins. | ||
Augie Fackler
|
r43347 | b'tree', | ||
b'parent', | ||||
b'author', | ||||
b'committer', | ||||
Gregory Szorc
|
r30660 | # Mercurial built-ins. | ||
Augie Fackler
|
r43347 | b'branch', | ||
b'close', | ||||
Martin von Zweigbergk
|
r32291 | } | ||
Gregory Szorc
|
r30660 | |||
Augie Fackler
|
r43346 | |||
Matt Mackall
|
r28670 | class convert_git(common.converter_source, common.commandline): | ||
Patrick Mezard
|
r5217 | # Windows does not support GIT_DIR= construct while other systems | ||
# cannot remove environment variable. Just assume none have | ||||
# both issues. | ||||
David Schleimer
|
r21630 | |||
Mateusz Kwapich
|
r28659 | def _gitcmd(self, cmd, *args, **kwargs): | ||
Augie Fackler
|
r43347 | return cmd(b'--git-dir=%s' % self.path, *args, **kwargs) | ||
Mateusz Kwapich
|
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) | ||||
Mateusz Kwapich
|
r28662 | def gitpipe(self, *args, **kwargs): | ||
return self._gitcmd(self._run3, *args, **kwargs) | ||||
Matt Harbison
|
r35168 | def __init__(self, ui, repotype, path, revs=None): | ||
super(convert_git, self).__init__(ui, repotype, path, revs=revs) | ||||
Augie Fackler
|
r43347 | common.commandline.__init__(self, ui, b'git') | ||
Durham Goode
|
r25748 | |||
Blake Burkhart
|
r29051 | # Pass an absolute path to git to prevent from ever being interpreted | ||
# as a URL | ||||
r48433 | path = util.abspath(path) | |||
Blake Burkhart
|
r29051 | |||
Augie Fackler
|
r43347 | if os.path.isdir(path + b"/.git"): | ||
path += b"/.git" | ||||
if not os.path.exists(path + b"/objects"): | ||||
Augie Fackler
|
r43346 | raise common.NoRepo( | ||
Augie Fackler
|
r43347 | _(b"%s does not look like a Git repository") % path | ||
Augie Fackler
|
r43346 | ) | ||
Patrick Mezard
|
r5497 | |||
Siddharth Agarwal
|
r22512 | # The default value (50) is based on the default for 'git diff'. | ||
Augie Fackler
|
r43347 | similarity = ui.configint(b'convert', b'git.similarity') | ||
Siddharth Agarwal
|
r22470 | if similarity < 0 or similarity > 100: | ||
Augie Fackler
|
r43347 | raise error.Abort(_(b'similarity must be between 0 and 100')) | ||
Siddharth Agarwal
|
r22470 | if similarity > 0: | ||
Augie Fackler
|
r43347 | self.simopt = [b'-C%d%%' % similarity] | ||
findcopiesharder = ui.configbool( | ||||
b'convert', b'git.findcopiesharder' | ||||
) | ||||
Siddharth Agarwal
|
r22471 | if findcopiesharder: | ||
Augie Fackler
|
r43347 | self.simopt.append(b'--find-copies-harder') | ||
Gregory Szorc
|
r30646 | |||
Augie Fackler
|
r43347 | renamelimit = ui.configint(b'convert', b'git.renamelimit') | ||
self.simopt.append(b'-l%d' % renamelimit) | ||||
Siddharth Agarwal
|
r22470 | else: | ||
Mateusz Kwapich
|
r28660 | self.simopt = [] | ||
Siddharth Agarwal
|
r22470 | |||
Augie Fackler
|
r43347 | common.checktool(b'git', b'git') | ||
Patrick Mezard
|
r5497 | |||
Brendan Cully
|
r4536 | self.path = path | ||
YaNan Xu
|
r17929 | self.submodules = [] | ||
Brendan Cully
|
r4536 | |||
Augie Fackler
|
r43347 | self.catfilepipe = self.gitpipe(b'cat-file', b'--batch') | ||
David Schleimer
|
r21630 | |||
Augie Fackler
|
r43347 | self.copyextrakeys = self.ui.configlist(b'convert', b'git.extrakeys') | ||
Gregory Szorc
|
r30660 | banned = set(self.copyextrakeys) & bannedextrakeys | ||
if banned: | ||||
Augie Fackler
|
r43346 | raise error.Abort( | ||
Augie Fackler
|
r43347 | _(b'copying of extra key is forbidden: %s') | ||
% _(b', ').join(sorted(banned)) | ||||
Augie Fackler
|
r43346 | ) | ||
Gregory Szorc
|
r30660 | |||
Augie Fackler
|
r43347 | committeractions = self.ui.configlist( | ||
b'convert', b'git.committeractions' | ||||
) | ||||
Gregory Szorc
|
r30813 | |||
messagedifferent = None | ||||
messagealways = None | ||||
for a in committeractions: | ||||
Augie Fackler
|
r43347 | if a.startswith((b'messagedifferent', b'messagealways')): | ||
Gregory Szorc
|
r30813 | k = a | ||
v = None | ||||
Augie Fackler
|
r43347 | if b'=' in a: | ||
k, v = a.split(b'=', 1) | ||||
Gregory Szorc
|
r30813 | |||
Augie Fackler
|
r43347 | if k == b'messagedifferent': | ||
messagedifferent = v or b'committer:' | ||||
elif k == b'messagealways': | ||||
messagealways = v or b'committer:' | ||||
Gregory Szorc
|
r30813 | |||
if messagedifferent and messagealways: | ||||
Augie Fackler
|
r43346 | raise error.Abort( | ||
_( | ||||
Augie Fackler
|
r43347 | b'committeractions cannot define both ' | ||
b'messagedifferent and messagealways' | ||||
Augie Fackler
|
r43346 | ) | ||
) | ||||
Gregory Szorc
|
r30813 | |||
Augie Fackler
|
r43347 | dropcommitter = b'dropcommitter' in committeractions | ||
replaceauthor = b'replaceauthor' in committeractions | ||||
Gregory Szorc
|
r30813 | |||
Gregory Szorc
|
r30815 | if dropcommitter and replaceauthor: | ||
Augie Fackler
|
r43346 | raise error.Abort( | ||
_( | ||||
Augie Fackler
|
r43347 | b'committeractions cannot define both ' | ||
b'dropcommitter and replaceauthor' | ||||
Augie Fackler
|
r43346 | ) | ||
) | ||||
Gregory Szorc
|
r30813 | |||
if dropcommitter and messagealways: | ||||
Augie Fackler
|
r43346 | raise error.Abort( | ||
_( | ||||
Augie Fackler
|
r43347 | b'committeractions cannot define both ' | ||
b'dropcommitter and messagealways' | ||||
Augie Fackler
|
r43346 | ) | ||
) | ||||
Gregory Szorc
|
r30813 | |||
if not messagedifferent and not messagealways: | ||||
Augie Fackler
|
r43347 | messagedifferent = b'committer:' | ||
Gregory Szorc
|
r30813 | |||
self.committeractions = { | ||||
Augie Fackler
|
r43347 | b'dropcommitter': dropcommitter, | ||
b'replaceauthor': replaceauthor, | ||||
b'messagedifferent': messagedifferent, | ||||
b'messagealways': messagealways, | ||||
Gregory Szorc
|
r30813 | } | ||
David Schleimer
|
r21630 | def after(self): | ||
for f in self.catfilepipe: | ||||
f.close() | ||||
Brendan Cully
|
r4536 | def getheads(self): | ||
Durham Goode
|
r25748 | if not self.revs: | ||
Augie Fackler
|
r43347 | output, status = self.gitrun( | ||
b'rev-parse', b'--branches', b'--remotes' | ||||
) | ||||
Mateusz Kwapich
|
r28660 | heads = output.splitlines() | ||
if status: | ||||
Augie Fackler
|
r43347 | raise error.Abort(_(b'cannot retrieve git heads')) | ||
Brendan Cully
|
r4768 | else: | ||
Durham Goode
|
r25749 | heads = [] | ||
for rev in self.revs: | ||||
Augie Fackler
|
r43347 | rawhead, ret = self.gitrun(b'rev-parse', b'--verify', rev) | ||
Durham Goode
|
r25749 | heads.append(rawhead[:-1]) | ||
if ret: | ||||
Augie Fackler
|
r43347 | raise error.Abort(_(b'cannot retrieve git head "%s"') % rev) | ||
Patrick Mezard
|
r10986 | return heads | ||
Brendan Cully
|
r4536 | |||
Pulkit Goyal
|
r36350 | def catfile(self, rev, ftype): | ||
Joerg Sonnenberger
|
r47771 | if rev == sha1nodeconstants.nullhex: | ||
Brodie Rao
|
r16687 | raise IOError | ||
Augie Fackler
|
r43347 | self.catfilepipe[0].write(rev + b'\n') | ||
David Schleimer
|
r21630 | self.catfilepipe[0].flush() | ||
info = self.catfilepipe[1].readline().split() | ||||
Pulkit Goyal
|
r36350 | if info[1] != ftype: | ||
Augie Fackler
|
r43346 | raise error.Abort( | ||
Augie Fackler
|
r43347 | _(b'cannot read %r object at %s') | ||
Augie Fackler
|
r43346 | % (pycompat.bytestr(ftype), rev) | ||
) | ||||
David Schleimer
|
r21630 | size = int(info[2]) | ||
data = self.catfilepipe[1].read(size) | ||||
if len(data) < size: | ||||
Augie Fackler
|
r43346 | raise error.Abort( | ||
Augie Fackler
|
r43347 | _(b'cannot read %r object at %s: unexpected size') | ||
% (ftype, rev) | ||||
Augie Fackler
|
r43346 | ) | ||
David Schleimer
|
r21630 | # read the trailing newline | ||
self.catfilepipe[1].read(1) | ||||
Patrick Mezard
|
r10986 | return data | ||
Brendan Cully
|
r4536 | |||
def getfile(self, name, rev): | ||||
Joerg Sonnenberger
|
r47771 | if rev == sha1nodeconstants.nullhex: | ||
Mads Kiilerich
|
r22296 | return None, None | ||
Augie Fackler
|
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'' | ||||
YaNan Xu
|
r17929 | else: | ||
Augie Fackler
|
r43347 | data = self.catfile(rev, b"blob") | ||
YaNan Xu
|
r17929 | mode = self.modecache[(name, rev)] | ||
Patrick Mezard
|
r11134 | return data, mode | ||
Brendan Cully
|
r4536 | |||
YaNan Xu
|
r17929 | def submoditer(self): | ||
Joerg Sonnenberger
|
r47771 | null = sha1nodeconstants.nullhex | ||
YaNan Xu
|
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() | ||||
Durham Goode
|
r25698 | # Each item in .gitmodules starts with whitespace that cant be parsed | ||
Augie Fackler
|
r43346 | c.parse( | ||
Augie Fackler
|
r43347 | b'.gitmodules', | ||
b'\n'.join(line.strip() for line in content.split(b'\n')), | ||||
Augie Fackler
|
r43346 | ) | ||
YaNan Xu
|
r17929 | for sec in c.sections(): | ||
r47153 | # turn the config object into a real dict | |||
s = dict(c.items(sec)) | ||||
Augie Fackler
|
r43347 | if b'url' in s and b'path' in s: | ||
self.submodules.append(submodule(s[b'path'], b'', s[b'url'])) | ||||
YaNan Xu
|
r17929 | |||
def retrievegitmodules(self, version): | ||||
Augie Fackler
|
r43347 | modules, ret = self.gitrun( | ||
b'show', b'%s:%s' % (version, b'.gitmodules') | ||||
) | ||||
YaNan Xu
|
r17929 | if ret: | ||
Durham Goode
|
r25699 | # This can happen if a file is in the repo that has permissions | ||
# 160000, but there is no .gitmodules file. | ||||
Augie Fackler
|
r43346 | self.ui.warn( | ||
Martin von Zweigbergk
|
r43387 | _(b"warning: cannot read submodules config file in %s\n") | ||
Augie Fackler
|
r43346 | % version | ||
) | ||||
Durham Goode
|
r25699 | return | ||
try: | ||||
self.parsegitmodules(modules) | ||||
except error.ParseError: | ||||
Augie Fackler
|
r43346 | self.ui.warn( | ||
Augie Fackler
|
r43347 | _(b"warning: unable to parse .gitmodules in %s\n") % version | ||
Augie Fackler
|
r43346 | ) | ||
Durham Goode
|
r25699 | return | ||
YaNan Xu
|
r17929 | for m in self.submodules: | ||
Augie Fackler
|
r43347 | node, ret = self.gitrun(b'rev-parse', b'%s:%s' % (version, m.path)) | ||
YaNan Xu
|
r17929 | if ret: | ||
continue | ||||
m.node = node.strip() | ||||
Mads Kiilerich
|
r22300 | def getchanges(self, version, full): | ||
if full: | ||||
Augie Fackler
|
r43347 | raise error.Abort(_(b"convert from git does not support --full")) | ||
Brendan Cully
|
r4536 | self.modecache = {} | ||
Augie Fackler
|
r43346 | cmd = ( | ||
Augie Fackler
|
r43347 | [b'diff-tree', b'-z', b'--root', b'-m', b'-r'] | ||
+ self.simopt | ||||
+ [version] | ||||
Augie Fackler
|
r43346 | ) | ||
Mateusz Kwapich
|
r28660 | output, status = self.gitrun(*cmd) | ||
if status: | ||||
Augie Fackler
|
r43347 | raise error.Abort(_(b'cannot read changes in %s') % version) | ||
Brendan Cully
|
r4536 | changes = [] | ||
Siddharth Agarwal
|
r22470 | copies = {} | ||
Benoit Boissinot
|
r8456 | seen = set() | ||
Patrick Mezard
|
r7242 | entry = None | ||
Siddharth Agarwal
|
r22469 | subexists = [False] | ||
subdeleted = [False] | ||||
Augie Fackler
|
r43347 | difftree = output.split(b'\x00') | ||
Siddharth Agarwal
|
r22467 | lcount = len(difftree) | ||
i = 0 | ||||
Siddharth Agarwal
|
r22469 | |||
Augie Fackler
|
r43347 | skipsubmodules = self.ui.configbool(b'convert', b'git.skipsubmodules') | ||
Augie Fackler
|
r43346 | |||
Siddharth Agarwal
|
r22470 | def add(entry, f, isdest): | ||
Siddharth Agarwal
|
r22469 | seen.add(f) | ||
h = entry[3] | ||||
Augie Fackler
|
r43347 | p = entry[1] == b"100755" | ||
s = entry[1] == b"120000" | ||||
renamesource = not isdest and entry[4][0] == b'R' | ||||
Siddharth Agarwal
|
r22469 | |||
Augie Fackler
|
r43347 | if f == b'.gitmodules': | ||
Durham Goode
|
r26077 | if skipsubmodules: | ||
return | ||||
Siddharth Agarwal
|
r22469 | subexists[0] = True | ||
Augie Fackler
|
r43347 | if entry[4] == b'D' or renamesource: | ||
Siddharth Agarwal
|
r22469 | subdeleted[0] = True | ||
Joerg Sonnenberger
|
r47771 | changes.append((b'.hgsub', sha1nodeconstants.nullhex)) | ||
Siddharth Agarwal
|
r22469 | else: | ||
Augie Fackler
|
r43347 | changes.append((b'.hgsub', b'')) | ||
elif entry[1] == b'160000' or entry[0] == b':160000': | ||||
Durham Goode
|
r26077 | if not skipsubmodules: | ||
subexists[0] = True | ||||
Siddharth Agarwal
|
r22469 | else: | ||
Siddharth Agarwal
|
r22470 | if renamesource: | ||
Joerg Sonnenberger
|
r47771 | h = sha1nodeconstants.nullhex | ||
Augie Fackler
|
r43347 | self.modecache[(f, h)] = (p and b"x") or (s and b"l") or b"" | ||
Siddharth Agarwal
|
r22469 | changes.append((f, h)) | ||
Siddharth Agarwal
|
r22467 | while i < lcount: | ||
l = difftree[i] | ||||
i += 1 | ||||
Patrick Mezard
|
r7242 | if not entry: | ||
Augie Fackler
|
r43347 | if not l.startswith(b':'): | ||
Patrick Mezard
|
r7242 | continue | ||
Augie Fackler
|
r41619 | entry = tuple(pycompat.bytestr(p) for p in l.split()) | ||
Alexis S. L. Carvalho
|
r5335 | continue | ||
Patrick Mezard
|
r7242 | f = l | ||
Augie Fackler
|
r43347 | if entry[4][0] == b'C': | ||
Durham Goode
|
r25997 | copysrc = f | ||
copydest = difftree[i] | ||||
i += 1 | ||||
f = copydest | ||||
copies[copydest] = copysrc | ||||
Patrick Mezard
|
r7242 | if f not in seen: | ||
Siddharth Agarwal
|
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. | ||||
Augie Fackler
|
r43347 | if entry[4][0] == b'R': | ||
Durham Goode
|
r25997 | # rename: next line is the destination | ||
Siddharth Agarwal
|
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 | ||||
Augie Fackler
|
r43347 | if f != b'.gitmodules' and fdest != b'.gitmodules': | ||
Siddharth Agarwal
|
r22470 | copies[fdest] = f | ||
Patrick Mezard
|
r7242 | entry = None | ||
YaNan Xu
|
r17929 | |||
Siddharth Agarwal
|
r22469 | if subexists[0]: | ||
if subdeleted[0]: | ||||
Joerg Sonnenberger
|
r47771 | changes.append((b'.hgsubstate', sha1nodeconstants.nullhex)) | ||
FUJIWARA Katsunori
|
r21868 | else: | ||
self.retrievegitmodules(version) | ||||
Augie Fackler
|
r43347 | changes.append((b'.hgsubstate', b'')) | ||
Mads Kiilerich
|
r24395 | return (changes, copies, set()) | ||
Brendan Cully
|
r4536 | |||
def getcommit(self, version): | ||||
Augie Fackler
|
r43347 | c = self.catfile(version, b"commit") # read the commit hash | ||
end = c.find(b"\n\n") | ||||
Augie Fackler
|
r43346 | message = c[end + 2 :] | ||
Brendan Cully
|
r4759 | message = self.recode(message) | ||
Brendan Cully
|
r4536 | l = c[:end].splitlines() | ||
parents = [] | ||||
Richard Quirk
|
r8271 | author = committer = None | ||
Gregory Szorc
|
r30660 | extra = {} | ||
Brendan Cully
|
r4536 | for e in l[1:]: | ||
Augie Fackler
|
r43347 | n, v = e.split(b" ", 1) | ||
if n == b"author": | ||||
Brendan Cully
|
r4536 | p = v.split() | ||
tm, tz = p[-2:] | ||||
Augie Fackler
|
r43347 | author = b" ".join(p[:-2]) | ||
if author[0] == b"<": | ||||
Boris Feld
|
r35646 | author = author[1:-1] | ||
Brendan Cully
|
r4759 | author = self.recode(author) | ||
Augie Fackler
|
r43347 | if n == b"committer": | ||
Brendan Cully
|
r4536 | p = v.split() | ||
tm, tz = p[-2:] | ||||
Augie Fackler
|
r43347 | committer = b" ".join(p[:-2]) | ||
if committer[0] == b"<": | ||||
Boris Feld
|
r35646 | committer = committer[1:-1] | ||
Brendan Cully
|
r4759 | committer = self.recode(committer) | ||
Augie Fackler
|
r43347 | if n == b"parent": | ||
Matt Mackall
|
r10282 | parents.append(v) | ||
Gregory Szorc
|
r30660 | if n in self.copyextrakeys: | ||
extra[n] = v | ||||
Brendan Cully
|
r4536 | |||
Augie Fackler
|
r43347 | if self.committeractions[b'dropcommitter']: | ||
Gregory Szorc
|
r30813 | committer = None | ||
Augie Fackler
|
r43347 | elif self.committeractions[b'replaceauthor']: | ||
Gregory Szorc
|
r30813 | author = committer | ||
if committer: | ||||
Augie Fackler
|
r43347 | messagealways = self.committeractions[b'messagealways'] | ||
messagedifferent = self.committeractions[b'messagedifferent'] | ||||
Gregory Szorc
|
r30813 | if messagealways: | ||
Augie Fackler
|
r43347 | message += b'\n%s %s\n' % (messagealways, committer) | ||
Gregory Szorc
|
r30813 | elif messagedifferent and author != committer: | ||
Augie Fackler
|
r43347 | message += b'\n%s %s\n' % (messagedifferent, committer) | ||
Gregory Szorc
|
r30813 | |||
Augie Fackler
|
r43347 | tzs, tzh, tzm = tz[-5:-4] + b"1", tz[-4:-2], tz[-2:] | ||
Brendan Cully
|
r4536 | tz = -int(tzs) * (int(tzh) * 3600 + int(tzm)) | ||
Augie Fackler
|
r43347 | date = tm + b" " + (b"%d" % tz) | ||
saverev = self.ui.configbool(b'convert', b'git.saverev') | ||||
Brendan Cully
|
r4536 | |||
Augie Fackler
|
r43346 | c = common.commit( | ||
parents=parents, | ||||
date=date, | ||||
author=author, | ||||
desc=message, | ||||
rev=version, | ||||
extra=extra, | ||||
saverev=saverev, | ||||
) | ||||
Brendan Cully
|
r4536 | return c | ||
Augie Fackler
|
r22413 | def numcommits(self): | ||
Augie Fackler
|
r43347 | output, ret = self.gitrunlines(b'rev-list', b'--all') | ||
Mateusz Kwapich
|
r28660 | if ret: | ||
Augie Fackler
|
r43346 | raise error.Abort( | ||
Augie Fackler
|
r43347 | _(b'cannot retrieve number of commits in %s') % self.path | ||
Augie Fackler
|
r43346 | ) | ||
Mateusz Kwapich
|
r28660 | return len(output) | ||
Augie Fackler
|
r22413 | |||
Brendan Cully
|
r4536 | def gettags(self): | ||
tags = {} | ||||
Edouard Gomez
|
r16259 | alltags = {} | ||
Augie Fackler
|
r43347 | output, status = self.gitrunlines(b'ls-remote', b'--tags', self.path) | ||
Mateusz Kwapich
|
r28660 | |||
if status: | ||||
Augie Fackler
|
r43347 | raise error.Abort(_(b'cannot read tags from %s') % self.path) | ||
prefix = b'refs/tags/' | ||||
Edouard Gomez
|
r16259 | |||
# Build complete list of tags, both annotated and bare ones | ||||
Mateusz Kwapich
|
r28660 | for line in output: | ||
Brendan Cully
|
r4536 | line = line.strip() | ||
Augie Fackler
|
r43347 | if line.startswith(b"error:") or line.startswith(b"fatal:"): | ||
raise error.Abort(_(b'cannot read tags from %s') % self.path) | ||||
Brendan Cully
|
r4536 | node, tag = line.split(None, 1) | ||
if not tag.startswith(prefix): | ||||
continue | ||||
Augie Fackler
|
r43346 | alltags[tag[len(prefix) :]] = node | ||
Brendan Cully
|
r4536 | |||
Edouard Gomez
|
r16259 | # Filter out tag objects for annotated tag refs | ||
for tag in alltags: | ||||
Augie Fackler
|
r43347 | if tag.endswith(b'^{}'): | ||
Edouard Gomez
|
r16259 | tags[tag[:-3]] = alltags[tag] | ||
else: | ||||
Augie Fackler
|
r43347 | if tag + b'^{}' in alltags: | ||
Edouard Gomez
|
r16259 | continue | ||
else: | ||||
tags[tag] = alltags[tag] | ||||
Brendan Cully
|
r4536 | return tags | ||
Alexis S. L. Carvalho
|
r5380 | |||
def getchangedfiles(self, version, i): | ||||
changes = [] | ||||
if i is None: | ||||
Augie Fackler
|
r43346 | output, status = self.gitrunlines( | ||
Augie Fackler
|
r43347 | b'diff-tree', b'--root', b'-m', b'-r', version | ||
Augie Fackler
|
r43346 | ) | ||
Mateusz Kwapich
|
r28660 | if status: | ||
Augie Fackler
|
r43347 | raise error.Abort(_(b'cannot read changes in %s') % version) | ||
Mateusz Kwapich
|
r28660 | for l in output: | ||
Augie Fackler
|
r43347 | if b"\t" not in l: | ||
Alexis S. L. Carvalho
|
r5380 | continue | ||
Augie Fackler
|
r43347 | m, f = l[:-1].split(b"\t") | ||
Alexis S. L. Carvalho
|
r5380 | changes.append(f) | ||
else: | ||||
Augie Fackler
|
r43346 | output, status = self.gitrunlines( | ||
Augie Fackler
|
r43347 | b'diff-tree', | ||
b'--name-only', | ||||
b'--root', | ||||
b'-r', | ||||
Augie Fackler
|
r43346 | version, | ||
Augie Fackler
|
r43347 | b'%s^%d' % (version, i + 1), | ||
b'--', | ||||
Augie Fackler
|
r43346 | ) | ||
Julien Cristau
|
r28816 | if status: | ||
Augie Fackler
|
r43347 | raise error.Abort(_(b'cannot read changes in %s') % version) | ||
changes = [f.rstrip(b'\n') for f in output] | ||||
Alexis S. L. Carvalho
|
r5380 | |||
return changes | ||||
Edouard Gomez
|
r13756 | |||
def getbookmarks(self): | ||||
bookmarks = {} | ||||
Durham Goode
|
r25905 | # Handle local and remote branches | ||
Augie Fackler
|
r43347 | remoteprefix = self.ui.config(b'convert', b'git.remoteprefix') | ||
Durham Goode
|
r25905 | reftypes = [ | ||
# (git prefix, hg prefix) | ||||
Augie Fackler
|
r43347 | (b'refs/remotes/origin/', remoteprefix + b'/'), | ||
(b'refs/heads/', b''), | ||||
Durham Goode
|
r25905 | ] | ||
Edouard Gomez
|
r13756 | |||
Martin von Zweigbergk
|
r32291 | exclude = { | ||
Augie Fackler
|
r43347 | b'refs/remotes/origin/HEAD', | ||
Martin von Zweigbergk
|
r32291 | } | ||
Edouard Gomez
|
r13756 | |||
Durham Goode
|
r25905 | try: | ||
Augie Fackler
|
r43347 | output, status = self.gitrunlines(b'show-ref') | ||
Mateusz Kwapich
|
r28660 | for line in output: | ||
Durham Goode
|
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: | ||||
Edouard Gomez
|
r13756 | continue | ||
Augie Fackler
|
r43347 | name = b'%s%s' % (hgprefix, name[len(gitprefix) :]) | ||
Edouard Gomez
|
r13756 | bookmarks[name] = rev | ||
Durham Goode
|
r25905 | except Exception: | ||
pass | ||||
Edouard Gomez
|
r13756 | |||
return bookmarks | ||||
Ben Goswami
|
r19121 | |||
Augie Fackler
|
r43347 | def checkrevformat(self, revstr, mapname=b'splicemap'): | ||
Kyle Lippincott
|
r47856 | """git revision string is a 40 byte hex""" | ||
Sean Farley
|
r20373 | self.checkhexformat(revstr, mapname) | ||