The v2_censor function is huge, now that its content has settled a bit it is a
good time to split individual part inside dedicated function.
The last part is the file copying and opening logic. It now have its own
function.
( )
Alphare |
hg-reviewers |
The v2_censor function is huge, now that its content has settled a bit it is a
good time to split individual part inside dedicated function.
The last part is the file copying and opening logic. It now have its own
function.
No Linters Available |
No Unit Test Coverage |
Path | Packages | |||
---|---|---|---|---|
M | mercurial/revlogutils/rewrite.py (133 lines) |
Commit | Parents | Author | Summary | Date |
---|---|---|---|---|
84763d3bb525 | 7d4195307fee | Pierre-Yves David | Jun 22 2021, 4:28 PM |
# rev → (new_base, data_start, data_end, compression_mode) | # rev → (new_base, data_start, data_end, compression_mode) | ||||
rewritten_entries = _precompute_rewritten_delta( | rewritten_entries = _precompute_rewritten_delta( | ||||
rl, | rl, | ||||
old_index, | old_index, | ||||
{censor_rev}, | {censor_rev}, | ||||
tmp_storage, | tmp_storage, | ||||
) | ) | ||||
old_index_filepath = rl.opener.join(docket.index_filepath()) | all_files = _setup_new_files( | ||||
old_data_filepath = rl.opener.join(docket.data_filepath()) | rl, | ||||
old_sidedata_filepath = rl.opener.join(docket.sidedata_filepath()) | index_cutoff, | ||||
data_cutoff, | |||||
new_index_filepath = rl.opener.join(docket.new_index_file()) | sidedata_cutoff, | ||||
new_data_filepath = rl.opener.join(docket.new_data_file()) | |||||
new_sidedata_filepath = rl.opener.join(docket.new_sidedata_file()) | |||||
util.copyfile( | |||||
old_index_filepath, new_index_filepath, nb_bytes=index_cutoff | |||||
) | |||||
util.copyfile( | |||||
old_data_filepath, new_data_filepath, nb_bytes=data_cutoff | |||||
) | |||||
util.copyfile( | |||||
old_sidedata_filepath, | |||||
new_sidedata_filepath, | |||||
nb_bytes=sidedata_cutoff, | |||||
) | |||||
rl.opener.register_file(docket.index_filepath()) | |||||
rl.opener.register_file(docket.data_filepath()) | |||||
rl.opener.register_file(docket.sidedata_filepath()) | |||||
docket.index_end = index_cutoff | |||||
docket.data_end = data_cutoff | |||||
docket.sidedata_end = sidedata_cutoff | |||||
# reload the revlog internal information | |||||
rl.clearcaches() | |||||
rl._loadindex(docket=docket) | |||||
@contextlib.contextmanager | |||||
def all_files(): | |||||
# hide opening in an helper function to please check-code, black | |||||
# and various python ersion at the same time | |||||
with open(old_data_filepath, 'rb') as old_data_file: | |||||
with open(old_sidedata_filepath, 'rb') as old_sidedata_file: | |||||
with open(new_index_filepath, 'r+b') as new_index_file: | |||||
with open(new_data_filepath, 'r+b') as new_data_file: | |||||
with open( | |||||
new_sidedata_filepath, 'r+b' | |||||
) as new_sidedata_file: | |||||
yield ( | |||||
old_data_file, | |||||
old_sidedata_file, | |||||
new_index_file, | |||||
new_data_file, | |||||
new_sidedata_file, | |||||
) | ) | ||||
# we dont need to open the old index file since its content already | # we dont need to open the old index file since its content already | ||||
# exist in a usable form in `old_index`. | # exist in a usable form in `old_index`. | ||||
with all_files() as open_files: | with all_files() as open_files: | ||||
( | ( | ||||
old_data_file, | old_data_file, | ||||
old_sidedata_file, | old_sidedata_file, | ||||
new_index_file, | new_index_file, | ||||
new_data_file, | new_data_file, | ||||
new_sidedata_file, | new_sidedata_file, | ||||
) = open_files | ) = open_files | ||||
new_index_file.seek(0, os.SEEK_END) | |||||
assert new_index_file.tell() == index_cutoff | |||||
new_data_file.seek(0, os.SEEK_END) | |||||
assert new_data_file.tell() == data_cutoff | |||||
new_sidedata_file.seek(0, os.SEEK_END) | |||||
assert new_sidedata_file.tell() == sidedata_cutoff | |||||
# writing the censored revision | # writing the censored revision | ||||
_rewrite_censor( | _rewrite_censor( | ||||
rl, | rl, | ||||
old_index, | old_index, | ||||
open_files, | open_files, | ||||
censor_rev, | censor_rev, | ||||
tombstone, | tombstone, | ||||
# using `tell` is a bit lazy, but we are not here for speed | # using `tell` is a bit lazy, but we are not here for speed | ||||
start = tmp_storage.tell() | start = tmp_storage.tell() | ||||
tmp_storage.write(d.data[1]) | tmp_storage.write(d.data[1]) | ||||
end = tmp_storage.tell() | end = tmp_storage.tell() | ||||
rewritten_entries[rev] = (d.base, start, end, comp_mode) | rewritten_entries[rev] = (d.base, start, end, comp_mode) | ||||
return rewritten_entries | return rewritten_entries | ||||
def _setup_new_files( | |||||
revlog, | |||||
index_cutoff, | |||||
data_cutoff, | |||||
sidedata_cutoff, | |||||
): | |||||
""" | |||||
return a context manager to open all the relevant files: | |||||
- old_data_file, | |||||
- old_sidedata_file, | |||||
- new_index_file, | |||||
- new_data_file, | |||||
- new_sidedata_file, | |||||
The old_index_file is not here because it is accessed through the | |||||
`old_index` object if the caller function. | |||||
""" | |||||
docket = revlog._docket | |||||
old_index_filepath = revlog.opener.join(docket.index_filepath()) | |||||
old_data_filepath = revlog.opener.join(docket.data_filepath()) | |||||
old_sidedata_filepath = revlog.opener.join(docket.sidedata_filepath()) | |||||
new_index_filepath = revlog.opener.join(docket.new_index_file()) | |||||
new_data_filepath = revlog.opener.join(docket.new_data_file()) | |||||
new_sidedata_filepath = revlog.opener.join(docket.new_sidedata_file()) | |||||
util.copyfile(old_index_filepath, new_index_filepath, nb_bytes=index_cutoff) | |||||
util.copyfile(old_data_filepath, new_data_filepath, nb_bytes=data_cutoff) | |||||
util.copyfile( | |||||
old_sidedata_filepath, | |||||
new_sidedata_filepath, | |||||
nb_bytes=sidedata_cutoff, | |||||
) | |||||
revlog.opener.register_file(docket.index_filepath()) | |||||
revlog.opener.register_file(docket.data_filepath()) | |||||
revlog.opener.register_file(docket.sidedata_filepath()) | |||||
docket.index_end = index_cutoff | |||||
docket.data_end = data_cutoff | |||||
docket.sidedata_end = sidedata_cutoff | |||||
# reload the revlog internal information | |||||
revlog.clearcaches() | |||||
revlog._loadindex(docket=docket) | |||||
@contextlib.contextmanager | |||||
def all_files_opener(): | |||||
# hide opening in an helper function to please check-code, black | |||||
# and various python ersion at the same time | |||||
with open(old_data_filepath, 'rb') as old_data_file: | |||||
with open(old_sidedata_filepath, 'rb') as old_sidedata_file: | |||||
with open(new_index_filepath, 'r+b') as new_index_file: | |||||
with open(new_data_filepath, 'r+b') as new_data_file: | |||||
with open( | |||||
new_sidedata_filepath, 'r+b' | |||||
) as new_sidedata_file: | |||||
new_index_file.seek(0, os.SEEK_END) | |||||
assert new_index_file.tell() == index_cutoff | |||||
new_data_file.seek(0, os.SEEK_END) | |||||
assert new_data_file.tell() == data_cutoff | |||||
new_sidedata_file.seek(0, os.SEEK_END) | |||||
assert new_sidedata_file.tell() == sidedata_cutoff | |||||
yield ( | |||||
old_data_file, | |||||
old_sidedata_file, | |||||
new_index_file, | |||||
new_data_file, | |||||
new_sidedata_file, | |||||
) | |||||
return all_files_opener | |||||
def _rewrite_simple( | def _rewrite_simple( | ||||
revlog, | revlog, | ||||
old_index, | old_index, | ||||
all_files, | all_files, | ||||
rev, | rev, | ||||
rewritten_entries, | rewritten_entries, | ||||
tmp_storage, | tmp_storage, | ||||
): | ): |