| 1 |
# git.py - git support for the convert extension |
| 2 |
# |
| 3 |
# Copyright 2005-2009 Olivia Mackall <[email protected]> and others |
| 4 |
# |
| 5 |
# This software may be used and distributed according to the terms of the |
| 6 |
# GNU General Public License version 2 or any later version. |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
import os |
| 11 |
|
| 12 |
from mercurial.i18n import _ |
| 13 |
from mercurial.node import sha1nodeconstants |
| 14 |
from mercurial import ( |
| 15 |
config, |
| 16 |
error, |
| 17 |
pycompat, |
| 18 |
util, |
| 19 |
) |
| 20 |
|
| 21 |
from . import common |
| 22 |
|
| 23 |
|
| 24 |
class submodule: |
| 25 |
def __init__(self, path, node, url): |
| 26 |
self.path = path |
| 27 |
self.node = node |
| 28 |
self.url = url |
| 29 |
|
| 30 |
def hgsub(self): |
| 31 |
return b"%s = [git]%s" % (self.path, self.url) |
| 32 |
|
| 33 |
def hgsubstate(self): |
| 34 |
return b"%s %s" % (self.node, self.path) |
| 35 |
|
| 36 |
|
| 37 |
# Keys in extra fields that should not be copied if the user requests. |
| 38 |
bannedextrakeys = { |
| 39 |
# Git commit object built-ins. |
| 40 |
b'tree', |
| 41 |
b'parent', |
| 42 |
b'author', |
| 43 |
b'committer', |
| 44 |
# Mercurial built-ins. |
| 45 |
b'branch', |
| 46 |
b'close', |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
class convert_git(common.converter_source, common.commandline): |
| 51 |
# Windows does not support GIT_DIR= construct while other systems |
| 52 |
# cannot remove environment variable. Just assume none have |
| 53 |
# both issues. |
| 54 |
|
| 55 |
def _gitcmd(self, cmd, *args, **kwargs): |
| 56 |
return cmd(b'--git-dir=%s' % self.path, *args, **kwargs) |
| 57 |
|
| 58 |
def gitrun0(self, *args, **kwargs): |
| 59 |
return self._gitcmd(self.run0, *args, **kwargs) |
| 60 |
|
| 61 |
def gitrun(self, *args, **kwargs): |
| 62 |
return self._gitcmd(self.run, *args, **kwargs) |
| 63 |
|
| 64 |
def gitrunlines0(self, *args, **kwargs): |
| 65 |
return self._gitcmd(self.runlines0, *args, **kwargs) |
| 66 |
|
| 67 |
def gitrunlines(self, *args, **kwargs): |
| 68 |
return self._gitcmd(self.runlines, *args, **kwargs) |
| 69 |
|
| 70 |
def gitpipe(self, *args, **kwargs): |
| 71 |
return self._gitcmd(self._run3, *args, **kwargs) |
| 72 |
|
| 73 |
def __init__(self, ui, repotype, path, revs=None): |
| 74 |
super().__init__(ui, repotype, path, revs=revs) |
| 75 |
common.commandline.__init__(self, ui, b'git') |
| 76 |
|
| 77 |
# Pass an absolute path to git to prevent from ever being interpreted |
| 78 |
# as a URL |
| 79 |
path = util.abspath(path) |
| 80 |
|
| 81 |
if os.path.isdir(path + b"/.git"): |
| 82 |
path += b"/.git" |
| 83 |
if not os.path.exists(path + b"/objects"): |
| 84 |
raise common.NoRepo( |
| 85 |
_(b"%s does not look like a Git repository") % path |
| 86 |
) |
| 87 |
|
| 88 |
# The default value (50) is based on the default for 'git diff'. |
| 89 |
similarity = ui.configint(b'convert', b'git.similarity') |
| 90 |
if similarity < 0 or similarity > 100: |
| 91 |
raise error.Abort(_(b'similarity must be between 0 and 100')) |
| 92 |
if similarity > 0: |
| 93 |
self.simopt = [b'-C%d%%' % similarity] |
| 94 |
findcopiesharder = ui.configbool( |
| 95 |
b'convert', b'git.findcopiesharder' |
| 96 |
) |
| 97 |
if findcopiesharder: |
| 98 |
self.simopt.append(b'--find-copies-harder') |
| 99 |
|
| 100 |
renamelimit = ui.configint(b'convert', b'git.renamelimit') |
| 101 |
self.simopt.append(b'-l%d' % renamelimit) |
| 102 |
else: |
| 103 |
self.simopt = [] |
| 104 |
|
| •105 |
common.checktool(b'git', b'git', debname=b'git') |
| 106 |
|
| 107 |
self.path = path |
| 108 |
self.submodules = [] |
| 109 |
|
| 110 |
self.catfilepipe = self.gitpipe(b'cat-file', b'--batch') |
| 111 |
|
| 112 |
self.copyextrakeys = self.ui.configlist(b'convert', b'git.extrakeys') |
| 113 |
banned = set(self.copyextrakeys) & bannedextrakeys |
| 114 |
if banned: |
| 115 |
raise error.Abort( |
| 116 |
_(b'copying of extra key is forbidden: %s') |
| 117 |
% _(b', ').join(sorted(banned)) |
| 118 |
) |
| 119 |
|
| 120 |
committeractions = self.ui.configlist( |
| 121 |
b'convert', b'git.committeractions' |
| 122 |
) |
| 123 |
|
| 124 |
messagedifferent = None |
| 125 |
messagealways = None |
| 126 |
for a in committeractions: |
| 127 |
if a.startswith((b'messagedifferent', b'messagealways')): |
| 128 |
k = a |
| 129 |
v = None |
| 130 |
if b'=' in a: |
| 131 |
k, v = a.split(b'=', 1) |
| 132 |
|
| 133 |
if k == b'messagedifferent': |
| 134 |
messagedifferent = v or b'committer:' |
| 135 |
elif k == b'messagealways': |
| 136 |
messagealways = v or b'committer:' |
| 137 |
|
| 138 |
if messagedifferent and messagealways: |
| 139 |
raise error.Abort( |
| 140 |
_( |
| 141 |
b'committeractions cannot define both ' |
| 142 |
b'messagedifferent and messagealways' |
| 143 |
) |
| 144 |
) |
| 145 |
|
| 146 |
dropcommitter = b'dropcommitter' in committeractions |
| 147 |
replaceauthor = b'replaceauthor' in committeractions |
| 148 |
|
| 149 |
if dropcommitter and replaceauthor: |
| 150 |
raise error.Abort( |
| 151 |
_( |
| 152 |
b'committeractions cannot define both ' |
| 153 |
b'dropcommitter and replaceauthor' |
| 154 |
) |
| 155 |
) |
| 156 |
|
| 157 |
if dropcommitter and messagealways: |
| 158 |
raise error.Abort( |
| 159 |
_( |
| 160 |
b'committeractions cannot define both ' |
| 161 |
b'dropcommitter and messagealways' |
| 162 |
) |
| 163 |
) |
| 164 |
|
| 165 |
if not messagedifferent and not messagealways: |
| 166 |
messagedifferent = b'committer:' |
| 167 |
|
| 168 |
self.committeractions = { |
| 169 |
b'dropcommitter': dropcommitter, |
| 170 |
b'replaceauthor': replaceauthor, |
| 171 |
b'messagedifferent': messagedifferent, |
| 172 |
b'messagealways': messagealways, |
| 173 |
} |
| 174 |
|
| 175 |
def after(self): |
| 176 |
for f in self.catfilepipe: |
| 177 |
f.close() |
| 178 |
|
| 179 |
def getheads(self): |
| 180 |
if not self.revs: |
| 181 |
output, status = self.gitrun( |
| 182 |
b'rev-parse', b'--branches', b'--remotes' |
| 183 |
) |
| 184 |
heads = output.splitlines() |
| 185 |
if status: |
| 186 |
raise error.Abort(_(b'cannot retrieve git heads')) |
| 187 |
else: |
| 188 |
heads = [] |
| 189 |
for rev in self.revs: |
| 190 |
rawhead, ret = self.gitrun(b'rev-parse', b'--verify', rev) |
| 191 |
heads.append(rawhead[:-1]) |
| 192 |
if ret: |
| 193 |
raise error.Abort(_(b'cannot retrieve git head "%s"') % rev) |
| 194 |
return heads |
| 195 |
|
| 196 |
def catfile(self, rev, ftype): |
| 197 |
if rev == sha1nodeconstants.nullhex: |
| 198 |
raise OSError |
| 199 |
self.catfilepipe[0].write(rev + b'\n') |
| 200 |
self.catfilepipe[0].flush() |
| 201 |
info = self.catfilepipe[1].readline().split() |
| 202 |
if info[1] != ftype: |
| 203 |
raise error.Abort( |
| 204 |
_(b'cannot read %r object at %s') |
| 205 |
% (pycompat.bytestr(ftype), rev) |
| 206 |
) |
| 207 |
size = int(info[2]) |
| 208 |
data = self.catfilepipe[1].read(size) |
| 209 |
if len(data) < size: |
| 210 |
raise error.Abort( |
| 211 |
_(b'cannot read %r object at %s: unexpected size') |
| 212 |
% (ftype, rev) |
| 213 |
) |
| 214 |
# read the trailing newline |
| 215 |
self.catfilepipe[1].read(1) |
| 216 |
return data |
| 217 |
|
| 218 |
def getfile(self, name, rev): |
| 219 |
if rev == sha1nodeconstants.nullhex: |
| 220 |
return None, None |
| 221 |
if name == b'.hgsub': |
| 222 |
data = b'\n'.join([m.hgsub() for m in self.submoditer()]) |
| 223 |
mode = b'' |
| 224 |
elif name == b'.hgsubstate': |
| 225 |
data = b'\n'.join([m.hgsubstate() for m in self.submoditer()]) |
| 226 |
mode = b'' |
| 227 |
else: |
| 228 |
data = self.catfile(rev, b"blob") |
| 229 |
mode = self.modecache[(name, rev)] |
| 230 |
return data, mode |
| 231 |
|
| 232 |
def submoditer(self): |
| 233 |
null = sha1nodeconstants.nullhex |
| 234 |
for m in sorted(self.submodules, key=lambda p: p.path): |
| 235 |
if m.node != null: |
| 236 |
yield m |
| 237 |
|
| 238 |
def parsegitmodules(self, content): |
| 239 |
"""Parse the formatted .gitmodules file, example file format: |
| 240 |
[submodule "sub"]\n |
| 241 |
\tpath = sub\n |
| 242 |
\turl = git://giturl\n |
| 243 |
""" |
| 244 |
self.submodules = [] |
| 245 |
c = config.config() |
| 246 |
# Each item in .gitmodules starts with whitespace that cant be parsed |
| 247 |
c.parse( |
| 248 |
b'.gitmodules', |
| 249 |
b'\n'.join(line.strip() for line in content.split(b'\n')), |
| 250 |
) |
| 251 |
for sec in c.sections(): |
| 252 |
# turn the config object into a real dict |
| 253 |
s = dict(c.items(sec)) |
| 254 |
if b'url' in s and b'path' in s: |
| 255 |
self.submodules.append(submodule(s[b'path'], b'', s[b'url'])) |
| 256 |
|
| 257 |
def retrievegitmodules(self, version): |
| 258 |
modules, ret = self.gitrun( |
| 259 |
b'show', b'%s:%s' % (version, b'.gitmodules') |
| 260 |
) |
| 261 |
if ret: |
| 262 |
# This can happen if a file is in the repo that has permissions |
| 263 |
# 160000, but there is no .gitmodules file. |
| 264 |
self.ui.warn( |
| 265 |
_(b"warning: cannot read submodules config file in %s\n") |
| 266 |
% version |
| 267 |
) |
| 268 |
return |
| 269 |
|
| 270 |
try: |
| 271 |
self.parsegitmodules(modules) |
| 272 |
except error.ParseError: |
| 273 |
self.ui.warn( |
| 274 |
_(b"warning: unable to parse .gitmodules in %s\n") % version |
| 275 |
) |
| 276 |
return |
| 277 |
|
| 278 |
for m in self.submodules: |
| 279 |
node, ret = self.gitrun(b'rev-parse', b'%s:%s' % (version, m.path)) |
| 280 |
if ret: |
| 281 |
continue |
| 282 |
m.node = node.strip() |
| 283 |
|
| 284 |
def getchanges(self, version, full): |
| 285 |
if full: |
| 286 |
raise error.Abort(_(b"convert from git does not support --full")) |
| 287 |
self.modecache = {} |
| 288 |
cmd = ( |
| 289 |
[b'diff-tree', b'-z', b'--root', b'-m', b'-r'] |
| 290 |
+ self.simopt |
| 291 |
+ [version] |
| 292 |
) |
| 293 |
output, status = self.gitrun(*cmd) |
| 294 |
if status: |
| 295 |
raise error.Abort(_(b'cannot read changes in %s') % version) |
| 296 |
changes = [] |
| 297 |
copies = {} |
| 298 |
seen = set() |
| 299 |
entry = None |
| 300 |
subexists = [False] |
| 301 |
subdeleted = [False] |
| 302 |
difftree = output.split(b'\x00') |
| 303 |
lcount = len(difftree) |
| 304 |
i = 0 |
| 305 |
|
| 306 |
skipsubmodules = self.ui.configbool(b'convert', b'git.skipsubmodules') |
| 307 |
|
| 308 |
def add(entry, f, isdest): |
| 309 |
seen.add(f) |
| 310 |
h = entry[3] |
| 311 |
p = entry[1] == b"100755" |
| 312 |
s = entry[1] == b"120000" |
| 313 |
renamesource = not isdest and entry[4][0] == b'R' |
| 314 |
|
| 315 |
if f == b'.gitmodules': |
| 316 |
if skipsubmodules: |
| 317 |
return |
| 318 |
|
| 319 |
subexists[0] = True |
| 320 |
if entry[4] == b'D' or renamesource: |
| 321 |
subdeleted[0] = True |
| 322 |
changes.append((b'.hgsub', sha1nodeconstants.nullhex)) |
| 323 |
else: |
| 324 |
changes.append((b'.hgsub', b'')) |
| 325 |
elif entry[1] == b'160000' or entry[0] == b':160000': |
| 326 |
if not skipsubmodules: |
| 327 |
subexists[0] = True |
| 328 |
else: |
| 329 |
if renamesource: |
| 330 |
h = sha1nodeconstants.nullhex |
| 331 |
self.modecache[(f, h)] = (p and b"x") or (s and b"l") or b"" |
| 332 |
changes.append((f, h)) |
| 333 |
|
| 334 |
while i < lcount: |
| 335 |
l = difftree[i] |
| 336 |
i += 1 |
| 337 |
if not entry: |
| 338 |
if not l.startswith(b':'): |
| 339 |
continue |
| 340 |
entry = tuple(pycompat.bytestr(p) for p in l.split()) |
| 341 |
continue |
| 342 |
f = l |
| 343 |
if entry[4][0] == b'C': |
| 344 |
copysrc = f |
| 345 |
copydest = difftree[i] |
| 346 |
i += 1 |
| 347 |
f = copydest |
| 348 |
copies[copydest] = copysrc |
| 349 |
if f not in seen: |
| 350 |
add(entry, f, False) |
| 351 |
# A file can be copied multiple times, or modified and copied |
| 352 |
# simultaneously. So f can be repeated even if fdest isn't. |
| 353 |
if entry[4][0] == b'R': |
| 354 |
# rename: next line is the destination |
| 355 |
fdest = difftree[i] |
| 356 |
i += 1 |
| 357 |
if fdest not in seen: |
| 358 |
add(entry, fdest, True) |
| 359 |
# .gitmodules isn't imported at all, so it being copied to |
| 360 |
# and fro doesn't really make sense |
| 361 |
if f != b'.gitmodules' and fdest != b'.gitmodules': |
| 362 |
copies[fdest] = f |
| 363 |
entry = None |
| 364 |
|
| 365 |
if subexists[0]: |
| 366 |
if subdeleted[0]: |
| 367 |
changes.append((b'.hgsubstate', sha1nodeconstants.nullhex)) |
| 368 |
else: |
| 369 |
self.retrievegitmodules(version) |
| 370 |
changes.append((b'.hgsubstate', b'')) |
| 371 |
return (changes, copies, set()) |
| 372 |
|
| 373 |
def getcommit(self, version): |
| 374 |
c = self.catfile(version, b"commit") # read the commit hash |
| 375 |
end = c.find(b"\n\n") |
| 376 |
message = c[end + 2 :] |
| 377 |
message = self.recode(message) |
| 378 |
l = c[:end].splitlines() |
| 379 |
parents = [] |
| 380 |
author = committer = None |
| 381 |
extra = {} |
| 382 |
for e in l[1:]: |
| 383 |
n, v = e.split(b" ", 1) |
| 384 |
if n == b"author": |
| 385 |
p = v.split() |
| 386 |
tm, tz = p[-2:] |
| 387 |
author = b" ".join(p[:-2]) |
| 388 |
if author[0] == b"<": |
| 389 |
author = author[1:-1] |
| 390 |
author = self.recode(author) |
| 391 |
if n == b"committer": |
| 392 |
p = v.split() |
| 393 |
tm, tz = p[-2:] |
| 394 |
committer = b" ".join(p[:-2]) |
| 395 |
if committer[0] == b"<": |
| 396 |
committer = committer[1:-1] |
| 397 |
committer = self.recode(committer) |
| 398 |
if n == b"parent": |
| 399 |
parents.append(v) |
| 400 |
if n in self.copyextrakeys: |
| 401 |
extra[n] = v |
| 402 |
|
| 403 |
if self.committeractions[b'dropcommitter']: |
| 404 |
committer = None |
| 405 |
elif self.committeractions[b'replaceauthor']: |
| 406 |
author = committer |
| 407 |
|
| 408 |
if committer: |
| 409 |
messagealways = self.committeractions[b'messagealways'] |
| 410 |
messagedifferent = self.committeractions[b'messagedifferent'] |
| 411 |
if messagealways: |
| 412 |
message += b'\n%s %s\n' % (messagealways, committer) |
| 413 |
elif messagedifferent and author != committer: |
| 414 |
message += b'\n%s %s\n' % (messagedifferent, committer) |
| 415 |
|
| 416 |
tzs, tzh, tzm = tz[-5:-4] + b"1", tz[-4:-2], tz[-2:] |
| 417 |
tz = -int(tzs) * (int(tzh) * 3600 + int(tzm) * 60) |
| 418 |
date = tm + b" " + (b"%d" % tz) |
| 419 |
saverev = self.ui.configbool(b'convert', b'git.saverev') |
| 420 |
|
| 421 |
c = common.commit( |
| 422 |
parents=parents, |
| 423 |
date=date, |
| 424 |
author=author, |
| 425 |
desc=message, |
| 426 |
rev=version, |
| 427 |
extra=extra, |
| 428 |
saverev=saverev, |
| 429 |
) |
| 430 |
return c |
| 431 |
|
| 432 |
def numcommits(self): |
| 433 |
output, ret = self.gitrunlines(b'rev-list', b'--all') |
| 434 |
if ret: |
| 435 |
raise error.Abort( |
| 436 |
_(b'cannot retrieve number of commits in %s') % self.path |
| 437 |
) |
| 438 |
return len(output) |
| 439 |
|
| 440 |
def gettags(self): |
| 441 |
tags = {} |
| 442 |
alltags = {} |
| 443 |
output, status = self.gitrunlines(b'ls-remote', b'--tags', self.path) |
| 444 |
|
| 445 |
if status: |
| 446 |
raise error.Abort(_(b'cannot read tags from %s') % self.path) |
| 447 |
prefix = b'refs/tags/' |
| 448 |
|
| 449 |
# Build complete list of tags, both annotated and bare ones |
| 450 |
for line in output: |
| 451 |
line = line.strip() |
| 452 |
if line.startswith(b"error:") or line.startswith(b"fatal:"): |
| 453 |
raise error.Abort(_(b'cannot read tags from %s') % self.path) |
| 454 |
node, tag = line.split(None, 1) |
| 455 |
if not tag.startswith(prefix): |
| 456 |
continue |
| 457 |
alltags[tag[len(prefix) :]] = node |
| 458 |
|
| 459 |
# Filter out tag objects for annotated tag refs |
| 460 |
for tag in alltags: |
| 461 |
if tag.endswith(b'^{}'): |
| 462 |
tags[tag[:-3]] = alltags[tag] |
| 463 |
else: |
| 464 |
if tag + b'^{}' in alltags: |
| 465 |
continue |
| 466 |
else: |
| 467 |
tags[tag] = alltags[tag] |
| 468 |
|
| 469 |
return tags |
| 470 |
|
| 471 |
def getchangedfiles(self, version, i): |
| 472 |
changes = [] |
| 473 |
if i is None: |
| 474 |
output, status = self.gitrunlines( |
| 475 |
b'diff-tree', b'--root', b'-m', b'-r', version |
| 476 |
) |
| 477 |
if status: |
| 478 |
raise error.Abort(_(b'cannot read changes in %s') % version) |
| 479 |
for l in output: |
| 480 |
if b"\t" not in l: |
| 481 |
continue |
| 482 |
m, f = l[:-1].split(b"\t") |
| 483 |
changes.append(f) |
| 484 |
else: |
| 485 |
output, status = self.gitrunlines( |
| 486 |
b'diff-tree', |
| 487 |
b'--name-only', |
| 488 |
b'--root', |
| 489 |
b'-r', |
| 490 |
version, |
| 491 |
b'%s^%d' % (version, i + 1), |
| 492 |
b'--', |
| 493 |
) |
| 494 |
if status: |
| 495 |
raise error.Abort(_(b'cannot read changes in %s') % version) |
| 496 |
changes = [f.rstrip(b'\n') for f in output] |
| 497 |
|
| 498 |
return changes |
| 499 |
|
| 500 |
def getbookmarks(self): |
| 501 |
bookmarks = {} |
| 502 |
|
| 503 |
# Handle local and remote branches |
| 504 |
remoteprefix = self.ui.config(b'convert', b'git.remoteprefix') |
| 505 |
reftypes = [ |
| 506 |
# (git prefix, hg prefix) |
| 507 |
(b'refs/remotes/origin/', remoteprefix + b'/'), |
| 508 |
(b'refs/heads/', b''), |
| 509 |
] |
| 510 |
|
| 511 |
exclude = { |
| 512 |
b'refs/remotes/origin/HEAD', |
| 513 |
} |
| 514 |
|
| 515 |
try: |
| 516 |
output, status = self.gitrunlines(b'show-ref') |
| 517 |
for line in output: |
| 518 |
line = line.strip() |
| 519 |
rev, name = line.split(None, 1) |
| 520 |
# Process each type of branch |
| 521 |
for gitprefix, hgprefix in reftypes: |
| 522 |
if not name.startswith(gitprefix) or name in exclude: |
| 523 |
continue |
| 524 |
name = b'%s%s' % (hgprefix, name[len(gitprefix) :]) |
| 525 |
bookmarks[name] = rev |
| 526 |
except Exception: |
| 527 |
pass |
| 528 |
|
| 529 |
return bookmarks |
| 530 |
|
| 531 |
def checkrevformat(self, revstr, mapname=b'splicemap'): |
| 532 |
"""git revision string is a 40 byte hex""" |
| 533 |
self.checkhexformat(revstr, mapname) |