/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz
0.8.9 by Szilveszter Farkas (Phanatic)
2006-07-08 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
1
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
0.8.7 by Szilveszter Farkas (Phanatic)
2006-06-20 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
2
# Some parts of the code are:
3
# Copyright (C) 2005, 2006 by Canonical Ltd
0.8.37 by Szilveszter Farkas (Phanatic)
Implemented Make directory functionality; some cleanups.
4
#
0.8.7 by Szilveszter Farkas (Phanatic)
2006-06-20 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
0.8.37 by Szilveszter Farkas (Phanatic)
Implemented Make directory functionality; some cleanups.
9
#
0.8.7 by Szilveszter Farkas (Phanatic)
2006-06-20 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
0.8.37 by Szilveszter Farkas (Phanatic)
Implemented Make directory functionality; some cleanups.
14
#
0.8.7 by Szilveszter Farkas (Phanatic)
2006-06-20 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
19
import bzrlib.errors as errors
20
21
from bzrlib.workingtree import WorkingTree
22
0.8.66 by Szilveszter Farkas (Phanatic)
Implemented Missing revisions functionality.
23
from errors import ConnectionError, NoLocationKnown, NotBranchError
0.8.7 by Szilveszter Farkas (Phanatic)
2006-06-20 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
24
25
def missing(branch, other_branch=None, reverse=False):
26
    """ Get the number of missing or extra revisions of local branch.
27
    
28
    :param branch: path to local branch
29
    
30
    :param other_branch: location of the other branch
31
    
32
    :param reverse: reverse the order of revisions
33
    
34
    :return: number of missing revisions (if < 0, then extra revisions * -1)
35
    """
36
    import bzrlib
37
    from bzrlib.missing import find_unmerged
38
    
0.8.66 by Szilveszter Farkas (Phanatic)
Implemented Missing revisions functionality.
39
    try:
40
        local_branch = bzrlib.branch.Branch.open_containing(branch)[0]
41
    except errors.NotBranchError:
42
        raise NotBranchError(branch)
43
    except:
44
        raise
45
    
0.8.7 by Szilveszter Farkas (Phanatic)
2006-06-20 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
46
    parent = local_branch.get_parent()
47
    if other_branch is None:
48
        other_branch = parent
49
        if other_branch is None:
50
            raise NoLocationKnown
0.8.66 by Szilveszter Farkas (Phanatic)
Implemented Missing revisions functionality.
51
    
52
    try:
53
        remote_branch = bzrlib.branch.Branch.open(other_branch)
54
    except errors.NotBranchError:
55
        raise NotBranchError(other_branch)
56
    except errors.ConnectionError:
57
        raise ConnectionError(other_branch)
58
    except:
59
        raise
60
    
0.8.7 by Szilveszter Farkas (Phanatic)
2006-06-20 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
61
    if remote_branch.base == local_branch.base:
62
        remote_branch = local_branch
63
    local_branch.lock_read()
64
65
    ret = 0
66
67
    try:
68
        remote_branch.lock_read()
69
        try:
70
            local_extra, remote_extra = find_unmerged(local_branch, remote_branch)
71
72
            if reverse is False:
73
                local_extra.reverse()
74
                remote_extra.reverse()
75
76
            if local_extra:
77
                ret = len(local_extra) * -1
78
79
            if remote_extra:
80
                ret = len(remote_extra)
81
82
            if not remote_extra and not local_extra:
83
                ret = 0
84
85
        finally:
86
            remote_branch.unlock()
87
    finally:
88
        local_branch.unlock()
89
90
    if not ret and parent is None and other_branch is not None:
91
        local_branch.lock_write()
92
        try:
93
            # handle race conditions - a parent might be set while we run.
94
            if local_branch.get_parent() is None:
95
                local_branch.set_parent(remote_branch.base)
96
        finally:
97
            local_branch.unlock()
98
    
99
    return ret
100
101
102
def pull(branch, location=None, remember=False, overwrite=False, revision=None):
103
    """ Pull revisions from another branch.
104
    
105
    :param branch: the local branch where you want to pull
106
    
107
    :param location: location of branch you'd like to pull from (can be a bundle, too)
108
    
109
    :param remeber: if True, the location will be stored
110
    
111
    :param overwrite: if True, forget local changes, and update the branch to match the remote one
112
    
113
    :param revision: if specified, only the given revision will be pulled (revno)
114
    
115
    :return: number of revisions pulled
116
    """
0.8.64 by Szilveszter Farkas (Phanatic)
ChangeLog updated; bundle related bug fixed.
117
    nobundle = False
118
    
0.8.7 by Szilveszter Farkas (Phanatic)
2006-06-20 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
119
    from bzrlib.branch import Branch
0.8.64 by Szilveszter Farkas (Phanatic)
ChangeLog updated; bundle related bug fixed.
120
    try:
121
        from bzrlib.bundle import read_bundle_from_url
122
        from bzrlib.bundle.apply_bundle import install_bundle
123
    except ImportError:
124
        # no bundle support
125
        nobundle = True
0.8.7 by Szilveszter Farkas (Phanatic)
2006-06-20 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
126
    
127
    try:
128
        tree_to = WorkingTree.open_containing(branch)[0]
129
        branch_to = tree_to.branch
130
    except errors.NoWorkingTree:
131
        tree_to = None
0.8.36 by Szilveszter Farkas (Phanatic)
Implemented pull functionality.
132
        try:
133
            branch_to = Branch.open_containing(branch)[0]
134
        except errors.NotBranchError:
135
            raise NotBranchError(location)
136
    except errors.NotBranchError:
137
        raise NotBranchError(location)
0.8.7 by Szilveszter Farkas (Phanatic)
2006-06-20 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
138
139
    reader = None
0.8.64 by Szilveszter Farkas (Phanatic)
ChangeLog updated; bundle related bug fixed.
140
    if location is not None and not nobundle:
0.8.7 by Szilveszter Farkas (Phanatic)
2006-06-20 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
141
        try:
142
            reader = read_bundle_from_url(location)
143
        except errors.NotABundle:
144
            pass # Continue on considering this url a Branch
145
146
    stored_loc = branch_to.get_parent()
147
    if location is None:
148
        if stored_loc is None:
149
            raise NoLocationKnown
150
        else:
151
            location = stored_loc
152
0.8.64 by Szilveszter Farkas (Phanatic)
ChangeLog updated; bundle related bug fixed.
153
    if reader is not None and not nobundle:
0.8.7 by Szilveszter Farkas (Phanatic)
2006-06-20 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
154
        install_bundle(branch_to.repository, reader)
155
        branch_from = branch_to
156
    else:
157
        branch_from = Branch.open(location)
158
159
        if branch_to.get_parent() is None or remember:
160
            branch_to.set_parent(branch_from.base)
161
162
    rev_id = None
163
    
164
    if revision is not None:
165
        rev_id = branch_from.get_rev_id(revision)
166
    else:
167
        if reader is not None:
168
            rev_id = reader.info.target
169
        
170
    old_rh = branch_to.revision_history()
171
    if tree_to is not None:
172
        count = tree_to.pull(branch_from, overwrite, rev_id)
173
    else:
174
        count = branch_to.pull(branch_from, overwrite, rev_id)
175
    
176
    return count
177
178
def update(location):
179
    """ Update a tree to have the latest code committed to its branch.
180
    
181
    :param location: the path to the branch/working tree
182
    
183
    :return: None if tree is up to date, 1 if there are conflicts, 0 if updated without having conflicts
184
    """
185
    tree = WorkingTree.open_containing(location)[0]
186
    tree.lock_write()
187
    try:
188
        if tree.last_revision() == tree.branch.last_revision():
189
            # may be up to date, check master too.
190
            master = tree.branch.get_master_branch()
191
            if master is None or master.last_revision == tree.last_revision():
192
                # tree is up to date.
193
                return None
194
        conflicts = tree.update()
195
        if conflicts != 0:
196
            return 1
197
        else:
198
            return 0
199
    finally:
200
        tree.unlock()