# HG changeset patch
# User Boris Feld <boris.feld@octobus.net>
# Date 1548436896 18000
#      Fri Jan 25 12:21:36 2019 -0500
# Node ID fdd4250c59f2c49925e7c77c90e9ee449e1f7888
# Parent  039688ae2325e9573a50bdfc6d5d88a84d3b8a76
Add the possibility to pass an hgrc file when making a reference

In combination with the previous commit, it will allow to tune the format of
the reference repository.

diff --git a/repo-scripts/make-reference b/repo-scripts/make-reference
--- a/repo-scripts/make-reference
+++ b/repo-scripts/make-reference
@@ -30,6 +30,9 @@
         help="Run extra optimization through debugupgraderepo before archiving",
         nargs="+",
     )
+    parser.add_argument(
+        "--hgrc", help="Put the given HGRC file in the repo when cloning it"
+    )
 
     args = parser.parse_args(args)
 
@@ -42,6 +45,23 @@
 
     repo_hg = join(repo_main, ".hg")
 
+    if args.hgrc and not os.path.isfile(args.hgrc):
+        print("Given hgrc {0!r} is not a file".format(args.hgrc))
+        sys.exit(1)
+
+    # Check that we can read it
+    if args.hgrc:
+        try:
+            with open(args.hgrc, "rb") as hgrc_file:
+                hgrc_file.read(1)
+        except (IOError, OSError):
+            print(
+                "Given hgrc {0!r} is not readable by current user".format(
+                    args.hgrc
+                )
+            )
+            sys.exit(1)
+
     ### initial clone
 
     if os.path.isdir(repo_hg):
@@ -57,6 +77,10 @@
         print("hg cloning repository from: {0}".format(repo_source))
         shell_exec(["hg", "clone", "--noupdate", repo_source, repo_main])
 
+        if args.hgrc:
+            print("copying hgrc file from: {0}".format(args.hgrc))
+            shutil.copy(args.hgrc, join(repo_main, ".hg", "hgrc"))
+
     ### Clean caches
     shell_exec(["hg", "-R", repo_main, "debugupdatecache"])
 
diff --git a/tests/test-make-reference.t b/tests/test-make-reference.t
--- a/tests/test-make-reference.t
+++ b/tests/test-make-reference.t
@@ -14,15 +14,18 @@
 ----------
 
   $ make-reference
-  usage: make-reference [-h] [--optimize OPTIMIZE [OPTIMIZE ...]] source id
+  usage: make-reference [-h] [--optimize OPTIMIZE [OPTIMIZE ...]] [--hgrc HGRC]
+                        source id
   make-reference: error: too few arguments
   [2]
   $ make-reference some-dir/my-source-repo
-  usage: make-reference [-h] [--optimize OPTIMIZE [OPTIMIZE ...]] source id
+  usage: make-reference [-h] [--optimize OPTIMIZE [OPTIMIZE ...]] [--hgrc HGRC]
+                        source id
   make-reference: error: too few arguments
   [2]
   $ make-reference my-id
-  usage: make-reference [-h] [--optimize OPTIMIZE [OPTIMIZE ...]] source id
+  usage: make-reference [-h] [--optimize OPTIMIZE [OPTIMIZE ...]] [--hgrc HGRC]
+                        source id
   make-reference: error: too few arguments
   [2]
 
@@ -280,3 +283,63 @@
       all: 53
       visible: 53
     source: some-dir/my-source-repo
+
+Call from scratch with given hgrc file
+--------------------------------------
+
+  $ rm -rf my-reference.benchrepo my-reference.tar my-reference-reference
+  $ ls -1
+  dst
+  editor.sh
+  my-reference-reference.tar
+  some-dir
+
+  $ cat << EOF > sparse-revlog-hgrc
+  > [format]
+  > sparse-revlog = false
+  > EOF
+
+  $ make-reference some-dir/my-source-repo my-reference --hgrc non-existing-hgrc
+  Given hgrc 'non-existing-hgrc' is not a file
+  [1]
+
+  $ make-reference some-dir/my-source-repo my-reference --hgrc sparse-revlog-hgrc
+  copy of old repository backed up at $TESTTMP/my-reference-reference/.hg/upgradebackup.* (glob)
+  the old repository will not be deleted; remove it to free up disk space once the upgraded repository is verified
+  hg cloning repository from: some-dir/my-source-repo
+  copying hgrc file from: sparse-revlog-hgrc
+  Running debugupgraderepo because of format mismatch:
+  	generaldelta: False != True (repo/config)
+  building a reference tarball
+  result available at: my-reference-reference.tar
+  $ ls -1
+  dst
+  editor.sh
+  my-reference-reference
+  my-reference-reference.tar
+  my-reference.benchrepo
+  some-dir
+  sparse-revlog-hgrc
+
+  $ cat my-reference.benchrepo
+  reference-repo:
+    enabled: true
+    format-info:
+      compression: zlib
+      dotencode: true
+      fncache: true
+      generaldelta: true
+      plain-cl-delta: true
+      sparserevlog: false
+    hg-version: * (glob)
+    id: my-reference
+    number-heads:
+      all: 3
+      visible: 3
+    number-named-branch:
+      all: 1
+      visible: 1
+    number-revisions:
+      all: 53
+      visible: 53
+    source: some-dir/my-source-repo