2009-11-29 18:29:18 +03:00
|
|
|
import os, math, urllib, re
|
2009-07-03 00:58:51 +04:00
|
|
|
|
2011-05-18 03:26:13 +04:00
|
|
|
from dulwich.errors import HangupException, GitProtocolError
|
2009-07-03 00:58:51 +04:00
|
|
|
from dulwich.index import commit_tree
|
2009-08-04 15:32:01 +04:00
|
|
|
from dulwich.objects import Blob, Commit, Tag, Tree, parse_timezone
|
2009-07-25 00:23:35 +04:00
|
|
|
from dulwich.pack import create_delta, apply_delta
|
2009-04-24 02:26:10 +04:00
|
|
|
from dulwich.repo import Repo
|
2010-02-25 06:08:19 +03:00
|
|
|
from dulwich import client
|
2009-07-03 00:58:51 +04:00
|
|
|
|
2011-02-25 01:51:40 +03:00
|
|
|
try:
|
|
|
|
from mercurial import bookmarks
|
|
|
|
bookmarks.update
|
2011-03-24 02:13:59 +03:00
|
|
|
from mercurial import commands
|
2011-02-25 01:51:40 +03:00
|
|
|
except ImportError:
|
|
|
|
from hgext import bookmarks
|
2011-10-06 00:44:29 +04:00
|
|
|
try:
|
|
|
|
from mercurial.error import RepoError
|
|
|
|
except ImportError:
|
|
|
|
from mercurial.repo import RepoError
|
|
|
|
|
2009-04-24 02:26:10 +04:00
|
|
|
from mercurial.i18n import _
|
2009-06-19 19:53:39 +04:00
|
|
|
from mercurial.node import hex, bin, nullid
|
2009-07-23 12:28:37 +04:00
|
|
|
from mercurial import context, util as hgutil
|
2010-03-28 06:37:58 +04:00
|
|
|
from mercurial import error
|
2009-07-23 12:28:37 +04:00
|
|
|
|
2010-12-23 01:57:26 +03:00
|
|
|
import _ssh
|
2010-04-30 19:35:13 +04:00
|
|
|
import util
|
2011-05-24 22:16:45 +04:00
|
|
|
from overlay import overlayrepo
|
2009-04-28 03:15:48 +04:00
|
|
|
|
2011-06-16 10:40:12 +04:00
|
|
|
class GitProgress(object):
|
|
|
|
"""convert git server progress strings into mercurial progress"""
|
|
|
|
def __init__(self, ui):
|
|
|
|
self.ui = ui
|
|
|
|
|
|
|
|
self.lasttopic = None
|
|
|
|
self.msgbuf = ''
|
|
|
|
|
|
|
|
def progress(self, msg):
|
|
|
|
# 'Counting objects: 33640, done.\n'
|
|
|
|
# 'Compressing objects: 0% (1/9955) \r
|
|
|
|
msgs = re.split('[\r\n]', self.msgbuf + msg)
|
|
|
|
self.msgbuf = msgs.pop()
|
|
|
|
|
|
|
|
for msg in msgs:
|
|
|
|
td = msg.split(':', 1)
|
|
|
|
data = td.pop()
|
|
|
|
if not td:
|
|
|
|
self.flush(data)
|
|
|
|
continue
|
|
|
|
topic = td[0]
|
|
|
|
|
|
|
|
m = re.search('\((\d+)/(\d+)\)', data)
|
|
|
|
if m:
|
|
|
|
if self.lasttopic and self.lasttopic != topic:
|
|
|
|
self.flush()
|
|
|
|
self.lasttopic = topic
|
|
|
|
|
|
|
|
pos, total = map(int, m.group(1, 2))
|
2011-06-18 00:01:31 +04:00
|
|
|
util.progress(self.ui, topic, pos, total=total)
|
2011-06-16 10:40:12 +04:00
|
|
|
else:
|
|
|
|
self.flush(msg)
|
|
|
|
|
|
|
|
def flush(self, msg=None):
|
|
|
|
if self.lasttopic:
|
2011-06-18 00:01:31 +04:00
|
|
|
util.progress(self.ui, self.lasttopic, None)
|
2011-06-16 10:40:12 +04:00
|
|
|
self.lasttopic = None
|
|
|
|
if msg:
|
|
|
|
self.ui.note(msg + '\n')
|
|
|
|
|
2009-04-24 02:26:10 +04:00
|
|
|
class GitHandler(object):
|
2010-03-26 04:24:00 +03:00
|
|
|
mapfile = 'git-mapfile'
|
|
|
|
tagsfile = 'git-tags'
|
2009-04-27 05:27:47 +04:00
|
|
|
|
2009-04-24 02:26:10 +04:00
|
|
|
def __init__(self, dest_repo, ui):
|
|
|
|
self.repo = dest_repo
|
|
|
|
self.ui = ui
|
2009-05-14 22:32:54 +04:00
|
|
|
|
2010-05-17 16:04:02 +04:00
|
|
|
if ui.configbool('git', 'intree'):
|
2009-05-14 22:32:54 +04:00
|
|
|
self.gitdir = self.repo.wjoin('.git')
|
|
|
|
else:
|
|
|
|
self.gitdir = self.repo.join('git')
|
|
|
|
|
2012-02-23 22:49:07 +04:00
|
|
|
self.init_author_file()
|
|
|
|
|
2009-06-16 17:44:19 +04:00
|
|
|
self.paths = ui.configitems('paths')
|
2009-05-25 06:20:44 +04:00
|
|
|
|
2011-12-19 03:54:16 +04:00
|
|
|
self.branch_bookmark_suffix = ui.config('git', 'branch_bookmark_suffix')
|
|
|
|
|
2009-04-25 01:05:50 +04:00
|
|
|
self.load_map()
|
2009-06-19 01:38:09 +04:00
|
|
|
self.load_tags()
|
2009-04-27 05:27:47 +04:00
|
|
|
|
|
|
|
# make the git data directory
|
2009-04-27 04:23:06 +04:00
|
|
|
def init_if_missing(self):
|
2009-10-16 00:45:15 +04:00
|
|
|
if os.path.exists(self.gitdir):
|
|
|
|
self.git = Repo(self.gitdir)
|
|
|
|
else:
|
2009-05-10 21:42:44 +04:00
|
|
|
os.mkdir(self.gitdir)
|
2009-10-16 00:45:15 +04:00
|
|
|
self.git = Repo.init_bare(self.gitdir)
|
2009-04-25 01:05:50 +04:00
|
|
|
|
2012-02-23 22:49:07 +04:00
|
|
|
def init_author_file(self):
|
|
|
|
self.author_map = {}
|
|
|
|
if self.ui.config('git', 'authors'):
|
|
|
|
with open(self.repo.wjoin(
|
|
|
|
self.ui.config('git', 'authors')
|
|
|
|
)) as f:
|
|
|
|
for line in f:
|
|
|
|
line = line.strip()
|
|
|
|
if not line or line.startswith('#'):
|
|
|
|
continue
|
|
|
|
from_, to = re.split(r'\s*=\s*', line, 2)
|
|
|
|
self.author_map[from_] = to
|
|
|
|
|
2009-04-27 03:25:04 +04:00
|
|
|
## FILE LOAD AND SAVE METHODS
|
|
|
|
|
2009-04-27 23:26:44 +04:00
|
|
|
def map_set(self, gitsha, hgsha):
|
|
|
|
self._map_git[gitsha] = hgsha
|
|
|
|
self._map_hg[hgsha] = gitsha
|
|
|
|
|
|
|
|
def map_hg_get(self, gitsha):
|
2009-07-07 20:05:43 +04:00
|
|
|
return self._map_git.get(gitsha)
|
2009-04-27 23:26:44 +04:00
|
|
|
|
|
|
|
def map_git_get(self, hgsha):
|
2009-07-07 20:05:43 +04:00
|
|
|
return self._map_hg.get(hgsha)
|
2009-04-29 06:33:03 +04:00
|
|
|
|
2009-04-25 01:05:50 +04:00
|
|
|
def load_map(self):
|
2009-04-27 23:26:44 +04:00
|
|
|
self._map_git = {}
|
|
|
|
self._map_hg = {}
|
2009-05-10 21:37:23 +04:00
|
|
|
if os.path.exists(self.repo.join(self.mapfile)):
|
|
|
|
for line in self.repo.opener(self.mapfile):
|
2009-04-25 01:05:50 +04:00
|
|
|
gitsha, hgsha = line.strip().split(' ', 1)
|
2009-04-27 23:26:44 +04:00
|
|
|
self._map_git[gitsha] = hgsha
|
|
|
|
self._map_hg[hgsha] = gitsha
|
2009-04-27 05:27:47 +04:00
|
|
|
|
2009-04-25 01:05:50 +04:00
|
|
|
def save_map(self):
|
2009-05-10 21:37:23 +04:00
|
|
|
file = self.repo.opener(self.mapfile, 'w+', atomictemp=True)
|
2009-08-01 02:27:20 +04:00
|
|
|
for hgsha, gitsha in sorted(self._map_hg.iteritems()):
|
2009-04-25 01:05:50 +04:00
|
|
|
file.write("%s %s\n" % (gitsha, hgsha))
|
2011-09-10 01:00:52 +04:00
|
|
|
# If this complains that NoneType is not callable, then
|
|
|
|
# atomictempfile no longer has either of rename (pre-1.9) or
|
|
|
|
# close (post-1.9)
|
|
|
|
getattr(file, 'rename', getattr(file, 'close', None))()
|
2009-04-27 05:27:47 +04:00
|
|
|
|
2009-06-19 01:38:09 +04:00
|
|
|
def load_tags(self):
|
|
|
|
self.tags = {}
|
|
|
|
if os.path.exists(self.repo.join(self.tagsfile)):
|
|
|
|
for line in self.repo.opener(self.tagsfile):
|
|
|
|
sha, name = line.strip().split(' ', 1)
|
|
|
|
self.tags[name] = sha
|
|
|
|
|
|
|
|
def save_tags(self):
|
|
|
|
file = self.repo.opener(self.tagsfile, 'w+', atomictemp=True)
|
|
|
|
for name, sha in sorted(self.tags.iteritems()):
|
2009-07-07 14:46:19 +04:00
|
|
|
if not self.repo.tagtype(name) == 'global':
|
|
|
|
file.write("%s %s\n" % (sha, name))
|
2011-09-10 01:00:52 +04:00
|
|
|
# If this complains that NoneType is not callable, then
|
|
|
|
# atomictempfile no longer has either of rename (pre-1.9) or
|
|
|
|
# close (post-1.9)
|
|
|
|
getattr(file, 'rename', getattr(file, 'close', None))()
|
2009-06-19 01:38:09 +04:00
|
|
|
|
2009-04-27 03:25:04 +04:00
|
|
|
## END FILE LOAD AND SAVE METHODS
|
2009-04-25 01:05:50 +04:00
|
|
|
|
2009-06-16 16:39:11 +04:00
|
|
|
## COMMANDS METHODS
|
|
|
|
|
2009-05-15 02:48:24 +04:00
|
|
|
def import_commits(self, remote_name):
|
|
|
|
self.import_git_objects(remote_name)
|
2011-07-19 17:11:02 +04:00
|
|
|
self.update_hg_bookmarks(self.git.get_refs())
|
2009-05-15 02:48:24 +04:00
|
|
|
self.save_map()
|
|
|
|
|
2009-08-01 20:55:54 +04:00
|
|
|
def fetch(self, remote, heads):
|
2009-08-04 15:30:16 +04:00
|
|
|
self.export_commits()
|
2009-08-01 20:55:54 +04:00
|
|
|
refs = self.fetch_pack(remote, heads)
|
2009-06-16 17:44:19 +04:00
|
|
|
remote_name = self.remote_name(remote)
|
|
|
|
|
2011-05-19 02:12:32 +04:00
|
|
|
oldrefs = self.git.get_refs()
|
2009-04-30 00:55:22 +04:00
|
|
|
if refs:
|
2009-05-28 02:58:07 +04:00
|
|
|
self.import_git_objects(remote_name, refs)
|
2009-06-19 01:38:09 +04:00
|
|
|
self.import_tags(refs)
|
2009-06-16 17:44:19 +04:00
|
|
|
self.update_hg_bookmarks(refs)
|
|
|
|
if remote_name:
|
|
|
|
self.update_remote_branches(remote_name, refs)
|
|
|
|
elif not self.paths:
|
|
|
|
# intial cloning
|
|
|
|
self.update_remote_branches('default', refs)
|
2011-05-15 21:24:25 +04:00
|
|
|
|
2011-05-11 08:13:18 +04:00
|
|
|
# "Activate" a tipmost bookmark.
|
2011-05-15 21:24:25 +04:00
|
|
|
bms = getattr(self.repo['tip'], 'bookmarks',
|
|
|
|
lambda : None)()
|
2011-05-11 08:13:18 +04:00
|
|
|
if bms:
|
|
|
|
bookmarks.setcurrent(self.repo, bms[0])
|
2011-05-19 02:12:32 +04:00
|
|
|
|
|
|
|
def remoteref(ref):
|
|
|
|
rn = remote_name or 'default'
|
|
|
|
return 'refs/remotes/' + rn + ref[10:]
|
|
|
|
|
2011-05-24 22:16:45 +04:00
|
|
|
modheads = [refs[k] for k in refs if k.startswith('refs/heads/')
|
|
|
|
and not k.endswith('^{}')
|
|
|
|
and refs[k] != oldrefs.get(remoteref(k))]
|
2011-05-19 02:12:32 +04:00
|
|
|
|
|
|
|
if not modheads:
|
|
|
|
self.ui.status(_("no changes found\n"))
|
2009-06-16 17:44:19 +04:00
|
|
|
|
2009-04-25 01:05:50 +04:00
|
|
|
self.save_map()
|
|
|
|
|
2011-05-19 02:12:32 +04:00
|
|
|
return len(modheads)
|
|
|
|
|
2009-08-04 15:30:16 +04:00
|
|
|
def export_commits(self):
|
|
|
|
try:
|
2009-06-03 20:55:45 +04:00
|
|
|
self.export_git_objects()
|
2009-08-04 15:30:16 +04:00
|
|
|
self.export_hg_tags()
|
|
|
|
self.update_references()
|
|
|
|
finally:
|
|
|
|
self.save_map()
|
2009-04-27 05:27:47 +04:00
|
|
|
|
2009-07-31 21:15:02 +04:00
|
|
|
def get_refs(self, remote):
|
|
|
|
self.export_commits()
|
|
|
|
client, path = self.get_transport_and_path(remote)
|
|
|
|
old_refs = {}
|
|
|
|
new_refs = {}
|
|
|
|
def changed(refs):
|
|
|
|
old_refs.update(refs)
|
|
|
|
to_push = set(self.local_heads().values() + self.tags.values())
|
|
|
|
new_refs.update(self.get_changed_refs(refs, to_push, True))
|
|
|
|
# don't push anything
|
|
|
|
return {}
|
|
|
|
|
|
|
|
try:
|
2012-01-27 21:06:27 +04:00
|
|
|
client.send_pack(path, changed, lambda have, want: [])
|
2009-08-08 14:14:49 +04:00
|
|
|
|
|
|
|
changed_refs = [ref for ref, sha in new_refs.iteritems()
|
|
|
|
if sha != old_refs.get(ref)]
|
|
|
|
new = [bin(self.map_hg_get(new_refs[ref])) for ref in changed_refs]
|
2011-07-14 09:15:04 +04:00
|
|
|
old = {}
|
|
|
|
for r in old_refs:
|
|
|
|
old_ref = self.map_hg_get(old_refs[r])
|
|
|
|
if old_ref:
|
|
|
|
old[bin(old_ref)] = 1
|
2009-07-31 21:15:02 +04:00
|
|
|
|
|
|
|
return old, new
|
2011-05-18 03:26:13 +04:00
|
|
|
except (HangupException, GitProtocolError), e:
|
|
|
|
raise hgutil.Abort(_("git remote error: ") + str(e))
|
2009-07-31 21:15:02 +04:00
|
|
|
|
2009-07-31 01:09:53 +04:00
|
|
|
def push(self, remote, revs, force):
|
2009-07-23 11:47:23 +04:00
|
|
|
self.export_commits()
|
2009-07-31 01:09:53 +04:00
|
|
|
changed_refs = self.upload_pack(remote, revs, force)
|
2009-06-23 22:22:49 +04:00
|
|
|
remote_name = self.remote_name(remote)
|
|
|
|
|
|
|
|
if remote_name and changed_refs:
|
|
|
|
for ref, sha in changed_refs.iteritems():
|
2010-03-26 03:39:00 +03:00
|
|
|
self.ui.status(" %s::%s => GIT:%s\n" %
|
|
|
|
(remote_name, ref, sha[0:8]))
|
2009-06-23 22:22:49 +04:00
|
|
|
|
|
|
|
self.update_remote_branches(remote_name, changed_refs)
|
|
|
|
|
2009-06-16 16:39:11 +04:00
|
|
|
def clear(self):
|
|
|
|
mapfile = self.repo.join(self.mapfile)
|
|
|
|
if os.path.exists(self.gitdir):
|
|
|
|
for root, dirs, files in os.walk(self.gitdir, topdown=False):
|
|
|
|
for name in files:
|
|
|
|
os.remove(os.path.join(root, name))
|
|
|
|
for name in dirs:
|
|
|
|
os.rmdir(os.path.join(root, name))
|
|
|
|
os.rmdir(self.gitdir)
|
|
|
|
if os.path.exists(mapfile):
|
|
|
|
os.remove(mapfile)
|
2009-05-18 03:42:34 +04:00
|
|
|
|
2011-05-24 22:16:45 +04:00
|
|
|
# incoming support
|
|
|
|
def getremotechanges(self, remote, revs):
|
|
|
|
self.export_commits()
|
|
|
|
refs = self.fetch_pack(remote.path, revs)
|
|
|
|
|
|
|
|
# refs contains all remote refs. Prune to only those requested.
|
|
|
|
if revs:
|
|
|
|
reqrefs = {}
|
|
|
|
for rev in revs:
|
|
|
|
for n in ('refs/heads/' + rev, 'refs/tags/' + rev):
|
|
|
|
if n in refs:
|
|
|
|
reqrefs[n] = refs[n]
|
|
|
|
else:
|
|
|
|
reqrefs = refs
|
|
|
|
|
|
|
|
commits = [bin(c) for c in self.getnewgitcommits(reqrefs)[1]]
|
|
|
|
|
|
|
|
b = overlayrepo(self, commits, refs)
|
|
|
|
|
|
|
|
return (b, commits, lambda: None)
|
|
|
|
|
2009-06-16 16:39:11 +04:00
|
|
|
## CHANGESET CONVERSION METHODS
|
2009-05-18 02:29:18 +04:00
|
|
|
|
2009-04-27 23:26:44 +04:00
|
|
|
def export_git_objects(self):
|
2009-10-16 00:45:15 +04:00
|
|
|
self.init_if_missing()
|
|
|
|
|
2009-06-21 14:07:44 +04:00
|
|
|
nodes = [self.repo.lookup(n) for n in self.repo]
|
|
|
|
export = [node for node in nodes if not hex(node) in self._map_hg]
|
|
|
|
total = len(export)
|
2011-05-18 11:31:36 +04:00
|
|
|
if total:
|
|
|
|
self.ui.status(_("exporting hg objects to git\n"))
|
2009-06-21 14:07:44 +04:00
|
|
|
for i, rev in enumerate(export):
|
2011-05-18 11:31:36 +04:00
|
|
|
util.progress(self.ui, 'exporting', i, total=total)
|
2009-05-28 04:14:41 +04:00
|
|
|
ctx = self.repo.changectx(rev)
|
|
|
|
state = ctx.extra().get('hg-git', None)
|
|
|
|
if state == 'octopus':
|
2010-03-26 03:39:00 +03:00
|
|
|
self.ui.debug("revision %d is a part "
|
|
|
|
"of octopus explosion\n" % ctx.rev())
|
2009-05-28 04:14:41 +04:00
|
|
|
continue
|
2009-06-21 14:07:44 +04:00
|
|
|
self.export_hg_commit(rev)
|
2011-02-22 16:58:35 +03:00
|
|
|
util.progress(self.ui, 'importing', None, total=total)
|
2010-02-25 06:08:38 +03:00
|
|
|
|
2009-04-29 06:33:03 +04:00
|
|
|
|
2009-04-28 03:15:48 +04:00
|
|
|
# convert this commit into git objects
|
|
|
|
# go through the manifest, convert all blobs/trees we don't have
|
|
|
|
# write the commit object (with metadata info)
|
2009-04-27 23:26:44 +04:00
|
|
|
def export_hg_commit(self, rev):
|
2010-03-21 19:36:39 +03:00
|
|
|
self.ui.note(_("converting revision %s\n") % hex(rev))
|
2009-04-29 06:33:03 +04:00
|
|
|
|
2009-06-29 18:28:25 +04:00
|
|
|
oldenc = self.swap_out_encoding()
|
|
|
|
|
2009-05-28 04:14:41 +04:00
|
|
|
ctx = self.repo.changectx(rev)
|
|
|
|
extra = ctx.extra()
|
|
|
|
|
2009-08-04 16:19:30 +04:00
|
|
|
commit = Commit()
|
|
|
|
|
|
|
|
(time, timezone) = ctx.date()
|
|
|
|
commit.author = self.get_git_author(ctx)
|
|
|
|
commit.author_time = int(time)
|
|
|
|
commit.author_timezone = -timezone
|
|
|
|
|
|
|
|
if 'committer' in extra:
|
|
|
|
# fixup timezone
|
|
|
|
(name, timestamp, timezone) = extra['committer'].rsplit(' ', 2)
|
|
|
|
commit.committer = name
|
|
|
|
commit.commit_time = timestamp
|
|
|
|
|
|
|
|
# work around a timezone format change
|
|
|
|
if int(timezone) % 60 != 0: #pragma: no cover
|
|
|
|
timezone = parse_timezone(timezone)
|
2010-10-25 01:00:32 +04:00
|
|
|
# Newer versions of Dulwich return a tuple here
|
|
|
|
if isinstance(timezone, tuple):
|
|
|
|
timezone, neg_utc = timezone
|
2010-10-25 01:19:39 +04:00
|
|
|
commit._commit_timezone_neg_utc = neg_utc
|
2009-08-04 16:19:30 +04:00
|
|
|
else:
|
|
|
|
timezone = -int(timezone)
|
|
|
|
commit.commit_timezone = timezone
|
2009-05-28 04:14:41 +04:00
|
|
|
else:
|
2009-08-04 16:19:30 +04:00
|
|
|
commit.committer = commit.author
|
|
|
|
commit.commit_time = commit.author_time
|
|
|
|
commit.commit_timezone = commit.author_timezone
|
2009-05-28 04:14:41 +04:00
|
|
|
|
2009-08-04 16:19:30 +04:00
|
|
|
commit.parents = []
|
|
|
|
for parent in self.get_git_parents(ctx):
|
|
|
|
hgsha = hex(parent.node())
|
|
|
|
git_sha = self.map_git_get(hgsha)
|
|
|
|
if git_sha:
|
|
|
|
commit.parents.append(git_sha)
|
2009-04-29 06:33:03 +04:00
|
|
|
|
2009-08-04 16:19:30 +04:00
|
|
|
commit.message = self.get_git_message(ctx)
|
2009-06-05 14:56:22 +04:00
|
|
|
|
2009-08-04 16:19:30 +04:00
|
|
|
if 'encoding' in extra:
|
|
|
|
commit.encoding = extra['encoding']
|
|
|
|
|
|
|
|
tree_sha = commit_tree(self.git.object_store, self.iterblobs(ctx))
|
|
|
|
commit.tree = tree_sha
|
|
|
|
|
|
|
|
self.git.object_store.add_object(commit)
|
|
|
|
self.map_set(commit.id, ctx.hex())
|
|
|
|
|
|
|
|
self.swap_out_encoding(oldenc)
|
|
|
|
return commit.id
|
2009-04-30 23:46:54 +04:00
|
|
|
|
2011-09-10 01:12:49 +04:00
|
|
|
def get_valid_git_username_email(self, name):
|
2012-02-15 05:30:06 +04:00
|
|
|
r"""Sanitize usernames and emails to fit git's restrictions.
|
|
|
|
|
|
|
|
The following is taken from the man page of git's fast-import
|
|
|
|
command:
|
|
|
|
|
|
|
|
[...] Likewise LF means one (and only one) linefeed [...]
|
|
|
|
|
|
|
|
committer
|
|
|
|
The committer command indicates who made this commit,
|
|
|
|
and when they made it.
|
|
|
|
|
|
|
|
Here <name> is the person's display name (for example
|
|
|
|
"Com M Itter") and <email> is the person's email address
|
|
|
|
("cm@example.com[1]"). LT and GT are the literal
|
|
|
|
less-than (\x3c) and greater-than (\x3e) symbols. These
|
|
|
|
are required to delimit the email address from the other
|
|
|
|
fields in the line. Note that <name> and <email> are
|
|
|
|
free-form and may contain any sequence of bytes, except
|
|
|
|
LT, GT and LF. <name> is typically UTF-8 encoded.
|
|
|
|
|
|
|
|
Accordingly, this function makes sure that there are none of the
|
|
|
|
characters <, >, or \n in any string which will be used for
|
|
|
|
a git username or email. Before this, it first removes left
|
|
|
|
angle brackets and spaces from the beginning, and right angle
|
|
|
|
brackets and spaces from the end, of this string, to convert
|
|
|
|
such things as " <john@doe.com> " to "john@doe.com" for
|
|
|
|
convenience.
|
|
|
|
|
|
|
|
TESTS:
|
|
|
|
|
|
|
|
>>> from mercurial.ui import ui
|
|
|
|
>>> g = GitHandler('', ui()).get_valid_git_username_email
|
|
|
|
>>> g('John Doe')
|
|
|
|
'John Doe'
|
|
|
|
>>> g('john@doe.com')
|
|
|
|
'john@doe.com'
|
|
|
|
>>> g(' <john@doe.com> ')
|
|
|
|
'john@doe.com'
|
|
|
|
>>> g(' <random<\n<garbage\n> > > ')
|
|
|
|
'random???garbage?'
|
|
|
|
>>> g('Typo in hgrc >but.hg-git@handles.it.gracefully>')
|
|
|
|
'Typo in hgrc ?but.hg-git@handles.it.gracefully'
|
|
|
|
"""
|
|
|
|
return re.sub('[<>\n]', '?', name.lstrip('< ').rstrip('> '))
|
2011-09-10 01:12:49 +04:00
|
|
|
|
2009-08-04 16:19:30 +04:00
|
|
|
def get_git_author(self, ctx):
|
2009-07-25 00:23:35 +04:00
|
|
|
# hg authors might not have emails
|
|
|
|
author = ctx.user()
|
|
|
|
|
2012-02-23 22:49:07 +04:00
|
|
|
# see if a translation exists
|
|
|
|
if author in self.author_map:
|
|
|
|
author = self.author_map[author]
|
|
|
|
|
2009-07-25 00:23:35 +04:00
|
|
|
# check for git author pattern compliance
|
2011-09-10 01:12:49 +04:00
|
|
|
regex = re.compile('^(.*?) ?\<(.*?)(?:\>(.*))?$')
|
2009-07-25 00:23:35 +04:00
|
|
|
a = regex.match(author)
|
|
|
|
|
|
|
|
if a:
|
2012-02-15 05:30:06 +04:00
|
|
|
name = self.get_valid_git_username_email(a.group(1))
|
|
|
|
email = self.get_valid_git_username_email(a.group(2))
|
2011-09-10 01:12:49 +04:00
|
|
|
if a.group(3) != None and len(a.group(3)) != 0:
|
2009-07-25 00:23:35 +04:00
|
|
|
name += ' ext:(' + urllib.quote(a.group(3)) + ')'
|
2011-09-10 01:12:49 +04:00
|
|
|
author = self.get_valid_git_username_email(name) + ' <' + self.get_valid_git_username_email(email) + '>'
|
2011-05-24 02:29:33 +04:00
|
|
|
elif '@' in author:
|
2011-09-10 01:12:49 +04:00
|
|
|
author = self.get_valid_git_username_email(author) + ' <' + self.get_valid_git_username_email(author) + '>'
|
2009-06-03 06:33:48 +04:00
|
|
|
else:
|
2011-09-10 01:12:49 +04:00
|
|
|
author = self.get_valid_git_username_email(author) + ' <none@none>'
|
2009-07-25 00:23:35 +04:00
|
|
|
|
2009-08-04 16:19:30 +04:00
|
|
|
if 'author' in ctx.extra():
|
2010-04-02 15:56:50 +04:00
|
|
|
author = "".join(apply_delta(author, ctx.extra()['author']))
|
2009-06-18 19:49:13 +04:00
|
|
|
|
2009-08-04 16:19:30 +04:00
|
|
|
return author
|
2009-08-02 22:53:08 +04:00
|
|
|
|
2009-08-04 16:19:30 +04:00
|
|
|
def get_git_parents(self, ctx):
|
|
|
|
def is_octopus_part(ctx):
|
|
|
|
return ctx.extra().get('hg-git', None) in ('octopus', 'octopus-done')
|
2009-06-18 19:49:13 +04:00
|
|
|
|
2009-08-04 16:19:30 +04:00
|
|
|
parents = []
|
|
|
|
if ctx.extra().get('hg-git', None) == 'octopus-done':
|
|
|
|
# implode octopus parents
|
|
|
|
part = ctx
|
|
|
|
while is_octopus_part(part):
|
|
|
|
(p1, p2) = part.parents()
|
|
|
|
assert not is_octopus_part(p1)
|
|
|
|
parents.append(p1)
|
|
|
|
part = p2
|
|
|
|
parents.append(p2)
|
|
|
|
else:
|
|
|
|
parents = ctx.parents()
|
2009-04-30 03:18:37 +04:00
|
|
|
|
2009-08-04 16:19:30 +04:00
|
|
|
return parents
|
2009-08-04 15:32:01 +04:00
|
|
|
|
2009-08-04 16:19:30 +04:00
|
|
|
def get_git_message(self, ctx):
|
|
|
|
extra = ctx.extra()
|
2009-07-03 00:58:51 +04:00
|
|
|
|
2009-08-04 16:19:30 +04:00
|
|
|
message = ctx.description() + "\n"
|
|
|
|
if 'message' in extra:
|
2010-04-02 15:56:50 +04:00
|
|
|
message = "".join(apply_delta(message, extra['message']))
|
2009-05-09 07:57:02 +04:00
|
|
|
|
2009-04-30 00:26:13 +04:00
|
|
|
# HG EXTRA INFORMATION
|
|
|
|
add_extras = False
|
2009-04-30 23:55:56 +04:00
|
|
|
extra_message = ''
|
2009-04-30 00:26:13 +04:00
|
|
|
if not ctx.branch() == 'default':
|
|
|
|
add_extras = True
|
2009-04-30 23:55:56 +04:00
|
|
|
extra_message += "branch : " + ctx.branch() + "\n"
|
2009-04-30 03:18:37 +04:00
|
|
|
|
2009-08-04 16:19:30 +04:00
|
|
|
renames = []
|
2009-08-16 01:41:45 +04:00
|
|
|
for f in ctx.files():
|
|
|
|
if f not in ctx.manifest():
|
|
|
|
continue
|
2009-08-04 16:19:30 +04:00
|
|
|
rename = ctx.filectx(f).renamed()
|
|
|
|
if rename:
|
|
|
|
renames.append((rename[0], f))
|
|
|
|
|
2009-04-30 23:55:56 +04:00
|
|
|
if renames:
|
|
|
|
add_extras = True
|
|
|
|
for oldfile, newfile in renames:
|
|
|
|
extra_message += "rename : " + oldfile + " => " + newfile + "\n"
|
2009-05-30 00:10:14 +04:00
|
|
|
|
|
|
|
for key, value in extra.iteritems():
|
2009-06-29 18:28:25 +04:00
|
|
|
if key in ('author', 'committer', 'encoding', 'message', 'branch', 'hg-git'):
|
2009-05-30 00:10:14 +04:00
|
|
|
continue
|
|
|
|
else:
|
2009-06-05 14:56:22 +04:00
|
|
|
add_extras = True
|
2009-05-30 00:10:14 +04:00
|
|
|
extra_message += "extra : " + key + " : " + urllib.quote(value) + "\n"
|
|
|
|
|
2009-04-30 00:26:13 +04:00
|
|
|
if add_extras:
|
2009-08-04 16:19:30 +04:00
|
|
|
message += "\n--HG--\n" + extra_message
|
2009-04-30 03:18:37 +04:00
|
|
|
|
2009-08-04 16:19:30 +04:00
|
|
|
return message
|
2009-07-03 00:58:51 +04:00
|
|
|
|
|
|
|
def iterblobs(self, ctx):
|
|
|
|
for f in ctx:
|
2009-08-02 22:12:16 +04:00
|
|
|
fctx = ctx[f]
|
|
|
|
blobid = self.map_git_get(hex(fctx.filenode()))
|
|
|
|
|
|
|
|
if not blobid:
|
|
|
|
blob = Blob.from_string(fctx.data())
|
2009-07-03 00:58:51 +04:00
|
|
|
self.git.object_store.add_object(blob)
|
2009-08-02 22:12:16 +04:00
|
|
|
self.map_set(blob.id, hex(fctx.filenode()))
|
|
|
|
blobid = blob.id
|
|
|
|
|
2009-07-03 00:58:51 +04:00
|
|
|
if 'l' in ctx.flags(f):
|
|
|
|
mode = 0120000
|
|
|
|
elif 'x' in ctx.flags(f):
|
|
|
|
mode = 0100755
|
2009-06-03 20:55:45 +04:00
|
|
|
else:
|
2009-07-03 00:58:51 +04:00
|
|
|
mode = 0100644
|
2009-08-02 22:12:16 +04:00
|
|
|
|
|
|
|
yield f, blobid, mode
|
2009-04-29 06:33:03 +04:00
|
|
|
|
2011-05-24 22:16:45 +04:00
|
|
|
def getnewgitcommits(self, refs=None):
|
2009-10-16 00:45:15 +04:00
|
|
|
self.init_if_missing()
|
|
|
|
|
2009-06-16 16:39:11 +04:00
|
|
|
# import heads and fetched tags as remote references
|
|
|
|
todo = []
|
|
|
|
done = set()
|
|
|
|
convert_list = {}
|
|
|
|
|
|
|
|
# get a list of all the head shas
|
2010-02-24 19:21:20 +03:00
|
|
|
seenheads = set()
|
|
|
|
if refs is None:
|
2010-02-26 21:27:32 +03:00
|
|
|
refs = self.git.refs.as_dict()
|
2009-06-16 16:39:11 +04:00
|
|
|
if refs:
|
2010-02-24 19:21:20 +03:00
|
|
|
for sha in refs.itervalues():
|
|
|
|
# refs contains all the refs in the server, not just the ones
|
|
|
|
# we are pulling
|
|
|
|
if sha in self.git.object_store:
|
|
|
|
obj = self.git.get_object(sha)
|
|
|
|
while isinstance(obj, Tag):
|
2010-05-20 02:16:59 +04:00
|
|
|
obj_type, sha = obj.object
|
2010-02-24 19:21:20 +03:00
|
|
|
obj = self.git.get_object(sha)
|
|
|
|
if isinstance (obj, Commit) and sha not in seenheads:
|
|
|
|
seenheads.add(sha)
|
|
|
|
todo.append(sha)
|
2009-06-16 16:39:11 +04:00
|
|
|
|
2010-02-24 19:21:25 +03:00
|
|
|
# sort by commit date
|
|
|
|
def commitdate(sha):
|
|
|
|
obj = self.git.get_object(sha)
|
|
|
|
return obj.commit_time-obj.commit_timezone
|
|
|
|
|
|
|
|
todo.sort(key=commitdate, reverse=True)
|
|
|
|
|
2009-06-16 16:39:11 +04:00
|
|
|
# traverse the heads getting a list of all the unique commits
|
2010-02-24 19:21:23 +03:00
|
|
|
commits = []
|
|
|
|
seen = set(todo)
|
2009-06-16 16:39:11 +04:00
|
|
|
while todo:
|
2010-02-24 19:21:23 +03:00
|
|
|
sha = todo[-1]
|
2009-06-16 16:39:11 +04:00
|
|
|
if sha in done:
|
2010-02-24 19:21:23 +03:00
|
|
|
todo.pop()
|
2009-06-16 16:39:11 +04:00
|
|
|
continue
|
2010-02-24 19:21:23 +03:00
|
|
|
assert isinstance(sha, str)
|
2009-06-16 16:39:11 +04:00
|
|
|
obj = self.git.get_object(sha)
|
2010-02-24 19:21:20 +03:00
|
|
|
assert isinstance(obj, Commit)
|
2010-02-24 19:21:23 +03:00
|
|
|
for p in obj.parents:
|
|
|
|
if p not in done:
|
|
|
|
todo.append(p)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
commits.append(sha)
|
|
|
|
convert_list[sha] = obj
|
|
|
|
done.add(sha)
|
|
|
|
todo.pop()
|
2009-06-16 16:39:11 +04:00
|
|
|
|
2011-05-24 22:16:45 +04:00
|
|
|
return convert_list, [commit for commit in commits if not commit in self._map_git]
|
|
|
|
|
|
|
|
def import_git_objects(self, remote_name=None, refs=None):
|
|
|
|
convert_list, commits = self.getnewgitcommits(refs)
|
2009-06-16 16:39:11 +04:00
|
|
|
# import each of the commits, oldest first
|
|
|
|
total = len(commits)
|
2011-05-18 11:31:36 +04:00
|
|
|
if total:
|
|
|
|
self.ui.status(_("importing git objects into hg\n"))
|
|
|
|
|
2009-06-16 16:39:11 +04:00
|
|
|
for i, csha in enumerate(commits):
|
2011-02-22 16:58:35 +03:00
|
|
|
util.progress(self.ui, 'importing', i, total=total, unit='commits')
|
2009-06-16 16:39:11 +04:00
|
|
|
commit = convert_list[csha]
|
2009-06-21 14:07:44 +04:00
|
|
|
self.import_git_commit(commit)
|
2011-02-22 16:58:35 +03:00
|
|
|
util.progress(self.ui, 'importing', None, total=total, unit='commits')
|
2009-06-16 16:39:11 +04:00
|
|
|
|
2011-05-21 09:41:43 +04:00
|
|
|
# Remove any dangling tag references.
|
|
|
|
for name, rev in self.repo.tags().items():
|
|
|
|
if not rev in self.repo:
|
|
|
|
if hasattr(self, 'tagscache') and self.tagscache and \
|
|
|
|
'name' in self.tagscache:
|
|
|
|
# Mercurial 1.4 and earlier.
|
|
|
|
del self.repo.tagscache[name]
|
|
|
|
elif hasattr(self, '_tags') and self._tags and \
|
|
|
|
'name' in self._tags:
|
|
|
|
# Mercurial 1.5 and later.
|
|
|
|
del self.repo._tags[name]
|
|
|
|
if name in self.repo._tagtypes:
|
|
|
|
del self.repo._tagtypes[name]
|
|
|
|
|
2009-06-16 16:39:11 +04:00
|
|
|
def import_git_commit(self, commit):
|
|
|
|
self.ui.debug(_("importing: %s\n") % commit.id)
|
|
|
|
|
2010-03-26 03:39:00 +03:00
|
|
|
(strip_message, hg_renames,
|
|
|
|
hg_branch, extra) = self.extract_hg_metadata(commit.message)
|
2009-06-16 16:39:11 +04:00
|
|
|
|
|
|
|
# get a list of the changed, added, removed files
|
2009-07-03 00:58:51 +04:00
|
|
|
files = self.get_files_changed(commit)
|
2009-06-16 16:39:11 +04:00
|
|
|
|
|
|
|
date = (commit.author_time, -commit.author_timezone)
|
|
|
|
text = strip_message
|
|
|
|
|
2009-07-29 00:29:27 +04:00
|
|
|
origtext = text
|
2009-06-18 19:49:13 +04:00
|
|
|
try:
|
|
|
|
text.decode('utf-8')
|
|
|
|
except UnicodeDecodeError:
|
2009-07-03 00:58:51 +04:00
|
|
|
text = self.decode_guess(text, commit.encoding)
|
2009-07-29 00:29:27 +04:00
|
|
|
|
|
|
|
text = '\n'.join([l.rstrip() for l in text.splitlines()]).strip('\n')
|
|
|
|
if text + '\n' != origtext:
|
|
|
|
extra['message'] = create_delta(text +'\n', origtext)
|
2009-06-18 19:49:13 +04:00
|
|
|
|
|
|
|
author = commit.author
|
|
|
|
|
|
|
|
# convert extra data back to the end
|
|
|
|
if ' ext:' in commit.author:
|
|
|
|
regex = re.compile('^(.*?)\ ext:\((.*)\) <(.*)\>$')
|
|
|
|
m = regex.match(commit.author)
|
|
|
|
if m:
|
|
|
|
name = m.group(1)
|
|
|
|
ex = urllib.unquote(m.group(2))
|
|
|
|
email = m.group(3)
|
|
|
|
author = name + ' <' + email + '>' + ex
|
|
|
|
|
|
|
|
if ' <none@none>' in commit.author:
|
|
|
|
author = commit.author[:-12]
|
|
|
|
|
|
|
|
try:
|
|
|
|
author.decode('utf-8')
|
|
|
|
except UnicodeDecodeError:
|
2009-07-25 00:23:35 +04:00
|
|
|
origauthor = author
|
2009-07-03 00:58:51 +04:00
|
|
|
author = self.decode_guess(author, commit.encoding)
|
2009-07-25 00:23:35 +04:00
|
|
|
extra['author'] = create_delta(author, origauthor)
|
2009-06-18 19:49:13 +04:00
|
|
|
|
|
|
|
oldenc = self.swap_out_encoding()
|
|
|
|
|
2011-05-24 22:04:10 +04:00
|
|
|
def findconvergedfiles(p1, p2):
|
|
|
|
# If any files have the same contents in both parents of a merge
|
|
|
|
# (and are therefore not reported as changed by Git) but are at
|
|
|
|
# different file revisions in Mercurial (because they arrived at
|
|
|
|
# those contents in different ways), we need to include them in
|
|
|
|
# the list of changed files so that Mercurial can join up their
|
|
|
|
# filelog histories (same as if the merge was done in Mercurial to
|
|
|
|
# begin with).
|
|
|
|
if p2 == nullid:
|
|
|
|
return []
|
|
|
|
manifest1 = self.repo.changectx(p1).manifest()
|
|
|
|
manifest2 = self.repo.changectx(p2).manifest()
|
|
|
|
return [path for path, node1 in manifest1.iteritems()
|
|
|
|
if path not in files and manifest2.get(path, node1) != node1]
|
2009-12-25 10:56:20 +03:00
|
|
|
|
2011-05-24 22:04:10 +04:00
|
|
|
def getfilectx(repo, memctx, f):
|
|
|
|
info = files.get(f)
|
|
|
|
if info != None:
|
|
|
|
# it's a file reported as modified from Git
|
|
|
|
delete, mode, sha = info
|
|
|
|
if delete:
|
|
|
|
raise IOError
|
|
|
|
|
|
|
|
data = self.git[sha].data
|
|
|
|
copied_path = hg_renames.get(f)
|
|
|
|
e = self.convert_git_int_mode(mode)
|
|
|
|
else:
|
|
|
|
# it's a converged file
|
|
|
|
fc = context.filectx(self.repo, f, changeid=memctx.p1().rev())
|
|
|
|
data = fc.data()
|
|
|
|
e = fc.flags()
|
|
|
|
copied_path = fc.renamed()
|
2009-12-25 10:56:20 +03:00
|
|
|
|
2009-06-16 16:39:11 +04:00
|
|
|
return context.memfilectx(f, data, 'l' in e, 'x' in e, copied_path)
|
|
|
|
|
|
|
|
gparents = map(self.map_hg_get, commit.parents)
|
|
|
|
p1, p2 = (nullid, nullid)
|
|
|
|
octopus = False
|
|
|
|
|
|
|
|
if len(gparents) > 1:
|
|
|
|
# merge, possibly octopus
|
|
|
|
def commit_octopus(p1, p2):
|
2011-05-24 22:04:10 +04:00
|
|
|
ctx = context.memctx(self.repo, (p1, p2), text,
|
|
|
|
list(files) + findconvergedfiles(p1, p2),
|
|
|
|
getfilectx, author, date, {'hg-git': 'octopus'})
|
2009-06-16 16:39:11 +04:00
|
|
|
return hex(self.repo.commitctx(ctx))
|
|
|
|
|
|
|
|
octopus = len(gparents) > 2
|
|
|
|
p2 = gparents.pop()
|
|
|
|
p1 = gparents.pop()
|
|
|
|
while len(gparents) > 0:
|
|
|
|
p2 = commit_octopus(p1, p2)
|
|
|
|
p1 = gparents.pop()
|
|
|
|
else:
|
|
|
|
if gparents:
|
|
|
|
p1 = gparents.pop()
|
|
|
|
|
|
|
|
pa = None
|
|
|
|
if not (p2 == nullid):
|
|
|
|
node1 = self.repo.changectx(p1)
|
|
|
|
node2 = self.repo.changectx(p2)
|
|
|
|
pa = node1.ancestor(node2)
|
|
|
|
|
|
|
|
# if named branch, add to extra
|
|
|
|
if hg_branch:
|
|
|
|
extra['branch'] = hg_branch
|
|
|
|
|
|
|
|
# if committer is different than author, add it to extra
|
2009-07-03 00:58:51 +04:00
|
|
|
if commit.author != commit.committer \
|
|
|
|
or commit.author_time != commit.commit_time \
|
|
|
|
or commit.author_timezone != commit.commit_timezone:
|
2010-03-26 03:39:00 +03:00
|
|
|
extra['committer'] = "%s %d %d" % (
|
|
|
|
commit.committer, commit.commit_time, -commit.commit_timezone)
|
2009-06-16 16:39:11 +04:00
|
|
|
|
2009-07-03 00:58:51 +04:00
|
|
|
if commit.encoding:
|
|
|
|
extra['encoding'] = commit.encoding
|
2009-06-16 16:39:11 +04:00
|
|
|
|
|
|
|
if hg_branch:
|
|
|
|
extra['branch'] = hg_branch
|
|
|
|
|
|
|
|
if octopus:
|
|
|
|
extra['hg-git'] ='octopus-done'
|
|
|
|
|
2010-03-28 06:37:58 +04:00
|
|
|
# TODO use 'n in self.repo' when we require hg 1.5
|
|
|
|
def repo_contains(n):
|
|
|
|
try:
|
|
|
|
return bool(self.repo.lookup(n))
|
|
|
|
except error.RepoLookupError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if not (repo_contains(p1) and repo_contains(p2)):
|
|
|
|
raise hgutil.Abort(_('you appear to have run strip - '
|
|
|
|
'please run hg git-cleanup'))
|
2011-05-24 22:04:10 +04:00
|
|
|
ctx = context.memctx(self.repo, (p1, p2), text,
|
|
|
|
list(files) + findconvergedfiles(p1, p2),
|
|
|
|
getfilectx, author, date, extra)
|
2009-06-16 16:39:11 +04:00
|
|
|
|
2009-07-29 00:29:27 +04:00
|
|
|
node = self.repo.commitctx(ctx)
|
2009-06-16 16:39:11 +04:00
|
|
|
|
2009-06-18 19:49:13 +04:00
|
|
|
self.swap_out_encoding(oldenc)
|
|
|
|
|
2009-06-16 16:39:11 +04:00
|
|
|
# save changeset to mapping file
|
|
|
|
cs = hex(node)
|
|
|
|
self.map_set(commit.id, cs)
|
|
|
|
|
|
|
|
## PACK UPLOADING AND FETCHING
|
2009-04-27 05:27:47 +04:00
|
|
|
|
2009-07-31 01:09:53 +04:00
|
|
|
def upload_pack(self, remote, revs, force):
|
2009-06-16 17:44:19 +04:00
|
|
|
client, path = self.get_transport_and_path(remote)
|
2009-07-31 01:09:53 +04:00
|
|
|
def changed(refs):
|
|
|
|
to_push = revs or set(self.local_heads().values() + self.tags.values())
|
|
|
|
return self.get_changed_refs(refs, to_push, force)
|
|
|
|
|
2009-07-03 00:58:51 +04:00
|
|
|
genpack = self.git.object_store.generate_pack_contents
|
2009-04-28 10:35:49 +04:00
|
|
|
try:
|
2009-05-10 05:04:19 +04:00
|
|
|
self.ui.status(_("creating and sending data\n"))
|
2009-04-29 21:03:16 +04:00
|
|
|
changed_refs = client.send_pack(path, changed, genpack)
|
2009-07-02 16:27:14 +04:00
|
|
|
return changed_refs
|
2011-05-18 11:31:40 +04:00
|
|
|
except (HangupException, GitProtocolError), e:
|
|
|
|
raise hgutil.Abort(_("git remote error: ") + str(e))
|
2009-07-31 01:09:53 +04:00
|
|
|
|
|
|
|
def get_changed_refs(self, refs, revs, force):
|
|
|
|
new_refs = refs.copy()
|
2009-08-08 13:52:30 +04:00
|
|
|
|
|
|
|
#The remote repo is empty and the local one doesn't have bookmarks/tags
|
2009-08-17 02:18:08 +04:00
|
|
|
if refs.keys()[0] == 'capabilities^{}':
|
2009-08-08 13:52:30 +04:00
|
|
|
del new_refs['capabilities^{}']
|
2009-08-17 02:18:08 +04:00
|
|
|
if not self.local_heads():
|
|
|
|
tip = hex(self.repo.lookup('tip'))
|
2011-03-24 02:26:32 +03:00
|
|
|
try:
|
|
|
|
commands.bookmark(self.ui, self.repo, 'master', tip, force=True)
|
|
|
|
except NameError:
|
|
|
|
bookmarks.bookmark(self.ui, self.repo, 'master', tip, force=True)
|
2009-08-17 02:18:08 +04:00
|
|
|
bookmarks.setcurrent(self.repo, 'master')
|
|
|
|
new_refs['refs/heads/master'] = self.map_git_get(tip)
|
2009-08-08 13:52:30 +04:00
|
|
|
|
2009-07-31 01:09:53 +04:00
|
|
|
for rev in revs:
|
|
|
|
ctx = self.repo[rev]
|
2011-03-01 17:23:09 +03:00
|
|
|
if getattr(ctx, 'bookmarks', None):
|
2011-12-19 03:54:16 +04:00
|
|
|
labels = lambda c: ctx.tags() + [
|
2012-02-15 05:30:06 +04:00
|
|
|
fltr for fltr, bm
|
2011-12-19 03:54:16 +04:00
|
|
|
in self._filter_for_bookmarks(ctx.bookmarks())
|
|
|
|
]
|
2011-03-01 17:23:09 +03:00
|
|
|
else:
|
|
|
|
labels = lambda c: ctx.tags()
|
2011-03-23 02:03:29 +03:00
|
|
|
prep = lambda itr: [i.replace(' ', '_') for i in itr]
|
|
|
|
|
|
|
|
heads = [t for t in prep(labels(ctx)) if t in self.local_heads()]
|
|
|
|
tags = [t for t in prep(labels(ctx)) if t in self.tags]
|
2009-07-31 01:09:53 +04:00
|
|
|
|
|
|
|
if not (heads or tags):
|
|
|
|
raise hgutil.Abort("revision %s cannot be pushed since"
|
|
|
|
" it doesn't have a ref" % ctx)
|
|
|
|
|
2010-03-15 07:53:54 +03:00
|
|
|
# Check if the tags the server is advertising are annotated tags,
|
|
|
|
# by attempting to retrieve it from the our git repo, and building a
|
|
|
|
# list of these tags.
|
|
|
|
#
|
|
|
|
# This is possible, even though (currently) annotated tags are
|
|
|
|
# dereferenced and stored as lightweight ones, as the annotated tag
|
|
|
|
# is still stored in the git repo.
|
|
|
|
uptodate_annotated_tags = []
|
|
|
|
for r in tags:
|
|
|
|
ref = 'refs/tags/'+r
|
|
|
|
# Check tag.
|
|
|
|
if not ref in refs:
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
# We're not using Repo.tag(), as it's deprecated.
|
|
|
|
tag = self.git.get_object(refs[ref])
|
|
|
|
if not isinstance(tag, Tag):
|
|
|
|
continue
|
|
|
|
except KeyError:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# If we've reached here, the tag's good.
|
|
|
|
uptodate_annotated_tags.append(ref)
|
|
|
|
|
2009-07-31 01:09:53 +04:00
|
|
|
for r in heads + tags:
|
|
|
|
if r in heads:
|
|
|
|
ref = 'refs/heads/'+r
|
|
|
|
else:
|
|
|
|
ref = 'refs/tags/'+r
|
|
|
|
|
|
|
|
if ref not in refs:
|
|
|
|
new_refs[ref] = self.map_git_get(ctx.hex())
|
|
|
|
elif new_refs[ref] in self._map_git:
|
|
|
|
rctx = self.repo[self.map_hg_get(new_refs[ref])]
|
|
|
|
if rctx.ancestor(ctx) == rctx or force:
|
|
|
|
new_refs[ref] = self.map_git_get(ctx.hex())
|
|
|
|
else:
|
|
|
|
raise hgutil.Abort("pushing %s overwrites %s"
|
|
|
|
% (ref, ctx))
|
2010-03-15 07:53:54 +03:00
|
|
|
elif ref in uptodate_annotated_tags:
|
|
|
|
# we already have the annotated tag.
|
|
|
|
pass
|
2009-07-31 01:09:53 +04:00
|
|
|
else:
|
|
|
|
raise hgutil.Abort("%s changed on the server, please pull "
|
|
|
|
"and merge before pushing" % ref)
|
|
|
|
|
|
|
|
return new_refs
|
2009-04-29 06:33:03 +04:00
|
|
|
|
|
|
|
|
2009-08-01 20:55:54 +04:00
|
|
|
def fetch_pack(self, remote_name, heads):
|
2009-06-08 22:15:58 +04:00
|
|
|
client, path = self.get_transport_and_path(remote_name)
|
2009-07-03 00:58:51 +04:00
|
|
|
graphwalker = self.git.get_graph_walker()
|
2009-08-01 20:55:54 +04:00
|
|
|
def determine_wants(refs):
|
|
|
|
if heads:
|
|
|
|
want = []
|
2010-06-02 16:14:26 +04:00
|
|
|
# contains pairs of ('refs/(heads|tags|...)/foo', 'foo')
|
|
|
|
# if ref is just '<foo>', then we get ('foo', 'foo')
|
|
|
|
stripped_refs = [
|
|
|
|
(r, r[r.find('/', r.find('/')+1)+1:])
|
|
|
|
for r in refs]
|
2009-08-01 20:55:54 +04:00
|
|
|
for h in heads:
|
2010-06-02 16:14:26 +04:00
|
|
|
r = [pair[0] for pair in stripped_refs if pair[1] == h]
|
2009-08-01 20:55:54 +04:00
|
|
|
if not r:
|
2010-03-26 03:37:00 +03:00
|
|
|
raise hgutil.Abort("ref %s not found on remote server" % h)
|
2009-08-01 20:55:54 +04:00
|
|
|
elif len(r) == 1:
|
|
|
|
want.append(refs[r[0]])
|
|
|
|
else:
|
2010-03-26 03:37:00 +03:00
|
|
|
raise hgutil.Abort("ambiguous reference %s: %r" % (h, r))
|
2009-08-01 20:55:54 +04:00
|
|
|
else:
|
|
|
|
want = [sha for ref, sha in refs.iteritems()
|
2011-07-16 02:33:48 +04:00
|
|
|
if not ref.endswith('^{}')
|
|
|
|
and ( ref.startswith('refs/heads/') or ref.startswith('refs/tags/') ) ]
|
2011-05-18 11:31:40 +04:00
|
|
|
want = [x for x in want if x not in self.git]
|
2009-08-01 20:55:54 +04:00
|
|
|
return want
|
2009-04-24 02:26:10 +04:00
|
|
|
f, commit = self.git.object_store.add_pack()
|
|
|
|
try:
|
2009-11-29 18:24:34 +03:00
|
|
|
try:
|
2011-06-16 10:40:12 +04:00
|
|
|
progress = GitProgress(self.ui)
|
|
|
|
ret = client.fetch_pack(path, determine_wants, graphwalker,
|
|
|
|
f.write, progress.progress)
|
|
|
|
progress.flush()
|
|
|
|
return ret
|
2011-05-18 11:31:40 +04:00
|
|
|
except (HangupException, GitProtocolError), e:
|
|
|
|
raise hgutil.Abort(_("git remote error: ") + str(e))
|
2009-06-05 14:56:22 +04:00
|
|
|
finally:
|
2009-07-03 00:58:51 +04:00
|
|
|
commit()
|
2009-04-24 02:26:10 +04:00
|
|
|
|
2009-06-16 16:39:11 +04:00
|
|
|
## REFERENCES HANDLING
|
|
|
|
|
|
|
|
def update_references(self):
|
2009-06-23 22:20:15 +04:00
|
|
|
heads = self.local_heads()
|
2009-06-16 16:39:11 +04:00
|
|
|
|
2009-06-23 22:20:15 +04:00
|
|
|
# Create a local Git branch name for each
|
|
|
|
# Mercurial bookmark.
|
|
|
|
for key in heads:
|
2011-07-14 09:15:04 +04:00
|
|
|
git_ref = self.map_git_get(heads[key])
|
|
|
|
if git_ref:
|
|
|
|
self.git.refs['refs/heads/' + key] = self.map_git_get(heads[key])
|
2009-06-16 16:39:11 +04:00
|
|
|
|
|
|
|
def export_hg_tags(self):
|
|
|
|
for tag, sha in self.repo.tags().iteritems():
|
2009-07-05 19:39:28 +04:00
|
|
|
if self.repo.tagtype(tag) in ('global', 'git'):
|
2011-03-23 02:03:29 +03:00
|
|
|
tag = tag.replace(' ', '_')
|
2009-07-03 00:58:51 +04:00
|
|
|
self.git.refs['refs/tags/' + tag] = self.map_git_get(hex(sha))
|
2009-07-07 14:46:19 +04:00
|
|
|
self.tags[tag] = hex(sha)
|
2009-06-16 16:39:11 +04:00
|
|
|
|
2011-12-19 03:54:16 +04:00
|
|
|
def _filter_for_bookmarks(self, bms):
|
|
|
|
if not self.branch_bookmark_suffix:
|
|
|
|
return [(bm, bm) for bm in bms]
|
|
|
|
else:
|
|
|
|
def _filter_bm(bm):
|
|
|
|
if bm.endswith(self.branch_bookmark_suffix):
|
|
|
|
return bm[0:-(len(self.branch_bookmark_suffix))]
|
|
|
|
else:
|
|
|
|
return bm
|
|
|
|
return [(_filter_bm(bm), bm) for bm in bms]
|
|
|
|
|
2009-06-16 16:39:11 +04:00
|
|
|
def local_heads(self):
|
2009-06-22 01:44:41 +04:00
|
|
|
try:
|
2009-12-26 21:22:06 +03:00
|
|
|
if getattr(bookmarks, 'parse', None):
|
|
|
|
bms = bookmarks.parse(self.repo)
|
|
|
|
else:
|
|
|
|
bms = self.repo._bookmarks
|
2012-02-15 05:30:06 +04:00
|
|
|
return dict([(filtered_bm, hex(bms[bm])) for
|
2011-12-19 03:54:16 +04:00
|
|
|
filtered_bm, bm in self._filter_for_bookmarks(bms)])
|
2009-07-07 20:46:14 +04:00
|
|
|
except AttributeError: #pragma: no cover
|
2009-06-22 01:44:41 +04:00
|
|
|
return {}
|
2009-06-16 16:39:11 +04:00
|
|
|
|
2009-06-19 01:38:09 +04:00
|
|
|
def import_tags(self, refs):
|
2009-05-28 02:58:07 +04:00
|
|
|
keys = refs.keys()
|
|
|
|
if not keys:
|
2009-06-19 01:38:09 +04:00
|
|
|
return
|
|
|
|
for k in keys[:]:
|
2009-05-28 02:58:07 +04:00
|
|
|
ref_name = k
|
|
|
|
parts = k.split('/')
|
2009-06-22 01:44:41 +04:00
|
|
|
if parts[0] == 'refs' and parts[1] == 'tags':
|
2009-05-28 02:58:07 +04:00
|
|
|
ref_name = "/".join([v for v in parts[2:]])
|
2009-08-01 20:55:54 +04:00
|
|
|
# refs contains all the refs in the server, not just
|
|
|
|
# the ones we are pulling
|
|
|
|
if refs[k] not in self.git.object_store:
|
|
|
|
continue
|
2009-05-28 02:58:07 +04:00
|
|
|
if ref_name[-3:] == '^{}':
|
|
|
|
ref_name = ref_name[:-3]
|
|
|
|
if not ref_name in self.repo.tags():
|
|
|
|
obj = self.git.get_object(refs[k])
|
|
|
|
sha = None
|
|
|
|
if isinstance (obj, Commit): # lightweight
|
|
|
|
sha = self.map_hg_get(refs[k])
|
2009-06-19 01:38:09 +04:00
|
|
|
self.tags[ref_name] = sha
|
|
|
|
elif isinstance (obj, Tag): # annotated
|
2010-05-20 02:16:59 +04:00
|
|
|
(obj_type, obj_sha) = obj.object
|
2009-05-28 02:58:07 +04:00
|
|
|
obj = self.git.get_object(obj_sha)
|
2009-06-05 14:56:22 +04:00
|
|
|
if isinstance (obj, Commit):
|
2009-05-28 02:58:07 +04:00
|
|
|
sha = self.map_hg_get(obj_sha)
|
2009-06-19 01:38:09 +04:00
|
|
|
# TODO: better handling for annotated tags
|
|
|
|
self.tags[ref_name] = sha
|
|
|
|
self.save_tags()
|
2009-06-05 14:56:22 +04:00
|
|
|
|
2009-06-16 17:44:19 +04:00
|
|
|
def update_hg_bookmarks(self, refs):
|
2009-04-28 17:30:11 +04:00
|
|
|
try:
|
2009-12-26 21:22:06 +03:00
|
|
|
oldbm = getattr(bookmarks, 'parse', None)
|
|
|
|
if oldbm:
|
|
|
|
bms = bookmarks.parse(self.repo)
|
|
|
|
else:
|
|
|
|
bms = self.repo._bookmarks
|
2011-12-19 03:54:16 +04:00
|
|
|
|
2009-06-16 17:44:19 +04:00
|
|
|
heads = dict([(ref[11:],refs[ref]) for ref in refs
|
|
|
|
if ref.startswith('refs/heads/')])
|
2009-05-26 22:55:03 +04:00
|
|
|
|
|
|
|
for head, sha in heads.iteritems():
|
2009-08-01 20:55:54 +04:00
|
|
|
# refs contains all the refs in the server, not just
|
|
|
|
# the ones we are pulling
|
|
|
|
if sha not in self.git.object_store:
|
|
|
|
continue
|
2009-06-19 19:53:39 +04:00
|
|
|
hgsha = bin(self.map_hg_get(sha))
|
2009-06-16 17:44:19 +04:00
|
|
|
if not head in bms:
|
|
|
|
# new branch
|
|
|
|
bms[head] = hgsha
|
|
|
|
else:
|
|
|
|
bm = self.repo[bms[head]]
|
|
|
|
if bm.ancestor(self.repo[hgsha]) == bm:
|
|
|
|
# fast forward
|
|
|
|
bms[head] = hgsha
|
2011-12-19 03:54:16 +04:00
|
|
|
|
|
|
|
# if there's a branch bookmark suffix,
|
|
|
|
# then add it on to all bookmark names
|
|
|
|
# that would otherwise conflict with a branch
|
|
|
|
# name
|
|
|
|
if self.branch_bookmark_suffix:
|
|
|
|
real_branch_names = self.repo.branchmap()
|
|
|
|
bms = dict(
|
|
|
|
(
|
2012-02-15 05:30:06 +04:00
|
|
|
bm_name + self.branch_bookmark_suffix
|
2011-12-19 03:54:16 +04:00
|
|
|
if bm_name in real_branch_names
|
|
|
|
else bm_name,
|
|
|
|
bms[bm_name]
|
|
|
|
)
|
|
|
|
for bm_name in bms
|
|
|
|
)
|
2009-05-26 22:55:03 +04:00
|
|
|
if heads:
|
2009-12-26 21:22:06 +03:00
|
|
|
if oldbm:
|
|
|
|
bookmarks.write(self.repo, bms)
|
|
|
|
else:
|
|
|
|
self.repo._bookmarks = bms
|
|
|
|
bookmarks.write(self.repo)
|
2009-05-28 04:19:11 +04:00
|
|
|
|
2009-04-28 17:30:11 +04:00
|
|
|
except AttributeError:
|
2009-05-10 05:04:19 +04:00
|
|
|
self.ui.warn(_('creating bookmarks failed, do you have'
|
|
|
|
' bookmarks enabled?\n'))
|
2009-04-30 03:18:37 +04:00
|
|
|
|
2009-06-16 17:44:19 +04:00
|
|
|
def update_remote_branches(self, remote_name, refs):
|
2010-12-21 07:37:07 +03:00
|
|
|
tagfile = self.repo.join(os.path.join('git-remote-refs'))
|
|
|
|
tags = self.repo.gitrefs()
|
|
|
|
# since we re-write all refs for this remote each time, prune
|
|
|
|
# all entries matching this remote from our tags list now so
|
|
|
|
# that we avoid any stale refs hanging around forever
|
|
|
|
for t in list(tags):
|
|
|
|
if t.startswith(remote_name + '/'):
|
|
|
|
del tags[t]
|
2010-12-21 08:30:16 +03:00
|
|
|
tags = dict((k, hex(v)) for k, v in tags.iteritems())
|
2010-12-21 07:37:07 +03:00
|
|
|
store = self.git.object_store
|
2010-03-29 09:05:59 +04:00
|
|
|
for ref_name, sha in refs.iteritems():
|
2009-07-03 00:58:51 +04:00
|
|
|
if ref_name.startswith('refs/heads'):
|
2010-12-21 07:37:07 +03:00
|
|
|
if sha not in store:
|
|
|
|
continue
|
|
|
|
hgsha = self.map_hg_get(sha)
|
2010-03-29 09:05:59 +04:00
|
|
|
head = ref_name[11:]
|
2010-12-21 07:37:07 +03:00
|
|
|
tags['/'.join((remote_name, head))] = hgsha
|
|
|
|
# TODO(durin42): what is this doing?
|
2010-03-29 09:05:59 +04:00
|
|
|
new_ref = 'refs/remotes/%s/%s' % (remote_name, head)
|
|
|
|
self.git.refs[new_ref] = sha
|
2010-04-06 04:06:20 +04:00
|
|
|
elif (ref_name.startswith('refs/tags')
|
|
|
|
and not ref_name.endswith('^{}')):
|
2010-03-29 09:05:59 +04:00
|
|
|
self.git.refs[ref_name] = sha
|
2009-06-23 22:22:49 +04:00
|
|
|
|
2010-12-21 07:37:07 +03:00
|
|
|
tf = open(tagfile, 'wb')
|
|
|
|
for tag, node in tags.iteritems():
|
|
|
|
tf.write('%s %s\n' % (node, tag))
|
|
|
|
tf.close()
|
|
|
|
|
2009-06-23 22:22:49 +04:00
|
|
|
|
2009-06-16 16:39:11 +04:00
|
|
|
## UTILITY FUNCTIONS
|
|
|
|
|
2009-04-29 22:50:56 +04:00
|
|
|
def convert_git_int_mode(self, mode):
|
2009-07-07 20:05:43 +04:00
|
|
|
# TODO: make these into constants
|
2009-04-29 22:50:56 +04:00
|
|
|
convert = {
|
2009-05-19 16:58:06 +04:00
|
|
|
0100644: '',
|
|
|
|
0100755: 'x',
|
|
|
|
0120000: 'l'}
|
2009-04-29 22:50:56 +04:00
|
|
|
if mode in convert:
|
|
|
|
return convert[mode]
|
|
|
|
return ''
|
2009-04-30 03:18:37 +04:00
|
|
|
|
2009-05-01 00:54:33 +04:00
|
|
|
def extract_hg_metadata(self, message):
|
2009-07-29 02:07:37 +04:00
|
|
|
split = message.split("\n--HG--\n", 1)
|
2009-05-01 00:54:33 +04:00
|
|
|
renames = {}
|
2009-05-30 00:10:14 +04:00
|
|
|
extra = {}
|
2009-05-08 02:07:18 +04:00
|
|
|
branch = False
|
2009-05-01 00:54:33 +04:00
|
|
|
if len(split) == 2:
|
|
|
|
message, meta = split
|
|
|
|
lines = meta.split("\n")
|
|
|
|
for line in lines:
|
|
|
|
if line == '':
|
2009-06-05 14:56:22 +04:00
|
|
|
continue
|
|
|
|
|
2009-05-01 00:54:33 +04:00
|
|
|
command, data = line.split(" : ", 1)
|
2009-06-05 14:56:22 +04:00
|
|
|
|
2009-05-01 00:54:33 +04:00
|
|
|
if command == 'rename':
|
|
|
|
before, after = data.split(" => ", 1)
|
|
|
|
renames[after] = before
|
|
|
|
if command == 'branch':
|
2009-05-08 02:07:18 +04:00
|
|
|
branch = data
|
2009-05-30 00:10:14 +04:00
|
|
|
if command == 'extra':
|
|
|
|
before, after = data.split(" : ", 1)
|
|
|
|
extra[before] = urllib.unquote(after)
|
2009-06-30 02:20:37 +04:00
|
|
|
return (message, renames, branch, extra)
|
2009-06-05 14:56:22 +04:00
|
|
|
|
2009-07-03 00:58:51 +04:00
|
|
|
def get_file(self, commit, f):
|
|
|
|
otree = self.git.tree(commit.tree)
|
|
|
|
parts = f.split('/')
|
|
|
|
for part in parts:
|
|
|
|
(mode, sha) = otree[part]
|
|
|
|
obj = self.git.get_object(sha)
|
|
|
|
if isinstance (obj, Blob):
|
|
|
|
return (mode, sha, obj._text)
|
|
|
|
elif isinstance(obj, Tree):
|
|
|
|
otree = obj
|
|
|
|
|
|
|
|
def get_files_changed(self, commit):
|
2009-12-25 10:56:20 +03:00
|
|
|
tree = commit.tree
|
|
|
|
btree = None
|
|
|
|
|
|
|
|
if commit.parents:
|
|
|
|
btree = self.git[commit.parents[0]].tree
|
|
|
|
|
|
|
|
changes = self.git.object_store.tree_changes(btree, tree)
|
|
|
|
files = {}
|
|
|
|
for (oldfile, newfile), (oldmode, newmode), (oldsha, newsha) in changes:
|
2010-02-24 00:23:09 +03:00
|
|
|
# don't create new submodules
|
|
|
|
if newmode == 0160000:
|
|
|
|
if oldfile:
|
|
|
|
# become a regular delete
|
|
|
|
newfile, newmode = None, None
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
# so old submodules shoudn't exist
|
|
|
|
if oldmode == 0160000:
|
|
|
|
if newfile:
|
|
|
|
# become a regular add
|
|
|
|
oldfile, oldmode = None, None
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
|
2009-12-25 10:56:20 +03:00
|
|
|
if newfile is None:
|
|
|
|
file = oldfile
|
|
|
|
delete = True
|
|
|
|
else:
|
|
|
|
file = newfile
|
|
|
|
delete = False
|
|
|
|
|
|
|
|
files[file] = (delete, newmode, newsha)
|
|
|
|
|
|
|
|
return files
|
2009-07-03 00:58:51 +04:00
|
|
|
|
2009-06-16 17:44:19 +04:00
|
|
|
def remote_name(self, remote):
|
|
|
|
names = [name for name, path in self.paths if path == remote]
|
|
|
|
if names:
|
|
|
|
return names[0]
|
|
|
|
|
2009-06-18 19:49:13 +04:00
|
|
|
# Stolen from hgsubversion
|
|
|
|
def swap_out_encoding(self, new_encoding='UTF-8'):
|
|
|
|
try:
|
|
|
|
from mercurial import encoding
|
|
|
|
old = encoding.encoding
|
|
|
|
encoding.encoding = new_encoding
|
|
|
|
except ImportError:
|
|
|
|
old = hgutil._encoding
|
|
|
|
hgutil._encoding = new_encoding
|
|
|
|
return old
|
|
|
|
|
|
|
|
def decode_guess(self, string, encoding):
|
|
|
|
# text is not valid utf-8, try to make sense of it
|
|
|
|
if encoding:
|
|
|
|
try:
|
|
|
|
return string.decode(encoding).encode('utf-8')
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
return string.decode('latin-1').encode('utf-8')
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
return string.decode('ascii', 'replace').encode('utf-8')
|
|
|
|
|
2009-04-24 02:26:10 +04:00
|
|
|
def get_transport_and_path(self, uri):
|
2010-12-23 01:57:26 +03:00
|
|
|
# pass hg's ui.ssh config to dulwich
|
|
|
|
if not issubclass(client.get_ssh_vendor, _ssh.SSHVendor):
|
|
|
|
client.get_ssh_vendor = _ssh.generate_ssh_vendor(self.ui)
|
|
|
|
|
2012-01-27 22:24:31 +04:00
|
|
|
# Test for git:// and git+ssh:// URI.
|
|
|
|
# Support several URL forms, including separating the
|
|
|
|
# host and path with either a / or : (sepr)
|
|
|
|
git_pattern = re.compile(
|
|
|
|
r'^(?P<scheme>git([+]ssh)?://)(?P<host>.*?)(:(?P<port>\d+))?'
|
|
|
|
r'(?P<sepr>[:/])(?P<path>.*)$'
|
|
|
|
)
|
|
|
|
git_match = git_pattern.match(uri)
|
|
|
|
if git_match:
|
|
|
|
res = git_match.groupdict()
|
|
|
|
transport = client.SSHGitClient if 'ssh' in res['scheme'] else client.TCPGitClient
|
|
|
|
host, port, sepr, path = res['host'], res['port'], res['sepr'], res['path']
|
|
|
|
if sepr == '/':
|
|
|
|
path = '/' + path
|
2012-01-28 07:48:55 +04:00
|
|
|
# strip trailing slash for heroku-style URLs
|
|
|
|
# ssh+git://git@heroku.com:project.git/
|
|
|
|
if sepr == ':' and path.endswith('.git/'):
|
|
|
|
path = path.rstrip('/')
|
2012-01-27 22:24:31 +04:00
|
|
|
if port:
|
|
|
|
client.port = port
|
|
|
|
|
|
|
|
return transport(host, thin_packs=False, port=port), path
|
2011-10-06 00:44:29 +04:00
|
|
|
|
|
|
|
httpclient = getattr(client, 'HttpGitClient', None)
|
|
|
|
|
|
|
|
if uri.startswith('git+http://') or uri.startswith('git+https://'):
|
|
|
|
uri = uri[4:]
|
|
|
|
|
|
|
|
if uri.startswith('http://') or uri.startswith('https://'):
|
|
|
|
if not httpclient:
|
|
|
|
raise RepoError('git via HTTP requires dulwich 0.8.1 or later')
|
|
|
|
else:
|
|
|
|
return client.HttpGitClient(uri, thin_packs=False), uri
|
|
|
|
|
2009-04-24 02:26:10 +04:00
|
|
|
# if its not git or git+ssh, try a local url..
|
2010-02-25 06:08:38 +03:00
|
|
|
return client.SubprocessGitClient(thin_packs=False), uri
|