~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commit.py

  • Committer: aaron.bentley at utoronto
  • Date: 2005-08-21 04:15:04 UTC
  • mto: (1092.1.41) (1185.3.4)
  • mto: This revision was merged to the branch mainline in revision 1110.
  • Revision ID: aaron.bentley@utoronto.ca-20050821041504-866f53c120065af3
Added revision-based common-ancestor checking

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
 
18
18
 
 
19
# FIXME: "bzr commit doc/format" commits doc/format.txt!
 
20
 
19
21
def commit(branch, message,
20
22
           timestamp=None,
21
23
           timezone=None,
22
24
           committer=None,
23
25
           verbose=True,
24
26
           specific_files=None,
25
 
           rev_id=None):
 
27
           rev_id=None,
 
28
           allow_pointless=True):
26
29
    """Commit working copy as a new revision.
27
30
 
28
31
    The basic approach is to add all the file texts into the
39
42
    be robust against files disappearing, moving, etc.  So the
40
43
    whole thing is a bit hard.
41
44
 
 
45
    This raises PointlessCommit if there are no changes, no new merges,
 
46
    and allow_pointless  is false.
 
47
 
42
48
    timestamp -- if not None, seconds-since-epoch for a
43
49
         postdated/predated commit.
44
50
 
53
59
        If null (default), a time/random revision id is generated.
54
60
    """
55
61
 
56
 
    import os, time, tempfile
 
62
    import time, tempfile
57
63
 
58
 
    from inventory import Inventory
59
 
    from osutils import isdir, isfile, sha_string, quotefn, \
60
 
         local_time_offset, username, kind_marker, is_inside_any
61
 
    
62
 
    from branch import gen_file_id
63
 
    from errors import BzrError
64
 
    from revision import Revision
65
 
    from trace import mutter, note
 
64
    from bzrlib.osutils import local_time_offset, username
 
65
    from bzrlib.branch import gen_file_id
 
66
    from bzrlib.errors import BzrError, PointlessCommit
 
67
    from bzrlib.revision import Revision, RevisionReference
 
68
    from bzrlib.trace import mutter, note
 
69
    from bzrlib.xml import pack_xml
66
70
 
67
71
    branch.lock_write()
68
72
 
77
81
 
78
82
        work_tree = branch.working_tree()
79
83
        work_inv = work_tree.inventory
80
 
        inv = Inventory()
81
84
        basis = branch.basis_tree()
82
85
        basis_inv = basis.inventory
83
 
        missing_ids = []
84
86
 
85
87
        if verbose:
86
88
            note('looking for changes...')
87
89
 
88
 
        for path, entry in work_inv.iter_entries():
89
 
            ## TODO: Check that the file kind has not changed from the previous
90
 
            ## revision of this file (if any).
91
 
 
92
 
            entry = entry.copy()
93
 
 
94
 
            p = branch.abspath(path)
95
 
            file_id = entry.file_id
96
 
            mutter('commit prep file %s, id %r ' % (p, file_id))
97
 
 
98
 
            if specific_files and not is_inside_any(specific_files, path):
99
 
                if basis_inv.has_id(file_id):
100
 
                    # carry over with previous state
101
 
                    inv.add(basis_inv[file_id].copy())
102
 
                else:
103
 
                    # omit this from committed inventory
104
 
                    pass
105
 
                continue
106
 
 
107
 
            if not work_tree.has_id(file_id):
108
 
                if verbose:
109
 
                    print('deleted %s%s' % (path, kind_marker(entry.kind)))
110
 
                mutter("    file is missing, removing from inventory")
111
 
                missing_ids.append(file_id)
112
 
                continue
113
 
 
114
 
            inv.add(entry)
115
 
 
116
 
            if basis_inv.has_id(file_id):
117
 
                old_kind = basis_inv[file_id].kind
118
 
                if old_kind != entry.kind:
119
 
                    raise BzrError("entry %r changed kind from %r to %r"
120
 
                            % (file_id, old_kind, entry.kind))
121
 
 
122
 
            if entry.kind == 'directory':
123
 
                if not isdir(p):
124
 
                    raise BzrError("%s is entered as directory but not a directory"
125
 
                                   % quotefn(p))
126
 
            elif entry.kind == 'file':
127
 
                if not isfile(p):
128
 
                    raise BzrError("%s is entered as file but is not a file" % quotefn(p))
129
 
 
130
 
                new_sha1 = work_tree.get_file_sha1(file_id)
131
 
 
132
 
                old_ie = basis_inv.has_id(file_id) and basis_inv[file_id]
133
 
                if (old_ie
134
 
                    and old_ie.text_sha1 == new_sha1):
135
 
                    ## assert content == basis.get_file(file_id).read()
136
 
                    entry.text_id = old_ie.text_id
137
 
                    entry.text_sha1 = new_sha1
138
 
                    entry.text_size = old_ie.text_size
139
 
                    mutter('    unchanged from previous text_id {%s}' %
140
 
                           entry.text_id)
141
 
                else:
142
 
                    content = file(p, 'rb').read()
143
 
 
144
 
                    # calculate the sha again, just in case the file contents
145
 
                    # changed since we updated the cache
146
 
                    entry.text_sha1 = sha_string(content)
147
 
                    entry.text_size = len(content)
148
 
 
149
 
                    entry.text_id = gen_file_id(entry.name)
150
 
                    branch.text_store.add(content, entry.text_id)
151
 
                    mutter('    stored with text_id {%s}' % entry.text_id)
152
 
                    if verbose:
153
 
                        if not old_ie:
154
 
                            print('added %s' % path)
155
 
                        elif (old_ie.name == entry.name
156
 
                              and old_ie.parent_id == entry.parent_id):
157
 
                            print('modified %s' % path)
158
 
                        else:
159
 
                            print('renamed %s' % path)
160
 
 
 
90
        pending_merges = branch.pending_merges()
 
91
 
 
92
        missing_ids, new_inv, any_changes = \
 
93
                     _gather_commit(branch,
 
94
                                    work_tree,
 
95
                                    work_inv,
 
96
                                    basis_inv,
 
97
                                    specific_files,
 
98
                                    verbose)
 
99
 
 
100
        if not (any_changes or allow_pointless or pending_merges):
 
101
            raise PointlessCommit()
161
102
 
162
103
        for file_id in missing_ids:
163
104
            # Any files that have been deleted are now removed from the
180
121
        inv_id = rev_id
181
122
 
182
123
        inv_tmp = tempfile.TemporaryFile()
183
 
        inv.write_xml(inv_tmp)
 
124
        pack_xml(new_inv, inv_tmp)
184
125
        inv_tmp.seek(0)
185
126
        branch.inventory_store.add(inv_tmp, inv_id)
186
127
        mutter('new inventory_id is {%s}' % inv_id)
187
128
 
 
129
        # We could also just sha hash the inv_tmp file
 
130
        # however, in the case that branch.inventory_store.add()
 
131
        # ever actually does anything special
 
132
        inv_sha1 = branch.get_inventory_sha1(inv_id)
 
133
 
188
134
        branch._write_inventory(work_inv)
189
135
 
190
136
        if timestamp == None:
200
146
        rev = Revision(timestamp=timestamp,
201
147
                       timezone=timezone,
202
148
                       committer=committer,
203
 
                       precursor = branch.last_patch(),
204
149
                       message = message,
205
150
                       inventory_id=inv_id,
 
151
                       inventory_sha1=inv_sha1,
206
152
                       revision_id=rev_id)
207
153
 
 
154
        rev.parents = []
 
155
        precursor_id = branch.last_patch()
 
156
        if precursor_id:
 
157
            precursor_sha1 = branch.get_revision_sha1(precursor_id)
 
158
            rev.parents.append(RevisionReference(precursor_id, precursor_sha1))
 
159
        for merge_rev in pending_merges:
 
160
            rev.parents.append(RevisionReference(merge_rev))            
 
161
 
208
162
        rev_tmp = tempfile.TemporaryFile()
209
 
        rev.write_xml(rev_tmp)
 
163
        pack_xml(rev, rev_tmp)
210
164
        rev_tmp.seek(0)
211
165
        branch.revision_store.add(rev_tmp, rev_id)
212
166
        mutter("new revision_id is {%s}" % rev_id)
223
177
 
224
178
        branch.append_revision(rev_id)
225
179
 
 
180
        branch.set_pending_merges([])
 
181
 
226
182
        if verbose:
227
183
            note("commited r%d" % branch.revno())
228
184
    finally:
240
196
    return s
241
197
 
242
198
 
 
199
def _gather_commit(branch, work_tree, work_inv, basis_inv, specific_files,
 
200
                   verbose):
 
201
    """Build inventory preparatory to commit.
 
202
 
 
203
    Returns missing_ids, new_inv, any_changes.
 
204
 
 
205
    This adds any changed files into the text store, and sets their
 
206
    test-id, sha and size in the returned inventory appropriately.
 
207
 
 
208
    missing_ids
 
209
        Modified to hold a list of files that have been deleted from
 
210
        the working directory; these should be removed from the
 
211
        working inventory.
 
212
    """
 
213
    from bzrlib.inventory import Inventory
 
214
    from bzrlib.osutils import isdir, isfile, sha_string, quotefn, \
 
215
         local_time_offset, username, kind_marker, is_inside_any
 
216
    
 
217
    from bzrlib.branch import gen_file_id
 
218
    from bzrlib.errors import BzrError
 
219
    from bzrlib.revision import Revision
 
220
    from bzrlib.trace import mutter, note
 
221
 
 
222
    any_changes = False
 
223
    inv = Inventory(work_inv.root.file_id)
 
224
    missing_ids = []
 
225
    
 
226
    for path, entry in work_inv.iter_entries():
 
227
        ## TODO: Check that the file kind has not changed from the previous
 
228
        ## revision of this file (if any).
 
229
 
 
230
        p = branch.abspath(path)
 
231
        file_id = entry.file_id
 
232
        mutter('commit prep file %s, id %r ' % (p, file_id))
 
233
 
 
234
        if specific_files and not is_inside_any(specific_files, path):
 
235
            mutter('  skipping file excluded from commit')
 
236
            if basis_inv.has_id(file_id):
 
237
                # carry over with previous state
 
238
                inv.add(basis_inv[file_id].copy())
 
239
            else:
 
240
                # omit this from committed inventory
 
241
                pass
 
242
            continue
 
243
 
 
244
        if not work_tree.has_id(file_id):
 
245
            if verbose:
 
246
                print('deleted %s%s' % (path, kind_marker(entry.kind)))
 
247
                any_changes = True
 
248
            mutter("    file is missing, removing from inventory")
 
249
            missing_ids.append(file_id)
 
250
            continue
 
251
 
 
252
        # this is present in the new inventory; may be new, modified or
 
253
        # unchanged.
 
254
        old_ie = basis_inv.has_id(file_id) and basis_inv[file_id]
 
255
        
 
256
        entry = entry.copy()
 
257
        inv.add(entry)
 
258
 
 
259
        if old_ie:
 
260
            old_kind = old_ie.kind
 
261
            if old_kind != entry.kind:
 
262
                raise BzrError("entry %r changed kind from %r to %r"
 
263
                        % (file_id, old_kind, entry.kind))
 
264
 
 
265
        if entry.kind == 'directory':
 
266
            if not isdir(p):
 
267
                raise BzrError("%s is entered as directory but not a directory"
 
268
                               % quotefn(p))
 
269
        elif entry.kind == 'file':
 
270
            if not isfile(p):
 
271
                raise BzrError("%s is entered as file but is not a file" % quotefn(p))
 
272
 
 
273
            new_sha1 = work_tree.get_file_sha1(file_id)
 
274
 
 
275
            if (old_ie
 
276
                and old_ie.text_sha1 == new_sha1):
 
277
                ## assert content == basis.get_file(file_id).read()
 
278
                entry.text_id = old_ie.text_id
 
279
                entry.text_sha1 = new_sha1
 
280
                entry.text_size = old_ie.text_size
 
281
                mutter('    unchanged from previous text_id {%s}' %
 
282
                       entry.text_id)
 
283
            else:
 
284
                content = file(p, 'rb').read()
 
285
 
 
286
                # calculate the sha again, just in case the file contents
 
287
                # changed since we updated the cache
 
288
                entry.text_sha1 = sha_string(content)
 
289
                entry.text_size = len(content)
 
290
 
 
291
                entry.text_id = gen_file_id(entry.name)
 
292
                branch.text_store.add(content, entry.text_id)
 
293
                mutter('    stored with text_id {%s}' % entry.text_id)
 
294
 
 
295
        if verbose:
 
296
            marked = path + kind_marker(entry.kind)
 
297
            if not old_ie:
 
298
                print 'added', marked
 
299
                any_changes = True
 
300
            elif old_ie == entry:
 
301
                pass                    # unchanged
 
302
            elif (old_ie.name == entry.name
 
303
                  and old_ie.parent_id == entry.parent_id):
 
304
                print 'modified', marked
 
305
                any_changes = True
 
306
            else:
 
307
                print 'renamed', marked
 
308
                any_changes = True
 
309
                        
 
310
    return missing_ids, inv, any_changes
 
311
 
 
312