~benoit.pierre/bzrtools/shell_improvements

« back to all changes in this revision

Viewing changes to fetch_ghosts.py

  • Committer: Benoît Pierre
  • Date: 2009-08-02 12:55:24 UTC
  • mfrom: (682.1.36 bzrtools)
  • Revision ID: benoit.pierre@gmail.com-20090802125524-1p2oot1y2l0y3o9w
Merge with upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
from bzrlib.branch import Branch
17
 
from bzrlib.errors import NoSuchRevision, InstallFailed, BzrCommandError
18
 
def fetch_ghosts(branch, no_fix):
19
 
    """Install ghosts from copies in another branch."""
20
 
    this_branch = Branch.open_containing('.')[0]
21
 
    if branch is None:
22
 
        branch = this_branch.get_parent()
23
 
        if branch is None:
24
 
            raise BzrCommandError('No branch specified and no location saved.')
25
 
        else:
26
 
            print "Using saved location %s" % branch
27
 
 
28
 
    other_branch = Branch.open_containing(branch)[0]
29
 
    installed = []
30
 
    failed = []
31
 
    lock_other = this_branch.base != other_branch.base
32
 
 
33
 
    this_branch.lock_write()
34
 
    try:
35
 
        if lock_other:
36
 
            other_branch.lock_read()
 
17
from bzrlib.trace import note
 
18
from bzrlib.errors import NoSuchRevision, BzrCommandError
 
19
 
 
20
class GhostFetcher(object):
 
21
 
 
22
    @classmethod
 
23
    def from_cmdline(klass, other):
 
24
        this_branch = Branch.open_containing('.')[0]
 
25
        if other is None:
 
26
            other = this_branch.get_parent()
 
27
            if other is None:
 
28
                raise BzrCommandError('No branch specified and no location'
 
29
                                      ' saved.')
 
30
            else:
 
31
                note("Using saved location %s.", branch)
 
32
        other_branch = Branch.open_containing(other)[0]
 
33
        return klass(this_branch, other_branch)
 
34
 
 
35
    def __init__(self, this_branch, other_branch):
 
36
        self.this_branch = this_branch
 
37
        self.other_branch = other_branch
 
38
 
 
39
    def run(self):
 
40
        lock_other = self.this_branch.base != self.other_branch.base
 
41
        self.this_branch.lock_write()
37
42
        try:
38
 
            # Because iter_ghosts tests for existence after our last fetch
39
 
            # is complete, it won't falsely report an ancestor as a ghost.
40
 
            # Yay iterators!
41
 
            ghosts = iter_ghosts(this_branch)
42
 
            for revision in ghosts:
 
43
            if lock_other:
 
44
                self.other_branch.lock_read()
 
45
            try:
 
46
                return self._run_locked()
 
47
            finally:
 
48
                if lock_other:
 
49
                    self.other_branch.unlock()
 
50
        finally:
 
51
            self.this_branch.unlock()
 
52
 
 
53
    def iter_ghosts(self):
 
54
        """Find all ancestors that aren't stored in this branch."""
 
55
        seen = set()
 
56
        lines = [self.this_branch.last_revision()]
 
57
        if lines[0] is None:
 
58
            return
 
59
        while len(lines) > 0:
 
60
            new_lines = []
 
61
            for line in lines:
 
62
                if line in seen:
 
63
                    continue
 
64
                seen.add(line)
43
65
                try:
44
 
                    this_branch.fetch(other_branch, revision)
45
 
                    installed.append(revision)
46
 
                except InstallFailed:
47
 
                    failed.append(revision)
48
 
            else:
49
 
                if this_branch.last_revision() is None:
50
 
                    print "No revisions in branch."
51
 
            if len(installed) > 0:
52
 
                print "Installed:"
53
 
                for rev in installed:
54
 
                    print rev
55
 
            if len(failed) > 0:
56
 
                print "Still missing:"
57
 
                for rev in failed:
58
 
                    print rev
59
 
        finally:
60
 
            if lock_other:
61
 
                other_branch.unlock()
62
 
    finally:
63
 
        this_branch.unlock()
64
 
 
65
 
    if not no_fix and len(installed) > 0:
 
66
                    revision = self.this_branch.repository.get_revision(line)
 
67
                    new_lines.extend(revision.parent_ids)
 
68
                except NoSuchRevision:
 
69
                    yield line
 
70
            lines = new_lines
 
71
 
 
72
    def _run_locked(self):
 
73
        installed = []
 
74
        failed = []
 
75
        if self.this_branch.last_revision() is None:
 
76
            print "No revisions in branch."
 
77
            return
 
78
        # Because iter_ghosts tests for existence after our last fetch
 
79
        # is complete, it won't falsely report an ancestor as a ghost.
 
80
        # Yay iterators!
 
81
        ghosts = self.iter_ghosts()
 
82
        for revision in ghosts:
 
83
            try:
 
84
                self.this_branch.fetch(self.other_branch, revision)
 
85
                installed.append(revision)
 
86
            except NoSuchRevision:
 
87
                failed.append(revision)
 
88
        return installed, failed
 
89
 
 
90
def fetch_ghosts(branch, do_reconcile):
 
91
    """Install ghosts from copies in another branch."""
 
92
    installed, failed = GhostFetcher.from_cmdline(branch).run()
 
93
    if len(installed) > 0:
 
94
        print "Installed:"
 
95
        for rev in installed:
 
96
            print rev
 
97
    if len(failed) > 0:
 
98
        print "Still missing:"
 
99
        for rev in failed:
 
100
            print rev
 
101
    if do_reconcile and len(installed) > 0:
66
102
        from bzrlib.builtins import cmd_reconcile
67
103
        cmd_reconcile().run(".")
68
104
 
69
 
def iter_ghosts(branch):
70
 
    """Find all ancestors that aren't stored in this branch."""
71
 
    seen = set()
72
 
    lines = [branch.last_revision()]
73
 
    if lines[0] is None:
74
 
        return
75
 
    while len(lines) > 0:
76
 
        new_lines = []
77
 
        for line in lines:
78
 
            if line in seen:
79
 
                continue
80
 
            seen.add(line)
81
 
            try:
82
 
                revision = branch.repository.get_revision(line)
83
 
                new_lines.extend(revision.parent_ids)
84
 
            except NoSuchRevision:
85
 
                yield line
86
 
        lines = new_lines
 
105