/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
106
107
# 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
# 


"""The current-loom state object."""

from __future__ import absolute_import

from bzrlib.revision import NULL_REVISION


class LoomState(object):
    """The LoomState represents the content of the current-loom branch file.
    
    It is planned to not need access to repository data - it will be driven
    by the LoomBranch and have data fed into it.
    """

    def __init__(self, reader=None):
        """Create a loom state object.

        :param reader: If not None, this should be a LoomStateReader from
            which this LoomState is meant to retrieve its current data.
        """
        self._parents = []
        self._threads = []
        if reader is not None:
            # perhaps this should be lazy evaluated at some point?
            self._parents = reader.read_parents()
            for thread in reader.read_thread_details():
                self._threads.append(thread)

    def get_basis_revision_id(self):
        """Get the revision id for the basis revision.

        None is return if there is no basis revision.
        """
        if not self._parents:
            return NULL_REVISION
        else:
            return self._parents[0]
 
    def get_parents(self):
        """Get the list of loom revisions that are parents to this state."""
        return self._parents

    def get_threads(self):
        """Get the threads for the current state."""
        return list(self._threads)

    def get_threads_dict(self):
        """Get the threads as a dict. 

        This loses ordering, but is useful for quickly locating the details on 
        a given thread.
        """
        return dict((thread[0], thread[1:]) for thread in self._threads)

    def thread_index(self, thread):
        """Find the index of thread in threads."""
        # Avoid circular import
        from bzrlib.plugins.loom.branch import NoSuchThread
        thread_names = [name for name, rev, parents in self._threads]
        try:
            return thread_names.index(thread)
        except ValueError:
            raise NoSuchThread(self, thread)

    def get_new_thread_after_deleting(self, current_thread):
        if len(self._threads) == 1:
            return None
        current_index = self.thread_index(current_thread)
        if current_index == 0:
            new_index = 1
        else:
            new_index = current_index - 1
        return self._threads[new_index][0]

    def set_parents(self, parent_list):
        """Set the parents of this state to parent_list.

        :param parent_list: A list of (parent_id, threads) tuples.
        """
        self._parents = list(parent_list)

    def set_threads(self, threads):
        """Set the current threads to threads.

        :param threads: A list of (name, revid) pairs that make up the threads.
            If the list is altered after calling set_threads, there is no 
            effect on the LoomState.
        """
        self._threads = list(threads)