~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to upstream_import.py

  • Committer: Aaron Bentley
  • Date: 2006-05-18 16:36:49 UTC
  • Revision ID: abentley@panoramicfeedback.com-20060518163649-f37d1b11e4eac43e
Handle adds and removes efficiently

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
"""Import upstream source into a branch"""
2
2
 
 
3
import errno
3
4
import os
4
5
from shutil import rmtree
5
6
from StringIO import StringIO
40
41
    return possible_prefix
41
42
 
42
43
 
43
 
def do_directory(tt, trans_id, relative_path, path):
 
44
def do_directory(tt, trans_id, tree, relative_path, path):
44
45
    if isdir(path) and tree.path2id(relative_path) is not None:
45
46
        tt.cancel_deletion(trans_id)
46
47
    else:
62
63
    """
63
64
    tar_file = tarfile.open('lala', 'r', tar_input)
64
65
    prefix = common_directory(tar_file.getnames())
65
 
    added = []
66
66
    tt = TreeTransform(tree)
67
67
 
 
68
    removed = set()
68
69
    for path, entry in tree.inventory.iter_entries():
69
70
        trans_id = tt.trans_id_tree_path(path)
70
71
        tt.delete_contents(trans_id)
 
72
        removed.add(path)
71
73
 
 
74
    added = set() 
72
75
    implied_parents = set()
73
76
    for member in tar_file.getmembers():
74
77
        relative_path = member.name 
78
81
            continue
79
82
        add_implied_parents(implied_parents, relative_path)
80
83
        trans_id = tt.trans_id_tree_path(relative_path)
81
 
        added.append(relative_path)
 
84
        added.add(relative_path.rstrip('/'))
82
85
        path = tree.abspath(relative_path)
83
86
        if member.isreg():
84
87
            tt.create_file(file_iterator(tar_file.extractfile(member)), 
85
88
                           trans_id)
86
89
        elif member.isdir():
87
 
            do_directory(tt, trans_id, relative_path, path)
 
90
            do_directory(tt, trans_id, tree, relative_path, path)
88
91
        elif member.issym():
89
92
            tt.create_symlink(member.linkname, trans_id)
90
93
 
91
 
    for relative_path in implied_parents.difference(set(added)):
 
94
    for relative_path in implied_parents.difference(added):
92
95
        if relative_path == "":
93
96
            continue
94
97
        trans_id = tt.trans_id_tree_path(relative_path)
95
98
        path = tree.abspath(relative_path)
96
 
        do_directory(tt, trans_id, relative_path, path)
97
 
        added.append(relative_path)
 
99
        do_directory(tt, trans_id, tree, relative_path, path)
 
100
        added.add(relative_path)
98
101
 
99
102
    for conflict in cook_conflicts(resolve_conflicts(tt), tt):
100
103
        warning(conflict)
101
104
    tt.apply()
102
 
    update_ids(tree, added)
103
 
 
104
 
 
105
 
def update_ids(tree, added):
 
105
    update_ids(tree, added, removed)
 
106
 
 
107
 
 
108
def update_ids(tree, added, removed):
106
109
    """Make sure that all present files files have file_ids.
107
110
    """
108
111
    # XXX detect renames
109
 
    for path in sorted(added):
110
 
        if tree.path2id(path) is None:
111
 
            tree.add(path)
 
112
    new = added.difference(removed)
 
113
    deleted = removed.difference(added)
 
114
    tree.add(sorted(new))
 
115
    tree.remove(sorted(deleted, reverse=True))
112
116
 
113
117
 
114
118
def do_import(source, tree_directory=None):
132
136
            source.endswith('.tar.bz2')):
133
137
            try:
134
138
                tar_input = file(source, 'rb')
135
 
            except:
 
139
            except IOError, e:
136
140
                if e.errno == errno.ENOENT:
137
141
                    raise NoSuchFile(source)
138
142
            try: