/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/add.py

  • Committer: John Arbash Meinel
  • Date: 2006-08-09 19:53:02 UTC
  • mto: This revision was merged to the branch mainline in revision 1927.
  • Revision ID: john@arbash-meinel.com-20060809195302-38b349ff2e1473b4
Adding the AddFromBaseAction, which tries to reuse file ids from another tree

Show diffs side-by-side

added added

removed removed

Lines of Context:
79
79
        return None
80
80
 
81
81
 
 
82
class AddFromBaseAction(AddAction):
 
83
    """This class will try to extract file ids from another tree."""
 
84
 
 
85
    def __init__(self, base_tree, base_path, to_file=None, should_print=None):
 
86
        super(AddFromBaseAction, self).__init__(to_file=to_file,
 
87
                                                should_print=should_print)
 
88
        self.base_tree = base_tree
 
89
        self.base_path = base_path
 
90
 
 
91
    def __call__(self, inv, parent_ie, path, kind):
 
92
        # Place the parent call
 
93
        # Now check to see if we can extract an id for this file
 
94
        file_id, base_path = self._get_base_file_id(path, parent_ie)
 
95
        if file_id is not None:
 
96
            if self.should_print:
 
97
                self._to_file.write('added %s w/ file id from %s\n'
 
98
                                    % (path.raw_path, base_path))
 
99
        else:
 
100
            # we aren't doing anything special, so let the default
 
101
            # reporter happen
 
102
            file_id = super(AddFromBaseAction, self).__call__(
 
103
                        inv, parent_ie, path, kind)
 
104
        return file_id
 
105
 
 
106
    def _get_base_file_id(self, path, parent_ie):
 
107
        """Look for a file id in the base branch.
 
108
 
 
109
        First, if the base tree has the parent directory,
 
110
        we look for a file with the same name in that directory.
 
111
        Else, we look for an entry in the base tree with the same path.
 
112
        """
 
113
 
 
114
        if (parent_ie.file_id in self.base_tree):
 
115
            base_parent_ie = self.base_tree.inventory[parent_ie.file_id]
 
116
            base_child_ie = base_parent_ie.children.get(path.base_path)
 
117
            if base_child_ie is not None:
 
118
                return (base_child_ie.file_id,
 
119
                        self.base_tree.id2path(base_child_ie.file_id))
 
120
        full_base_path = bzrlib.osutils.pathjoin(self.base_path, path.raw_path)
 
121
        # This may return None, but it is our last attempt
 
122
        return self.base_tree.path2id(full_base_path), full_base_path
 
123
 
 
124
 
82
125
# TODO: jam 20050105 These could be used for compatibility
83
126
#       however, they bind against the current stdout, not the
84
127
#       one which exists at the time they are called, so they