~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commit.py

  • Committer: Martin Pool
  • Date: 2005-05-17 06:56:16 UTC
  • Revision ID: mbp@sourcefrog.net-20050517065616-6f23381d6184a8aa
- add space for un-merged patches

Show diffs side-by-side

added added

removed removed

Lines of Context:
53
53
        If null (default), a time/random revision id is generated.
54
54
    """
55
55
 
56
 
    import time, tempfile
57
 
 
58
 
    from bzrlib.osutils import local_time_offset, username
59
 
    from bzrlib.branch import gen_file_id
60
 
    from bzrlib.errors import BzrError
61
 
    from bzrlib.revision import Revision, RevisionReference
62
 
    from bzrlib.trace import mutter, note
63
 
 
64
 
    branch.lock_write()
65
 
 
66
 
    try:
67
 
        # First walk over the working inventory; and both update that
68
 
        # and also build a new revision inventory.  The revision
69
 
        # inventory needs to hold the text-id, sha1 and size of the
70
 
        # actual file versions committed in the revision.  (These are
71
 
        # not present in the working inventory.)  We also need to
72
 
        # detect missing/deleted files, and remove them from the
73
 
        # working inventory.
74
 
 
75
 
        work_tree = branch.working_tree()
76
 
        work_inv = work_tree.inventory
77
 
        basis = branch.basis_tree()
78
 
        basis_inv = basis.inventory
79
 
 
80
 
        if verbose:
81
 
            note('looking for changes...')
82
 
 
83
 
        missing_ids, new_inv = _gather_commit(branch,
84
 
                                              work_tree,
85
 
                                              work_inv,
86
 
                                              basis_inv,
87
 
                                              specific_files,
88
 
                                              verbose)
89
 
 
90
 
        for file_id in missing_ids:
91
 
            # Any files that have been deleted are now removed from the
92
 
            # working inventory.  Files that were not selected for commit
93
 
            # are left as they were in the working inventory and ommitted
94
 
            # from the revision inventory.
95
 
 
96
 
            # have to do this later so we don't mess up the iterator.
97
 
            # since parents may be removed before their children we
98
 
            # have to test.
99
 
 
100
 
            # FIXME: There's probably a better way to do this; perhaps
101
 
            # the workingtree should know how to filter itbranch.
102
 
            if work_inv.has_id(file_id):
103
 
                del work_inv[file_id]
104
 
 
105
 
 
106
 
        if rev_id is None:
107
 
            rev_id = _gen_revision_id(time.time())
108
 
        inv_id = rev_id
109
 
 
110
 
        inv_tmp = tempfile.TemporaryFile()
111
 
        new_inv.write_xml(inv_tmp)
112
 
        inv_tmp.seek(0)
113
 
        branch.inventory_store.add(inv_tmp, inv_id)
114
 
        mutter('new inventory_id is {%s}' % inv_id)
115
 
 
116
 
        # We could also just sha hash the inv_tmp file
117
 
        # however, in the case that branch.inventory_store.add()
118
 
        # ever actually does anything special
119
 
        inv_sha1 = branch.get_inventory_sha1(inv_id)
120
 
 
121
 
        branch._write_inventory(work_inv)
122
 
 
123
 
        if timestamp == None:
124
 
            timestamp = time.time()
125
 
 
126
 
        if committer == None:
127
 
            committer = username()
128
 
 
129
 
        if timezone == None:
130
 
            timezone = local_time_offset()
131
 
 
132
 
        mutter("building commit log message")
133
 
        rev = Revision(timestamp=timestamp,
134
 
                       timezone=timezone,
135
 
                       committer=committer,
136
 
                       message = message,
137
 
                       inventory_id=inv_id,
138
 
                       inventory_sha1=inv_sha1,
139
 
                       revision_id=rev_id)
140
 
 
141
 
        precursor_id = branch.last_patch()
142
 
        if precursor_id:
143
 
            precursor_sha1 = branch.get_revision_sha1(precursor_id)
144
 
            rev.parents = [RevisionReference(precursor_id, precursor_sha1)]
145
 
 
146
 
        rev_tmp = tempfile.TemporaryFile()
147
 
        rev.write_xml(rev_tmp)
148
 
        rev_tmp.seek(0)
149
 
        branch.revision_store.add(rev_tmp, rev_id)
150
 
        mutter("new revision_id is {%s}" % rev_id)
151
 
 
152
 
        ## XXX: Everything up to here can simply be orphaned if we abort
153
 
        ## the commit; it will leave junk files behind but that doesn't
154
 
        ## matter.
155
 
 
156
 
        ## TODO: Read back the just-generated changeset, and make sure it
157
 
        ## applies and recreates the right state.
158
 
 
159
 
        ## TODO: Also calculate and store the inventory SHA1
160
 
        mutter("committing patch r%d" % (branch.revno() + 1))
161
 
 
162
 
        branch.append_revision(rev_id)
163
 
 
164
 
        if verbose:
165
 
            note("commited r%d" % branch.revno())
166
 
    finally:
167
 
        branch.unlock()
168
 
 
169
 
 
170
 
 
171
 
def _gen_revision_id(when):
172
 
    """Return new revision-id."""
173
 
    from binascii import hexlify
174
 
    from osutils import rand_bytes, compact_date, user_email
175
 
 
176
 
    s = '%s-%s-' % (user_email(), compact_date(when))
177
 
    s += hexlify(rand_bytes(8))
178
 
    return s
179
 
 
180
 
 
181
 
def _gather_commit(branch, work_tree, work_inv, basis_inv, specific_files,
182
 
                   verbose):
183
 
    """Build inventory preparatory to commit.
184
 
 
185
 
    This adds any changed files into the text store, and sets their
186
 
    test-id, sha and size in the returned inventory appropriately.
187
 
 
188
 
    missing_ids
189
 
        Modified to hold a list of files that have been deleted from
190
 
        the working directory; these should be removed from the
191
 
        working inventory.
192
 
    """
193
 
    from bzrlib.inventory import Inventory
 
56
    import os, time, tempfile
 
57
 
 
58
    from inventory import Inventory
194
59
    from osutils import isdir, isfile, sha_string, quotefn, \
195
60
         local_time_offset, username, kind_marker, is_inside_any
196
61
    
197
62
    from branch import gen_file_id
198
63
    from errors import BzrError
199
64
    from revision import Revision
200
 
    from bzrlib.trace import mutter, note
201
 
 
 
65
    from trace import mutter, note
 
66
 
 
67
    branch._need_writelock()
 
68
 
 
69
    # First walk over the working inventory; and both update that
 
70
    # and also build a new revision inventory.  The revision
 
71
    # inventory needs to hold the text-id, sha1 and size of the
 
72
    # actual file versions committed in the revision.  (These are
 
73
    # not present in the working inventory.)  We also need to
 
74
    # detect missing/deleted files, and remove them from the
 
75
    # working inventory.
 
76
 
 
77
    work_tree = branch.working_tree()
 
78
    work_inv = work_tree.inventory
202
79
    inv = Inventory()
 
80
    basis = branch.basis_tree()
 
81
    basis_inv = basis.inventory
203
82
    missing_ids = []
204
 
    
 
83
 
 
84
    if verbose:
 
85
        note('looking for changes...')
 
86
        
205
87
    for path, entry in work_inv.iter_entries():
206
88
        ## TODO: Check that the file kind has not changed from the previous
207
89
        ## revision of this file (if any).
208
90
 
 
91
        entry = entry.copy()
 
92
 
209
93
        p = branch.abspath(path)
210
94
        file_id = entry.file_id
211
95
        mutter('commit prep file %s, id %r ' % (p, file_id))
226
110
            missing_ids.append(file_id)
227
111
            continue
228
112
 
229
 
        # this is present in the new inventory; may be new, modified or
230
 
        # unchanged.
231
 
        old_ie = basis_inv.has_id(file_id) and basis_inv[file_id]
232
 
        
233
 
        entry = entry.copy()
234
113
        inv.add(entry)
235
114
 
236
 
        if old_ie:
237
 
            old_kind = old_ie.kind
 
115
        if basis_inv.has_id(file_id):
 
116
            old_kind = basis_inv[file_id].kind
238
117
            if old_kind != entry.kind:
239
118
                raise BzrError("entry %r changed kind from %r to %r"
240
119
                        % (file_id, old_kind, entry.kind))
249
128
 
250
129
            new_sha1 = work_tree.get_file_sha1(file_id)
251
130
 
 
131
            old_ie = basis_inv.has_id(file_id) and basis_inv[file_id]
252
132
            if (old_ie
253
133
                and old_ie.text_sha1 == new_sha1):
254
134
                ## assert content == basis.get_file(file_id).read()
268
148
                entry.text_id = gen_file_id(entry.name)
269
149
                branch.text_store.add(content, entry.text_id)
270
150
                mutter('    stored with text_id {%s}' % entry.text_id)
271
 
 
272
 
        if verbose:
273
 
            marked = path + kind_marker(entry.kind)
274
 
            if not old_ie:
275
 
                print 'added', marked
276
 
            elif old_ie == entry:
277
 
                pass                    # unchanged
278
 
            elif (old_ie.name == entry.name
279
 
                  and old_ie.parent_id == entry.parent_id):
280
 
                print 'modified', marked
281
 
            else:
282
 
                print 'renamed', marked
283
 
                        
284
 
    return missing_ids, inv
 
151
                if verbose:
 
152
                    if not old_ie:
 
153
                        print('added %s' % path)
 
154
                    elif (old_ie.name == entry.name
 
155
                          and old_ie.parent_id == entry.parent_id):
 
156
                        print('modified %s' % path)
 
157
                    else:
 
158
                        print('renamed %s' % path)
 
159
 
 
160
 
 
161
    for file_id in missing_ids:
 
162
        # Any files that have been deleted are now removed from the
 
163
        # working inventory.  Files that were not selected for commit
 
164
        # are left as they were in the working inventory and ommitted
 
165
        # from the revision inventory.
 
166
        
 
167
        # have to do this later so we don't mess up the iterator.
 
168
        # since parents may be removed before their children we
 
169
        # have to test.
 
170
 
 
171
        # FIXME: There's probably a better way to do this; perhaps
 
172
        # the workingtree should know how to filter itbranch.
 
173
        if work_inv.has_id(file_id):
 
174
            del work_inv[file_id]
 
175
 
 
176
 
 
177
    if rev_id is None:
 
178
        rev_id = _gen_revision_id(time.time())
 
179
    inv_id = rev_id
 
180
 
 
181
    inv_tmp = tempfile.TemporaryFile()
 
182
    inv.write_xml(inv_tmp)
 
183
    inv_tmp.seek(0)
 
184
    branch.inventory_store.add(inv_tmp, inv_id)
 
185
    mutter('new inventory_id is {%s}' % inv_id)
 
186
 
 
187
    branch._write_inventory(work_inv)
 
188
 
 
189
    if timestamp == None:
 
190
        timestamp = time.time()
 
191
 
 
192
    if committer == None:
 
193
        committer = username()
 
194
 
 
195
    if timezone == None:
 
196
        timezone = local_time_offset()
 
197
 
 
198
    mutter("building commit log message")
 
199
    rev = Revision(timestamp=timestamp,
 
200
                   timezone=timezone,
 
201
                   committer=committer,
 
202
                   precursor = branch.last_patch(),
 
203
                   message = message,
 
204
                   inventory_id=inv_id,
 
205
                   revision_id=rev_id)
 
206
 
 
207
    rev_tmp = tempfile.TemporaryFile()
 
208
    rev.write_xml(rev_tmp)
 
209
    rev_tmp.seek(0)
 
210
    branch.revision_store.add(rev_tmp, rev_id)
 
211
    mutter("new revision_id is {%s}" % rev_id)
 
212
 
 
213
    ## XXX: Everything up to here can simply be orphaned if we abort
 
214
    ## the commit; it will leave junk files behind but that doesn't
 
215
    ## matter.
 
216
 
 
217
    ## TODO: Read back the just-generated changeset, and make sure it
 
218
    ## applies and recreates the right state.
 
219
 
 
220
    ## TODO: Also calculate and store the inventory SHA1
 
221
    mutter("committing patch r%d" % (branch.revno() + 1))
 
222
 
 
223
    branch.append_revision(rev_id)
 
224
 
 
225
    if verbose:
 
226
        note("commited r%d" % branch.revno())
 
227
 
 
228
 
 
229
 
 
230
def _gen_revision_id(when):
 
231
    """Return new revision-id."""
 
232
    from binascii import hexlify
 
233
    from osutils import rand_bytes, compact_date, user_email
 
234
 
 
235
    s = '%s-%s-' % (user_email(), compact_date(when))
 
236
    s += hexlify(rand_bytes(8))
 
237
    return s
285
238
 
286
239