~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commit.py

  • Committer: Martin Pool
  • Date: 2005-08-22 17:52:11 UTC
  • Revision ID: mbp@sourcefrog.net-20050822175211-90caf03af7d0cf07
- fix bug where bzr upgrade aborts when trying to fix trees that mention revisions
  which are not locally present

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
 
57
63
 
58
64
    from bzrlib.osutils import local_time_offset, username
59
65
    from bzrlib.branch import gen_file_id
60
 
    from bzrlib.errors import BzrError
 
66
    from bzrlib.errors import BzrError, PointlessCommit
61
67
    from bzrlib.revision import Revision, RevisionReference
62
68
    from bzrlib.trace import mutter, note
 
69
    from bzrlib.xml import pack_xml
63
70
 
64
71
    branch.lock_write()
65
72
 
80
87
        if verbose:
81
88
            note('looking for changes...')
82
89
 
83
 
        missing_ids, new_inv = _gather_commit(branch,
84
 
                                              work_tree,
85
 
                                              work_inv,
86
 
                                              basis_inv,
87
 
                                              specific_files,
88
 
                                              verbose)
 
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()
89
102
 
90
103
        for file_id in missing_ids:
91
104
            # Any files that have been deleted are now removed from the
102
115
            if work_inv.has_id(file_id):
103
116
                del work_inv[file_id]
104
117
 
105
 
 
106
118
        if rev_id is None:
107
 
            rev_id = _gen_revision_id(time.time())
 
119
            rev_id = _gen_revision_id(branch, time.time())
108
120
        inv_id = rev_id
109
121
 
110
122
        inv_tmp = tempfile.TemporaryFile()
111
 
        new_inv.write_xml(inv_tmp)
 
123
        pack_xml(new_inv, inv_tmp)
112
124
        inv_tmp.seek(0)
113
125
        branch.inventory_store.add(inv_tmp, inv_id)
114
126
        mutter('new inventory_id is {%s}' % inv_id)
124
136
            timestamp = time.time()
125
137
 
126
138
        if committer == None:
127
 
            committer = username()
 
139
            committer = username(branch)
128
140
 
129
141
        if timezone == None:
130
142
            timezone = local_time_offset()
138
150
                       inventory_sha1=inv_sha1,
139
151
                       revision_id=rev_id)
140
152
 
 
153
        rev.parents = []
141
154
        precursor_id = branch.last_patch()
142
155
        if precursor_id:
143
156
            precursor_sha1 = branch.get_revision_sha1(precursor_id)
144
 
            rev.parents = [RevisionReference(precursor_id, precursor_sha1)]
 
157
            rev.parents.append(RevisionReference(precursor_id, precursor_sha1))
 
158
        for merge_rev in pending_merges:
 
159
            rev.parents.append(RevisionReference(merge_rev))            
145
160
 
146
161
        rev_tmp = tempfile.TemporaryFile()
147
 
        rev.write_xml(rev_tmp)
 
162
        pack_xml(rev, rev_tmp)
148
163
        rev_tmp.seek(0)
149
164
        branch.revision_store.add(rev_tmp, rev_id)
150
165
        mutter("new revision_id is {%s}" % rev_id)
161
176
 
162
177
        branch.append_revision(rev_id)
163
178
 
 
179
        branch.set_pending_merges([])
 
180
 
164
181
        if verbose:
165
182
            note("commited r%d" % branch.revno())
166
183
    finally:
168
185
 
169
186
 
170
187
 
171
 
def _gen_revision_id(when):
 
188
def _gen_revision_id(branch, when):
172
189
    """Return new revision-id."""
173
190
    from binascii import hexlify
174
 
    from osutils import rand_bytes, compact_date, user_email
 
191
    from bzrlib.osutils import rand_bytes, compact_date, user_email
175
192
 
176
 
    s = '%s-%s-' % (user_email(), compact_date(when))
 
193
    s = '%s-%s-' % (user_email(branch), compact_date(when))
177
194
    s += hexlify(rand_bytes(8))
178
195
    return s
179
196
 
182
199
                   verbose):
183
200
    """Build inventory preparatory to commit.
184
201
 
 
202
    Returns missing_ids, new_inv, any_changes.
 
203
 
185
204
    This adds any changed files into the text store, and sets their
186
205
    test-id, sha and size in the returned inventory appropriately.
187
206
 
191
210
        working inventory.
192
211
    """
193
212
    from bzrlib.inventory import Inventory
194
 
    from osutils import isdir, isfile, sha_string, quotefn, \
 
213
    from bzrlib.osutils import isdir, isfile, sha_string, quotefn, \
195
214
         local_time_offset, username, kind_marker, is_inside_any
196
215
    
197
 
    from branch import gen_file_id
198
 
    from errors import BzrError
199
 
    from revision import Revision
 
216
    from bzrlib.branch import gen_file_id
 
217
    from bzrlib.errors import BzrError
 
218
    from bzrlib.revision import Revision
200
219
    from bzrlib.trace import mutter, note
201
220
 
202
 
    inv = Inventory()
 
221
    any_changes = False
 
222
    inv = Inventory(work_inv.root.file_id)
203
223
    missing_ids = []
204
224
    
205
225
    for path, entry in work_inv.iter_entries():
211
231
        mutter('commit prep file %s, id %r ' % (p, file_id))
212
232
 
213
233
        if specific_files and not is_inside_any(specific_files, path):
 
234
            mutter('  skipping file excluded from commit')
214
235
            if basis_inv.has_id(file_id):
215
236
                # carry over with previous state
216
237
                inv.add(basis_inv[file_id].copy())
222
243
        if not work_tree.has_id(file_id):
223
244
            if verbose:
224
245
                print('deleted %s%s' % (path, kind_marker(entry.kind)))
 
246
                any_changes = True
225
247
            mutter("    file is missing, removing from inventory")
226
248
            missing_ids.append(file_id)
227
249
            continue
273
295
            marked = path + kind_marker(entry.kind)
274
296
            if not old_ie:
275
297
                print 'added', marked
 
298
                any_changes = True
276
299
            elif old_ie == entry:
277
300
                pass                    # unchanged
278
301
            elif (old_ie.name == entry.name
279
302
                  and old_ie.parent_id == entry.parent_id):
280
303
                print 'modified', marked
 
304
                any_changes = True
281
305
            else:
282
306
                print 'renamed', marked
 
307
                any_changes = True
283
308
                        
284
 
    return missing_ids, inv
 
309
    return missing_ids, inv, any_changes
285
310
 
286
311