~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commit.py

  • Committer: Martin Pool
  • Date: 2005-08-25 07:46:11 UTC
  • Revision ID: mbp@sourcefrog.net-20050825074611-98130ea6d05d9d2a
- add functions to enable and disable default logging, so that we can
  turn it off while running the tests

- default logging gets turned on from the bzr main function so that
  other applications using the library can make their own decisions

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
 
78
85
        basis_inv = basis.inventory
79
86
 
80
87
        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)
 
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()
89
105
 
90
106
        for file_id in missing_ids:
91
107
            # Any files that have been deleted are now removed from the
102
118
            if work_inv.has_id(file_id):
103
119
                del work_inv[file_id]
104
120
 
105
 
 
106
121
        if rev_id is None:
107
 
            rev_id = _gen_revision_id(time.time())
 
122
            rev_id = _gen_revision_id(branch, time.time())
108
123
        inv_id = rev_id
109
124
 
110
125
        inv_tmp = tempfile.TemporaryFile()
111
 
        new_inv.write_xml(inv_tmp)
 
126
        pack_xml(new_inv, inv_tmp)
112
127
        inv_tmp.seek(0)
113
128
        branch.inventory_store.add(inv_tmp, inv_id)
114
129
        mutter('new inventory_id is {%s}' % inv_id)
124
139
            timestamp = time.time()
125
140
 
126
141
        if committer == None:
127
 
            committer = username()
 
142
            committer = username(branch)
128
143
 
129
144
        if timezone == None:
130
145
            timezone = local_time_offset()
138
153
                       inventory_sha1=inv_sha1,
139
154
                       revision_id=rev_id)
140
155
 
 
156
        rev.parents = []
141
157
        precursor_id = branch.last_patch()
142
158
        if precursor_id:
143
159
            precursor_sha1 = branch.get_revision_sha1(precursor_id)
144
 
            rev.parents = [RevisionReference(precursor_id, precursor_sha1)]
 
160
            rev.parents.append(RevisionReference(precursor_id, precursor_sha1))
 
161
        for merge_rev in pending_merges:
 
162
            rev.parents.append(RevisionReference(merge_rev))            
145
163
 
146
164
        rev_tmp = tempfile.TemporaryFile()
147
 
        rev.write_xml(rev_tmp)
 
165
        pack_xml(rev, rev_tmp)
148
166
        rev_tmp.seek(0)
149
167
        branch.revision_store.add(rev_tmp, rev_id)
150
168
        mutter("new revision_id is {%s}" % rev_id)
161
179
 
162
180
        branch.append_revision(rev_id)
163
181
 
 
182
        branch.set_pending_merges([])
 
183
 
164
184
        if verbose:
165
 
            note("commited r%d" % branch.revno())
 
185
            # disabled; should go through logging
 
186
            # note("commited r%d" % branch.revno())
 
187
            # print ("commited r%d" % branch.revno())
 
188
            pass
166
189
    finally:
167
190
        branch.unlock()
168
191
 
169
192
 
170
193
 
171
 
def _gen_revision_id(when):
 
194
def _gen_revision_id(branch, when):
172
195
    """Return new revision-id."""
173
196
    from binascii import hexlify
174
 
    from osutils import rand_bytes, compact_date, user_email
 
197
    from bzrlib.osutils import rand_bytes, compact_date, user_email
175
198
 
176
 
    s = '%s-%s-' % (user_email(), compact_date(when))
 
199
    s = '%s-%s-' % (user_email(branch), compact_date(when))
177
200
    s += hexlify(rand_bytes(8))
178
201
    return s
179
202
 
182
205
                   verbose):
183
206
    """Build inventory preparatory to commit.
184
207
 
 
208
    Returns missing_ids, new_inv, any_changes.
 
209
 
185
210
    This adds any changed files into the text store, and sets their
186
211
    test-id, sha and size in the returned inventory appropriately.
187
212
 
191
216
        working inventory.
192
217
    """
193
218
    from bzrlib.inventory import Inventory
194
 
    from osutils import isdir, isfile, sha_string, quotefn, \
 
219
    from bzrlib.osutils import isdir, isfile, sha_string, quotefn, \
195
220
         local_time_offset, username, kind_marker, is_inside_any
196
221
    
197
 
    from branch import gen_file_id
198
 
    from errors import BzrError
199
 
    from revision import Revision
 
222
    from bzrlib.branch import gen_file_id
 
223
    from bzrlib.errors import BzrError
 
224
    from bzrlib.revision import Revision
200
225
    from bzrlib.trace import mutter, note
201
226
 
202
 
    inv = Inventory()
 
227
    any_changes = False
 
228
    inv = Inventory(work_inv.root.file_id)
203
229
    missing_ids = []
204
230
    
205
231
    for path, entry in work_inv.iter_entries():
211
237
        mutter('commit prep file %s, id %r ' % (p, file_id))
212
238
 
213
239
        if specific_files and not is_inside_any(specific_files, path):
 
240
            mutter('  skipping file excluded from commit')
214
241
            if basis_inv.has_id(file_id):
215
242
                # carry over with previous state
216
243
                inv.add(basis_inv[file_id].copy())
222
249
        if not work_tree.has_id(file_id):
223
250
            if verbose:
224
251
                print('deleted %s%s' % (path, kind_marker(entry.kind)))
 
252
                any_changes = True
225
253
            mutter("    file is missing, removing from inventory")
226
254
            missing_ids.append(file_id)
227
255
            continue
273
301
            marked = path + kind_marker(entry.kind)
274
302
            if not old_ie:
275
303
                print 'added', marked
 
304
                any_changes = True
276
305
            elif old_ie == entry:
277
306
                pass                    # unchanged
278
307
            elif (old_ie.name == entry.name
279
308
                  and old_ie.parent_id == entry.parent_id):
280
309
                print 'modified', marked
 
310
                any_changes = True
281
311
            else:
282
312
                print 'renamed', marked
 
313
                any_changes = True
283
314
                        
284
 
    return missing_ids, inv
 
315
    return missing_ids, inv, any_changes
285
316
 
286
317