Bug 1388811 - [try fuzzy] Add ability to run non-interactive fuzzy queries, r?armenzg draft
authorAndrew Halberstadt <ahalberstadt@mozilla.com>
Wed, 09 Aug 2017 13:11:47 -0400
changeset 643368 d95a8936cfedc137e9cb721c65ec332cefe4d8cc
parent 643173 4c5fbf49376351679dcc49f4cff26c3c2e055ccc
child 725283 ad6fdff65ee2e0f00e5cb84612a394305fb35df4
push id73075
push userahalberstadt@mozilla.com
push dateWed, 09 Aug 2017 17:18:58 +0000
reviewersarmenzg
bugs1388811
milestone57.0a1
Bug 1388811 - [try fuzzy] Add ability to run non-interactive fuzzy queries, r?armenzg MozReview-Commit-ID: JojBS8pZHSk
tools/tryselect/mach_commands.py
tools/tryselect/selectors/fuzzy.py
--- a/tools/tryselect/mach_commands.py
+++ b/tools/tryselect/mach_commands.py
@@ -65,19 +65,23 @@ class TrySelect(MachCommandBase):
         parser = syntax_parser()
         kwargs = vars(parser.parse_args(args))
         return self._mach_context.commands.dispatch(
             'try', subcommand='syntax', context=self._mach_context, **kwargs)
 
     @SubCommand('try',
                 'fuzzy',
                 description='Select tasks on try using a fuzzy finder')
+    @CommandArgument('-q', '--query', metavar='STR',
+                     help="Use the given query instead of entering the selection "
+                          "interface. Equivalent to typing <query><ctrl-a><enter> "
+                          "from the interface.")
     @CommandArgument('-u', '--update', action='store_true', default=False,
-                     help="Update fzf before running")
-    def try_fuzzy(self, update):
+                     help="Update fzf before running.")
+    def try_fuzzy(self, *args, **kwargs):
         """Select which tasks to use with fzf.
 
         This selector runs all task labels through a fuzzy finding interface.
         All selected task labels and their dependencies will be scheduled on
         try.
 
         Keyboard Shortcuts
         ------------------
@@ -111,17 +115,17 @@ class TrySelect(MachCommandBase):
           !word: exact negation match (line must not contain literal "word")
           'a | 'b: OR operator (joins two exact match operators together)
 
         For example:
 
           ^start 'exact | !ignore fuzzy end$
         """
         from tryselect.selectors.fuzzy import run_fuzzy_try
-        return run_fuzzy_try(update)
+        return run_fuzzy_try(*args, **kwargs)
 
     @SubCommand('try',
                 'syntax',
                 description='Select tasks on try using try syntax',
                 parser=syntax_parser)
     def try_syntax(self, **kwargs):
         """Push the current tree to try, with the specified syntax.
 
--- a/tools/tryselect/selectors/fuzzy.py
+++ b/tools/tryselect/selectors/fuzzy.py
@@ -161,17 +161,17 @@ def fzf_bootstrap(update=False):
 def format_header():
     shortcuts = []
     for action, key in sorted(fzf_header_shortcuts.iteritems()):
         shortcuts.append('{t.white}{action}{t.normal}: {t.yellow}<{key}>{t.normal}'.format(
                          t=terminal, action=action, key=key))
     return FZF_HEADER.format(shortcuts=', '.join(shortcuts), t=terminal)
 
 
-def run_fuzzy_try(update):
+def run_fuzzy_try(update=False, query=None):
     fzf = fzf_bootstrap(update)
 
     if not fzf:
         print(FZF_NOT_FOUND)
         return
 
     vcs = VCSHelper.create()
     vcs.check_working_directory()
@@ -183,16 +183,20 @@ def run_fuzzy_try(update):
         fzf, '-m',
         '--bind', ','.join(key_shortcuts),
         '--header', format_header(),
         # Using python to split the preview string is a bit convoluted,
         # but is guaranteed to be available on all platforms.
         '--preview', 'python -c "print(\\"\\n\\".join(sorted([s.strip(\\"\'\\") for s in \\"{+}\\".split()])))"',  # noqa
         '--preview-window=right:20%',
     ]
+
+    if query:
+        cmd.extend(['-f', query])
+
     proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
     selected = proc.communicate('\n'.join(all_tasks))[0].splitlines()
 
     if not selected:
         print("no tasks selected")
         return
 
     return vcs.push_to_try("Pushed via 'mach try fuzzy', see diff for scheduled tasks", selected)