truncate: add truncate implementation

Summary:
The `truncate` tool isn't availble on OSX, so include our own in the test
suite.

Reviewed By: DurhamG

Differential Revision: D12815613

fbshipit-source-id: 510b2936f07c5193671baaeaec6620872c3ec982
This commit is contained in:
Mark Thomas 2018-10-27 10:57:44 -07:00 committed by Facebook Github Bot
parent e130401a2a
commit 1ac1128def
2 changed files with 27 additions and 3 deletions

View File

@ -72,7 +72,7 @@ Create some new data to pack into it
Truncate the historypack file in the middle of the filename length for "y"
$ chmod +w $TESTTMP/hgcache/master/packs/37db2caec222ca26824a52d6bdc778344e0d1440.histpack
$ truncate --size 173 $TESTTMP/hgcache/master/packs/37db2caec222ca26824a52d6bdc778344e0d1440.histpack
$ python $TESTDIR/truncate.py --size 173 $TESTTMP/hgcache/master/packs/37db2caec222ca26824a52d6bdc778344e0d1440.histpack
Repack
$ hg repack
@ -109,11 +109,11 @@ Create some local commits and pack them into a pack file
Truncate the history in the middle of the filename length for "n"
$ chmod +w .hg/store/packs/822f755410ca9f664d7c706957b8391248327318.histpack
$ truncate --size 173 .hg/store/packs/822f755410ca9f664d7c706957b8391248327318.histpack
$ python $TESTDIR/truncate.py --size 173 .hg/store/packs/822f755410ca9f664d7c706957b8391248327318.histpack
Truncate the data in the middle of the filename length for "o"
$ chmod +w .hg/store/packs/a6bc602042c5c7853bcc1bb89c5de05bb34fd862.datapack
$ truncate --size 290 .hg/store/packs/a6bc602042c5c7853bcc1bb89c5de05bb34fd862.datapack
$ python $TESTDIR/truncate.py --size 290 .hg/store/packs/a6bc602042c5c7853bcc1bb89c5de05bb34fd862.datapack
Repack
$ hg repack

24
tests/truncate.py Executable file
View File

@ -0,0 +1,24 @@
#!/usr/bin/env python
import argparse
if __name__ == "__main__":
ap = argparse.ArgumentParser(usage="%(prog)s [options] file...")
ap.add_argument(
"-s",
"--size",
type=int,
default=0,
help="size in bytes to truncate to",
metavar="BYTES",
)
ap.add_argument("file", nargs="+", help="file to truncate", metavar="FILE")
args = ap.parse_args()
size = args.size
if size < 0:
ap.error("size cannot be negative")
for filename in args.file:
with open(filename, "a+b") as f:
f.truncate(size)