/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/ls.py

  • Committer: Ian Clatworthy
  • Date: 2009-05-18 07:48:02 UTC
  • mto: (4456.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4457.
  • Revision ID: ian.clatworthy@canonical.com-20090518074802-dbx8ats78vl134y6
refactor ls command to use new APIs

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2004, 2005, 2006, 2007, 2008, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""List files in a tree."""
 
18
 
 
19
 
 
20
from bzrlib.lazy_import import lazy_import
 
21
lazy_import(globals(), """
 
22
from bzrlib import (
 
23
    errors,
 
24
    osutils,
 
25
    )
 
26
from bzrlib.trace import mutter, note
 
27
from bzrlib.workingtree import WorkingTree
 
28
""")
 
29
 
 
30
 
 
31
def ls(tree, outf, from_dir=None, recursive=False, kind=None, unknown=False,
 
32
    versioned=False, ignored=False, verbose=False, null=False, show_ids=False,
 
33
    prefix=None, from_root=False):
 
34
    """List files for a tree.
 
35
 
 
36
    If unknown, versioned and ignored are all False, then all are displayed.
 
37
 
 
38
    :param tree: the tree to display files for
 
39
    :param outf: the output stream
 
40
    :param from_dir: just from this directory
 
41
    :param recursive: whether to recurse into subdirectories or not
 
42
    :param kind: one of 'file', 'symlink', 'directory' or None for all
 
43
    :param unknown: include unknown files or not
 
44
    :param versioned: include versioned files or not
 
45
    :param ignored: include ignored files or not
 
46
    :param verbose: show file kinds, not just paths
 
47
    :param null: separate entries with null characters instead of newlines
 
48
    :param show_ids: show file_ids or not
 
49
    :param prefix: prefix paths with this string or None for no prefix
 
50
    :param from_root: show paths from the root instead of relative
 
51
    """
 
52
    mutter("ls from: %s" % (from_dir,))
 
53
    # Tell the user if a view if being applied
 
54
    apply_view = False
 
55
    if isinstance(tree, WorkingTree) and tree.supports_views():
 
56
        view_files = tree.views.lookup_view()
 
57
        if view_files:
 
58
            apply_view = True
 
59
            view_str = views.view_display_str(view_files)
 
60
            note("Ignoring files outside view. View is %s" % view_str)
 
61
 
 
62
    # Find and display the files
 
63
    all = not (unknown or versioned or ignored)
 
64
    selection = {'I':ignored, '?':unknown, 'V':versioned}
 
65
    #if from_dir and from_root:
 
66
    #    prefix = from_dir
 
67
    #else:
 
68
    #    prefix = None
 
69
    tree.lock_read()
 
70
    try:
 
71
        for fp, fc, fkind, fid, entry in tree.list_files(include_root=False,
 
72
            from_dir=from_dir, recursive=recursive):
 
73
            # Apply additional masking
 
74
            if not all and not selection[fc]:
 
75
                continue
 
76
            if kind is not None and fkind != kind:
 
77
                continue
 
78
            if apply_view:
 
79
                if from_dir:
 
80
                    fullpath = osutils.pathjoin(from_dir, fp)
 
81
                else:
 
82
                    fullpath = fp
 
83
                try:
 
84
                    views.check_path_in_view(tree, fullpath)
 
85
                except errors.FileOutsideView:
 
86
                    continue
 
87
 
 
88
            # Output the entry
 
89
            kindch = entry.kind_character()
 
90
            if prefix is not None:
 
91
                fp = osutils.pathjoin(prefix, fp)
 
92
            outstring = fp + kindch
 
93
            if verbose:
 
94
                outstring = '%-8s %s' % (fc, outstring)
 
95
                if show_ids and fid is not None:
 
96
                    outstring = "%-50s %s" % (outstring, fid)
 
97
                outf.write(outstring + '\n')
 
98
            elif null:
 
99
                outf.write(fp + '\0')
 
100
                if show_ids:
 
101
                    if fid is not None:
 
102
                        outf.write(fid)
 
103
                    outf.write('\0')
 
104
                outf.flush()
 
105
            else:
 
106
                if show_ids:
 
107
                    if fid is not None:
 
108
                        my_id = fid
 
109
                    else:
 
110
                        my_id = ''
 
111
                    outf.write('%-50s %s\n' % (outstring, my_id))
 
112
                else:
 
113
                    outf.write(outstring + '\n')
 
114
    finally:
 
115
        tree.unlock()