/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.36 by Szilveszter Farkas (Phanatic)
Implemented pull functionality.
23
from errors import 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
    
39
    local_branch = bzrlib.branch.Branch.open_containing(branch)[0]
40
    parent = local_branch.get_parent()
41
    if other_branch is None:
42
        other_branch = parent
43
        if other_branch is None:
44
            raise NoLocationKnown
45
    remote_branch = bzrlib.branch.Branch.open(other_branch)
46
    if remote_branch.base == local_branch.base:
47
        remote_branch = local_branch
48
    local_branch.lock_read()
49
50
    ret = 0
51
52
    try:
53
        remote_branch.lock_read()
54
        try:
55
            local_extra, remote_extra = find_unmerged(local_branch, remote_branch)
56
57
            if reverse is False:
58
                local_extra.reverse()
59
                remote_extra.reverse()
60
61
            if local_extra:
62
                ret = len(local_extra) * -1
63
64
            if remote_extra:
65
                ret = len(remote_extra)
66
67
            if not remote_extra and not local_extra:
68
                ret = 0
69
70
        finally:
71
            remote_branch.unlock()
72
    finally:
73
        local_branch.unlock()
74
75
    if not ret and parent is None and other_branch is not None:
76
        local_branch.lock_write()
77
        try:
78
            # handle race conditions - a parent might be set while we run.
79
            if local_branch.get_parent() is None:
80
                local_branch.set_parent(remote_branch.base)
81
        finally:
82
            local_branch.unlock()
83
    
84
    return ret
85
86
87
def pull(branch, location=None, remember=False, overwrite=False, revision=None):
88
    """ Pull revisions from another branch.
89
    
90
    :param branch: the local branch where you want to pull
91
    
92
    :param location: location of branch you'd like to pull from (can be a bundle, too)
93
    
94
    :param remeber: if True, the location will be stored
95
    
96
    :param overwrite: if True, forget local changes, and update the branch to match the remote one
97
    
98
    :param revision: if specified, only the given revision will be pulled (revno)
99
    
100
    :return: number of revisions pulled
101
    """
0.8.64 by Szilveszter Farkas (Phanatic)
ChangeLog updated; bundle related bug fixed.
102
    nobundle = False
103
    
0.8.7 by Szilveszter Farkas (Phanatic)
2006-06-20 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
104
    from bzrlib.branch import Branch
0.8.64 by Szilveszter Farkas (Phanatic)
ChangeLog updated; bundle related bug fixed.
105
    try:
106
        from bzrlib.bundle import read_bundle_from_url
107
        from bzrlib.bundle.apply_bundle import install_bundle
108
    except ImportError:
109
        # no bundle support
110
        nobundle = True
0.8.7 by Szilveszter Farkas (Phanatic)
2006-06-20 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
111
    
112
    try:
113
        tree_to = WorkingTree.open_containing(branch)[0]
114
        branch_to = tree_to.branch
115
    except errors.NoWorkingTree:
116
        tree_to = None
0.8.36 by Szilveszter Farkas (Phanatic)
Implemented pull functionality.
117
        try:
118
            branch_to = Branch.open_containing(branch)[0]
119
        except errors.NotBranchError:
120
            raise NotBranchError(location)
121
    except errors.NotBranchError:
122
        raise NotBranchError(location)
0.8.7 by Szilveszter Farkas (Phanatic)
2006-06-20 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
123
124
    reader = None
0.8.64 by Szilveszter Farkas (Phanatic)
ChangeLog updated; bundle related bug fixed.
125
    if location is not None and not nobundle:
0.8.7 by Szilveszter Farkas (Phanatic)
2006-06-20 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
126
        try:
127
            reader = read_bundle_from_url(location)
128
        except errors.NotABundle:
129
            pass # Continue on considering this url a Branch
130
131
    stored_loc = branch_to.get_parent()
132
    if location is None:
133
        if stored_loc is None:
134
            raise NoLocationKnown
135
        else:
136
            location = stored_loc
137
0.8.64 by Szilveszter Farkas (Phanatic)
ChangeLog updated; bundle related bug fixed.
138
    if reader is not None and not nobundle:
0.8.7 by Szilveszter Farkas (Phanatic)
2006-06-20 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
139
        install_bundle(branch_to.repository, reader)
140
        branch_from = branch_to
141
    else:
142
        branch_from = Branch.open(location)
143
144
        if branch_to.get_parent() is None or remember:
145
            branch_to.set_parent(branch_from.base)
146
147
    rev_id = None
148
    
149
    if revision is not None:
150
        rev_id = branch_from.get_rev_id(revision)
151
    else:
152
        if reader is not None:
153
            rev_id = reader.info.target
154
        
155
    old_rh = branch_to.revision_history()
156
    if tree_to is not None:
157
        count = tree_to.pull(branch_from, overwrite, rev_id)
158
    else:
159
        count = branch_to.pull(branch_from, overwrite, rev_id)
160
    
161
    return count
162
163
def update(location):
164
    """ Update a tree to have the latest code committed to its branch.
165
    
166
    :param location: the path to the branch/working tree
167
    
168
    :return: None if tree is up to date, 1 if there are conflicts, 0 if updated without having conflicts
169
    """
170
    tree = WorkingTree.open_containing(location)[0]
171
    tree.lock_write()
172
    try:
173
        if tree.last_revision() == tree.branch.last_revision():
174
            # may be up to date, check master too.
175
            master = tree.branch.get_master_branch()
176
            if master is None or master.last_revision == tree.last_revision():
177
                # tree is up to date.
178
                return None
179
        conflicts = tree.update()
180
        if conflicts != 0:
181
            return 1
182
        else:
183
            return 0
184
    finally:
185
        tree.unlock()