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_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_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_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 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