[Updated] D10901: censor: extract the part about creating and opening new files in a function
marmoute (Pierre-Yves David)
phabricator at mercurial-scm.org
Thu Jul 1 10:21:27 UTC 2021
Closed by commit rHGd6afe1478a2a: censor: extract the part about creating and opening new files in a function (authored by marmoute).
This revision was automatically updated to reflect the committed changes.
CHANGED PRIOR TO COMMIT
https://phab.mercurial-scm.org/D10901?vs=28680&id=28712#toc
REPOSITORY
rHG Mercurial
CHANGES SINCE LAST UPDATE
https://phab.mercurial-scm.org/D10901?vs=28680&id=28712
CHANGES SINCE LAST ACTION
https://phab.mercurial-scm.org/D10901/new/
REVISION DETAIL
https://phab.mercurial-scm.org/D10901
AFFECTED FILES
mercurial/revlogutils/rewrite.py
CHANGE DETAILS
diff --git a/mercurial/revlogutils/rewrite.py b/mercurial/revlogutils/rewrite.py
--- a/mercurial/revlogutils/rewrite.py
+++ b/mercurial/revlogutils/rewrite.py
@@ -163,55 +163,12 @@
tmp_storage,
)
- old_index_filepath = rl.opener.join(docket.index_filepath())
- old_data_filepath = rl.opener.join(docket.data_filepath())
- old_sidedata_filepath = rl.opener.join(docket.sidedata_filepath())
-
- new_index_filepath = rl.opener.join(docket.new_index_file())
- 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,
+ all_files = _setup_new_files(
+ rl,
+ index_cutoff,
+ data_cutoff,
+ 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
# exist in a usable form in `old_index`.
@@ -223,12 +180,6 @@
new_data_file,
new_sidedata_file,
) = 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
_rewrite_censor(
@@ -305,6 +256,80 @@
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 version 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(
revlog,
old_index,
To: marmoute, #hg-reviewers, Alphare
Cc: mercurial-patches
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurial-scm.org/pipermail/mercurial-patches/attachments/20210701/d9485143/attachment-0002.html>
More information about the Mercurial-patches
mailing list