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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
# Some parts of the code are:
# Copyright (C) 2005, 2006 by Canonical Ltd
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import bzrlib.errors as errors
from bzrlib.workingtree import WorkingTree
from errors import NoLocationKnown
def missing(branch, other_branch=None, reverse=False):
""" Get the number of missing or extra revisions of local branch.
:param branch: path to local branch
:param other_branch: location of the other branch
:param reverse: reverse the order of revisions
:return: number of missing revisions (if < 0, then extra revisions * -1)
"""
import bzrlib
from bzrlib.missing import find_unmerged
local_branch = bzrlib.branch.Branch.open_containing(branch)[0]
parent = local_branch.get_parent()
if other_branch is None:
other_branch = parent
if other_branch is None:
raise NoLocationKnown
remote_branch = bzrlib.branch.Branch.open(other_branch)
if remote_branch.base == local_branch.base:
remote_branch = local_branch
local_branch.lock_read()
ret = 0
try:
remote_branch.lock_read()
try:
local_extra, remote_extra = find_unmerged(local_branch, remote_branch)
if reverse is False:
local_extra.reverse()
remote_extra.reverse()
if local_extra:
ret = len(local_extra) * -1
if remote_extra:
ret = len(remote_extra)
if not remote_extra and not local_extra:
ret = 0
finally:
remote_branch.unlock()
finally:
local_branch.unlock()
if not ret and parent is None and other_branch is not None:
local_branch.lock_write()
try:
# handle race conditions - a parent might be set while we run.
if local_branch.get_parent() is None:
local_branch.set_parent(remote_branch.base)
finally:
local_branch.unlock()
return ret
def pull(branch, location=None, remember=False, overwrite=False, revision=None):
""" Pull revisions from another branch.
:param branch: the local branch where you want to pull
:param location: location of branch you'd like to pull from (can be a bundle, too)
:param remeber: if True, the location will be stored
:param overwrite: if True, forget local changes, and update the branch to match the remote one
:param revision: if specified, only the given revision will be pulled (revno)
:return: number of revisions pulled
"""
from bzrlib.branch import Branch
from bzrlib.bundle import read_bundle_from_url
from bzrlib.bundle.apply_bundle import install_bundle
try:
tree_to = WorkingTree.open_containing(branch)[0]
branch_to = tree_to.branch
except errors.NoWorkingTree:
tree_to = None
branch_to = Branch.open_containing(branch)[0]
reader = None
if location is not None:
try:
reader = read_bundle_from_url(location)
except errors.NotABundle:
pass # Continue on considering this url a Branch
stored_loc = branch_to.get_parent()
if location is None:
if stored_loc is None:
raise NoLocationKnown
else:
location = stored_loc
if reader is not None:
install_bundle(branch_to.repository, reader)
branch_from = branch_to
else:
branch_from = Branch.open(location)
if branch_to.get_parent() is None or remember:
branch_to.set_parent(branch_from.base)
rev_id = None
if revision is not None:
rev_id = branch_from.get_rev_id(revision)
else:
if reader is not None:
rev_id = reader.info.target
old_rh = branch_to.revision_history()
if tree_to is not None:
count = tree_to.pull(branch_from, overwrite, rev_id)
else:
count = branch_to.pull(branch_from, overwrite, rev_id)
return count
def update(location):
""" Update a tree to have the latest code committed to its branch.
:param location: the path to the branch/working tree
:return: None if tree is up to date, 1 if there are conflicts, 0 if updated without having conflicts
"""
tree = WorkingTree.open_containing(location)[0]
tree.lock_write()
try:
if tree.last_revision() == tree.branch.last_revision():
# may be up to date, check master too.
master = tree.branch.get_master_branch()
if master is None or master.last_revision == tree.last_revision():
# tree is up to date.
return None
conflicts = tree.update()
if conflicts != 0:
return 1
else:
return 0
finally:
tree.unlock()
|