From c8227e0cf3bc4e8d98de84d6068b3affe95235e5 Mon Sep 17 00:00:00 2001
From: Andrew Gallant <jamslam@gmail.com>
Date: Sat, 24 Sep 2016 20:22:02 -0400
Subject: [PATCH] Don't ignore first path when using --files.

This is a docopt oddity, but probably not a bug. If --files is given,
then just interpret the pattern (if not empty) as the first file path.

Fixes #64.
---
 src/args.rs    |  9 ++++++++-
 tests/tests.rs | 19 +++++++++++++++++++
 2 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/src/args.rs b/src/args.rs
index f8908be7..7306e0b6 100644
--- a/src/args.rs
+++ b/src/args.rs
@@ -463,7 +463,7 @@ impl Args {
                 }
             }
         }
-        let raw: RawArgs =
+        let mut raw: RawArgs =
             Docopt::new(USAGE)
                 .and_then(|d| d.argv(argv).version(Some(version())).decode())
                 .unwrap_or_else(|e| e.exit());
@@ -478,6 +478,13 @@ impl Args {
             errored!("failed to initialize logger: {}", err);
         }
 
+        // *sigh*... If --files is given, then the first path ends up in
+        // pattern.
+        if raw.flag_files {
+            if !raw.arg_pattern.is_empty() {
+                raw.arg_path.insert(0, raw.arg_pattern.clone());
+            }
+        }
         raw.to_args().map_err(From::from)
     }
 
diff --git a/tests/tests.rs b/tests/tests.rs
index 3f8c3aa8..55cb2684 100644
--- a/tests/tests.rs
+++ b/tests/tests.rs
@@ -744,6 +744,25 @@ fn files() {
     }
 }
 
+// See: https://github.com/BurntSushi/ripgrep/issues/64
+#[test]
+fn regression_64() {
+    let wd = WorkDir::new("regression_64");
+    wd.create_dir("dir");
+    wd.create_dir("foo");
+    wd.create("dir/abc", "");
+    wd.create("foo/abc", "");
+
+    let mut cmd = wd.command();
+    cmd.arg("--files").arg("foo");
+    let lines: String = wd.stdout(&mut cmd);
+    if cfg!(windows) {
+        assert_eq!(lines, "foo\\abc\n");
+    } else {
+        assert_eq!(lines, "foo/abc\n");
+    }
+}
+
 #[test]
 fn type_list() {
     let wd = WorkDir::new("type_list");