From a3cd74c29be02dde0d65e0dcf79bccc8d41952ed Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Thu, 10 Oct 2019 10:00:45 -0700
Subject: [PATCH] `--metadata-file`: when multiple files specified, second
 takes precedence...

on conflicting fields.  This changes earlier behavior (but not in
a release), where first took precedence.

Note that this may seem inconsistent with the behavior of
multiple YAML blocks within a document, where the first takes
precedence.  Still, it is convenient to be able to override
defaults with options later on the command line.
---
 MANUAL.txt                                | 18 ++++++++++--------
 src/Text/Pandoc/App.hs                    |  4 ++++
 src/Text/Pandoc/App/CommandLineOptions.hs |  2 +-
 test/command/5700-metadata-file-1.yml     |  2 +-
 test/command/5700-metadata-file-2.yml     |  2 +-
 5 files changed, 17 insertions(+), 11 deletions(-)

diff --git a/MANUAL.txt b/MANUAL.txt
index f7373b4c4..25f76b9aa 100644
--- a/MANUAL.txt
+++ b/MANUAL.txt
@@ -606,14 +606,16 @@ header when requesting a document from a URL:
 
 `--metadata-file=`*FILE*
 
-:   Read metadata from the supplied YAML (or JSON) file. This option can be used
-    with every input format, but string scalars in the YAML file will always be
-    parsed as Markdown. Generally, the input will be handled the same as in
-    [YAML metadata blocks][Extension: `yaml_metadata_block`]. This option can be
-    used repeatedly to include multiple metadata files; values in files specified
-    first will be preferred over those specified in later files. Metadata values
-    specified inside the document, or by using `-M`, overwrite values specified
-    with this option.
+:   Read metadata from the supplied YAML (or JSON) file. This
+    option can be used with every input format, but string scalars
+    in the YAML file will always be parsed as Markdown. Generally,
+    the input will be handled the same as in [YAML metadata
+    blocks][Extension: `yaml_metadata_block`]. This option can be
+    used repeatedly to include multiple metadata files; values in
+    files specified later on the command line will be preferred
+    over those specified in earlier files. Metadata values
+    specified inside the document, or by using `-M`, overwrite
+    values specified with this option.
 
 `-p`, `--preserve-tabs`
 
diff --git a/src/Text/Pandoc/App.hs b/src/Text/Pandoc/App.hs
index ed5963f1a..f3d342ebf 100644
--- a/src/Text/Pandoc/App.hs
+++ b/src/Text/Pandoc/App.hs
@@ -231,6 +231,10 @@ convertWithOpts opts = do
         []    -> return mempty
         paths -> mapM readFileLazy paths >>= mapM (yamlToMeta readerOpts)
                    >>= return . (foldr1 (<>))
+        -- Note: this list is in reverse order from the order on the
+        -- command line.  So this code ensures that metadata files
+        -- specified later in the command line take precedence over
+        -- those specified earlier.
 
     let transforms = (case optShiftHeadingLevelBy opts of
                           0             -> id
diff --git a/src/Text/Pandoc/App/CommandLineOptions.hs b/src/Text/Pandoc/App/CommandLineOptions.hs
index f9f825a44..a7b20a6e3 100644
--- a/src/Text/Pandoc/App/CommandLineOptions.hs
+++ b/src/Text/Pandoc/App/CommandLineOptions.hs
@@ -209,7 +209,7 @@ options =
     , Option "" ["metadata-file"]
                  (ReqArg
                   (\arg opt -> return opt{ optMetadataFile =
-                    (optMetadataFile opt) <> [normalizePath arg] })
+                    normalizePath arg : optMetadataFile opt })
                   "FILE")
                  ""
 
diff --git a/test/command/5700-metadata-file-1.yml b/test/command/5700-metadata-file-1.yml
index df64e99c5..8289c392b 100644
--- a/test/command/5700-metadata-file-1.yml
+++ b/test/command/5700-metadata-file-1.yml
@@ -1 +1 @@
-title: Multiple metadata files test
+title: This title should be overridden by 5700-metadta-file-2.yml
diff --git a/test/command/5700-metadata-file-2.yml b/test/command/5700-metadata-file-2.yml
index e865ed55d..1d5510e07 100644
--- a/test/command/5700-metadata-file-2.yml
+++ b/test/command/5700-metadata-file-2.yml
@@ -1,2 +1,2 @@
-title: This title should be overridden by 5700-metadta-file-2.yml
+title: Multiple metadata files test
 desc: Both of these files should be loaded.