From e6e0d1128c5a10d485852963fc6e1381aaa9baf0 Mon Sep 17 00:00:00 2001
From: hyuraku <32809703+hyuraku@users.noreply.github.com>
Date: Tue, 3 Dec 2019 00:45:11 +0900
Subject: [PATCH] create new envoronments

---
 Library/Homebrew/brew.sh       | 15 +++++++++++++++
 Library/Homebrew/cmd/update.sh | 34 ++++++++++++++++++++++------------
 Library/Homebrew/diagnostic.rb |  5 +++--
 Library/Homebrew/global.rb     |  3 +++
 4 files changed, 43 insertions(+), 14 deletions(-)

diff --git a/Library/Homebrew/brew.sh b/Library/Homebrew/brew.sh
index aaaa934589..67dbe322be 100644
--- a/Library/Homebrew/brew.sh
+++ b/Library/Homebrew/brew.sh
@@ -251,6 +251,8 @@ export HOMEBREW_MACOS_VERSION_NUMERIC
 export HOMEBREW_USER_AGENT
 export HOMEBREW_USER_AGENT_CURL
 export HOMEBREW_BOTTLE_DEFAULT_DOMAIN
+export HOMEBREW_BREW_REMOTE
+export HOMEBREW_DEFAULT_CORE_REMOTE
 
 if [[ -n "$HOMEBREW_MACOS" && -x "/usr/bin/xcode-select" ]]
 then
@@ -374,6 +376,19 @@ then
   export HOMEBREW_BOTTLE_DOMAIN="$HOMEBREW_BOTTLE_DEFAULT_DOMAIN"
 fi
 
+HOMEBREW_DEFAULT_BREW_REMOTE="https://github.com/Homebrew/brew.git"
+if [[ -z "$HOMEBREW_BREW_REMOTE" ]]
+then
+  HOMEBREW_BREW_REMOTE="$HOMEBREW_DEFAULT_BREW_REMOTE"
+fi
+
+if [[ -n "$HOMEBREW_MACOS" ]] || [[ -n "$HOMEBREW_FORCE_HOMEBREW_ON_LINUX" ]]
+then
+  HOMEBREW_DEFAULT_CORE_REMOTE="https://github.com/Homebrew/homebrew-core"
+else
+  HOMEBREW_DEFAULT_CORE_REMOTE="https://github.com/Homebrew/linuxbrew-core"
+fi
+
 if [[ -f "$HOMEBREW_LIBRARY/Homebrew/cmd/$HOMEBREW_COMMAND.sh" ]]
 then
   HOMEBREW_BASH_COMMAND="$HOMEBREW_LIBRARY/Homebrew/cmd/$HOMEBREW_COMMAND.sh"
diff --git a/Library/Homebrew/cmd/update.sh b/Library/Homebrew/cmd/update.sh
index 483f770080..4d50c0300c 100644
--- a/Library/Homebrew/cmd/update.sh
+++ b/Library/Homebrew/cmd/update.sh
@@ -23,14 +23,6 @@ git() {
 }
 
 git_init_if_necessary() {
-  BREW_OFFICIAL_REMOTE="https://github.com/Homebrew/brew"
-  if [[ -n "$HOMEBREW_MACOS" ]] || [[ -n "$HOMEBREW_FORCE_HOMEBREW_ON_LINUX" ]]
-  then
-    CORE_OFFICIAL_REMOTE="https://github.com/Homebrew/homebrew-core"
-  else
-    CORE_OFFICIAL_REMOTE="https://github.com/Homebrew/linuxbrew-core"
-  fi
-
   safe_cd "$HOMEBREW_REPOSITORY"
   if [[ ! -d ".git" ]]
   then
@@ -38,7 +30,7 @@ git_init_if_necessary() {
     trap '{ rm -rf .git; exit 1; }' EXIT
     git init
     git config --bool core.autocrlf false
-    git config remote.origin.url "$BREW_OFFICIAL_REMOTE"
+    git config remote.origin.url "$HOMEBREW_BREW_REMOTE"
     git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
     latest_tag="$(git ls-remote --tags --refs -q origin | tail -n1 | cut -f2)"
     git fetch --force origin --shallow-since="$latest_tag"
@@ -56,7 +48,7 @@ git_init_if_necessary() {
     trap '{ rm -rf .git; exit 1; }' EXIT
     git init
     git config --bool core.autocrlf false
-    git config remote.origin.url "$CORE_OFFICIAL_REMOTE"
+    git config remote.origin.url "$HOMEBREW_CORE_REMOTE"
     git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
     git fetch --force --depth=1 origin refs/heads/master:refs/remotes/origin/master
     git reset --hard origin/master
@@ -402,6 +394,24 @@ EOS
 
   git_init_if_necessary
 
+  if [[ "$HOMEBREW_DEFAULT_BREW_REMOTE" != "$HOMEBREW_BREW_REMOTE" ]]
+  then
+    safe_cd "$HOMEBREW_REPOSITORY"
+    git remote set-url origin "$HOMEBREW_BREW_REMOTE"
+    git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
+    latest_tag="$(git ls-remote --tags --refs -q origin | tail -n1 | cut -f2)"
+    git fetch --force origin --shallow-since="$latest_tag"
+  fi
+
+  if [[ "$HOMEBREW_DEFAULT_CORE_REMOTE" != "$HOMEBREW_CORE_REMOTE" ]] && 
+     [[ -d "$HOMEBREW_LIBRARY/Taps/homebrew/homebrew-core" ]]
+  then
+    safe_cd "$HOMEBREW_LIBRARY/Taps/homebrew/homebrew-core"
+    git remote set-url origin "$HOMEBREW_CORE_REMOTE"
+    git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
+    git fetch --force --depth=1 origin refs/heads/master:refs/remotes/origin/master
+  fi
+
   safe_cd "$HOMEBREW_REPOSITORY"
 
   # if an older system had a newer curl installed, change each repo's remote URL from GIT to HTTPS
@@ -409,8 +419,8 @@ EOS
         -x "$HOMEBREW_PREFIX/opt/curl/bin/curl" &&
         "$(git config remote.origin.url)" =~ ^git:// ]]
   then
-    git config remote.origin.url "$BREW_OFFICIAL_REMOTE"
-    git config -f "$HOMEBREW_LIBRARY/Taps/homebrew/homebrew-core/.git/config" remote.origin.url "$CORE_OFFICIAL_REMOTE"
+    git config remote.origin.url "$HOMEBREW_BREW_REMOTE"
+    git config -f "$HOMEBREW_LIBRARY/Taps/homebrew/homebrew-core/.git/config" remote.origin.url "$HOMEBREW_CORE_REMOTE"
   fi
 
   # kill all of subprocess on interrupt
diff --git a/Library/Homebrew/diagnostic.rb b/Library/Homebrew/diagnostic.rb
index 77247e746c..d6ccf2f4f4 100644
--- a/Library/Homebrew/diagnostic.rb
+++ b/Library/Homebrew/diagnostic.rb
@@ -113,7 +113,7 @@ module Homebrew
         return if !Utils.git_available? || !repository_path.git?
 
         current_origin = repository_path.git_origin
-
+        user_origins = [HOMEBREW_BREW_REMOTE, HOMEBREW_CORE_REMOTE]
         if current_origin.nil?
           <<~EOS
             Missing #{desired_origin} git origin remote.
@@ -122,7 +122,8 @@ module Homebrew
             properly. You can solve this by adding the remote:
               git -C "#{repository_path}" remote add origin #{Formatter.url("https://github.com/#{desired_origin}.git")}
           EOS
-        elsif !current_origin.match?(%r{#{desired_origin}(\.git|/)?$}i)
+        elsif !current_origin.match?(%r{#{desired_origin}(\.git|/)?$}i) && \
+              !user_origins.include?(current_origin)
           <<~EOS
             Suspicious #{desired_origin} git origin remote found.
             The current git origin is:
diff --git a/Library/Homebrew/global.rb b/Library/Homebrew/global.rb
index e4e68fbdb6..c016193b09 100644
--- a/Library/Homebrew/global.rb
+++ b/Library/Homebrew/global.rb
@@ -53,6 +53,9 @@ HOMEBREW_USER_AGENT_FAKE_SAFARI =
 HOMEBREW_BOTTLE_DEFAULT_DOMAIN = ENV["HOMEBREW_BOTTLE_DEFAULT_DOMAIN"]
 HOMEBREW_BOTTLE_DOMAIN = ENV["HOMEBREW_BOTTLE_DOMAIN"]
 
+HOMEBREW_BREW_REMOTE = ENV["HOMEBREW_BREW_REMOTE"]
+HOMEBREW_CORE_REMOTE = ENV["HOMEBREW_CORE_REMOTE"]
+
 HOMEBREW_DEFAULT_PREFIX = "/usr/local"
 LINUXBREW_DEFAULT_PREFIX = "/home/linuxbrew/.linuxbrew"
 
-- 
GitLab