~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to bzrtools.py

  • Committer: Aaron Bentley
  • Date: 2006-02-21 05:08:07 UTC
  • Revision ID: aaron.bentley@utoronto.ca-20060221050807-3cc86585e1ec6edc
Updated to match API changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
17
 
import bzrlib
18
 
import bzrlib.errors
 
17
import codecs
 
18
import errno
19
19
import os
20
 
import os.path
21
 
import sys
 
20
import re
22
21
import tempfile
23
22
import shutil
24
 
import errno
25
23
from subprocess import Popen, PIPE
26
 
import codecs
27
 
import re
28
 
 
29
 
def temp_branch():
 
24
import sys
 
25
 
 
26
import bzrlib
 
27
import bzrlib.errors
 
28
from bzrlib.bzrdir import BzrDir
 
29
 
 
30
def temp_tree():
30
31
    dirname = tempfile.mkdtemp("temp-branch")
31
 
    return bzrlib.branch.Branch.initialize(dirname)
32
 
 
33
 
def rm_branch(br):
34
 
    shutil.rmtree(br.base)
35
 
 
36
 
def is_clean(cur_branch):
 
32
    return BzrDir.create_standalone_workingtree(dirname)
 
33
 
 
34
def rm_tree(tree):
 
35
    shutil.rmtree(tree.basedir)
 
36
 
 
37
def is_clean(cur_tree):
37
38
    """
38
39
    Return true if no files are modifed or unknown
39
40
    >>> import bzrlib.add
40
 
    >>> br = temp_branch()
41
 
    >>> is_clean(br)
 
41
    >>> tree = temp_tree()
 
42
    >>> is_clean(tree)
42
43
    (True, [])
43
 
    >>> fooname = os.path.join(br.base, "foo")
 
44
    >>> fooname = os.path.join(tree.basedir, "foo")
44
45
    >>> file(fooname, "wb").write("bar")
45
 
    >>> is_clean(br)
 
46
    >>> is_clean(tree)
46
47
    (True, [u'foo'])
47
 
    >>> bzrlib.add.smart_add_tree(br.working_tree(), [br.base])
 
48
    >>> bzrlib.add.smart_add_tree(tree, [tree.basedir])
48
49
    ([u'foo'], {})
49
 
    >>> is_clean(br)
 
50
    >>> is_clean(tree)
50
51
    (False, [])
51
 
    >>> br.working_tree().commit("added file")
52
 
    >>> is_clean(br)
 
52
    >>> tree.commit("added file")
 
53
    >>> is_clean(tree)
53
54
    (True, [])
54
 
    >>> rm_branch(br)
 
55
    >>> rm_tree(tree)
55
56
    """
56
57
    from bzrlib.diff import compare_trees
57
 
    old_tree = cur_branch.basis_tree()
58
 
    new_tree = cur_branch.working_tree()
 
58
    old_tree = cur_tree.basis_tree()
 
59
    new_tree = cur_tree
59
60
    non_source = []
60
61
    for path, file_class, kind, file_id, entry in new_tree.list_files():
61
62
        if file_class in ('?', 'I'):
63
64
    delta = compare_trees(old_tree, new_tree, want_unchanged=False)
64
65
    return not delta.has_changed(), non_source
65
66
 
66
 
def set_push_data(br, location):
67
 
    push_file = file (br.control_files.controlfilename("x-push-data"), "wb")
 
67
def set_push_data(tree, location):
 
68
    push_file = file (tree._control_files.controlfilename("x-push-data"), "wb")
68
69
    push_file.write("%s\n" % location)
69
70
 
70
 
def get_push_data(br):
 
71
def get_push_data(tree):
71
72
    """
72
 
    >>> br = temp_branch()
73
 
    >>> get_push_data(br) is None
 
73
    >>> tree = temp_tree()
 
74
    >>> get_push_data(tree) is None
74
75
    True
75
 
    >>> set_push_data(br, 'http://somewhere')
76
 
    >>> get_push_data(br)
 
76
    >>> set_push_data(tree, 'http://somewhere')
 
77
    >>> get_push_data(tree)
77
78
    'http://somewhere'
78
 
    >>> rm_branch(br)
 
79
    >>> rm_tree(tree)
79
80
    """
80
 
    filename = br.control_files.controlfilename("x-push-data")
 
81
    filename = tree._control_files.controlfilename("x-push-data")
81
82
    if not os.path.exists(filename):
82
83
        return None
83
84
    push_file = file (filename, "rb")
232
233
    except RsyncNoFile:
233
234
        return True
234
235
 
235
 
def push(cur_branch, location=None, overwrite=False, working_tree=True):
236
 
    push_location = get_push_data(cur_branch)
 
236
def push(tree, location=None, overwrite=False, working_tree=True):
 
237
    push_location = get_push_data(tree)
237
238
    if location is not None:
238
239
        if not location.endswith('/'):
239
240
            location += '/'
248
249
    if push_location.find(':') == -1:
249
250
        raise bzrlib.errors.MustUseDecorated
250
251
 
251
 
    clean, non_source = is_clean(cur_branch)
 
252
    clean, non_source = is_clean(tree)
252
253
    if not clean:
253
254
        print """Error: This tree has uncommitted changes or unknown (?) files.
254
255
Use "bzr status" to list them."""
256
257
    if working_tree:
257
258
        final_exclusions = non_source[:]
258
259
    else:
259
 
        wt = cur_branch.working_tree()
 
260
        wt = tree
260
261
        final_exclusions = []
261
262
        for path, status, kind, file_id, entry in wt.list_files():
262
263
            final_exclusions.append(path)
264
265
    final_exclusions.extend(exclusions)
265
266
    if not overwrite:
266
267
        try:
267
 
            if not history_subset(push_location, cur_branch):
 
268
            if not history_subset(push_location, tree.branch):
268
269
                raise bzrlib.errors.BzrCommandError("Local branch is not a"
269
270
                                                    " newer version of remote"
270
271
                                                    " branch.")
278
279
                " specified location.  Please ensure that"
279
280
                ' "%s" is of the form "machine:/path".' % push_location)
280
281
    print "Pushing to %s" % push_location
281
 
    rsync(cur_branch.base+'/', push_location, ssh=True, 
 
282
    rsync(tree.bzrdir.transport.base+'/', push_location, ssh=True, 
282
283
          excludes=final_exclusions)
283
284
 
284
285
    set_push_data(cur_branch, push_location)