def test_bzr_branch(self): tree = self.make_branch_and_tree(".") revid = tree.commit("acommit") text = "ab" * 20 revspec = RevisionSpec.from_string("hg:%s" % text) self.assertRaises(InvalidRevisionSpec, revspec.as_revision_id, tree.branch)
def test_as_revision_id_uncommitted(self): spec = RevisionSpec.from_string('annotate:annotate-tree/file1:3') e = self.assertRaises(errors.InvalidRevisionSpec, spec.as_revision_id, self.tree.branch) self.assertContainsRe( str(e), r"Requested revision: \'annotate:annotate-tree/file1:3\' does not" " exist in branch: .*\nLine 3 has not been committed.")
def test_invalid_line(self): spec = RevisionSpec.from_string('annotate:annotate-tree/file1:q') e = self.assertRaises(errors.InvalidRevisionSpec, spec.as_revision_id, self.tree.branch) self.assertContainsRe( str(e), r"Requested revision: \'annotate:annotate-tree/file1:q\' does not" " exist in branch: .*\nNo such line: q")
def test_no_such_file_with_colon(self): spec = RevisionSpec.from_string('annotate:annotate-tree/fi:le2:1') e = self.assertRaises(errors.InvalidRevisionSpec, spec.as_revision_id, self.tree.branch) self.assertContainsRe( str(e), r"Requested revision: \'annotate:annotate-tree/fi:le2:1\' does not" " exist in branch: .*\nFile 'fi:le2' is not versioned")
def test_invalid_revnum(self): revspec = RevisionSpec.from_string("svn:foo") repos_url = self.make_svn_repository("a") dc = self.get_commit_editor(repos_url) dc.add_file("bar").modify() dc.close() branch = Branch.open(repos_url) self.assertRaises(InvalidRevisionSpec, revspec._match_on, branch, None)
def test_as_revision_id(self): self.assertAsRevisionId(b'r1', 'mainline:1') self.assertAsRevisionId(b'r2', 'mainline:1.1.1') self.assertAsRevisionId(b'r2', 'mainline:revid:alt_r2') spec = RevisionSpec.from_string('mainline:revid:alt_r22') e = self.assertRaises(errors.InvalidRevisionSpec, spec.as_revision_id, self.tree.branch) self.assertContainsRe( str(e), "Requested revision: 'mainline:revid:alt_r22' does not exist in" " branch: ")
def test_by_single_revno(self): revspec = RevisionSpec.from_string("svn:2") repos_url = self.make_svn_repository("a") dc = self.get_commit_editor(repos_url) dc.add_file("foo").modify() dc.close() dc = self.get_commit_editor(repos_url) dc.add_file("bar").modify() dc.close() branch = Branch.open(repos_url) revinfo = revspec._match_on(branch, None) self.assertEquals(RevisionInfo.from_revision_id(branch, branch.last_revision()), revinfo)
def validate(self): """Check that the user really wants to uncommit the given revisions.""" revision = self._revision_identifier() if revision is None: log_rqst = log.make_log_request_dict(limit=1) else: rev_spec = RevisionSpec.from_string(revision) revno = rev_spec.in_history(self.branch).revno # We need to offset the revno by +1 because we'll be uncommitting # *back* to revno, meaning those after it are 'deleted' log_rqst = log.make_log_request_dict(start_revision=revno + 1) log_data = log_as_html(self.branch, log_rqst) question = gettext("Do you really want to uncommit these revisions?") if self.ask_confirmation('<font color="red">%s</font><br/>%s' % (question, log_data), type='warning'): return True return False
def assertInvalid(self, revision_spec, extra='', invalid_as_revision_id=True): try: self.get_in_history(revision_spec) except errors.InvalidRevisionSpec as e: self.assertEqual(revision_spec, e.spec) self.assertEqual(extra, e.extra) else: self.fail('Expected InvalidRevisionSpec to be raised for' ' %r.in_history' % (revision_spec, )) if invalid_as_revision_id: try: spec = RevisionSpec.from_string(revision_spec) spec.as_revision_id(self.tree.branch) except errors.InvalidRevisionSpec as e: self.assertEqual(revision_spec, e.spec) self.assertEqual(extra, e.extra) else: self.fail('Expected InvalidRevisionSpec to be raised for' ' %r.as_revision_id' % (revision_spec, ))
def test_present(self): self.assertIsNot(None, RevisionSpec.from_string("svn:foo"))
def get_as_tree(self, revision_spec, tree=None): if tree is None: tree = self.tree spec = RevisionSpec.from_string(revision_spec) return spec.as_tree(tree.branch)
def test_get_branch(self): self.assertIs(None, RevisionSpec.from_string("svn:foo").get_branch())
def test_needs_branch(self): self.assertTrue(RevisionSpec.from_string("svn:foo").needs_branch())
def test_from_string_tag(self): spec = RevisionSpec.from_string('tag:bzr-0.14') self.assertIsInstance(spec, RevisionSpec_tag) self.assertEqual(spec.spec, 'bzr-0.14')
def spec_in_history(spec, branch): """A simple helper to change a revision spec into a branch search""" return RevisionSpec.from_string(spec).in_history(branch)
def test_non_exact_branch(self): # It seems better to require an exact path to the branch # Branch.open() rather than using Branch.open_containing() spec = RevisionSpec.from_string('revno:2:tree2/a') self.assertRaises(errors.NotBranchError, spec.in_history, self.tree.branch)
def test_search(self): tree = self.make_branch_and_tree(".", format="hg") revid = tree.commit("acommit") text = binascii.hexlify(tree.branch._tip())[:20] revspec = RevisionSpec.from_string("hg:%s" % text) self.assertEquals(revid, revspec.as_revision_id(tree.branch))
def test_non_svn_branch(self): revspec = RevisionSpec.from_string("svn:0") branch = ControlDir.create_standalone_workingtree("a").branch self.assertRaises(BzrError, revspec._match_on, branch, None)
def assertAsRevisionId(self, revision_id, revision_spec): """Calling as_revision_id() should return the specified id.""" spec = RevisionSpec.from_string(revision_spec) self.assertEqual(revision_id, spec.as_revision_id(self.tree.branch))
def grep_diff(opts): wt, branch, relpath = \ controldir.ControlDir.open_containing_tree_or_branch('.') with branch.lock_read(): if opts.revision: start_rev = opts.revision[0] else: # if no revision is sepcified for diff grep we grep all changesets. opts.revision = [RevisionSpec.from_string('revno:1'), RevisionSpec.from_string('last:1')] start_rev = opts.revision[0] start_revid = start_rev.as_revision_id(branch) if start_revid == b'null:': return srevno_tuple = branch.revision_id_to_dotted_revno(start_revid) if len(opts.revision) == 2: end_rev = opts.revision[1] end_revid = end_rev.as_revision_id(branch) if end_revid is None: end_revno, end_revid = branch.last_revision_info() erevno_tuple = branch.revision_id_to_dotted_revno(end_revid) grep_mainline = (_rev_on_mainline(srevno_tuple) and _rev_on_mainline(erevno_tuple)) # ensure that we go in reverse order if srevno_tuple > erevno_tuple: srevno_tuple, erevno_tuple = erevno_tuple, srevno_tuple start_revid, end_revid = end_revid, start_revid # Optimization: Traversing the mainline in reverse order is much # faster when we don't want to look at merged revs. We try this # with _linear_view_revisions. If all revs are to be grepped we # use the slower _graph_view_revisions if opts.levels == 1 and grep_mainline: given_revs = _linear_view_revisions( branch, start_revid, end_revid) else: given_revs = _graph_view_revisions( branch, start_revid, end_revid) else: # We do an optimization below. For grepping a specific revison # We don't need to call _graph_view_revisions which is slow. # We create the start_rev_tuple for only that specific revision. # _graph_view_revisions is used only for revision range. start_revno = '.'.join(map(str, srevno_tuple)) start_rev_tuple = (start_revid, start_revno, 0) given_revs = [start_rev_tuple] repo = branch.repository diff_pattern = re.compile( b"^[+\\-].*(" + opts.pattern.encode(_user_encoding) + b")") file_pattern = re.compile(b"=== (modified|added|removed) file '.*'") outputter = _GrepDiffOutputter(opts) writeline = outputter.get_writer() writerevno = outputter.get_revision_header_writer() writefileheader = outputter.get_file_header_writer() file_encoding = _user_encoding for revid, revno, merge_depth in given_revs: if opts.levels == 1 and merge_depth != 0: # with level=1 show only top level continue rev_spec = RevisionSpec_revid.from_string( "revid:" + revid.decode('utf-8')) new_rev = repo.get_revision(revid) new_tree = rev_spec.as_tree(branch) if len(new_rev.parent_ids) == 0: ancestor_id = _mod_revision.NULL_REVISION else: ancestor_id = new_rev.parent_ids[0] old_tree = repo.revision_tree(ancestor_id) s = BytesIO() diff.show_diff_trees(old_tree, new_tree, s, old_label='', new_label='') display_revno = True display_file = False file_header = None text = s.getvalue() for line in text.splitlines(): if file_pattern.search(line): file_header = line display_file = True elif diff_pattern.search(line): if display_revno: writerevno("=== revno:%s ===" % (revno,)) display_revno = False if display_file: writefileheader( " %s" % (file_header.decode(file_encoding, 'replace'),)) display_file = False line = line.decode(file_encoding, 'replace') writeline(" %s" % (line,))