59
53
If null (default), a time/random revision id is generated.
56
import os, time, tempfile
64
from bzrlib.osutils import local_time_offset, username
65
from bzrlib.branch import gen_file_id
66
from bzrlib.errors import BzrError, PointlessCommit
67
from bzrlib.revision import Revision, RevisionReference
68
from bzrlib.trace import mutter, note
69
from bzrlib.xml import pack_xml
58
from inventory import Inventory
59
from osutils import isdir, isfile, sha_string, quotefn, \
60
local_time_offset, username, kind_marker, is_inside_any
62
from branch import gen_file_id
63
from errors import BzrError
64
from revision import Revision
65
from trace import mutter, note
71
67
branch.lock_write()
82
78
work_tree = branch.working_tree()
83
79
work_inv = work_tree.inventory
84
81
basis = branch.basis_tree()
85
82
basis_inv = basis.inventory
88
86
note('looking for changes...')
90
pending_merges = branch.pending_merges()
92
missing_ids, new_inv, any_changes = \
93
_gather_commit(branch,
100
if not (any_changes or allow_pointless or pending_merges):
101
raise PointlessCommit()
88
for path, entry in work_inv.iter_entries():
89
## TODO: Check that the file kind has not changed from the previous
90
## revision of this file (if any).
94
p = branch.abspath(path)
95
file_id = entry.file_id
96
mutter('commit prep file %s, id %r ' % (p, file_id))
98
if specific_files and not is_inside_any(specific_files, path):
99
if basis_inv.has_id(file_id):
100
# carry over with previous state
101
inv.add(basis_inv[file_id].copy())
103
# omit this from committed inventory
107
if not work_tree.has_id(file_id):
109
print('deleted %s%s' % (path, kind_marker(entry.kind)))
110
mutter(" file is missing, removing from inventory")
111
missing_ids.append(file_id)
116
if basis_inv.has_id(file_id):
117
old_kind = basis_inv[file_id].kind
118
if old_kind != entry.kind:
119
raise BzrError("entry %r changed kind from %r to %r"
120
% (file_id, old_kind, entry.kind))
122
if entry.kind == 'directory':
124
raise BzrError("%s is entered as directory but not a directory"
126
elif entry.kind == 'file':
128
raise BzrError("%s is entered as file but is not a file" % quotefn(p))
130
new_sha1 = work_tree.get_file_sha1(file_id)
132
old_ie = basis_inv.has_id(file_id) and basis_inv[file_id]
134
and old_ie.text_sha1 == new_sha1):
135
## assert content == basis.get_file(file_id).read()
136
entry.text_id = old_ie.text_id
137
entry.text_sha1 = new_sha1
138
entry.text_size = old_ie.text_size
139
mutter(' unchanged from previous text_id {%s}' %
142
content = file(p, 'rb').read()
144
# calculate the sha again, just in case the file contents
145
# changed since we updated the cache
146
entry.text_sha1 = sha_string(content)
147
entry.text_size = len(content)
149
entry.text_id = gen_file_id(entry.name)
150
branch.text_store.add(content, entry.text_id)
151
mutter(' stored with text_id {%s}' % entry.text_id)
154
print('added %s' % path)
155
elif (old_ie.name == entry.name
156
and old_ie.parent_id == entry.parent_id):
157
print('modified %s' % path)
159
print('renamed %s' % path)
103
162
for file_id in missing_ids:
104
163
# Any files that have been deleted are now removed from the
146
200
rev = Revision(timestamp=timestamp,
147
201
timezone=timezone,
148
202
committer=committer,
203
precursor = branch.last_patch(),
149
204
message = message,
150
205
inventory_id=inv_id,
151
inventory_sha1=inv_sha1,
152
206
revision_id=rev_id)
155
precursor_id = branch.last_patch()
157
precursor_sha1 = branch.get_revision_sha1(precursor_id)
158
rev.parents.append(RevisionReference(precursor_id, precursor_sha1))
159
for merge_rev in pending_merges:
160
rev.parents.append(RevisionReference(merge_rev))
162
208
rev_tmp = tempfile.TemporaryFile()
163
pack_xml(rev, rev_tmp)
209
rev.write_xml(rev_tmp)
165
211
branch.revision_store.add(rev_tmp, rev_id)
166
212
mutter("new revision_id is {%s}" % rev_id)
199
def _gather_commit(branch, work_tree, work_inv, basis_inv, specific_files,
201
"""Build inventory preparatory to commit.
203
Returns missing_ids, new_inv, any_changes.
205
This adds any changed files into the text store, and sets their
206
test-id, sha and size in the returned inventory appropriately.
209
Modified to hold a list of files that have been deleted from
210
the working directory; these should be removed from the
213
from bzrlib.inventory import Inventory
214
from bzrlib.osutils import isdir, isfile, sha_string, quotefn, \
215
local_time_offset, username, kind_marker, is_inside_any
217
from bzrlib.branch import gen_file_id
218
from bzrlib.errors import BzrError
219
from bzrlib.revision import Revision
220
from bzrlib.trace import mutter, note
223
inv = Inventory(work_inv.root.file_id)
226
for path, entry in work_inv.iter_entries():
227
## TODO: Check that the file kind has not changed from the previous
228
## revision of this file (if any).
230
p = branch.abspath(path)
231
file_id = entry.file_id
232
mutter('commit prep file %s, id %r ' % (p, file_id))
234
if specific_files and not is_inside_any(specific_files, path):
235
mutter(' skipping file excluded from commit')
236
if basis_inv.has_id(file_id):
237
# carry over with previous state
238
inv.add(basis_inv[file_id].copy())
240
# omit this from committed inventory
244
if not work_tree.has_id(file_id):
246
print('deleted %s%s' % (path, kind_marker(entry.kind)))
248
mutter(" file is missing, removing from inventory")
249
missing_ids.append(file_id)
252
# this is present in the new inventory; may be new, modified or
254
old_ie = basis_inv.has_id(file_id) and basis_inv[file_id]
260
old_kind = old_ie.kind
261
if old_kind != entry.kind:
262
raise BzrError("entry %r changed kind from %r to %r"
263
% (file_id, old_kind, entry.kind))
265
if entry.kind == 'directory':
267
raise BzrError("%s is entered as directory but not a directory"
269
elif entry.kind == 'file':
271
raise BzrError("%s is entered as file but is not a file" % quotefn(p))
273
new_sha1 = work_tree.get_file_sha1(file_id)
276
and old_ie.text_sha1 == new_sha1):
277
## assert content == basis.get_file(file_id).read()
278
entry.text_id = old_ie.text_id
279
entry.text_sha1 = new_sha1
280
entry.text_size = old_ie.text_size
281
mutter(' unchanged from previous text_id {%s}' %
284
content = file(p, 'rb').read()
286
# calculate the sha again, just in case the file contents
287
# changed since we updated the cache
288
entry.text_sha1 = sha_string(content)
289
entry.text_size = len(content)
291
entry.text_id = gen_file_id(entry.name)
292
branch.text_store.add(content, entry.text_id)
293
mutter(' stored with text_id {%s}' % entry.text_id)
296
marked = path + kind_marker(entry.kind)
298
print 'added', marked
300
elif old_ie == entry:
302
elif (old_ie.name == entry.name
303
and old_ie.parent_id == entry.parent_id):
304
print 'modified', marked
307
print 'renamed', marked
310
return missing_ids, inv, any_changes