commit:     d706b559da30d046df7200c17293ece6d4af6ea2
Author:     Robin H. Johnson <robbat2 <AT> gentoo <DOT> org>
AuthorDate: Wed Feb 25 23:47:54 2015 +0000
Commit:     Robin H. Johnson <robbat2 <AT> gentoo <DOT> org>
CommitDate: Wed Feb 25 23:47:54 2015 +0000
URL:        http://sources.gentoo.org/gitweb/?p=proj/ag.git;a=commit;h=d706b559

Refactor to simplify for a quick bug I hit.

Signed-off-by: Robin H. Johnson <robbat2 <AT> gentoo.org>

---
 ag | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)

diff --git a/ag b/ag
index b3fdb18..f3c106e 100755
--- a/ag
+++ b/ag
@@ -154,12 +154,8 @@ Ag::Utils.proc_count = $options.jobs
 
 def do_full
   abort "Wrong argument type: #{$options.argmode.to_s}" unless 
$options.argmode == :dir
-  begin
-    Ag::Storage.delete_index($options.name) unless $options.readonly
-  rescue Elasticsearch::Transport::Transport::Errors::NotFound => e
-    $stderr.puts "Index did not exist yet. Creating." if $options.debug
-  end
-  Ag::Storage.create_index($options.name)
+  do_delete_index(ignore_missing: true) unless $options.readonly
+  do_create_index(ignore_exists: true)
 
   messages = $maildir.list(:cur)
 
@@ -184,6 +180,7 @@ end
 def do_incremental
   abort "Wrong argument type: #{$options.argmode.to_s}" unless 
$options.argmode == :dir
   messages = $maildir.list(:new)
+  do_create_index(ignore_exists: true)
 
   opts = {
     :in_processes => Ag::Utils.proc_count,
@@ -218,18 +215,24 @@ def do_delete_msg
   end
 end
 
-def do_delete_index
+def do_delete_index(ignore_missing: false)
   begin
     Ag::Storage.delete_index($options.name)
   rescue Elasticsearch::Transport::Transport::Errors::NotFound => e
-    $stderr.puts "Index does not exist: #{e}"
+    $stderr.puts "Index does not exist: #{e}" unless ignore_missing
   rescue => e
     $stderr.puts "Cannot delete index: #{e}"
   end
 end
 
-def do_create_index
-  Ag::Storage.create_index($options.name)
+def do_create_index(ignore_exists: false)
+  begin
+    Ag::Storage.create_index($options.name)
+  rescue Elasticsearch::Transport::Transport::Errors::BadRequest => e
+    if ignore_exists and e !~ /IndexAlreadyExistsException/
+        raise e
+    end
+  end
 end
 
 def do_reindex

Reply via email to