1
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
 
 
2
# Some parts of the code are:
 
 
3
# Copyright (C) 2005, 2006 by Canonical Ltd
 
 
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.
 
 
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.
 
 
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
 
 
19
import bzrlib.errors as errors
 
 
21
from bzrlib.workingtree import WorkingTree
 
 
23
from errors import NoLocationKnown, NotBranchError
 
 
25
def missing(branch, other_branch=None, reverse=False):
 
 
26
    """ Get the number of missing or extra revisions of local branch.
 
 
28
    :param branch: path to local branch
 
 
30
    :param other_branch: location of the other branch
 
 
32
    :param reverse: reverse the order of revisions
 
 
34
    :return: number of missing revisions (if < 0, then extra revisions * -1)
 
 
37
    from bzrlib.missing import find_unmerged
 
 
39
    local_branch = bzrlib.branch.Branch.open_containing(branch)[0]
 
 
40
    parent = local_branch.get_parent()
 
 
41
    if other_branch is None:
 
 
43
        if other_branch is None:
 
 
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()
 
 
53
        remote_branch.lock_read()
 
 
55
            local_extra, remote_extra = find_unmerged(local_branch, remote_branch)
 
 
59
                remote_extra.reverse()
 
 
62
                ret = len(local_extra) * -1
 
 
65
                ret = len(remote_extra)
 
 
67
            if not remote_extra and not local_extra:
 
 
71
            remote_branch.unlock()
 
 
75
    if not ret and parent is None and other_branch is not None:
 
 
76
        local_branch.lock_write()
 
 
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)
 
 
87
def pull(branch, location=None, remember=False, overwrite=False, revision=None):
 
 
88
    """ Pull revisions from another branch.
 
 
90
    :param branch: the local branch where you want to pull
 
 
92
    :param location: location of branch you'd like to pull from (can be a bundle, too)
 
 
94
    :param remeber: if True, the location will be stored
 
 
96
    :param overwrite: if True, forget local changes, and update the branch to match the remote one
 
 
98
    :param revision: if specified, only the given revision will be pulled (revno)
 
 
100
    :return: number of revisions pulled
 
 
102
    from bzrlib.branch import Branch
 
 
103
    from bzrlib.bundle import read_bundle_from_url
 
 
104
    from bzrlib.bundle.apply_bundle import install_bundle
 
 
107
        tree_to = WorkingTree.open_containing(branch)[0]
 
 
108
        branch_to = tree_to.branch
 
 
109
    except errors.NoWorkingTree:
 
 
112
            branch_to = Branch.open_containing(branch)[0]
 
 
113
        except errors.NotBranchError:
 
 
114
            raise NotBranchError(location)
 
 
115
    except errors.NotBranchError:
 
 
116
        raise NotBranchError(location)
 
 
119
    if location is not None:
 
 
121
            reader = read_bundle_from_url(location)
 
 
122
        except errors.NotABundle:
 
 
123
            pass # Continue on considering this url a Branch
 
 
125
    stored_loc = branch_to.get_parent()
 
 
127
        if stored_loc is None:
 
 
128
            raise NoLocationKnown
 
 
130
            location = stored_loc
 
 
132
    if reader is not None:
 
 
133
        install_bundle(branch_to.repository, reader)
 
 
134
        branch_from = branch_to
 
 
136
        branch_from = Branch.open(location)
 
 
138
        if branch_to.get_parent() is None or remember:
 
 
139
            branch_to.set_parent(branch_from.base)
 
 
143
    if revision is not None:
 
 
144
        rev_id = branch_from.get_rev_id(revision)
 
 
146
        if reader is not None:
 
 
147
            rev_id = reader.info.target
 
 
149
    old_rh = branch_to.revision_history()
 
 
150
    if tree_to is not None:
 
 
151
        count = tree_to.pull(branch_from, overwrite, rev_id)
 
 
153
        count = branch_to.pull(branch_from, overwrite, rev_id)
 
 
157
def update(location):
 
 
158
    """ Update a tree to have the latest code committed to its branch.
 
 
160
    :param location: the path to the branch/working tree
 
 
162
    :return: None if tree is up to date, 1 if there are conflicts, 0 if updated without having conflicts
 
 
164
    tree = WorkingTree.open_containing(location)[0]
 
 
167
        if tree.last_revision() == tree.branch.last_revision():
 
 
168
            # may be up to date, check master too.
 
 
169
            master = tree.branch.get_master_branch()
 
 
170
            if master is None or master.last_revision == tree.last_revision():
 
 
171
                # tree is up to date.
 
 
173
        conflicts = tree.update()