~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/info.py

  • Committer: Aaron Bentley
  • Date: 2007-07-17 13:27:14 UTC
  • mfrom: (2624 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2631.
  • Revision ID: abentley@panoramicfeedback.com-20070717132714-tmzx9khmg9501k51
Merge from bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
__all__ = ['show_bzrdir_info']
18
18
 
 
19
import os
19
20
import time
20
 
 
 
21
import sys
21
22
 
22
23
from bzrlib import (
23
24
    bzrdir,
24
25
    diff,
 
26
    errors,
25
27
    osutils,
26
28
    urlutils,
27
29
    )
41
43
        return 's'
42
44
 
43
45
 
44
 
def _repo_rel_url(repo_url, inner_url):
45
 
    """Return path with common prefix of repository path removed.
46
 
 
47
 
    If path is not part of the repository, the original path is returned.
48
 
    If path is equal to the repository, the current directory marker '.' is
49
 
    returned.
50
 
    Otherwise, a relative path is returned, with trailing '/' stripped.
51
 
    """
52
 
    inner_url = urlutils.normalize_url(inner_url)
53
 
    repo_url = urlutils.normalize_url(repo_url)
54
 
    if inner_url == repo_url:
55
 
        return '.'
56
 
    result = urlutils.relative_url(repo_url, inner_url)
57
 
    if result != inner_url:
58
 
        result = result.rstrip('/')
59
 
    return result
60
 
 
61
 
class _UrlList(object):
62
 
 
63
 
    def __init__(self):
64
 
        self.urls = []
65
 
 
66
 
    def add_url(self, label, url):
67
 
        self.add_path(label, urlutils.unescape_for_display(url, 'ascii'))
68
 
 
69
 
    def add_url(self, label, url):
70
 
        self.add_path(label, url)
 
46
class LocationList(object):
 
47
 
 
48
    def __init__(self, base_path):
 
49
        self.locs = []
 
50
        self.base_path = base_path
 
51
 
 
52
    def add_url(self, label, url):
 
53
        """Add a URL to the list, converting it to a path if possible"""
 
54
        if url is None:
 
55
            return
 
56
        try:
 
57
            path = urlutils.local_path_from_url(url)
 
58
        except errors.InvalidURL:
 
59
            self.locs.append((label, url))
 
60
        else:
 
61
            self.add_path(label, path)
71
62
 
72
63
    def add_path(self, label, path):
73
 
        self.urls.append((label, path))
 
64
        """Add a path, converting it to a relative path if possible"""
 
65
        try:
 
66
            path = osutils.relpath(self.base_path, path)
 
67
        except errors.PathNotChild:
 
68
            pass
 
69
        else:
 
70
            if path == '':
 
71
                path = '.'
 
72
        if path != '/':
 
73
            path = path.rstrip('/')
 
74
        self.locs.append((label, path))
74
75
 
75
 
    def print_lines(self):
76
 
        max_len = max(len(l) for l, u in self.urls)
77
 
        for label, url in self.urls:
78
 
            print "  %*s: %s" % (max_len, label, url)
 
76
    def get_lines(self):
 
77
        max_len = max(len(l) for l, u in self.locs)
 
78
        return ["  %*s: %s\n" % (max_len, l, u) for l, u in self.locs ]
79
79
 
80
80
 
81
81
def gather_location_info(repository, branch=None, working=None):
101
101
        if working_path != master_path:
102
102
            locs['checkout of branch'] = master_path
103
103
        elif repository.is_shared():
104
 
            locs['repository branch'] = _repo_rel_url(repository_path,
105
 
                branch_path)
 
104
            locs['repository branch'] = branch_path
106
105
        elif branch_path is not None:
107
106
            # standalone
108
107
            locs['branch root'] = branch_path
111
110
        if repository.is_shared():
112
111
            # lightweight checkout of branch in shared repository
113
112
            if branch_path is not None:
114
 
                locs['repository branch'] = _repo_rel_url(repository_path,
115
 
                                                          branch_path)
 
113
                locs['repository branch'] = branch_path
116
114
        elif branch_path is not None:
117
115
            # standalone
118
116
            locs['branch root'] = branch_path
133
131
def _show_location_info(locs):
134
132
    """Show known locations for working, branch and repository."""
135
133
    print 'Location:'
136
 
    path_list = _UrlList()
 
134
    path_list = LocationList(os.getcwd())
137
135
    for name, loc in locs:
138
136
        path_list.add_url(name, loc)
139
 
    path_list.print_lines()
140
 
 
141
 
 
142
 
def _show_related_info(branch):
 
137
    sys.stdout.writelines(path_list.get_lines())
 
138
 
 
139
def _gather_related_branches(branch):
 
140
    locs = LocationList(os.getcwd())
 
141
    locs.add_url('public branch', branch.get_public_branch())
 
142
    locs.add_url('push branch', branch.get_push_location())
 
143
    locs.add_url('parent branch', branch.get_parent())
 
144
    locs.add_url('submit branch', branch.get_submit_branch())
 
145
    return locs
 
146
 
 
147
def _show_related_info(branch, outfile):
143
148
    """Show parent and push location of branch."""
144
 
    if branch.get_parent() or branch.get_push_location():
145
 
        print
146
 
        print 'Related branches:'
147
 
        if branch.get_parent():
148
 
            if branch.get_push_location():
149
 
                print '      parent branch: %s' % branch.get_parent()
150
 
            else:
151
 
                print '  parent branch: %s' % branch.get_parent()
152
 
        if branch.get_push_location():
153
 
            print '  publish to branch: %s' % branch.get_push_location()
 
149
    locs = _gather_related_branches(branch)
 
150
    if len(locs.locs) > 0:
 
151
        print >> outfile
 
152
        print >> outfile, 'Related branches:'
 
153
        outfile.writelines(locs.get_lines())
154
154
 
155
155
 
156
156
def _show_format_info(control=None, repository=None, branch=None, working=None):
343
343
    format = describe_format(control, repository, branch, working)
344
344
    print "%s (format: %s)" % (layout, format)
345
345
    _show_location_info(gather_location_info(repository, branch, working))
 
346
    if branch is not None:
 
347
        _show_related_info(branch, sys.stdout)
346
348
    if verbose == 0:
347
349
        return
348
 
    if branch is not None:
349
 
        _show_related_info(branch)
350
350
    _show_format_info(control, repository, branch, working)
351
351
    _show_locking_info(repository, branch, working)
352
352
    if branch is not None: