File: status_data.py

package info (click to toggle)
bzr-explorer 1.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 6,696 kB
  • sloc: python: 21,837; xml: 277; makefile: 31
file content (136 lines) | stat: -rw-r--r-- 4,717 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# Copyright (C) 2009 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA

"""Helper routines for collecting status data.

Note: These might migrate down into bzrlib after they mature.
"""

import codecs
from cStringIO import StringIO

from bzrlib import (
    branch as mod_branch,
    errors, 
    revisionspec, 
    status,
    )


def get_status_info(wt, specific_files=None, versioned=False):
    """Get the status information for a working tree.

    Pending merges are available separately. See get_pending_merges.

    :return: delta, conflicts, nonexistents
    :raises OutOfDateTree: bzr update needs to be run
    """
    # Note: This is status.show_tree_status() returning the data
    wt.lock_read()
    try:
        if wt.last_revision() != wt.branch.last_revision():
            raise errors.OutOfDateTree(wt)
        return changes_between_trees(wt, wt.basis_tree(), specific_files,
            versioned)
    finally:
        wt.unlock()


def changes_between_trees(new, old, specific_files=None, versioned=False):
    """Get the changes between two trees.

    :return: delta, conflicts, nonexistents
    """
    old.lock_read()
    new.lock_read()
    try:
        specific_files, nonexistents \
            = status._filter_nonexistent(specific_files, old, new)
        want_unversioned = not versioned
        delta = new.changes_from(old,
                              specific_files=specific_files,
                              want_unversioned=want_unversioned)

        # filter out unknown files. We may want a tree method for this
        delta.unversioned = [unversioned for unversioned in
            delta.unversioned if not new.is_ignored(unversioned[0])]

        # Get the new conflicts only for now. XXX: get them from the delta.
        conflicts = new.conflicts()
        if specific_files is not None:
            conflicts = conflicts.select_conflicts(new, specific_files,
                ignore_misses=True, recurse=True)[1]

        return delta, conflicts, nonexistents
    finally:
        old.unlock()
        new.unlock()


def get_pending_merges(wt, verbose=False):
    """Get the pending merges in a working tree.
    
    :param verbose: if False, only the tips are returned
    """
    # simple implementation for now
    sio = codecs.getwriter('utf-8')(StringIO())
    status.show_pending_merges(wt, to_file=sio, verbose=verbose)
    lines = sio.getvalue().decode('utf-8').splitlines()
    # take off the top intro line, if any
    if lines:
        return lines[1:]
    else:
        return None


def get_submission_info(branch):
    """Get the status information for a branch versus its submit branch.

    :return: delta
    """
    new_tree = branch.basis_tree()
    rev_spec = revisionspec.RevisionSpec.from_string('submit:')
    old_tree = rev_spec.as_tree(branch)
    return changes_between_trees(new_tree, old_tree)[0]

def get_parent_submission_info(branch):
    """Get the status information for a branch versus its parent branch.

    We get delta as equivalent of bzr command:
        bzr status -r ancestor::parent..-1

    We use common ancestor of both branches as base revision to make sure
    we only show changes made in this branch and exclude side-effects
    of changes made in parent branch (see bug #956268 for details).

    :return: delta
    """
    new_tree = branch.basis_tree()
    old_branch = mod_branch.Branch.open_containing(branch.get_parent())[0]
    branch.lock_read()
    old_branch.lock_read()
    try:
        # Thanks to Aaron for code snippet
        # Get a graph, so we can find the common ancestor (LCA)
        graph = old_branch.repository.get_graph(branch.repository)
        # Find the LCA of the revisions (equivalent of `-r ancestor::parent`)
        lca = graph.find_unique_lca(branch.last_revision(),
                                    old_branch.last_revision())
        old_tree = old_branch.repository.revision_tree(lca)
        return changes_between_trees(new_tree, old_tree)[0]
    finally:
        old_branch.unlock()
        branch.unlock()