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