def test_nested(self): d = {'foo': 'foo', 'bar': 'qux'} d1 = VersionedDict(d) d1['hoge'] = 'hoge' d1['foo'] = 'foo2' del d1['bar'] d2 = VersionedDict(d1) del d2['hoge'] d2['fuga'] = 'fuga' self.assertTrue('hoge' in d1) self.assertTrue('hoge' not in d2) self.assertEquals(sorted(d2.iterchanges()), [ (d2.CREATED, 'fuga', 'fuga'), (d2.REMOVED, 'hoge', 'hoge'), ]) d3 = d2.flattened() self.assertTrue('hoge' not in d3) self.assertTrue('fuga' in d3) self.assertEquals(sorted(d3.iterchanges()), [ (d2.CREATED, 'fuga', 'fuga'), (d2.MODIFIED, 'foo', 'foo2'), (d2.REMOVED, 'bar', 'qux'), ])
def test_change_previous(self): d = {'foo': 'foo', 'bar': 'qux'} d1 = VersionedDict(d) d1['hoge'] = 'hoge' del d1['hoge'] self.assertTrue('hoge' not in d1) self.assertEquals(sorted(d1.iterchanges()), []) d['hoge'] = 'fuga' self.assertTrue('hoge' not in d1) self.assertEquals(sorted(d1.iterchanges()), [ (d1.REMOVED, 'hoge', 'fuga'), ]) d1['foo'] = 'foo' self.assertEquals(sorted(d1.iterchanges()), [ (d1.REMOVED, 'hoge', 'fuga'), ]) d['foo'] = 'bar' self.assertEqual(d1['foo'], 'foo') self.assertEquals(sorted(d1.iterchanges()), [ (d1.MODIFIED, 'foo', 'foo'), (d1.REMOVED, 'hoge', 'fuga'), ])
def test_update(self): d1 = {'a': 1, 'b': 2} d2 = {'c': 3, 'd': 4} for content, kwargs in ((d1, {}), ((), d2), (d1, d2)): d = {} v = VersionedDict() args = content and [content] d.update(*args, **kwargs) v.update(*args, **kwargs) self.check_state(v, d)
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 do_rollback(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: if sha1 == metadata: break commit = GitCommit(metadata) flags = commit.body.split(' ') if len(commit.parents) == 5 + ('files-meta' in flags): metadata = commit.parents[-1] else: metadata = None if not metadata: logging.error( 'Cannot rollback to %s, it is not in the history of ' 'the current metadata.', ref) return 1 refs = VersionedDict((ref, commit) for commit, ref in Git.for_each_ref( 'refs/cinnabar', 'refs/notes/cinnabar')) for ref in refs: del refs[ref] if sha1 != NULL_NODE_ID: refs['refs/cinnabar/metadata'] = sha1 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) Git._close_update_ref() return 0
def test_init(self): d = {'foo': 'foo', 'bar': 'qux'} d1 = VersionedDict(d) self.assertIs(d1._previous, d) d1 = VersionedDict(**d) self.assertIsInstance(d1._previous, dict) self.assertIsNot(d1._previous, d) self.assertEquals(d1._previous, d) d1 = VersionedDict(d.items()) self.assertIsInstance(d1._previous, dict) self.assertIsNot(d1._previous, d) self.assertEquals(d1._previous, d) d1 = VersionedDict(d, bar='bar', hoge='hoge', fuga='toto') self.assertIsInstance(d1._previous, VersionedDict) self.assertEquals(sorted(d1._previous.iterchanges()), [ (d1.CREATED, 'fuga', 'toto'), (d1.CREATED, 'hoge', 'hoge'), (d1.MODIFIED, 'bar', 'bar'), ]) d2 = VersionedDict(d1) self.assertIs(d2._previous, d1) d2 = VersionedDict(d1, hoge=42, toto='titi') self.assertIsInstance(d2._previous, VersionedDict) self.assertIsNot(d2._previous, d1) self.assertEquals(sorted(d2._previous.iterchanges()), [ (d1.CREATED, 'toto', 'titi'), (d1.MODIFIED, 'hoge', 42), ])