~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commit.py

  • Committer: Martin Pool
  • Date: 2005-06-22 06:37:43 UTC
  • Revision ID: mbp@sourcefrog.net-20050622063743-e395f04c4db8977f
- move old blackbox code from testbzr into bzrlib.selftest.blackbox

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
 
 
21
19
def commit(branch, message,
22
20
           timestamp=None,
23
21
           timezone=None,
24
22
           committer=None,
25
23
           verbose=True,
26
24
           specific_files=None,
27
 
           rev_id=None,
28
 
           allow_pointless=True):
 
25
           rev_id=None):
29
26
    """Commit working copy as a new revision.
30
27
 
31
28
    The basic approach is to add all the file texts into the
42
39
    be robust against files disappearing, moving, etc.  So the
43
40
    whole thing is a bit hard.
44
41
 
45
 
    This raises PointlessCommit if there are no changes, no new merges,
46
 
    and allow_pointless  is false.
47
 
 
48
42
    timestamp -- if not None, seconds-since-epoch for a
49
43
         postdated/predated commit.
50
44
 
63
57
 
64
58
    from bzrlib.osutils import local_time_offset, username
65
59
    from bzrlib.branch import gen_file_id
66
 
    from bzrlib.errors import BzrError, PointlessCommit
 
60
    from bzrlib.errors import BzrError
67
61
    from bzrlib.revision import Revision, RevisionReference
68
62
    from bzrlib.trace import mutter, note
69
 
    from bzrlib.xml import pack_xml
70
63
 
71
64
    branch.lock_write()
72
65
 
85
78
        basis_inv = basis.inventory
86
79
 
87
80
        if verbose:
88
 
            # note('looking for changes...')
89
 
            # print 'looking for changes...'
90
 
            # disabled; should be done at a higher level
91
 
            pass
92
 
 
93
 
        pending_merges = branch.pending_merges()
94
 
 
95
 
        missing_ids, new_inv, any_changes = \
96
 
                     _gather_commit(branch,
97
 
                                    work_tree,
98
 
                                    work_inv,
99
 
                                    basis_inv,
100
 
                                    specific_files,
101
 
                                    verbose)
102
 
 
103
 
        if not (any_changes or allow_pointless or pending_merges):
104
 
            raise PointlessCommit()
 
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)
105
89
 
106
90
        for file_id in missing_ids:
107
91
            # Any files that have been deleted are now removed from the
118
102
            if work_inv.has_id(file_id):
119
103
                del work_inv[file_id]
120
104
 
 
105
 
121
106
        if rev_id is None:
122
 
            rev_id = _gen_revision_id(branch, time.time())
 
107
            rev_id = _gen_revision_id(time.time())
123
108
        inv_id = rev_id
124
109
 
125
110
        inv_tmp = tempfile.TemporaryFile()
126
 
        pack_xml(new_inv, inv_tmp)
 
111
        new_inv.write_xml(inv_tmp)
127
112
        inv_tmp.seek(0)
128
113
        branch.inventory_store.add(inv_tmp, inv_id)
129
114
        mutter('new inventory_id is {%s}' % inv_id)
139
124
            timestamp = time.time()
140
125
 
141
126
        if committer == None:
142
 
            committer = username(branch)
 
127
            committer = username()
143
128
 
144
129
        if timezone == None:
145
130
            timezone = local_time_offset()
153
138
                       inventory_sha1=inv_sha1,
154
139
                       revision_id=rev_id)
155
140
 
156
 
        rev.parents = []
157
141
        precursor_id = branch.last_patch()
158
142
        if precursor_id:
159
143
            precursor_sha1 = branch.get_revision_sha1(precursor_id)
160
 
            rev.parents.append(RevisionReference(precursor_id, precursor_sha1))
161
 
        for merge_rev in pending_merges:
162
 
            rev.parents.append(RevisionReference(merge_rev))            
 
144
            rev.parents = [RevisionReference(precursor_id, precursor_sha1)]
163
145
 
164
146
        rev_tmp = tempfile.TemporaryFile()
165
 
        pack_xml(rev, rev_tmp)
 
147
        rev.write_xml(rev_tmp)
166
148
        rev_tmp.seek(0)
167
149
        branch.revision_store.add(rev_tmp, rev_id)
168
150
        mutter("new revision_id is {%s}" % rev_id)
179
161
 
180
162
        branch.append_revision(rev_id)
181
163
 
182
 
        branch.set_pending_merges([])
183
 
 
184
164
        if verbose:
185
 
            # disabled; should go through logging
186
 
            # note("commited r%d" % branch.revno())
187
 
            # print ("commited r%d" % branch.revno())
188
 
            pass
 
165
            note("commited r%d" % branch.revno())
189
166
    finally:
190
167
        branch.unlock()
191
168
 
192
169
 
193
170
 
194
 
def _gen_revision_id(branch, when):
 
171
def _gen_revision_id(when):
195
172
    """Return new revision-id."""
196
173
    from binascii import hexlify
197
 
    from bzrlib.osutils import rand_bytes, compact_date, user_email
 
174
    from osutils import rand_bytes, compact_date, user_email
198
175
 
199
 
    s = '%s-%s-' % (user_email(branch), compact_date(when))
 
176
    s = '%s-%s-' % (user_email(), compact_date(when))
200
177
    s += hexlify(rand_bytes(8))
201
178
    return s
202
179
 
205
182
                   verbose):
206
183
    """Build inventory preparatory to commit.
207
184
 
208
 
    Returns missing_ids, new_inv, any_changes.
209
 
 
210
185
    This adds any changed files into the text store, and sets their
211
186
    test-id, sha and size in the returned inventory appropriately.
212
187
 
216
191
        working inventory.
217
192
    """
218
193
    from bzrlib.inventory import Inventory
219
 
    from bzrlib.osutils import isdir, isfile, sha_string, quotefn, \
 
194
    from osutils import isdir, isfile, sha_string, quotefn, \
220
195
         local_time_offset, username, kind_marker, is_inside_any
221
196
    
222
 
    from bzrlib.branch import gen_file_id
223
 
    from bzrlib.errors import BzrError
224
 
    from bzrlib.revision import Revision
 
197
    from branch import gen_file_id
 
198
    from errors import BzrError
 
199
    from revision import Revision
225
200
    from bzrlib.trace import mutter, note
226
201
 
227
 
    any_changes = False
228
 
    inv = Inventory(work_inv.root.file_id)
 
202
    inv = Inventory()
229
203
    missing_ids = []
230
204
    
231
205
    for path, entry in work_inv.iter_entries():
237
211
        mutter('commit prep file %s, id %r ' % (p, file_id))
238
212
 
239
213
        if specific_files and not is_inside_any(specific_files, path):
240
 
            mutter('  skipping file excluded from commit')
241
214
            if basis_inv.has_id(file_id):
242
215
                # carry over with previous state
243
216
                inv.add(basis_inv[file_id].copy())
249
222
        if not work_tree.has_id(file_id):
250
223
            if verbose:
251
224
                print('deleted %s%s' % (path, kind_marker(entry.kind)))
252
 
                any_changes = True
253
225
            mutter("    file is missing, removing from inventory")
254
226
            missing_ids.append(file_id)
255
227
            continue
301
273
            marked = path + kind_marker(entry.kind)
302
274
            if not old_ie:
303
275
                print 'added', marked
304
 
                any_changes = True
305
276
            elif old_ie == entry:
306
277
                pass                    # unchanged
307
278
            elif (old_ie.name == entry.name
308
279
                  and old_ie.parent_id == entry.parent_id):
309
280
                print 'modified', marked
310
 
                any_changes = True
311
281
            else:
312
282
                print 'renamed', marked
313
 
                any_changes = True
314
283
                        
315
 
    return missing_ids, inv, any_changes
 
284
    return missing_ids, inv
316
285
 
317
286