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

ruby-changes:74292

From: Nobuyoshi <ko1@a...>
Date: Sun, 30 Oct 2022 17:06:52 +0900 (JST)
Subject: [ruby-changes:74292] 00d5b7ce7c (master): vcs.rb: copy safe directory configuration

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

From 00d5b7ce7c2e72170b7f563b9de0e7ac4bc8f772 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Sun, 30 Oct 2022 15:05:34 +0900
Subject: vcs.rb: copy safe directory configuration

Now revision.tmp will be regenerated always and every times, even if
the recent file exists in the source directory, as far as using git.
On the other hand, VirtualBox mounts shared folders as root, and git
rejects the repository there as dubious ownership.
---
 tool/lib/vcs.rb | 47 ++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 42 insertions(+), 5 deletions(-)

diff --git a/tool/lib/vcs.rb b/tool/lib/vcs.rb
index 0014b67c28..d96caddbf2 100644
--- a/tool/lib/vcs.rb
+++ b/tool/lib/vcs.rb
@@ -2,6 +2,7 @@ https://github.com/ruby/ruby/blob/trunk/tool/lib/vcs.rb#L2
 require 'fileutils'
 require 'optparse'
 require 'pp'
+require 'tempfile'
 
 # 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.
@@ -419,8 +420,21 @@ class VCS https://github.com/ruby/ruby/blob/trunk/tool/lib/vcs.rb#L420
   end
 
   class GIT < self
-    register(".git") { |path, dir| File.exist?(File.join(path, dir)) }
-    COMMAND = ENV["GIT"] || 'git'
+    register(".git") do |path, dir|
+      SAFE_DIRECTORIES ||=
+        begin
+          command = ENV["GIT"] || 'git'
+          IO.popen(%W"#{command} config --global --get-all safe.directory", &:read).split("\n")
+        rescue
+          command = nil
+          []
+        ensure
+          VCS.dump(SAFE_DIRECTORIES, "safe.directory: ") if $DEBUG
+          COMMAND = command
+        end
+
+      COMMAND and File.exist?(File.join(path, dir))
+    end
 
     def cmd_args(cmds, srcdir = nil)
       (opts = cmds.last).kind_of?(Hash) or cmds << (opts = {})
@@ -459,7 +473,14 @@ class VCS https://github.com/ruby/ruby/blob/trunk/tool/lib/vcs.rb#L473
     def _get_revisions(path, srcdir = nil)
       ref = Branch === path ? path.to_str : 'HEAD'
       gitcmd = [COMMAND]
-      last = cmd_read_at(srcdir, [[*gitcmd, 'rev-parse', ref]]).rstrip
+      last = nil
+      IO.pipe do |r, w|
+        last = cmd_read_at(srcdir, [[*gitcmd, 'rev-parse', ref, err: w]]).rstrip
+        w.close
+        unless r.eof?
+          raise "#{COMMAND} rev-parse failed\n#{r.read.gsub(/^(?=\s*\S)/, '  ')}"
+        end
+      end
       log = cmd_read_at(srcdir, [[*gitcmd, 'log', '-n1', '--date=iso', '--pretty=fuller', *path]])
       changed = log[/\Acommit (\h+)/, 1]
       modified = log[/^CommitDate:\s+(.*)/, 1]
@@ -521,18 +542,34 @@ class VCS https://github.com/ruby/ruby/blob/trunk/tool/lib/vcs.rb#L542
     end
 
     def without_gitconfig
-      envs = %w'HOME XDG_CONFIG_HOME GIT_SYSTEM_CONFIG GIT_CONFIG_SYSTEM'.each_with_object({}) do |v, h|
+      envs = (%w'HOME XDG_CONFIG_HOME' + ENV.keys.grep(/\AGIT_/)).each_with_object({}) do |v, h|
         h[v] = ENV.delete(v)
-        ENV[v] = NullDevice if v.start_with?('GIT_')
       end
+      ENV['GIT_CONFIG_SYSTEM'] = NullDevice
+      ENV['GIT_CONFIG_GLOBAL'] = global_config
       yield
     ensure
       ENV.update(envs)
     end
 
+    def global_config
+      return NullDevice if SAFE_DIRECTORIES.empty?
+      unless @gitconfig
+        @gitconfig = Tempfile.new(%w"vcs_ .gitconfig")
+        @gitconfig.close
+        ENV['GIT_CONFIG_GLOBAL'] = @gitconfig.path
+        SAFE_DIRECTORIES.each do |dir|
+          system(*%W[#{COMMAND} config --global --add safe.directory #{dir}])
+        end
+        VCS.dump(`#{COMMAND} config --global --get-all safe.directory`, "safe.directory: ") if debug?
+      end
+      @gitconfig.path
+    end
+
     def initialize(*)
       super
       @srcdir = File.realpath(@srcdir)
+      @gitconfig = nil
       VCS.dump(@srcdir, "srcdir: ") if debug?
       self
     end
-- 
cgit v1.2.3


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

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