From 1ac1128def54ba63a2cedeadf7e79ea008db5a84 Mon Sep 17 00:00:00 2001 From: Mark Thomas Date: Sat, 27 Oct 2018 10:57:44 -0700 Subject: [PATCH] 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 --- ...st-fb-hgext-remotefilelog-repack-corrupt.t | 6 ++--- tests/truncate.py | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) create mode 100755 tests/truncate.py diff --git a/tests/test-fb-hgext-remotefilelog-repack-corrupt.t b/tests/test-fb-hgext-remotefilelog-repack-corrupt.t index c15468b0b5..b22c739c66 100644 --- a/tests/test-fb-hgext-remotefilelog-repack-corrupt.t +++ b/tests/test-fb-hgext-remotefilelog-repack-corrupt.t @@ -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 diff --git a/tests/truncate.py b/tests/truncate.py new file mode 100755 index 0000000000..0791ff7dfd --- /dev/null +++ b/tests/truncate.py @@ -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)