Commits

Michele Bini committed 87dcd38

Add option --svg-viewer

  • Participants
  • Parent commits ec38b98

Comments (0)

Files changed (1)

   end
 end
 
+class String
+  def shellsafe(s)
+    return !s.match(/[^-+_a-zA-Z0-9]/)
+  end
+end
+
 class Options < ClassyCLI
   def _h; self.__help; end
   def __help; self.__usage; end
     puts "Display directed graph of Git commits"
     puts
     puts "Options:"
-    puts "  --all          Include all local branches"
-    puts "  --remote       Include remote branches"
-    puts "  --max-depth N  Show branch history up to N commits deep"
-    puts "  --graphviz     Raw graphviz output, no display via eog"
-    # puts "  --svg       SVG output"
-    puts "  -v, --verbose  Verbose operations"
-    puts "  -h, --help     Display this help screen"
+    puts "--all           Include all local branches"
+    puts "--remote        Include remote branches"
+    puts "--max-depth N   Show branch history up to N commits deep"
+    puts "--graphviz      Raw graphviz output, no display via eog"
+    puts "--svg           SVG output"
+    puts "--svg-viewer V  Show graph using SVG viewer V"
+    puts "-v, --verbose   Verbose operations"
+    puts "-h, --help      Display this help screen"
     exit
   end
   def _v; self.__verbose; end
   def __graphviz
     $raw_graphviz = true
   end
+  $svg_viewer = "eog"
+  def __svg_viewer(v)
+    abort "Unsafe characters in program name: #{v}"  unless v.shellsafe
+    $svg_viewer = v
+  end
+  def __svg(d)
+    abort "Unsafe characters in file name: #{d}"     unless d.shellsafe
+    $svg_output = d
+  end
   def __max_depth(d)
     $max_depth = d.to_i
   end
 
 @head = g.gcommit('HEAD').sha
 
-unless $raw_graphviz
-  pipe = IO.popen("(dot -T svg >/tmp/git-graph.svg && eog /tmp/git-graph.svg; rm /tmp/git-graph.svg)", "w")
+if $raw_graphviz then
+elsif $svg_output.is_a?(String)
+  if $svg_output == "-" then
+    pipe = IO.popen("dot -T svg", "w")
+  else
+    pipe = IO.popen("dot -T svg >$svg_output", "w")
+  end
+  $stdout.reopen(pipe)
+else
+  pipe = IO.popen("(dot -T svg >/tmp/git-graph.svg && " + $svg_viewer + " /tmp/git-graph.svg; rm /tmp/git-graph.svg)", "w")
   $stdout.reopen(pipe)
 end