def unbundler(bundle): if unbundle20 and isinstance(bundle, unbundle20): parts = iter(bundle.iterparts()) for part in parts: if part.type != 'changegroup': logging.getLogger('bundle2').warning( 'ignoring bundle2 part: %s', part.type) continue logging.getLogger('bundle2').debug('part: %s', part.type) logging.getLogger('bundle2').debug('params: %r', part.params) version = part.params.get('version', '01') if version == '01': cg = cg1unpacker(part, 'UN') elif version == '02': cg = cg2unpacker(part, 'UN') else: raise Exception('Unknown changegroup version %s' % version) break else: raise Exception('No changegroups in the bundle') else: cg = bundle yield chunks_in_changegroup(cg, 'changeset') yield chunks_in_changegroup(cg, 'manifest') yield iterate_files(cg) if unbundle20 and isinstance(bundle, unbundle20): for part in parts: logging.getLogger('bundle2').warning( 'ignoring bundle2 part: %s', part.type)
def unbundler(bundle): if unbundle20 and isinstance(bundle, unbundle20): parts = iter(bundle.iterparts()) for part in parts: if part.type != 'changegroup': logging.getLogger('bundle2').warning( 'ignoring bundle2 part: %s', part.type) continue logging.getLogger('bundle2').debug('params: %r', part.params) version = part.params.get('version', '01') if version == '01': cg = cg1unpacker(part, 'UN') elif version == '02': cg = cg2unpacker(part, 'UN') else: raise Exception('Unknown changegroup version %s' % version) break else: raise Exception('No changegroups in the bundle') else: cg = bundle yield chunks_in_changegroup(cg) yield chunks_in_changegroup(cg) yield iterate_files(cg) if unbundle20 and isinstance(bundle, unbundle20): for part in parts: logging.getLogger('bundle2').warning( 'ignoring bundle2 part: %s', part.type)
def readbundle(fh): header = changegroup.readexactly(fh, 4) magic, version = header[0:2], header[2:4] if magic != 'HG': raise Exception('%s: not a Mercurial bundle' % fh.name) if version != '10': raise Exception('%s: unsupported bundle version %s' % (fh.name, version)) alg = changegroup.readexactly(fh, 2) return cg1unpacker(fh, alg)
def push(repo, store, what, repo_heads, repo_branches): fast_import = FastImport() store.init_fast_import(fast_import) def heads(): for sha1 in store.heads(repo_branches): yield '^%s' % store.changeset_ref(sha1) def local_bases(): for c in Git.iter('rev-list', '--stdin', '--topo-order', '--full-history', '--boundary', *(w for w in what if w), stdin=heads()): if c[0] != '-': continue yield store.hg_changeset(c[1:]) for w in what: rev = store.hg_changeset(w) if rev: yield rev common = findcommon(repo, store, set(local_bases())) logging.info('common: %s' % common) def revs(): for sha1 in common: yield '^%s' % store.changeset_ref(sha1) push_commits = list( Git.iter('rev-list', '--stdin', '--topo-order', '--full-history', '--parents', '--reverse', *(w for w in what if w), stdin=revs())) pushed = False if push_commits: chunks = util.chunkbuffer(create_bundle(store, push_commits)) cg = cg1unpacker(chunks, 'UN') if all(v[1] for v in what.values()): repo_heads = ['force'] else: repo_heads = [unhexlify(h) for h in repo_heads] if repo.local(): repo.local().ui.setconfig('server', 'validate', True) pushed = repo.unbundle(cg, repo_heads, '') != 0 return gitdag(push_commits) if pushed else ()
def unbundle_fh(fh, path): header = readexactly(fh, 4) magic, version = header[0:2], header[2:4] if magic != 'HG': raise Exception('%s: not a Mercurial bundle' % path) if version == '10': alg = readexactly(fh, 2) return cg1unpacker(fh, alg) elif unbundle20 and version.startswith('2'): return getunbundler(get_ui(), fh, header) else: raise Exception('%s: unsupported bundle version %s' % (path, version))
def push(repo, store, what, repo_heads, repo_branches): store.init_fast_import() def heads(): for sha1 in store.heads(repo_branches): yield '^%s' % store.changeset_ref(sha1) def local_bases(): for c in Git.iter('rev-list', '--stdin', '--topo-order', '--full-history', '--boundary', *(w for w in what if w), stdin=heads()): if c[0] != '-': continue yield store.hg_changeset(c[1:]) for w in what: rev = store.hg_changeset(w) if rev: yield rev common = findcommon(repo, store, set(local_bases())) logging.info('common: %s' % common) def revs(): for sha1 in common: yield '^%s' % store.changeset_ref(sha1) push_commits = list(Git.iter('rev-list', '--stdin', '--topo-order', '--full-history', '--parents', '--reverse', *(w for w in what if w), stdin=revs())) pushed = False if push_commits: has_root = any(len(p) == 40 for p in push_commits) force = all(v[1] for v in what.values()) if has_root and repo_heads: if not force: raise Exception('Cannot push a new root') else: logging.warn('Pushing a new root') chunks = util.chunkbuffer(create_bundle(store, push_commits)) cg = cg1unpacker(chunks, 'UN') if force: repo_heads = ['force'] else: if not repo_heads: repo_heads = [NULL_NODE_ID] repo_heads = [unhexlify(h) for h in repo_heads] if repo.local(): repo.local().ui.setconfig('server', 'validate', True) pushed = repo.unbundle(cg, repo_heads, '') != 0 return gitdag(push_commits) if pushed else ()
def unbundle_fh(fh, path): header = readexactly(fh, 4) magic, version = header[0:2], header[2:4] if magic != b'HG': raise Exception('%s: not a Mercurial bundle' % fsdecode(path)) if version == b'10': alg = readexactly(fh, 2) return cg1unpacker(fh, alg) elif unbundle20 and version.startswith(b'2'): return unbundle20(get_ui(), fh) else: raise Exception('%s: unsupported bundle version %s' % (fsdecode(path), version.decode('ascii')))
def unbundle_fh(fh, path): header = readexactly(fh, 4) magic, version = header[0:2], header[2:4] if magic != 'HG': raise Exception('%s: not a Mercurial bundle' % path) if version == '10': alg = readexactly(fh, 2) return cg1unpacker(fh, alg) elif unbundle20 and version.startswith('2'): return unbundle20(get_ui(), fh) else: raise Exception('%s: unsupported bundle version %s' % (path, version))
def __init__(self, path): self._url = path fh = open(path, 'r') header = readexactly(fh, 4) magic, version = header[0:2], header[2:4] if magic != 'HG': raise Exception('%s: not a Mercurial bundle' % path) if version == '10': alg = readexactly(fh, 2) self._bundle = cg1unpacker(fh, alg) elif unbundle20 and version.startswith('2'): self._bundle = getunbundler(get_ui(), fh, magicstring=header) else: raise Exception('%s: unsupported bundle version %s' % (path, version)) self._file = os.path.basename(path)
def push(repo, store, what, repo_heads, repo_branches, dry_run=False): def heads(): for sha1 in store.heads(repo_branches): yield '^%s' % store.changeset_ref(sha1) def local_bases(): h = chain(heads(), (w for w in what if w)) for c, t, p in GitHgHelper.rev_list('--topo-order', '--full-history', '--boundary', *h): if c[0] != '-': continue yield store.hg_changeset(c[1:]) for w in what: rev = store.hg_changeset(w) if rev: yield rev common = findcommon(repo, store, set(local_bases())) logging.info('common: %s', common) def revs(): for sha1 in common: yield '^%s' % store.changeset_ref(sha1) revs = chain(revs(), (w for w in what if w)) push_commits = list((c, p) for c, t, p in GitHgHelper.rev_list( '--topo-order', '--full-history', '--parents', '--reverse', *revs)) pushed = False if push_commits: has_root = any(len(p) == 40 for p in push_commits) force = all(v[1] for v in what.values()) if has_root and repo_heads: if not force: raise Exception('Cannot push a new root') else: logging.warn('Pushing a new root') if force: repo_heads = ['force'] else: if not repo_heads: repo_heads = [NULL_NODE_ID] repo_heads = [unhexlify(h) for h in repo_heads] if push_commits and not dry_run: if repo.local(): repo.local().ui.setconfig('server', 'validate', True) b2caps = bundle2caps(repo) if unbundle20 else {} logging.getLogger('bundle2').debug('%r', b2caps) if b2caps: b2caps['replycaps'] = encodecaps({'error': ['abort']}) cg = create_bundle(store, push_commits, b2caps) if not isinstance(repo, HelperRepo): cg = util.chunkbuffer(cg) if not b2caps: cg = cg1unpacker(cg, 'UN') reply = repo.unbundle(cg, repo_heads, '') if unbundle20 and isinstance(reply, unbundle20): parts = iter(reply.iterparts()) for part in parts: logging.getLogger('bundle2').debug('part: %s', part.type) logging.getLogger('bundle2').debug('params: %r', part.params) if part.type == 'output': sys.stderr.write(part.read()) elif part.type == 'reply:changegroup': # TODO: should check params['in-reply-to'] reply = int(part.params['return']) elif part.type == 'error:abort': raise error.Abort(part.params['message'], hint=part.params.get('hint')) else: logging.getLogger('bundle2').warning( 'ignoring bundle2 part: %s', part.type) pushed = reply != 0 return gitdag(push_commits) if pushed or dry_run else ()
def push(repo, store, what, repo_heads, repo_branches, dry_run=False): def heads(): for sha1 in store.heads(repo_branches): yield b'^%s' % store.changeset_ref(sha1) def local_bases(): h = chain(heads(), (w for w, _, _ in what if w)) for c, t, p in GitHgHelper.rev_list(b'--topo-order', b'--full-history', b'--boundary', *h): if c[:1] != b'-': continue yield store.hg_changeset(c[1:]) for w, _, _ in what: if w: rev = store.hg_changeset(w) if rev: yield rev common = findcommon(repo, store, set(local_bases())) logging.info('common: %s', common) def revs(): for sha1 in common: yield b'^%s' % store.changeset_ref(sha1) revs = chain(revs(), (w for w, _, _ in what if w)) push_commits = list((c, p) for c, t, p in GitHgHelper.rev_list( b'--topo-order', b'--full-history', b'--parents', b'--reverse', *revs)) pushed = False if push_commits: has_root = any(not p for (c, p) in push_commits) force = all(v for _, _, v in what) if has_root and repo_heads: if not force: raise Exception('Cannot push a new root') else: logging.warn('Pushing a new root') if force: repo_heads = [b'force'] else: if not repo_heads: repo_heads = [NULL_NODE_ID] repo_heads = [unhexlify(h) for h in repo_heads] if push_commits and not dry_run: if repo.local(): repo.local().ui.setconfig(b'server', b'validate', True) if unbundle20: b2caps = repo.capable(b'bundle2') or {} else: b2caps = {} if b2caps: b2caps = decodecaps(unquote_to_bytes(b2caps)) logging.getLogger('bundle2').debug('%r', b2caps) if b2caps: b2caps[b'replycaps'] = encodecaps({b'error': [b'abort']}) cg = create_bundle(store, push_commits, b2caps) if not isinstance(repo, HelperRepo): cg = chunkbuffer(cg) if not b2caps: cg = cg1unpacker(cg, b'UN') reply = repo.unbundle(cg, repo_heads, b'') if unbundle20 and isinstance(reply, unbundle20): parts = iter(reply.iterparts()) for part in parts: logging.getLogger('bundle2').debug('part: %s', part.type) logging.getLogger('bundle2').debug('params: %r', part.params) if part.type == b'output': sys.stderr.write(fsdecode(part.read())) elif part.type == b'reply:changegroup': # TODO: should check params['in-reply-to'] reply = int(part.params[b'return']) elif part.type == b'error:abort': message = part.params[b'message'].decode('utf-8') hint = part.params.get(b'hint') if hint: message += '\n\n' + hint.decode('utf-8') raise Exception(message) else: logging.getLogger(b'bundle2').warning( 'ignoring bundle2 part: %s', part.type) pushed = reply != 0 return gitdag(push_commits) if pushed or dry_run else ()
def push(repo, store, what, repo_heads, repo_branches): store.init_fast_import() def heads(): for sha1 in store.heads(repo_branches): yield '^%s' % store.changeset_ref(sha1) def local_bases(): for c in Git.iter('rev-list', '--stdin', '--topo-order', '--full-history', '--boundary', *(w for w in what if w), stdin=heads()): if c[0] != '-': continue yield store.hg_changeset(c[1:]) for w in what: rev = store.hg_changeset(w) if rev: yield rev common = findcommon(repo, store, set(local_bases())) logging.info('common: %s' % common) def revs(): for sha1 in common: yield '^%s' % store.changeset_ref(sha1) push_commits = list(Git.iter('rev-list', '--stdin', '--topo-order', '--full-history', '--parents', '--reverse', *(w for w in what if w), stdin=revs())) pushed = False if push_commits: has_root = any(len(p) == 40 for p in push_commits) force = all(v[1] for v in what.values()) if has_root and repo_heads: if not force: raise Exception('Cannot push a new root') else: logging.warn('Pushing a new root') if force: repo_heads = ['force'] else: if not repo_heads: repo_heads = [NULL_NODE_ID] repo_heads = [unhexlify(h) for h in repo_heads] if repo.local(): repo.local().ui.setconfig('server', 'validate', True) b2caps = bundle2caps(repo) if unbundle20 else {} if b2caps and (repo.url().startswith(('http://', 'https://')) or not isinstance(repo, HelperRepo)): b2caps['replycaps'] = True cg = create_bundle(store, push_commits, b2caps) if not isinstance(repo, HelperRepo): cg = util.chunkbuffer(cg) if not b2caps: cg = cg1unpacker(cg, 'UN') reply = repo.unbundle(cg, repo_heads, '') if unbundle20 and isinstance(reply, unbundle20): parts = iter(reply.iterparts()) for part in parts: if part.type == 'output': sys.stderr.write(part.read()) elif part.type == 'reply:changegroup': # TODO: should check params['in-reply-to'] reply = int(part.params['return']) else: logging.getLogger('bundle2').warning( 'ignoring bundle2 part: %s', part.type) pushed = reply != 0 return gitdag(push_commits) if pushed else ()
def push(repo, store, what, repo_heads, repo_branches, dry_run=False): def heads(): for sha1 in store.heads(repo_branches): yield '^%s' % store.changeset_ref(sha1) def local_bases(): h = chain(heads(), (w for w in what if w)) for c, t, p in GitHgHelper.rev_list('--topo-order', '--full-history', '--boundary', *h): if c[0] != '-': continue yield store.hg_changeset(c[1:]) for w in what: rev = store.hg_changeset(w) if rev: yield rev common = findcommon(repo, store, set(local_bases())) logging.info('common: %s', common) def revs(): for sha1 in common: yield '^%s' % store.changeset_ref(sha1) revs = chain(revs(), (w for w in what if w)) push_commits = list((c, p) for c, t, p in GitHgHelper.rev_list( '--topo-order', '--full-history', '--parents', '--reverse', *revs)) pushed = False if push_commits: has_root = any(len(p) == 40 for p in push_commits) force = all(v[1] for v in what.values()) if has_root and repo_heads: if not force: raise Exception('Cannot push a new root') else: logging.warn('Pushing a new root') if force: repo_heads = ['force'] else: if not repo_heads: repo_heads = [NULL_NODE_ID] repo_heads = [unhexlify(h) for h in repo_heads] if push_commits and not dry_run: if repo.local(): repo.local().ui.setconfig('server', 'validate', True) if unbundle20: b2caps = repo.capable('bundle2') or {} else: b2caps = {} if b2caps: b2caps = decodecaps(urllib.unquote(b2caps)) logging.getLogger('bundle2').debug('%r', b2caps) if b2caps: b2caps['replycaps'] = encodecaps({'error': ['abort']}) cg = create_bundle(store, push_commits, b2caps) if not isinstance(repo, HelperRepo): cg = chunkbuffer(cg) if not b2caps: cg = cg1unpacker(cg, 'UN') reply = repo.unbundle(cg, repo_heads, '') if unbundle20 and isinstance(reply, unbundle20): parts = iter(reply.iterparts()) for part in parts: logging.getLogger('bundle2').debug('part: %s', part.type) logging.getLogger('bundle2').debug('params: %r', part.params) if part.type == 'output': sys.stderr.write(part.read()) elif part.type == 'reply:changegroup': # TODO: should check params['in-reply-to'] reply = int(part.params['return']) elif part.type == 'error:abort': raise error.Abort(part.params['message'], hint=part.params.get('hint')) else: logging.getLogger('bundle2').warning( 'ignoring bundle2 part: %s', part.type) pushed = reply != 0 return gitdag(push_commits) if pushed or dry_run else ()