~ubuntu-branches/ubuntu/utopic/bzr-fastimport/utopic-proposed

« back to all changes in this revision

Viewing changes to branch_updater.py

  • Committer: Bazaar Package Importer
  • Author(s): Jelmer Vernooij
  • Date: 2009-05-05 20:23:22 UTC
  • mfrom: (0.1.3 squeeze) (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090505202322-t5ce971trg0ojsfj
Tags: 0.8.0~bzr181-1
* Move to section vcs.
* New upstream snapshot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""An object that updates a bunch of branches based on data imported."""
 
18
 
 
19
from operator import itemgetter
 
20
 
 
21
from bzrlib import bzrdir, errors
 
22
from bzrlib.trace import error, note
 
23
 
 
24
import branch_mapper
 
25
import helpers
 
26
 
 
27
 
 
28
class BranchUpdater(object):
 
29
 
 
30
    def __init__(self, repo, branch, cache_mgr, heads_by_ref, last_ref, tags):
 
31
        """Create an object responsible for updating branches.
 
32
 
 
33
        :param heads_by_ref: a dictionary where
 
34
          names are git-style references like refs/heads/master;
 
35
          values are one item lists of commits marks.
 
36
        """
 
37
        self.repo = repo
 
38
        self.branch = branch
 
39
        self.cache_mgr = cache_mgr
 
40
        self.heads_by_ref = heads_by_ref
 
41
        self.last_ref = last_ref
 
42
        self.tags = tags
 
43
        self.name_mapper = branch_mapper.BranchMapper()
 
44
 
 
45
    def update(self):
 
46
        """Update the Bazaar branches and tips matching the heads.
 
47
 
 
48
        If the repository is shared, this routine creates branches
 
49
        as required. If it isn't, warnings are produced about the
 
50
        lost of information.
 
51
 
 
52
        :return: updated, lost_heads where
 
53
          updated = the list of branches updated
 
54
          lost_heads = a list of (bazaar-name,revision) for branches that
 
55
            would have been created had the repository been shared
 
56
        """
 
57
        updated = []
 
58
        branch_tips, lost_heads = self._get_matching_branches()
 
59
        for br, tip in branch_tips:
 
60
            if self._update_branch(br, tip):
 
61
                updated.append(br)
 
62
        return updated, lost_heads
 
63
 
 
64
    def _get_matching_branches(self):
 
65
        """Get the Bazaar branches.
 
66
 
 
67
        :return: default_tip, branch_tips, lost_heads where
 
68
          default_tip = the last commit mark for the default branch
 
69
          branch_tips = a list of (branch,tip) tuples for other branches.
 
70
          lost_heads = a list of (bazaar-name,revision) for branches that
 
71
            would have been created had the repository been shared and
 
72
            everything succeeded
 
73
        """
 
74
        branch_tips = []
 
75
        lost_heads = []
 
76
        ref_names = self.heads_by_ref.keys()
 
77
        if self.branch is not None:
 
78
            trunk = self.select_trunk(ref_names)
 
79
            default_tip = self.heads_by_ref[trunk][0]
 
80
            branch_tips.append((self.branch, default_tip))
 
81
            ref_names.remove(trunk)
 
82
 
 
83
        # Convert the reference names into Bazaar speak
 
84
        git_to_bzr_map = self.name_mapper.git_to_bzr(ref_names)
 
85
 
 
86
        # Policy for locating branches
 
87
        def dir_under_current(name, ref_name):
 
88
            # Using the Bazaar name, get a directory under the current one
 
89
            return name
 
90
        def dir_sister_branch(name, ref_name):
 
91
            # Using the Bazaar name, get a sister directory to the branch
 
92
            return osutils.pathjoin(self.branch.base, "..", name)
 
93
        if self.branch is not None:
 
94
            dir_policy = dir_sister_branch
 
95
        else:
 
96
            dir_policy = dir_under_current
 
97
 
 
98
        # Create/track missing branches
 
99
        shared_repo = self.repo.is_shared()
 
100
        git_bzr_items = sorted(git_to_bzr_map.items(), key=itemgetter(1))
 
101
        for ref_name, name in git_bzr_items:
 
102
            tip = self.heads_by_ref[ref_name][0]
 
103
            if shared_repo:
 
104
                location = dir_policy(name, ref_name)
 
105
                try:
 
106
                    br = self.make_branch(location)
 
107
                    branch_tips.append((br,tip))
 
108
                    continue
 
109
                except errors.BzrError, ex:
 
110
                    error("ERROR: failed to create branch %s: %s",
 
111
                        location, ex)
 
112
            lost_head = self.cache_mgr.revision_ids[tip]
 
113
            lost_info = (name, lost_head)
 
114
            lost_heads.append(lost_info)
 
115
        return branch_tips, lost_heads
 
116
 
 
117
    def select_trunk(self, ref_names):
 
118
        """Given a set of ref names, choose one as the trunk."""
 
119
        for candidate in ['refs/heads/master']:
 
120
            if candidate in ref_names:
 
121
                return candidate
 
122
        # Use the last reference in the import stream
 
123
        return self.last_ref
 
124
 
 
125
    def make_branch(self, location):
 
126
        """Make a branch in the repository if not already there."""
 
127
        try:
 
128
            return bzrdir.BzrDir.open(location).open_branch()
 
129
        except errors.NotBranchError, ex:
 
130
            return bzrdir.BzrDir.create_branch_convenience(location)
 
131
 
 
132
    def _update_branch(self, br, last_mark):
 
133
        """Update a branch with last revision and tag information.
 
134
        
 
135
        :return: whether the branch was changed or not
 
136
        """
 
137
        last_rev_id = self.cache_mgr.revision_ids[last_mark]
 
138
        revs = list(self.repo.iter_reverse_revision_history(last_rev_id))
 
139
        revno = len(revs)
 
140
        existing_revno, existing_last_rev_id = br.last_revision_info()
 
141
        changed = False
 
142
        if revno != existing_revno or last_rev_id != existing_last_rev_id:
 
143
            br.set_last_revision_info(revno, last_rev_id)
 
144
            changed = True
 
145
        # apply tags known in this branch
 
146
        my_tags = {}
 
147
        if self.tags:
 
148
            for tag,rev in self.tags.items():
 
149
                if rev in revs:
 
150
                    my_tags[tag] = rev
 
151
            if my_tags:
 
152
                br.tags._set_tag_dict(my_tags)
 
153
                changed = True
 
154
        if changed:
 
155
            tagno = len(my_tags)
 
156
            note("\t branch %s now has %d %s and %d %s", br.nick,
 
157
                revno, helpers.single_plural(revno, "revision", "revisions"),
 
158
                tagno, helpers.single_plural(tagno, "tag", "tags"))
 
159
        return changed