14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program; if not, write to the Free Software
16
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
23
from subprocess import Popen, PIPE
28
from bzrlib.errors import BzrCommandError
29
from bzrlib.bzrdir import BzrDir
30
32
dirname = tempfile.mkdtemp("temp-branch")
31
return bzrlib.branch.Branch.initialize(dirname)
34
shutil.rmtree(br.base)
36
def is_clean(cur_branch):
33
return BzrDir.create_standalone_workingtree(dirname)
36
shutil.rmtree(tree.basedir)
38
def is_clean(cur_tree):
38
40
Return true if no files are modifed or unknown
39
41
>>> import bzrlib.add
40
>>> br = temp_branch()
42
>>> tree = temp_tree()
43
>>> fooname = os.path.join(br.base, "foo")
45
>>> fooname = os.path.join(tree.basedir, "foo")
44
46
>>> file(fooname, "wb").write("bar")
47
>>> br.working_tree().add(["foo"])
49
>>> bzrlib.add.smart_add_tree(tree, [tree.basedir])
50
>>> br.working_tree().commit("added file")
53
>>> tree.commit("added file")
55
58
from bzrlib.diff import compare_trees
56
old_tree = cur_branch.basis_tree()
57
new_tree = cur_branch.working_tree()
59
old_tree = cur_tree.basis_tree()
59
62
for path, file_class, kind, file_id, entry in new_tree.list_files():
60
63
if file_class in ('?', 'I'):
62
65
delta = compare_trees(old_tree, new_tree, want_unchanged=False)
63
66
return not delta.has_changed(), non_source
65
def set_pull_data(br, location, rev_id):
66
pull_file = file (br.control_files.controlfilename("x-pull-data"), "wb")
67
pull_file.write("%s\n%s\n" % (location, rev_id))
69
def get_pull_data(br):
71
>>> br = temp_branch()
74
>>> set_pull_data(br, 'http://somewhere', '888-777')
76
('http://somewhere', '888-777')
79
filename = br.control_files.controlfilename("x-pull-data")
80
if not os.path.exists(filename):
82
pull_file = file (filename, "rb")
83
location, rev_id = [f.rstrip('\n') for f in pull_file]
84
return location, rev_id
86
def set_push_data(br, location):
87
push_file = file (br.control_files.controlfilename("x-push-data"), "wb")
68
def set_push_data(tree, location):
69
push_file = file (tree._control_files.controlfilename("x-push-data"), "wb")
88
70
push_file.write("%s\n" % location)
90
def get_push_data(br):
72
def get_push_data(tree):
92
>>> br = temp_branch()
93
>>> get_push_data(br) is None
74
>>> tree = temp_tree()
75
>>> get_push_data(tree) is None
95
>>> set_push_data(br, 'http://somewhere')
77
>>> set_push_data(tree, 'http://somewhere')
78
>>> get_push_data(tree)
100
filename = br.control_files.controlfilename("x-push-data")
82
filename = tree._control_files.controlfilename("x-push-data")
101
83
if not os.path.exists(filename):
103
85
push_file = file (filename, "rb")
133
115
def rsync(source, target, ssh=False, excludes=(), silent=False,
134
116
rsync_name="rsync"):
136
>>> tempdir = tempfile.mkdtemp()
137
>>> rsync(tempdir + "/a", tempdir + "/b", silent=True) #doctest: +ELLIPSIS
118
>>> new_dir = tempfile.mkdtemp()
119
>>> old_dir = os.getcwd()
120
>>> os.chdir(new_dir)
121
>>> rsync("a", "b", silent=True)
138
122
Traceback (most recent call last):
139
123
RsyncNoFile: No such file...
140
124
>>> rsync(tempdir + "/a", tempdir + "/b", excludes=("*.py",), silent=True) #doctest: +ELLIPSIS
143
127
>>> rsync(tempdir + "/a", tempdir + "/b", excludes=("*.py",), silent=True, rsync_name="rsyncc")
144
128
Traceback (most recent call last):
145
129
NoRsync: rsyncc not found.
146
>>> os.rmdir(tempdir)
130
>>> os.chdir(old_dir)
131
>>> os.rmdir(new_dir)
148
133
cmd = [rsync_name, "-av", "--delete"]
249
234
except RsyncNoFile:
252
def push(cur_branch, location=None, overwrite=False):
253
push_location = get_push_data(cur_branch)
237
def push(tree, location=None, overwrite=False, working_tree=True):
238
push_location = get_push_data(tree)
254
239
if location is not None:
255
240
if not location.endswith('/'):
257
242
push_location = location
259
244
if push_location is None:
260
raise bzrlib.errors.MustUseDecorated
245
if tree.branch.get_push_location() is None:
246
raise BzrCommandError("No push location known or specified.")
248
raise bzrlib.errors.MustUseDecorated
262
250
if push_location.find('://') != -1:
263
251
raise bzrlib.errors.MustUseDecorated
265
253
if push_location.find(':') == -1:
266
254
raise bzrlib.errors.MustUseDecorated
268
clean, non_source = is_clean(cur_branch)
256
clean, non_source = is_clean(tree)
270
258
print """Error: This tree has uncommitted changes or unknown (?) files.
271
259
Use "bzr status" to list them."""
273
non_source.extend(exclusions)
262
final_exclusions = non_source[:]
265
final_exclusions = []
266
for path, status, kind, file_id, entry in wt.list_files():
267
final_exclusions.append(path)
269
final_exclusions.extend(exclusions)
274
270
if not overwrite:
276
if not history_subset(push_location, cur_branch):
272
if not history_subset(push_location, tree.branch):
277
273
raise bzrlib.errors.BzrCommandError("Local branch is not a"
278
274
" newer version of remote"
287
283
" specified location. Please ensure that"
288
284
' "%s" is of the form "machine:/path".' % push_location)
289
285
print "Pushing to %s" % push_location
290
rsync(cur_branch.base+'/', push_location, ssh=True, excludes=non_source)
286
rsync(tree.basedir+'/', push_location, ssh=True,
287
excludes=final_exclusions)
292
set_push_data(cur_branch, push_location)
289
set_push_data(tree, push_location)
295
292
def short_committer(committer):