~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/log.py

  • Committer: Martin Pool
  • Date: 2005-05-10 06:21:07 UTC
  • Revision ID: mbp@sourcefrog.net-20050510062107-8b15b4c6b52d0279
doc

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
 
18
18
 
19
 
"""Code to show logs of changes.
20
 
 
21
 
Various flavors of log can be produced:
22
 
 
23
 
* for one file, or the whole tree, and (not done yet) for
24
 
  files in a given directory
25
 
 
26
 
* in "verbose" mode with a description of what changed from one
27
 
  version to the next
28
 
 
29
 
* with file-ids and revision-ids shown
30
 
 
31
 
* from last to first or (not anymore) from first to last;
32
 
  the default is "reversed" because it shows the likely most
33
 
  relevant and interesting information first
34
 
 
35
 
* (not yet) in XML format
36
 
"""
37
 
 
38
 
 
39
 
from trace import mutter
40
19
 
41
20
def find_touching_revisions(branch, file_id):
42
21
    """Yield a description of revisions which affect the file_id.
46
25
    This is the list of revisions where the file is either added,
47
26
    modified, renamed or deleted.
48
27
 
 
28
    Revisions are returned in chronological order.
 
29
 
49
30
    TODO: Perhaps some way to limit this to only particular revisions,
50
 
    or to traverse a non-mainline set of revisions?
 
31
    or to traverse a non-branch set of revisions?
 
32
 
 
33
    TODO: If a directory is given, then by default look for all
 
34
    changes under that directory.
51
35
    """
52
36
    last_ie = None
53
37
    last_path = None
82
66
        revno += 1
83
67
 
84
68
 
85
 
 
86
69
def show_log(branch,
87
 
             specific_fileid=None,
 
70
             filename=None,
88
71
             show_timezone='original',
89
72
             verbose=False,
90
73
             show_ids=False,
91
 
             to_file=None,
92
 
             direction='reverse',
93
 
             start_revision=None,
94
 
             end_revision=None):
 
74
             to_file=None):
95
75
    """Write out human-readable log of commits to this branch.
96
76
 
97
 
    specific_fileid
 
77
    filename
98
78
        If true, list only the commits affecting the specified
99
79
        file, rather than all commits.
100
80
 
111
91
 
112
92
    to_file
113
93
        File to send log to; by default stdout.
114
 
 
115
 
    direction
116
 
        'reverse' (default) is latest to earliest;
117
 
        'forward' is earliest to latest.
118
 
 
119
 
    start_revision
120
 
        If not None, only show revisions >= start_revision
121
 
 
122
 
    end_revision
123
 
        If not None, only show revisions <= end_revision
124
94
    """
125
95
    from osutils import format_date
126
96
    from errors import BzrCheckError
 
97
    from diff import compare_inventories
127
98
    from textui import show_status
128
 
 
129
 
 
130
 
    if specific_fileid:
131
 
        mutter('get log for file_id %r' % specific_fileid)
 
99
    from inventory import Inventory
132
100
 
133
101
    if to_file == None:
134
102
        import sys
135
103
        to_file = sys.stdout
136
104
 
137
 
    which_revs = branch.enum_history(direction)
138
 
 
139
 
    if not (verbose or specific_fileid):
140
 
        # no need to know what changed between revisions
141
 
        with_deltas = deltas_for_log_dummy(branch, which_revs)
142
 
    elif direction == 'reverse':
143
 
        with_deltas = deltas_for_log_reverse(branch, which_revs)
144
 
    else:        
145
 
        raise NotImplementedError("sorry, verbose forward logs not done yet")
146
 
 
147
 
    for revno, rev, delta in with_deltas:
148
 
        if specific_fileid:
149
 
            if not delta.touches_file_id(specific_fileid):
150
 
                continue
151
 
 
152
 
        if start_revision is not None and revno < start_revision:
153
 
            continue
154
 
 
155
 
        if end_revision is not None and revno > end_revision:
156
 
            continue
157
 
        
158
 
        if not verbose:
159
 
            # although we calculated it, throw it away without display
160
 
            delta = None
161
 
            
162
 
        show_one_log(revno, rev, delta, show_ids, to_file, show_timezone)
163
 
 
164
 
 
165
 
 
166
 
def deltas_for_log_dummy(branch, which_revs):
167
 
    for revno, revision_id in which_revs:
168
 
        yield revno, branch.get_revision(revision_id), None
169
 
 
170
 
 
171
 
def deltas_for_log_reverse(branch, which_revs):
172
 
    """Compute deltas for display in reverse log.
173
 
 
174
 
    Given a sequence of (revno, revision_id) pairs, return
175
 
    (revno, rev, delta).
176
 
 
177
 
    The delta is from the given revision to the next one in the
178
 
    sequence, which makes sense if the log is being displayed from
179
 
    newest to oldest.
180
 
    """
181
 
    from tree import EmptyTree
182
 
    from diff import compare_trees
183
 
    
184
 
    last_revno = last_revision_id = last_tree = None
185
 
    for revno, revision_id in which_revs:
186
 
        this_tree = branch.revision_tree(revision_id)
187
 
        this_revision = branch.get_revision(revision_id)
188
 
        
189
 
        if last_revno:
190
 
            yield last_revno, last_revision, compare_trees(this_tree, last_tree, False)
191
 
 
192
 
        last_revno = revno
193
 
        last_revision = this_revision
194
 
        last_tree = this_tree
195
 
 
196
 
    if last_revno:
197
 
        this_tree = EmptyTree()
198
 
        yield last_revno, last_revision, compare_trees(this_tree, last_tree, False)
199
 
 
200
 
 
201
 
 
202
 
 
203
 
def show_one_log(revno, rev, delta, show_ids, to_file, show_timezone):
204
 
    from osutils import format_date
205
 
    
206
 
    print >>to_file,  '-' * 60
207
 
    print >>to_file,  'revno:', revno
208
 
    if show_ids:
209
 
        print >>to_file,  'revision-id:', rev.revision_id
210
 
    print >>to_file,  'committer:', rev.committer
211
 
    print >>to_file,  'timestamp: %s' % (format_date(rev.timestamp, rev.timezone or 0,
212
 
                                         show_timezone))
213
 
 
214
 
    print >>to_file,  'message:'
215
 
    if not rev.message:
216
 
        print >>to_file,  '  (no message)'
 
105
    if filename:
 
106
        file_id = branch.read_working_inventory().path2id(filename)
 
107
        def which_revs():
 
108
            for revno, revid, why in find_touching_revisions(branch, file_id):
 
109
                yield revno, revid
217
110
    else:
218
 
        for l in rev.message.split('\n'):
219
 
            print >>to_file,  '  ' + l
220
 
 
221
 
    if delta != None:
222
 
        delta.show(to_file, show_ids)
 
111
        def which_revs():
 
112
            for i, revid in enumerate(branch.revision_history()):
 
113
                yield i+1, revid
 
114
        
 
115
    branch._need_readlock()
 
116
    precursor = None
 
117
    if verbose:
 
118
        prev_inv = Inventory()
 
119
    for revno, revision_id in which_revs():
 
120
        print >>to_file,  '-' * 60
 
121
        print >>to_file,  'revno:', revno
 
122
        rev = branch.get_revision(revision_id)
 
123
        if show_ids:
 
124
            print >>to_file,  'revision-id:', revision_id
 
125
        print >>to_file,  'committer:', rev.committer
 
126
        print >>to_file,  'timestamp: %s' % (format_date(rev.timestamp, rev.timezone or 0,
 
127
                                             show_timezone))
 
128
 
 
129
        if revision_id != rev.revision_id:
 
130
            raise BzrCheckError("retrieved wrong revision: %r"
 
131
                                % (revision_id, rev.revision_id))
 
132
 
 
133
        print >>to_file,  'message:'
 
134
        if not rev.message:
 
135
            print >>to_file,  '  (no message)'
 
136
        else:
 
137
            for l in rev.message.split('\n'):
 
138
                print >>to_file,  '  ' + l
 
139
 
 
140
        # Don't show a list of changed files if we were asked about
 
141
        # one specific file.
 
142
 
 
143
        if verbose and not filename:
 
144
            this_inv = branch.get_inventory(rev.inventory_id)
 
145
            delta = compare_inventories(prev_inv, this_inv)
 
146
 
 
147
            if delta.removed:
 
148
                print >>to_file, 'removed files:'
 
149
                for path, fid in delta.removed:
 
150
                    if show_ids:
 
151
                        print >>to_file, '  %-30s %s' % (path, fid)
 
152
                    else:
 
153
                        print >>to_file, ' ', path
 
154
            if delta.added:
 
155
                print >>to_file, 'added files:'
 
156
                for path, fid in delta.added:
 
157
                    if show_ids:
 
158
                        print >>to_file, '  %-30s %s' % (path, fid)
 
159
                    else:
 
160
                        print >>to_file, '  ' + path
 
161
            if delta.renamed:
 
162
                print >>to_file, 'renamed files:'
 
163
                for oldpath, newpath, fid in delta.renamed:
 
164
                    if show_ids:
 
165
                        print >>to_file, '  %s => %s %s' % (oldpath, newpath, fid)
 
166
                    else:
 
167
                        print >>to_file, '  %s => %s' % (oldpath, newpath)
 
168
            if delta.modified:
 
169
                print >>to_file, 'modified files:'
 
170
                for path, fid in delta.modified:
 
171
                    if show_ids:
 
172
                        print >>to_file, '  %-30s %s' % (path, fid)
 
173
                    else:
 
174
                        print >>to_file, '  ' + path
 
175
 
 
176
            prev_inv = this_inv
 
177
 
 
178
        precursor = revision_id
 
179