[前][次][番号順一覧][スレッド一覧]

ruby-changes:74293

From: Nobuyoshi <ko1@a...>
Date: Sun, 30 Oct 2022 17:06:52 +0900 (JST)
Subject: [ruby-changes:74293] b64514f132 (master): vcs.rb: prettify debug print

https://git.ruby-lang.org/ruby.git/commit/?id=b64514f132

From b64514f132615c00982d008bb2d7d2d1230d4f1c Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Sun, 30 Oct 2022 12:39:43 +0900
Subject: vcs.rb: prettify debug print

---
 tool/lib/vcs.rb | 37 +++++++++++++++++++++++++++----------
 1 file changed, 27 insertions(+), 10 deletions(-)

diff --git a/tool/lib/vcs.rb b/tool/lib/vcs.rb
index 3b60385e59..0014b67c28 100644
--- a/tool/lib/vcs.rb
+++ b/tool/lib/vcs.rb
@@ -1,6 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/tool/lib/vcs.rb#L1
 # vcs
 require 'fileutils'
 require 'optparse'
+require 'pp'
 
 # This library is used by several other tools/ scripts to detect the current
 # VCS in use (e.g. SVN, Git) or to interact with that VCS.
@@ -9,6 +10,22 @@ ENV.delete('PWD') https://github.com/ruby/ruby/blob/trunk/tool/lib/vcs.rb#L10
 
 class VCS
   DEBUG_OUT = STDERR.dup
+
+  def self.dump(obj, pre = nil)
+    out = DEBUG_OUT
+    @pp ||= PP.new(out)
+    @pp.guard_inspect_key do
+      if pre
+        @pp.group(pre.size, pre) {
+          obj.pretty_print(@pp)
+        }
+      else
+        obj.pretty_print(@pp)
+      end
+      @pp.flush
+      out << "\n"
+    end
+  end
 end
 
 unless File.respond_to? :realpath
@@ -19,14 +36,14 @@ unless File.respond_to? :realpath https://github.com/ruby/ruby/blob/trunk/tool/lib/vcs.rb#L36
 end
 
 def IO.pread(*args)
-  VCS::DEBUG_OUT.puts(args.inspect) if $DEBUG
+  VCS.dump(args, "args: ") if $DEBUG
   popen(*args) {|f|f.read}
 end
 
 module DebugPOpen
   refine IO.singleton_class do
     def popen(*args)
-      VCS::DEBUG_OUT.puts args.inspect if $DEBUG
+      VCS.dump(args, "args: ") if $DEBUG
       super
     end
   end
@@ -34,7 +51,7 @@ end https://github.com/ruby/ruby/blob/trunk/tool/lib/vcs.rb#L51
 using DebugPOpen
 module DebugSystem
   def system(*args)
-    VCS::DEBUG_OUT.puts args.inspect if $DEBUG
+    VCS.dump(args, "args: ") if $DEBUG
     exception = false
     opts = Hash.try_convert(args[-1])
     if RUBY_VERSION >= "2.6"
@@ -394,7 +411,7 @@ class VCS https://github.com/ruby/ruby/blob/trunk/tool/lib/vcs.rb#L411
     def commit
       args = %W"#{COMMAND} commit"
       if dryrun?
-        VCS::DEBUG_OUT.puts(args.inspect)
+        VCS.dump(args, "commit: ")
         return true
       end
       system(*args)
@@ -411,7 +428,7 @@ class VCS https://github.com/ruby/ruby/blob/trunk/tool/lib/vcs.rb#L428
       if srcdir
         opts[:chdir] ||= srcdir
       end
-      VCS::DEBUG_OUT.puts cmds.inspect if debug?
+      VCS.dump(cmds, "cmds: ") if debug? and !$DEBUG
       cmds
     end
 
@@ -421,7 +438,7 @@ class VCS https://github.com/ruby/ruby/blob/trunk/tool/lib/vcs.rb#L438
 
     def cmd_read_at(srcdir, cmds)
       result = without_gitconfig { IO.pread(*cmd_args(cmds, srcdir)) }
-      VCS::DEBUG_OUT.puts result.inspect if debug?
+      VCS.dump(result, "result: ") if debug?
       result
     end
 
@@ -516,7 +533,7 @@ class VCS https://github.com/ruby/ruby/blob/trunk/tool/lib/vcs.rb#L533
     def initialize(*)
       super
       @srcdir = File.realpath(@srcdir)
-      VCS::DEBUG_OUT.puts @srcdir.inspect if debug?
+      VCS.dump(@srcdir, "srcdir: ") if debug?
       self
     end
 
@@ -721,13 +738,13 @@ class VCS https://github.com/ruby/ruby/blob/trunk/tool/lib/vcs.rb#L738
 
     def commit(opts = {})
       args = [COMMAND, "push"]
-      args << "-n" if dryrun
+      args << "-n" if dryrun?
       remote, branch = upstream
       args << remote
       branches = %W[refs/notes/commits:refs/notes/commits HEAD:#{branch}]
       if dryrun?
         branches.each do |b|
-          VCS::DEBUG_OUT.puts((args + [b]).inspect)
+          VCS.dump(args + [b], "commit: ")
         end
         return true
       end
@@ -757,7 +774,7 @@ class VCS https://github.com/ruby/ruby/blob/trunk/tool/lib/vcs.rb#L774
       commits.each_with_index do |l, i|
         r, a, c = l.split(' ')
         dcommit = [COMMAND, "svn", "dcommit"]
-        dcommit.insert(-2, "-n") if dryrun
+        dcommit.insert(-2, "-n") if dryrun?
         dcommit << "--add-author-from" unless a == c
         dcommit << r
         system(*dcommit) or return false
-- 
cgit v1.2.3


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]