~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 07:18:01 UTC
  • Revision ID: mbp@sourcefrog.net-20050622071801-916bbae5bd93cf0a
- new mkdir command

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