def test_notes(self): HEX = [b'%c' % c for c in b'0123456789abcdef'] self.assertEqual(GitHgHelper.put_blob(b''), EMPTY_BLOB) for prefix_len in range(20): prefix = b'0' * prefix_len for n in HEX: GitHgHelper.set(b'file', (prefix + b'0' + n + NULL_NODE_ID)[:40], EMPTY_BLOB) for o in HEX: GitHgHelper.set(b'file', (prefix + b'1' + n + o + NULL_NODE_ID)[:40], EMPTY_BLOB) for n in HEX: self.assertEqual(GitHgHelper.hg2git(prefix + b'0' + n), EMPTY_BLOB) for l in range(prefix_len + 1): self.assertEqual(GitHgHelper.hg2git((prefix + b'0')[:l + 1]), NULL_NODE_ID) self.assertEqual(GitHgHelper.hg2git((prefix + b'1')[:l + 1]), NULL_NODE_ID) for n in HEX: for l in range(prefix_len + 2): self.assertEqual( GitHgHelper.hg2git((prefix + b'1' + n)[:l + 1]), NULL_NODE_ID) for o in HEX: self.assertEqual(GitHgHelper.hg2git(prefix + b'1' + n + o), EMPTY_BLOB)
def close(self, rollback=False): if rollback: GitHgHelper.close(rollback) self._closed = True if self._closed: return super(PushStore, self).close()
def __call__(self, store): changeset_chunks = ChunksCollection(progress_iter( 'Reading %d changesets', next(self._bundle, None))) for mn in progress_iter( 'Reading and importing %d manifests', iter_initialized(store.manifest, iter_chunks(next(self._bundle, None), ManifestInfo))): store.store_manifest(mn) for rev_chunk in progress_iter( 'Reading and importing %d files', next(self._bundle, None)): GitHgHelper.store('file', rev_chunk) if next(self._bundle, None) is not None: assert False del self._bundle for cs in progress_iter( 'Importing %d changesets', changeset_chunks.iter_initialized(lambda x: x, store.changeset, Changeset.from_chunk)): try: store.store_changeset(cs) except NothingToGraftException: logging.warn('Cannot graft %s, not importing.', cs.node)
def tearDown(self): GitHgHelper.close(rollback=True) GitHgHelper._helper = False shutil.rmtree(os.environ['GIT_DIR']) if self.git_dir is None: del os.environ['GIT_DIR'] else: os.environ['GIT_DIR'] = self.git_dir
def _create_file_internal(self, sha1, parent1=NULL_NODE_ID, parent2=NULL_NODE_ID): hg_file = File() hg_file.content = GitHgHelper.cat_file(b'blob', sha1) FileFindParents.set_parents(hg_file, parent1, parent2) node = hg_file.node = hg_file.sha1 GitHgHelper.set(b'file', node, sha1) return hg_file
def tearDown(self): GitHgHelper.close(rollback=True) GitHgHelper._helper = False GitHgHelper.git2hg.invalidate() GitHgHelper.hg2git.invalidate() GitHgHelper._cat_commit.invalidate() shutil.rmtree(os.environ['GIT_DIR']) if self.git_dir is None: del os.environ['GIT_DIR'] else: os.environ['GIT_DIR'] = self.git_dir
def _create_file_internal(self, sha1, parent1=NULL_NODE_ID, parent2=NULL_NODE_ID, git_manifest_parents=None, path=None): hg_file = File() hg_file.content = GitHgHelper.cat_file('blob', sha1) FileFindParents.set_parents( hg_file, parent1, parent2, git_manifest_parents=git_manifest_parents, path=path) node = hg_file.node = hg_file.sha1 GitHgHelper.set('file', node, sha1) return hg_file
def do_rollback(ref): checked = Git.resolve_ref('refs/cinnabar/checked') if ref: sha1 = Git.resolve_ref(ref) if not sha1: logging.error('Invalid ref: %s', ref) return 1 if sha1 != NULL_NODE_ID: # Validate that the sha1 is in the history of the current metadata metadata = Git.resolve_ref('refs/cinnabar/metadata') while metadata and metadata != sha1: previous_metadata = get_previous_metadata(metadata) if checked == metadata: checked = previous_metadata metadata = previous_metadata if not metadata: logging.error('Cannot rollback to %s, it is not in the ' 'history of the current metadata.', ref) return 1 else: metadata = Git.resolve_ref('refs/cinnabar/metadata') if metadata: sha1 = get_previous_metadata(metadata) or NULL_NODE_ID else: sha1 = NULL_NODE_ID if checked and checked == metadata: checked = sha1 refs = VersionedDict( (ref, commit) for commit, ref in Git.for_each_ref('refs/cinnabar', 'refs/notes/cinnabar') ) for ref in refs: if sha1 == NULL_NODE_ID or ref not in (b'refs/cinnabar/checked', b'refs/cinnabar/broken'): del refs[ref] if sha1 != NULL_NODE_ID: refs[b'refs/cinnabar/metadata'] = sha1 if checked: refs[b'refs/cinnabar/checked'] = checked for line in Git.ls_tree(sha1): mode, typ, commit, path = line refs[b'refs/cinnabar/replace/%s' % path] = commit for status, ref, commit in refs.iterchanges(): if status == VersionedDict.REMOVED: Git.delete_ref(ref) else: Git.update_ref(ref, commit) GitHgHelper.close(rollback=False) return 0
def do_rollback(ref): checked = Git.resolve_ref('refs/cinnabar/checked') if ref: sha1 = Git.resolve_ref(ref) if not sha1: logging.error('Invalid ref: %s', ref) return 1 if sha1 != NULL_NODE_ID: # Validate that the sha1 is in the history of the current metadata metadata = Git.resolve_ref('refs/cinnabar/metadata') while metadata and metadata != sha1: previous_metadata = get_previous_metadata(metadata) if checked == metadata: checked = previous_metadata metadata = previous_metadata if not metadata: logging.error('Cannot rollback to %s, it is not in the ' 'history of the current metadata.', ref) return 1 else: metadata = Git.resolve_ref('refs/cinnabar/metadata') if metadata: sha1 = get_previous_metadata(metadata) or NULL_NODE_ID else: sha1 = NULL_NODE_ID if checked and checked == metadata: checked = sha1 refs = VersionedDict( (ref, commit) for commit, ref in Git.for_each_ref('refs/cinnabar', 'refs/notes/cinnabar') ) for ref in refs: if ref not in ('refs/cinnabar/checked', 'refs/cinnabar/broken'): del refs[ref] if sha1 != NULL_NODE_ID: refs['refs/cinnabar/metadata'] = sha1 if checked: refs['refs/cinnabar/checked'] = checked for line in Git.ls_tree(sha1): mode, typ, commit, path = line refs['refs/cinnabar/replace/%s' % path] = commit for status, ref, commit in refs.iterchanges(): if status == VersionedDict.REMOVED: Git.delete_ref(ref) else: Git.update_ref(ref, commit) GitHgHelper.close(rollback=False) return 0
def create_copy(self, hg_source, sha1, git_manifest_parents=None, path=None): path, rev = hg_source hg_file = File() hg_file.metadata = { 'copy': path, 'copyrev': rev, } hg_file.content = GitHgHelper.cat_file('blob', sha1) node = hg_file.node = hg_file.sha1 self._pushed.add(node) self._fast_import.put_blob(str(hg_file.metadata), want_sha1=False) GitHgHelper.set('file-meta', node, ':1') GitHgHelper.set('file', node, sha1) return node
def bundle(args): '''create a mercurial bundle''' bundle_commits = list((c, p) for c, t, p in GitHgHelper.rev_list( '--topo-order', '--full-history', '--parents', '--reverse', *args.rev)) if bundle_commits: # TODO: better UX. For instance, this will fail with an exception when # the parent commit doesn't have mercurial metadata. GRAFT = { None: False, 'false': False, 'true': True, } try: graft = Git.config('cinnabar.graft', values=GRAFT) except InvalidConfig as e: logging.error(e.message) return 1 store = PushStore(graft=graft) if args.version == 1: b2caps = {} elif args.version == 2: b2caps = { 'HG20': (), 'changegroup': ('01', '02'), } with open(args.path, 'wb') as fh: if not b2caps: fh.write('HG10UN') for data in create_bundle(store, bundle_commits, b2caps): fh.write(data) store.close(rollback=True)
def __init__(self, bundle): self._bundle = bundle self._use_store_changegroup = False if GitHgHelper.supports(GitHgHelper.STORE_CHANGEGROUP) and \ experiment('store-changegroup'): self._use_store_changegroup = True self._bundle = store_changegroup(bundle)
def __call__(self, parser, namespace, values, option_string=None): if values == 'cinnabar' or not values: print VERSION if values == 'module' or not values: # Import the remote_helper module, that is not imported by # git-cinnabar import cinnabar.remote_helper # Import the bdiff module, that is only imported if mercurial is # not installed import cinnabar.bdiff cinnabar_path = os.path.dirname(cinnabar.__file__) sha1 = tree_hash(iter_modules_in_path(cinnabar_path), cinnabar_path) if not values: print 'module-hash:', sha1 else: print sha1 if values == 'helper' or not values: from cinnabar.helper import GitHgHelper try: with GitHgHelper.query('version') as out: version = out.read(40) except Exception: version = 'unknown' sha1 = helper_hash() or 'unknown' if version != sha1: sha1 = '%s/%s' % (version, sha1) if not values: print 'helper-hash:', sha1 else: print sha1 parser.exit()
def do_one(arg): data = GitHgHelper.git2hg(arg.encode('ascii')) if data: assert data.startswith(b'changeset ') bytes_stdout.write(data[10:10 + args.abbrev]) else: bytes_stdout.write(NULL_NODE_ID[:args.abbrev]) bytes_stdout.write(b'\n')
def create_file(self, sha1, *parents): hg_file = GeneratedFileRev(NULL_NODE_ID, GitHgHelper.cat_file('blob', sha1)) hg_file.set_parents(*parents) node = hg_file.node = hg_file.sha1 self._push_files[node] = hg_file self._files.setdefault(node, PseudoString(sha1)) self._git_files.setdefault(node, PseudoString(sha1)) return node
def setUp(self): self.git_dir = os.environ.get('GIT_DIR') tmpdir = tempfile.mkdtemp() Git.run('init', '--bare', tmpdir, stdout=open(os.devnull, 'w')) os.environ['GIT_DIR'] = tmpdir os.environ['GIT_CINNABAR_EXPERIMENTS'] = \ 'store' if self.NEW_STORE else '' self.assertEquals( GitHgHelper.supports((b'store', b'new')), self.NEW_STORE)
def main(args): cmd = args.pop(0) if cmd == 'data': store = GitHgStore() if args[0] == '-c': sys.stdout.write(store.changeset(args[1]).data) elif args[0] == '-m': sys.stdout.write(store.manifest(args[1]).data) store.close() elif cmd == 'fsck': return fsck(args) elif cmd == 'reclone': for ref in Git.for_each_ref('refs/cinnabar', 'refs/remote-hg', 'refs/notes/cinnabar', 'refs/notes/remote-hg/git2hg', format='%(refname)'): Git.delete_ref(ref) Git.close() for line in Git.iter('config', '--get-regexp', 'remote\..*\.url'): config, url = line.split() name = config[len('remote.'):-len('.url')] skip_pref = 'remote.%s.skipDefaultUpdate' % name if (url.startswith('hg::') and Git.config(skip_pref, 'bool') != 'true'): Git.run('remote', 'update', '--prune', name) print 'Please note that reclone left your local branches untouched.' print 'They may be based on entirely different commits.' elif cmd == 'hg2git': for arg in args: print GitHgHelper.hg2git(arg) elif cmd == 'git2hg': for arg in args: data = GitHgHelper.git2hg(arg) if data: data = ChangesetData.parse(data) print data.get('changeset', NULL_NODE_ID) else: print NULL_NODE_ID else: print >> sys.stderr, 'Unknown command:', cmd return 1
def __call__(self, store): changeset_chunks = ChunksCollection( progress_iter('Reading {} changesets', next(self._bundle, None))) if experiment('store-manifest'): for rev_chunk in progress_iter( 'Reading and importing {} manifests', next(self._bundle, None)): GitHgHelper.store('manifest', rev_chunk) store.check_manifest(rev_chunk) else: for mn in progress_iter( 'Reading and importing {} manifests', iter_initialized( store.manifest, iter_chunks(next(self._bundle, None), ManifestInfo))): store.store_manifest(mn) def enumerate_files(iter): last_name = None count_names = 0 for count_chunks, (name, chunk) in enumerate(iter): if name != last_name: count_names += 1 last_name = name yield (count_chunks, count_names), chunk for rev_chunk in progress_enum( 'Reading and importing {} revisions of {} files', enumerate_files(next(self._bundle, None))): GitHgHelper.store('file', rev_chunk) if next(self._bundle, None) is not None: assert False del self._bundle for cs in progress_iter( 'Importing {} changesets', changeset_chunks.iter_initialized(lambda x: x, store.changeset, Changeset.from_chunk)): try: store.store_changeset(cs) except NothingToGraftException: logging.warn('Cannot graft %s, not importing.', cs.node)
def helper_version(): from cinnabar.helper import GitHgHelper try: with GitHgHelper.query('revision') as out: version = out.read(40) except Exception: version = 'unknown' sha1 = helper_hash() or 'unknown' return version, sha1
def helper_version(): from cinnabar.helper import GitHgHelper try: with GitHgHelper.query(b'revision') as out: version = out.read(40).decode('ascii') except Exception: version = 'unknown' sha1 = (helper_hash() or b'unknown').decode('ascii') return version, sha1
def main(args): cmd = args.pop(0) if cmd == 'data': store = GitHgStore() if args[0] == '-c': sys.stdout.write(store.changeset(args[1]).data) elif args[0] == '-m': sys.stdout.write(store.manifest(args[1]).data) store.close() elif cmd == 'fsck': return fsck(args) elif cmd == 'reclone': for ref in Git.for_each_ref('refs/cinnabar', 'refs/remote-hg', 'refs/notes/cinnabar', 'refs/notes/remote-hg/git2hg', format='%(refname)'): Git.delete_ref(ref) Git.close() for line in Git.iter('config', '--get-regexp', 'remote\..*\.url'): config, url = line.split() name = config[len('remote.'):-len('.url')] skip_pref = 'remote.%s.skipDefaultUpdate' % name if (url.startswith('hg::') and Git.config(skip_pref, 'bool') != 'true'): Git.run('remote', 'update', '--prune', name) print 'Please note that reclone left your local branches untouched.' print 'They may be based on entirely different commits.' elif cmd == 'hg2git': for arg in args: print GitHgHelper.hg2git(arg) elif cmd == 'git2hg': for arg in args: data = GitHgHelper.git2hg(arg) if data: data = ChangesetData.parse(data) print data.get('changeset', NULL_NODE_ID) else: print NULL_NODE_ID else: print >>sys.stderr, 'Unknown command:', cmd return 1
def git2hg(args): '''convert git sha1 to corresponding mercurial sha1''' for arg in args.sha1: data = GitHgHelper.git2hg(arg) if data: assert data.startswith('changeset ') print(data[10:10 + args.abbrev]) else: print(NULL_NODE_ID[:args.abbrev])
def git2hg(args): '''convert git sha1 to corresponding mercurial sha1''' for arg in args.sha1: data = GitHgHelper.git2hg(arg.encode('ascii')) if data: assert data.startswith(b'changeset ') bytes_stdout.write(data[10:10 + args.abbrev]) else: bytes_stdout.write(NULL_NODE_ID[:args.abbrev]) bytes_stdout.write(b'\n')
def list(self, arg=None): tags = sorted(self._store.tags()) # git fetch does a check-connection that calls # `git rev-list --objects --stdin --not --all` with the list of # sha1s from the list we're about to give it. With no refs on these # exact sha1s, the rev-list can take a long time on large repos. # So we temporarily create refs to make that rev-list faster. for tag, ref in tags: Git.update_ref(b'refs/cinnabar/refs/tags/' + tag, ref) GitHgHelper.reload() for tag, ref in tags: self._helper.write(b'%s refs/tags/%s\n' % (ref, tag)) self._helper.write(b'\n') self._helper.flush() # Now remove the refs. The deletion will only actually be committed # on the store close in main(), after git is done doing # check-connection. for tag, _ in tags: Git.delete_ref(b'refs/cinnabar/refs/tags/' + tag)
def create_copy(self, hg_source, sha1): data = '\1\ncopy: %s\ncopyrev: %s\n\1\n' % hg_source data += GitHgHelper.cat_file('blob', sha1) hg_file = GeneratedFileRev(NULL_NODE_ID, data) hg_file.set_parents() node = hg_file.node = hg_file.sha1 mark = self.file_ref(node, hg2git=False, create=True) self._push_files[node] = hg_file self._files.setdefault(node, mark) self._git_files.setdefault(node, PseudoString(sha1)) return node
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
def git2hg(args): '''convert git sha1 to corresponding mercurial sha1''' for sha1, ref in Git.for_each_ref('refs/cinnabar/replace'): Git._replace[ref[22:]] = sha1 for arg in args.sha1: data = GitHgHelper.git2hg(arg) if data: assert data.startswith('changeset ') print data[10:10 + args.abbrev] else: print NULL_NODE_ID[:args.abbrev]
def git2hg(args): '''convert git sha1 to corresponding mercurial sha1''' for sha1, ref in Git.for_each_ref('refs/cinnabar/replace'): Git._replace[ref[22:]] = sha1 for arg in args.sha1: data = GitHgHelper.git2hg(arg) if data: assert data.startswith('changeset ') print data[10:50] else: print NULL_NODE_ID
def __call__(self, store): changeset_chunks = ChunksCollection(progress_iter( 'Reading {} changesets', next(self._bundle, None))) for rev_chunk in progress_iter( 'Reading and importing {} manifests', next(self._bundle, None)): if not self._use_store_changegroup: GitHgHelper.store('manifest', rev_chunk) def enumerate_files(iter): last_name = None count_names = 0 for count_chunks, (name, chunk) in enumerate(iter, start=1): if name != last_name: count_names += 1 last_name = name yield (count_chunks, count_names), chunk for rev_chunk in progress_enum( 'Reading and importing {} revisions of {} files', enumerate_files(next(self._bundle, None))): if not self._use_store_changegroup: GitHgHelper.store('file', rev_chunk) if next(self._bundle, None) is not None: assert False del self._bundle for cs in progress_iter( 'Importing {} changesets', changeset_chunks.iter_initialized(lambda x: x, store.changeset, Changeset.from_chunk)): try: store.store_changeset(cs) except NothingToGraftException: logging.warn('Cannot graft %s, not importing.', cs.node)
def _create_file_internal(self, sha1, parent1=NULL_NODE_ID, parent2=NULL_NODE_ID, git_manifest_parents=None, path=None): hg_file = GeneratedFileRev(NULL_NODE_ID, GitHgHelper.cat_file('blob', sha1)) hg_file.set_parents(parent1, parent2, git_manifest_parents=git_manifest_parents, path=path) node = hg_file.node = hg_file.sha1 self._git_files.setdefault(node, PseudoString(sha1)) return hg_file
def create_copy(self, hg_source, sha1, git_manifest_parents=None, path=None): data = '\1\ncopy: %s\ncopyrev: %s\n\1\n' % hg_source data += GitHgHelper.cat_file('blob', sha1) hg_file = GeneratedFileRev(NULL_NODE_ID, data) hg_file.set_parents(git_manifest_parents=git_manifest_parents, path=path) node = hg_file.node = hg_file.sha1 mark = self.file_ref(node, hg2git=False, create=True) self._push_files[node] = hg_file self._files.setdefault(node, mark) self._git_files.setdefault(node, PseudoString(sha1)) return node
def create_copy(self, hg_source, sha1, path=None): path, rev = hg_source hg_file = File() hg_file.metadata = { b'copy': path, b'copyrev': rev, } hg_file.content = GitHgHelper.cat_file(b'blob', sha1) node = hg_file.node = hg_file.sha1 self._pushed.add(node) GitHgHelper.put_blob(hg_file.metadata.to_str(), want_sha1=False) GitHgHelper.set(b'file-meta', node, b':1') GitHgHelper.set(b'file', node, sha1) return node
def create_copy(self, hg_source, sha1, git_manifest_parents=None, path=None): path, rev = hg_source hg_file = File() hg_file.metadata = { 'copy': path, 'copyrev': rev, } hg_file.content = GitHgHelper.cat_file('blob', sha1) node = hg_file.node = hg_file.sha1 self._pushed.add(node) GitHgHelper.put_blob(str(hg_file.metadata), want_sha1=False) GitHgHelper.set('file-meta', node, ':1') GitHgHelper.set('file', node, sha1) return node
def upgrade(args): '''upgrade cinnabar metadata''' try: store = GitHgStore() print('No metadata to upgrade') return 2 except UpgradeAbort: store = UpgradeGitHgStore() if not GitHgHelper.upgrade(): print('Cannot finish upgrading... You may need to reclone.') return 1 print('Finalizing upgrade...') store.close(refresh=store.METADATA_REFS) print('You may want to run `git cinnabar fsck` to ensure the upgrade ' 'went well.\n') return 0
def upgrade(args): '''upgrade cinnabar metadata''' try: store = GitHgStore() print 'No metadata to upgrade' return 2 except UpgradeAbort: store = UpgradeGitHgStore() if not GitHgHelper.upgrade(): print 'Cannot finish upgrading... You may need to reclone.' return 1 print 'Finalizing upgrade...' store.close(refresh=store.METADATA_REFS) print ( 'You may want to run `git cinnabar fsck` to ensure the upgrade ' 'went well.\n' ) return 0
def upgrade(args): '''upgrade cinnabar metadata''' try: store = GitHgStore() print 'No metadata to upgrade' return 2 except UpgradeAbort: store = UpgradeGitHgStore() if not GitHgHelper.upgrade(): print 'Cannot finish upgrading... You may need to reclone.' return 1 print 'Finalizing upgrade...' store.close() print( 'You may want to run `git cinnabar fsck --manifests --files` to ' 'ensure the upgrade went well.\n' 'Please be aware this might take a while.') return 0
def store_changegroup(changegroup): changesets = next(changegroup, None) first_changeset = next(changesets, None) version = 1 if isinstance(first_changeset, RawRevChunk02): version = 2 with GitHgHelper.store_changegroup(version) as fh: def iter_chunks(iter): for chunk in iter: fh.write(struct.pack('>l', len(chunk) + 4)) fh.write(chunk) yield chunk fh.write(struct.pack('>l', 0)) yield iter_chunks(chain((first_changeset, ), changesets)) yield iter_chunks(next(changegroup, None)) def iter_files(iter): last_name = None for name, chunk in iter: if name != last_name: if last_name is not None: fh.write(struct.pack('>l', 0)) fh.write(struct.pack('>l', len(name) + 4)) fh.write(name) last_name = name fh.write(struct.pack('>l', len(chunk) + 4)) fh.write(chunk) yield name, chunk if last_name is not None: fh.write(struct.pack('>l', 0)) fh.write(struct.pack('>l', 0)) yield iter_files(next(changegroup, None)) if next(changegroup, None) is not None: assert False
def store_changegroup(changegroup): changesets = next(changegroup, None) first_changeset = next(changesets, None) version = 1 if isinstance(first_changeset, RawRevChunk02): version = 2 with GitHgHelper.store_changegroup(version) as fh: def iter_chunks(iter): for chunk in iter: fh.write(struct.pack('>l', len(chunk) + 4)) fh.write(chunk) yield chunk fh.write(struct.pack('>l', 0)) yield iter_chunks(chain((first_changeset,), changesets)) yield iter_chunks(next(changegroup, None)) def iter_files(iter): last_name = None for name, chunk in iter: if name != last_name: if last_name is not None: fh.write(struct.pack('>l', 0)) fh.write(struct.pack('>l', len(name) + 4)) fh.write(name) last_name = name fh.write(struct.pack('>l', len(chunk) + 4)) fh.write(chunk) yield name, chunk if last_name is not None: fh.write(struct.pack('>l', 0)) fh.write(struct.pack('>l', 0)) yield iter_files(next(changegroup, None)) if next(changegroup, None) is not None: assert False
def findcommon(repo, store, hgheads): logger = logging.getLogger('findcommon') logger.debug(hgheads) if not hgheads: logger.info('no requests') return set() sample_size = 100 sample = _sample(hgheads, sample_size) requests = 1 known = repo.known(unhexlify(h) for h in sample) known = set(h for h, k in izip(sample, known) if k) logger.debug('initial sample size: %d', len(sample)) if len(known) == len(hgheads): logger.debug('all heads known') logger.info('1 request') return hgheads git_heads = set(store.changeset_ref(h) for h in hgheads) git_known = set(store.changeset_ref(h) for h in known) if logger.isEnabledFor(logging.DEBUG): logger.debug('known (sub)set: (%d) %s', len(known), sorted(git_known)) args = ['--topo-order', '--full-history', '--parents'] def revs(): for h in git_known: yield '^%s' % h for h in git_heads: if h not in git_known: yield h args.extend(revs()) revs = ((c, parents) for c, t, parents in GitHgHelper.rev_list(*args)) dag = gitdag(chain(revs, ((k, ()) for k in git_known))) dag.tag_nodes_and_parents(git_known, 'known') def log_dag(tag): if not logger.isEnabledFor(logging.DEBUG): return logger.debug('%s dag size: %d', tag, sum(1 for n in dag.iternodes(tag))) heads = sorted(dag.heads(tag)) logger.debug('%s dag heads: (%d) %s', tag, len(heads), heads) roots = sorted(dag.roots(tag)) logger.debug('%s dag roots: (%d) %s', tag, len(roots), roots) log_dag('unknown') log_dag('known') while True: unknown = set(chain(dag.heads(), dag.roots())) if not unknown: break sample = set(_sample(unknown, sample_size)) if len(sample) < sample_size: sample |= set(_sample(set(dag.iternodes()), sample_size - len(sample))) sample = list(sample) hg_sample = [store.hg_changeset(h) for h in sample] requests += 1 known = repo.known(unhexlify(h) for h in hg_sample) unknown = set(h for h, k in izip(sample, known) if not k) known = set(h for h, k in izip(sample, known) if k) logger.debug('next sample size: %d', len(sample)) if logger.isEnabledFor(logging.DEBUG): logger.debug('known (sub)set: (%d) %s', len(known), sorted(known)) logger.debug('unknown (sub)set: (%d) %s', len(unknown), sorted(unknown)) dag.tag_nodes_and_parents(known, 'known') dag.tag_nodes_and_children(unknown, 'unknown') log_dag('unknown') log_dag('known') logger.info('%d requests', requests) return [store.hg_changeset(h) for h in dag.heads('known')]
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 __init__(self, bundle): self._bundle = bundle self._use_store_changegroup = False if GitHgHelper.supports(GitHgHelper.STORE_CHANGEGROUP): self._use_store_changegroup = True self._bundle = store_changegroup(bundle)
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 ()
def fsck(args): parser = argparse.ArgumentParser() parser.add_argument( '--manifests', action='store_true', help='Validate manifests hashes') parser.add_argument( '--files', action='store_true', help='Validate files hashes') parser.add_argument( 'commit', nargs='*', help='Specific commit or changeset to check') args = parser.parse_args(args) status = { 'broken': False, 'fixed': False, } def info(message): sys.stderr.write('\r') print message def fix(message): status['fixed'] = True info(message) def report(message): status['broken'] = True info(message) store = GitHgStore() store.init_fast_import(lambda: FastImport()) if args.commit: all_hg2git = {} all_notes = set() commits = set() all_git_commits = {} for c in args.commit: data = store.read_changeset_data(c) if data: all_notes.add(c) commits.add(c) c = data['changeset'] commit = GitHgHelper.hg2git(c) if commit == NULL_NODE_ID and not data: info('Unknown commit or changeset: %s' % c) return 1 if commit != NULL_NODE_ID: all_hg2git[c] = commit, 'commit' if not data: data = store.read_changeset_data(commit) commits.add(commit) if data: all_notes.add(commit) all_git_commits = Git.iter( 'log', '--no-walk=unsorted', '--stdin', '--format=%T %H', stdin=commits) else: all_hg2git = { path.replace('/', ''): (filesha1, intern(typ)) for mode, typ, filesha1, path in progress_iter('Reading %d mercurial to git mappings', Git.ls_tree('refs/cinnabar/hg2git', recursive=True)) } all_notes = set(path.replace('/', '') for mode, typ, filesha1, path in progress_iter( 'Reading %d commit to changeset mappings', Git.ls_tree('refs/notes/cinnabar', recursive=True))) manifest_commits = OrderedDict((m, None) for m in progress_iter( 'Reading %d manifest trees', Git.iter('rev-list', '--full-history', '--topo-order', 'refs/cinnabar/manifest')) ) all_git_heads = Git.for_each_ref('refs/cinnabar/branches', format='%(refname)') all_git_commits = Git.iter('log', '--topo-order', '--full-history', '--reverse', '--stdin', '--format=%T %H', stdin=all_git_heads) store._hg2git_cache = {p: s for p, (s, t) in all_hg2git.iteritems()} seen_changesets = set() seen_manifests = set() seen_manifest_refs = {} seen_files = set() seen_notes = set() hg_manifest = None dag = gitdag() for line in progress_iter('Checking %d changesets', all_git_commits): tree, node = line.split(' ') if node not in all_notes: report('Missing note for git commit: ' + node) continue seen_notes.add(node) changeset_data = store.read_changeset_data(node) changeset = changeset_data['changeset'] if 'extra' in changeset_data: extra = changeset_data['extra'] header, message = GitHgHelper.cat_file( 'commit', node).split('\n\n', 1) header = dict(l.split(' ', 1) for l in header.splitlines()) if 'committer' in extra: committer_info = store.hg_author_info(header['committer']) committer = '%s %d %d' % committer_info if (committer != extra['committer'] and header['committer'] != extra['committer'] and committer_info[0] != extra['committer']): report('Committer mismatch between commit and metadata for' ' changeset %s' % changeset) if committer == extra['committer']: fix('Fixing useless committer metadata for changeset %s' % changeset) del changeset_data['extra']['committer'] store._changesets[changeset] = LazyString(node) if header['committer'] != header['author'] and not extra: fix('Fixing useless empty extra metadata for changeset %s' % changeset) del changeset_data['extra'] store._changesets[changeset] = LazyString(node) seen_changesets.add(changeset) changeset_ref = store.changeset_ref(changeset) if not changeset_ref: report('Missing changeset in hg2git branch: %s' % changeset) elif str(changeset_ref) != node: report('Commit mismatch for changeset %s\n' ' hg2git: %s\n commit: %s' % (changeset, changeset_ref, node)) hg_changeset = store.changeset(changeset, include_parents=True) sha1 = hg_changeset.sha1 if hg_changeset.node != sha1: try_fixup = False if (changeset, sha1) in ( ('8c557b7c03a4a753e5c163038f04862e9f65fce1', '249b59139de8e08abeb6c4e261a137c756e7af0e'), ('ffdee4a4eb7fc7cae80dfc4cb2fe0c3178773dcf', '415e9d2eac83d508bf58a4df585c5f6b2b0f44ed'), ): header = hg_changeset.data.split('\n', 4) start = sum(len(h) for h in header[:3]) + 1 changeset_data['patch'] = ((start, start + 1, '1'),) try_fixup = True # Some know cases of corruptions involve a whitespace after the # timezone. Adding an empty extra metadata works around those. elif 'extra' not in changeset_data: changeset_data['extra'] = {} try_fixup = True if try_fixup: hg_changeset = store.changeset(changeset, include_parents=True) sha1 = hg_changeset.sha1 if hg_changeset.node == sha1: fix('Fixing known sha1 mismatch for changeset %s' % changeset) store._changesets[changeset] = LazyString(node) if hg_changeset.node != sha1: report('Sha1 mismatch for changeset %s' % changeset) dag.add(hg_changeset.node, (hg_changeset.parent1, hg_changeset.parent2), changeset_data.get('extra', {}).get('branch', 'default')) manifest = changeset_data['manifest'] if manifest in seen_manifests: continue seen_manifests.add(manifest) manifest_ref = store.manifest_ref(manifest) if manifest_ref: seen_manifest_refs[manifest_ref] = manifest if not manifest_ref: report('Missing manifest in hg2git branch: %s' % manifest) elif not args.commit and manifest_ref not in manifest_commits: report('Missing manifest commit in manifest branch: %s' % manifest_ref) if args.manifests or args.files: parents = tuple( store.read_changeset_data(store.changeset_ref(p))['manifest'] for p in (hg_changeset.parent1, hg_changeset.parent2) if p != NULL_NODE_ID ) if args.manifests: try: with GitHgHelper.query('check-manifest', manifest, *parents) as stdout: if stdout.readline().strip() != 'ok': report('Sha1 mismatch for manifest %s' % manifest) except NoHelperException: hg_manifest = store.manifest(manifest) hg_manifest.set_parents(*parents) if hg_manifest.node != hg_manifest.sha1: report('Sha1 mismatch for manifest %s' % manifest) git_ls = one(Git.ls_tree(manifest_ref, 'git')) if git_ls: mode, typ, sha1, path = git_ls else: header, message = GitHgHelper.cat_file( 'commit', manifest_ref).split('\n\n', 1) header = dict(l.split(' ', 1) for l in header.splitlines()) if header['tree'] == EMPTY_TREE: sha1 = EMPTY_TREE else: report('Missing git tree in manifest commit %s' % manifest_ref) sha1 = None if sha1 and sha1 != tree: report('Tree mismatch between manifest commit %s and commit %s' % (manifest_ref, node)) if args.files: changes = get_changes( manifest_ref, tuple(store.manifest_ref(p) for p in parents), 'hg') for path, hg_file, hg_fileparents in changes: if hg_file != NULL_NODE_ID and hg_file not in seen_files: file = store.file(hg_file) file.set_parents(*hg_fileparents) if file.node != file.sha1: report('Sha1 mismatch for file %s in manifest %s' % (hg_file, manifest_ref)) seen_files.add(hg_file) if args.files: all_hg2git = set(all_hg2git.iterkeys()) else: all_hg2git = set(k for k, (s, t) in all_hg2git.iteritems() if t == 'commit') adjusted = {} if not args.commit: dangling = set(manifest_commits) - set(seen_manifest_refs) if dangling: def iter_manifests(): removed_one = False yielded = False previous = None for obj in reversed(manifest_commits): if obj in dangling: fix('Removing metadata commit %s with no hg2git entry' % obj) removed_one = True else: if removed_one: yield obj, previous yielded = True previous = obj if removed_one and not yielded: yield obj, False for obj, parent in progress_iter('Adjusting %d metadata commits', iter_manifests()): mark = store._fast_import.new_mark() if parent is False: Git.update_ref('refs/cinnabar/manifest', obj) continue elif parent: parents = (adjusted.get(parent, parent),) with store._fast_import.commit( ref='refs/cinnabar/manifest', parents=parents, mark=mark) as commit: mode, typ, tree, path = store._fast_import.ls(obj) commit.filemodify('', tree, typ='tree') adjusted[obj] = Mark(mark) dangling = all_hg2git - seen_changesets - seen_manifests - seen_files if dangling or adjusted: with store._fast_import.commit( ref='refs/cinnabar/hg2git', parents=('refs/cinnabar/hg2git^0',)) as commit: for obj in dangling: fix('Removing dangling metadata for ' + obj) commit.filedelete(sha1path(obj)) for obj, mark in progress_iter( 'Updating hg2git for %d metadata commits', adjusted.iteritems()): commit.filemodify(sha1path(seen_manifest_refs[obj]), mark, typ='commit') dangling = all_notes - seen_notes if dangling: with store._fast_import.commit( ref='refs/notes/cinnabar', parents=('refs/notes/cinnabar^0',)) as commit: for c in dangling: fix('Removing dangling note for commit ' + c) # That's brute force, but meh. for l in range(0, 10): commit.filedelete(sha1path(c, l)) if status['broken']: info('Your git-cinnabar repository appears to be corrupted. There\n' 'are known issues in older revisions that have been fixed.\n' 'Please try running the following command to reset:\n' ' git cinnabar reclone\n\n' 'Please note this command may change the commit sha1s. Your\n' 'local branches will however stay untouched.\n' 'Please report any corruption that fsck would detect after a\n' 'reclone.') if not args.commit: info('Checking head references...') computed_heads = defaultdict(set) for branch, head in dag.all_heads(): computed_heads[branch].add(head) for branch in sorted(dag.tags()): stored_heads = store.heads({branch}) for head in computed_heads[branch] - stored_heads: fix('Adding missing head %s in branch %s' % (head, branch)) store.add_head(head) for head in stored_heads - computed_heads[branch]: fix('Removing non-head reference to %s in branch %s' % (head, branch)) store._hgheads.remove((branch, head)) store.close() if status['broken']: return 1 if status['fixed']: return 2 return 0
def hg2git(args): '''convert mercurial sha1 to corresponding git sha1''' for arg in args.sha1: print GitHgHelper.hg2git(arg)[:args.abbrev]
def fsck(args): '''check cinnabar metadata consistency''' if not args.commit and not args.full: return fsck_quick() status = FsckStatus() store = GitHgStore() if args.full and args.commit: logging.error('Cannot pass both --full and a commit') return 1 if args.commit: commits = set() all_git_commits = {} for c in args.commit: cs = store.hg_changeset(c) if cs: commits.add(c) c = cs.node commit = GitHgHelper.hg2git(c) if commit == NULL_NODE_ID and not cs: status.info('Unknown commit or changeset: %s' % c) return 1 if not cs: cs = store.hg_changeset(commit) commits.add(commit) all_git_commits = GitHgHelper.rev_list('--no-walk=unsorted', *commits) else: all_refs = dict((ref, sha1) for sha1, ref in Git.for_each_ref('refs/cinnabar')) if 'refs/cinnabar/metadata' in all_refs: git_heads = '%s^^@' % all_refs['refs/cinnabar/metadata'] else: assert False all_git_commits = GitHgHelper.rev_list( '--topo-order', '--full-history', '--reverse', git_heads) dag = gitdag() GitHgHelper.reset_heads('manifests') full_file_check = FileFindParents.logger.isEnabledFor(logging.DEBUG) for node, tree, parents in progress_iter('Checking {} changesets', all_git_commits): node = store._replace.get(node, node) hg_node = store.hg_changeset(node) if not hg_node: status.report('Missing note for git commit: ' + node) continue GitHgHelper.seen('git2hg', node) changeset_data = store.changeset(hg_node) changeset = changeset_data.node GitHgHelper.seen('hg2git', changeset) changeset_ref = store.changeset_ref(changeset) if not changeset_ref: status.report('Missing changeset in hg2git branch: %s' % changeset) continue elif str(changeset_ref) != node: status.report('Commit mismatch for changeset %s\n' ' hg2git: %s\n commit: %s' % (changeset, changeset_ref, node)) hg_changeset = store.changeset(changeset, include_parents=True) if hg_changeset.node != hg_changeset.sha1: status.report('Sha1 mismatch for changeset %s' % changeset) dag.add(hg_changeset.node, (hg_changeset.parent1, hg_changeset.parent2), changeset_data.branch or 'default') raw_changeset = Changeset.from_git_commit(node) patcher = ChangesetPatcher.from_diff(raw_changeset, changeset_data) if patcher != store.read_changeset_data(node): status.fix('Adjusted changeset metadata for %s' % changeset) GitHgHelper.set('changeset', changeset, NULL_NODE_ID) GitHgHelper.set('changeset', changeset, node) GitHgHelper.put_blob(patcher, want_sha1=False) GitHgHelper.set('changeset-metadata', changeset, NULL_NODE_ID) GitHgHelper.set('changeset-metadata', changeset, ':1') manifest = changeset_data.manifest if GitHgHelper.seen('hg2git', manifest) or manifest == NULL_NODE_ID: continue manifest_ref = store.manifest_ref(manifest) if not manifest_ref: status.report('Missing manifest in hg2git branch: %s' % manifest) parents = tuple( store.changeset(p).manifest for p in hg_changeset.parents ) git_parents = tuple(store.manifest_ref(p) for p in parents if p != NULL_NODE_ID) # This doesn't change the value but makes the helper track the manifest # dag. GitHgHelper.set('manifest', manifest, manifest_ref) if not GitHgHelper.check_manifest(manifest): status.report('Sha1 mismatch for manifest %s' % manifest) manifest_commit_parents = GitCommit(manifest_ref).parents if sorted(manifest_commit_parents) != sorted(git_parents): # TODO: better error status.report('%s(%s) %s != %s' % (manifest, manifest_ref, manifest_commit_parents, git_parents)) # TODO: check that manifest content matches changeset content changes = get_changes(manifest_ref, git_parents) for path, hg_file, hg_fileparents in changes: if hg_file != NULL_NODE_ID and (hg_file == HG_EMPTY_FILE or GitHgHelper.seen('hg2git', hg_file)): if full_file_check: file = store.file(hg_file, hg_fileparents, git_parents, store.manifest_path(path)) valid = file.node == file.sha1 else: valid = GitHgHelper.check_file(hg_file, *hg_fileparents) if not valid: status.report( 'Sha1 mismatch for file %s in manifest %s' % (hg_file, manifest_ref)) if not args.commit and not status('broken'): store_manifest_heads = set(store._manifest_heads_orig) manifest_heads = set(GitHgHelper.heads('manifests')) if store_manifest_heads != manifest_heads: def iter_manifests(a, b): for h in a - b: yield h for h in b: yield '^%s' % h for m, t, p in GitHgHelper.rev_list( '--topo-order', '--full-history', '--reverse', *iter_manifests(manifest_heads, store_manifest_heads)): status.fix('Missing manifest commit in manifest branch: %s' % m) for m, t, p in GitHgHelper.rev_list( '--topo-order', '--full-history', '--reverse', *iter_manifests(store_manifest_heads, manifest_heads)): status.fix('Removing metadata commit %s with no corresponding ' 'changeset' % (m)) for h in store_manifest_heads - manifest_heads: if GitHgHelper.seen('hg2git', store.hg_manifest(h)): status.fix('Removing non-head reference to %s in manifests' ' metadata.' % h) dangling = () if not args.commit and not status('broken'): dangling = GitHgHelper.dangling('hg2git') for obj in dangling: status.fix('Removing dangling metadata for ' + obj) # Theoretically, we should figure out if they are files, manifests # or changesets and set the right variable accordingly, but in # practice, it makes no difference. Reevaluate when GitHgStore.close # is modified, though. GitHgHelper.set('file', obj, NULL_NODE_ID) GitHgHelper.set('file-meta', obj, NULL_NODE_ID) if not args.commit and not status('broken'): dangling = GitHgHelper.dangling('git2hg') for c in dangling: status.fix('Removing dangling note for commit ' + c) GitHgHelper.set('changeset-metadata', c, NULL_NODE_ID) if status('broken'): status.info( 'Your git-cinnabar repository appears to be corrupted. There\n' 'are known issues in older revisions that have been fixed.\n' 'Please try running the following command to reset:\n' ' git cinnabar reclone\n\n' 'Please note this command may change the commit sha1s. Your\n' 'local branches will however stay untouched.\n' 'Please report any corruption that fsck would detect after a\n' 'reclone.') if not args.commit: status.info('Checking head references...') computed_heads = defaultdict(set) for branch, head in dag.all_heads(): computed_heads[branch].add(head) for branch in sorted(dag.tags()): stored_heads = store.heads({branch}) for head in computed_heads[branch] - stored_heads: status.fix('Adding missing head %s in branch %s' % (head, branch)) store.add_head(head) for head in stored_heads - computed_heads[branch]: status.fix('Removing non-head reference to %s in branch %s' % (head, branch)) del store._hgheads[head] metadata_commit = Git.resolve_ref('refs/cinnabar/metadata') if status('broken'): Git.update_ref('refs/cinnabar/broken', metadata_commit) return 1 if args.full: Git.update_ref('refs/cinnabar/checked', metadata_commit) interval_expired('fsck', 0) store.close() if status('fixed'): return 2 return 0
def fsck_quick(): status = FsckStatus() store = GitHgStore() metadata_commit = Git.resolve_ref('refs/cinnabar/metadata') if not metadata_commit: status.info( 'There does not seem to be any git-cinnabar metadata.\n' 'Is this a git-cinnabar clone?' ) return 1 commit = GitCommit(metadata_commit) if commit.body != 'files-meta unified-manifests-v2': status.info( 'The git-cinnabar metadata is incompatible with this version.\n' 'Please use the git-cinnabar version it was used with last.\n' ) return 1 if len(commit.parents) > 6 or len(commit.parents) < 5: status.report('The git-cinnabar metadata seems to be corrupted in ' 'unexpected ways.\n') return 1 changesets, manifests, hg2git, git2hg, files_meta = commit.parents[:5] commit = GitCommit(changesets) heads = OrderedDict( (node, branch) for node, _, branch in (d.partition(' ') for d in commit.body.splitlines())) if len(heads) != len(commit.parents): status.report('The git-cinnabar metadata seems to be corrupted in ' 'unexpected ways.\n') return 1 manifest_nodes = [] parents = None fix_changeset_heads = False # TODO: Check that the recorded heads are actually dag heads. for c, changeset_node in progress_iter( 'Checking {} changeset heads', izip(commit.parents, heads)): gitsha1 = GitHgHelper.hg2git(changeset_node) if gitsha1 == NULL_NODE_ID: status.report('Missing hg2git metadata for changeset %s' % changeset_node) continue if gitsha1 != c: if parents is None: parents = set(commit.parents) if gitsha1 not in parents: status.report( 'Inconsistent metadata:\n' ' Head metadata says changeset %s maps to %s\n' ' but hg2git metadata says it maps to %s' % (changeset_node, c, gitsha1)) continue fix_changeset_heads = True changeset = store._changeset(c, include_parents=True) if not changeset: status.report('Missing git2hg metadata for git commit %s' % c) continue if changeset.node != changeset_node: if changeset.node not in heads: status.report( 'Inconsistent metadata:\n' ' Head metadata says %s maps to changeset %s\n' ' but git2hg metadata says it maps to changeset %s' % (c, changeset_node, changeset.node)) continue fix_changeset_heads = True if changeset.node != changeset.sha1: status.report('Sha1 mismatch for changeset %s' % changeset.node) continue changeset_branch = changeset.branch or 'default' if heads[changeset.node] != changeset_branch: status.report( 'Inconsistent metadata:\n' ' Head metadata says changeset %s is in branch %s\n' ' but git2hg metadata says it is in branch %s' % (changeset.node, heads[changeset.node], changeset_branch)) continue manifest_nodes.append(changeset.manifest) if status('broken'): return 1 # Rebuilding manifests benefits from limiting the difference with # the last rebuilt manifest. Similarly, building the list of unique # files in all manifests benefits from that too. # Unfortunately, the manifest heads are not ordered in a topological # relevant matter, and the differences between two consecutive manifests # can be much larger than they could be. The consequence is spending a # large amount of time rebuilding the manifests and gathering the files # list. It's actually faster to attempt to reorder them according to # some heuristics first, such that the differences are smaller. # Here, we use the depth from the root node(s) to reorder the manifests. # This doesn't give the most optimal ordering, but it's already much # faster. On a clone of multiple mozilla-* repositories with > 1400 heads, # it's close to an order of magnitude difference on the "Checking # manifests" loop. depths = {} roots = [] manifest_queue = [] for m, _, parents in progress_iter( 'Loading {} manifests', GitHgHelper.rev_list( '--topo-order', '--reverse', '--full-history', '%s^@' % manifests)): manifest_queue.append((m, parents)) if parents: depth = {} for p in parents: for root, num in depths[p].iteritems(): if root in depth: depth[root] = max(depth[root], num + 1) else: depth[root] = num + 1 depths[m] = depth del depth else: depths[m] = {m: 0} roots.append(m) if status('broken'): return 1 # TODO: check that all manifest_nodes gathered above are available in the # manifests dag, and that the dag heads are the recorded heads. manifests_commit = GitCommit(manifests) depths = [ [depths[p].get(r, 0) for r in roots] for p in manifests_commit.parents ] manifests_commit_parents = [ p for _, p in sorted(zip(depths, manifests_commit.parents)) ] previous = None all_interesting = set() for m in progress_iter('Checking {} manifest heads', manifests_commit_parents): c = GitCommit(m) if not SHA1_RE.match(c.body): status.report('Invalid manifest metadata in git commit %s' % m) continue gitsha1 = GitHgHelper.hg2git(c.body) if gitsha1 == NULL_NODE_ID: status.report('Missing hg2git metadata for manifest %s' % c.body) continue if not GitHgHelper.check_manifest(c.body): status.report('Sha1 mismatch for manifest %s' % c.body) files = {} if previous: for _, _, before, after, d, path in GitHgHelper.diff_tree( previous, m): if d in 'AM' and before != after and \ (path, after) not in all_interesting: files[path] = after else: for _, t, sha1, path in GitHgHelper.ls_tree(m, recursive=True): if (path, sha1) not in all_interesting: files[path] = sha1 all_interesting.update(files.iteritems()) previous = m if status('broken'): return 1 progress = Progress('Checking {} files') while all_interesting and manifest_queue: (m, parents) = manifest_queue.pop() changes = get_changes(m, parents, all=True) for path, hg_file, hg_fileparents in changes: if hg_fileparents[1:] == (hg_file,): continue elif hg_fileparents[:1] == (hg_file,): continue # Reaching here means the file received a modification compared # to its parents. If it's a file we're going to check below, # it means we don't need to check its parents if somehow they were # going to be checked. If it's not a file we're going to check # below, it's because it's either a file we weren't interested in # in the first place, or it's the parent of a file we have checked. # Either way, we aren't interested in the parents. for p in hg_fileparents: all_interesting.discard((path, p)) if (path, hg_file) not in all_interesting: continue all_interesting.remove((path, hg_file)) if not GitHgHelper.check_file(hg_file, *hg_fileparents): p = store.manifest_path(path) status.report( 'Sha1 mismatch for file %s\n' ' revision %s' % (p, hg_file)) print_parents = ' '.join(p for p in hg_fileparents if p != NULL_NODE_ID) if print_parents: status.report(' with parent%s %s' % ( 's' if len(print_parents) > 41 else '', print_parents)) progress.progress() progress.finish() if all_interesting: status.info('Could not find the following files:') for path, sha1 in sorted(all_interesting): p = store.manifest_path(path) status.info(' %s %s' % (sha1, path)) status.info( 'This might be a bug in `git cinnabar fsck`. Please open ' 'an issue, with the message above, on\n' 'https://github.com/glandium/git-cinnabar/issues') return 1 if status('broken'): status.info( 'Your git-cinnabar repository appears to be corrupted.\n' 'Please open an issue, with the information above, on\n' 'https://github.com/glandium/git-cinnabar/issues') Git.update_ref('refs/cinnabar/broken', metadata_commit) if Git.resolve_ref('refs/cinnabar/checked'): status.info( '\nThen please try to run `git cinnabar rollback --fsck` to ' 'restore last known state, and to update from the mercurial ' 'repository.') else: status.info('\nThen please try to run `git cinnabar reclone`.') status.info( '\nPlease note this may affect the commit sha1s of mercurial ' 'changesets, and may require to rebase your local branches.') status.info( '\nAlternatively, you may start afresh with a new clone. In any ' 'case, please keep this corrupted repository around for further ' 'debugging.') return 1 refresh = [] if fix_changeset_heads: status.fix('Fixing changeset heads metadata order.') refresh.append('refs/cinnabar/changesets') interval_expired('fsck', 0) store.close(refresh=refresh) GitHgHelper._helper = False metadata_commit = Git.resolve_ref('refs/cinnabar/metadata') Git.update_ref('refs/cinnabar/checked', metadata_commit) return 0
def manifest_diff(a, b): for line in GitHgHelper.diff_tree(a, b): mode_before, mode_after, sha1_before, sha1_after, status, path = line if sha1_before != sha1_after: yield path, sha1_after, sha1_before
def create_hg_manifest(self, commit, parents): manifest = GeneratedManifestInfo(NULL_NODE_ID) changeset_files = [] if parents: parent_changeset = self.changeset(self.hg_changeset(parents[0])) parent_manifest = self.manifest(parent_changeset.manifest) parent_node = parent_manifest.node if len(parents) == 2: parent2_changeset = self.changeset(self.hg_changeset(parents[1])) parent2_manifest = self.manifest(parent2_changeset.manifest) parent2_node = parent2_manifest.node if parent_node == parent2_node: parents = parents[:1] if not parents: for line in Git.ls_tree(commit, recursive=True): mode, typ, sha1, path = line node = self.create_file(sha1, git_manifest_parents=(), path=path) manifest.append_line(ManifestLine(path, node, self.ATTR[mode]), modified=True) changeset_files.append(path) manifest.set_parents(NULL_NODE_ID) manifest.delta_node = NULL_NODE_ID return manifest, changeset_files elif len(parents) == 2: if not experiment('merge'): raise Exception('Pushing merges is not supported yet') if not self._merge_warn: logging.warning('Pushing merges is experimental.') logging.warning('This may irremediably push bad state to the ' 'mercurial server!') self._merge_warn = 1 git_manifests = (self.manifest_ref(parent_node), self.manifest_ref(parent2_node)) # TODO: this would benefit from less git queries changes = list(get_changes(commit, parents)) files = [(path, mode, sha1) for mode, _, sha1, path in Git.ls_tree(commit, recursive=True)] manifests = sorted_merge(parent_manifest._lines, parent2_manifest._lines, key=lambda i: i.name, non_key=lambda i: i) for line in sorted_merge(files, sorted_merge(changes, manifests)): path, f, (change, (manifest_line_p1, manifest_line_p2)) = line if not f: # File was removed if manifest_line_p1: manifest.removed.add(path) changeset_files.append(path) continue mode, sha1 = f attr = self.ATTR[mode] if manifest_line_p1 and not manifest_line_p2: file_parents = (manifest_line_p1.node,) elif manifest_line_p2 and not manifest_line_p1: file_parents = (manifest_line_p2.node,) elif not manifest_line_p1 and not manifest_line_p2: file_parents = () elif manifest_line_p1.node == manifest_line_p2.node: file_parents = (manifest_line_p1.node,) else: if self._merge_warn == 1: logging.warning('This may take a while...') self._merge_warn = 2 file_parents = (manifest_line_p1.node, manifest_line_p2.node) assert file_parents is not None f = self._create_file_internal( sha1, *file_parents, git_manifest_parents=git_manifests, path=path ) file_parents = tuple(p for p in (f.parent1, f.parent2) if p != NULL_NODE_ID) merged = len(file_parents) == 2 if not merged and file_parents: if self.git_file_ref(file_parents[0]) == sha1: node = file_parents[0] else: merged = True if merged: node = self._store_file_internal(f) else: node = file_parents[0] attr_change = (manifest_line_p1 and manifest_line_p1.attr != attr) manifest.append_line(ManifestLine(path, node, attr), modified=merged or attr_change) if merged or attr_change: changeset_files.append(path) if manifest.data == parent_manifest.data: return parent_manifest, [] manifest.set_parents(parent_node, parent2_node) return manifest, changeset_files def process_diff(diff): for (mode_before, mode_after, sha1_before, sha1_after, status, path) in diff: if status[0] == 'R': yield status[1:], ( '000000', sha1_before, NULL_NODE_ID, 'D') yield path, (mode_after, sha1_before, sha1_after, status) git_diff = sorted( l for l in process_diff(GitHgHelper.diff_tree( parents[0], commit, detect_copy=True)) ) if not git_diff: return parent_manifest, [] parent_lines = OrderedDict((l.name, l) for l in parent_manifest._lines) for line in sorted_merge(parent_lines.iteritems(), git_diff, non_key=lambda i: i[1]): path, manifest_line, change = line if not change: manifest.append_line(manifest_line) continue mode_after, sha1_before, sha1_after, status = change path2 = status[1:] status = status[0] attr = self.ATTR.get(mode_after) if status == 'D': manifest.removed.add(path) changeset_files.append(path) continue if status in 'MT': if sha1_before == sha1_after: node = manifest_line.node else: node = self.create_file( sha1_after, str(manifest_line.node), git_manifest_parents=( self.manifest_ref(parent_node),), path=path) elif status in 'RC': if sha1_after != EMPTY_BLOB: node = self.create_copy( (path2, parent_lines[path2].node), sha1_after, git_manifest_parents=( self.manifest_ref(parent_node),), path=path) else: node = self.create_file( sha1_after, git_manifest_parents=( self.manifest_ref(parent_node),), path=path) else: assert status == 'A' node = self.create_file( sha1_after, git_manifest_parents=( self.manifest_ref(parent_node),), path=path) manifest.append_line(ManifestLine(path, node, attr), modified=True) changeset_files.append(path) manifest.set_parents(parent_node) manifest.delta_node = parent_node return manifest, changeset_files