/breezy-loom/trunk

To get this branch, use:
bzr branch https://code.breezy-vcs.org/breezy-loom/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Loom, a plugin for bzr to assist in developing focused patches.
# Copyright (C) 2006, 2008 Canonical Limited.
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
# 

"""Loom specific revision-specifiers."""

from __future__ import absolute_import

from bzrlib.plugins.loom.branch import NoLowerThread
from bzrlib.plugins.loom.formats import require_loom_branch
from bzrlib.revisionspec import RevisionSpec, RevisionInfo


class LoomRevisionSpec(RevisionSpec):
    """A revision spec that needs a loom."""

    def _match_on(self, branch, revs):
         return RevisionInfo(branch, None, self._as_revision_id(branch))

    def _as_revision_id(self, branch):
        require_loom_branch(branch)
        branch.lock_read()
        try:
            state = branch.get_loom_state()
            threads = state.get_threads()
            return self._as_thread_revision_id(branch, state, threads)
        finally:
            branch.unlock()


class RevisionSpecBelow(LoomRevisionSpec):
    """The below: revision specifier."""

    help_txt = """Selects the tip of the thread below a thread from a loom.

    Selects the tip of the thread below a thread in a loom.  

    Examples::

      below:                   -> return the tip of the next lower thread.
      below:foo                -> return the tip of the thread under the one
                                  named 'foo'

    see also: loom, the thread: revision specifier
    """

    prefix = 'below:'

    def _as_thread_revision_id(self, branch, state, threads):
        # '' -> next lower
        # foo -> thread under foo
        if len(self.spec):
            index = state.thread_index(self.spec)
        else:
            current_thread = branch.nick
            index = state.thread_index(current_thread)
        if index < 1:
            raise NoLowerThread()
        return threads[index - 1][1]


class RevisionSpecThread(LoomRevisionSpec):
    """The thread: revision specifier."""

    help_txt = """Selects the tip of a thread from a loom.

    Selects the tip of a thread in a loom.  

    Examples::

      thread:                   -> return the tip of the next lower thread.
      thread:foo                -> return the tip of the thread named 'foo'

    see also: loom, the below: revision specifier
    """

    prefix = 'thread:'

    def _as_thread_revision_id(self, branch, state, threads):
        # '' -> next lower
        # foo -> named
        if len(self.spec):
            index = state.thread_index(self.spec)
        else:
            current_thread = branch.nick
            index = state.thread_index(current_thread) - 1
            if index < 0:
                raise NoLowerThread()
        return threads[index][1]