Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENV/scm/git: rewrite in Bash #496

Merged
merged 1 commit into from
Jul 12, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 105 additions & 56 deletions Library/ENV/scm/git
Original file line number Diff line number Diff line change
@@ -1,71 +1,120 @@
#!/bin/sh
# Make sure this shim uses the same Ruby interpreter that is used by Homebrew.
unset RUBYLIB
unset RUBYOPT
if [ -z "$HOMEBREW_RUBY_PATH" ]
then
echo "${0##*/}: The build tool has reset ENV; --env=std required." >&2
exit 1
fi
exec "$HOMEBREW_RUBY_PATH" -x "$0" "$@"
#!/usr/bin/env ruby -W0
#!/bin/bash

# This script because we support $GIT, $HOMEBREW_SVN, etc., Xcode-only and
# no Xcode/CLT configurations. Order is careful to be what the user would want.

require "pathname"
set +o posix

quiet_safe_cd() {
cd "$1" >/dev/null || { echo "Error: failed to cd to $1" >&2; exit 1; }
}

realpath() {
local path="$1"
local dir
local base
local dest

dir="$(quiet_safe_cd "${path%/*}/" && pwd -P)"
base="${path##*/}"
path="$dir/$base"

SELF_REAL = Pathname.new(__FILE__).realpath
F = File.basename(__FILE__).freeze
D = File.expand_path(File.dirname(__FILE__)).freeze
while [[ -L "$path" ]]
do
dest="$(readlink "$path")"
if [[ "$dest" = "/"* ]]
then
path="$dest"
else
path="$dir/$dest"
fi
dir="$(quiet_safe_cd "${path%/*}/" && pwd -P)"
base="${path##*/}"
path="$dir/$base"
done

def executable?(file)
File.file?(file) && File.executable?(file)
end
echo "$path"
}

def exec(*args)
executable() {
local file="$1"
[[ -f "$file" && -x "$file" ]]
}

lowercase() {
echo "$1" | tr '[:upper:]' '[:lower:]'
}

safe_exec() {
local arg0="$1"
if ! executable "$arg0"
then
return
fi
# prevent fork-bombs
arg0 = args.first
return if arg0 =~ /^#{F}/i || Pathname.new(arg0).realpath == SELF_REAL
if ARGV == %w[--homebrew=print-path]
puts arg0
if [[ "$(lowercase "$arg0")" = "$SCM_FILE" || "$(realpath "$arg0")" = "$SCM_REAL" ]]
then
return
fi
if [[ "$HOMEBREW" = "print-path" ]]
then
echo "$arg0"
exit
end
super
end

case F.downcase
when "git" then %W[HOMEBREW_GIT GIT]
when "svn" then %W[HOMEBREW_SVN]
else []
end.each do |key|
exec ENV[key], *ARGV if ENV[key] && executable?(ENV[key])
end

brew_version = File.expand_path("#{D}/../../../bin/#{F}")
exec brew_version, *ARGV if executable? brew_version

`/usr/bin/which -a #{F} 2>/dev/null`.split("\n").each do |path|
exec path, *ARGV unless path == "/usr/bin/#{F}"
end

popup_stub = false
if executable? "/usr/bin/xcode-select"
fi
exec "$@"
}

SCM_FILE="${0##*/}"
SCM_DIR="$(quiet_safe_cd "${0%/*}/" && pwd -P)"
SCM_REAL="$(realpath "$0")"

if [[ "$1" = --homebrew=* ]]
then
HOMEBREW="${1:11}"
shift
fi

case "$(lowercase "$SCM_FILE")" in
git)
[[ -n "$HOMEBREW_GIT" ]] && safe_exec "$(which "$HOMEBREW_GIT")" "$@"
[[ -n "$GIT" ]] && safe_exec "$(which "$GIT")" "$@"
;;
svn)
[[ -n "$HOMEBREW_SVN" ]] && safe_exec "$(which "$HOMEBREW_SVN")" "$@"
;;
esac

brew_version="$(quiet_safe_cd "$SCM_DIR/../../../bin" && pwd -P)/$SCM_FILE"
safe_exec "$brew_version" "$@"

IFS=$'\n'
for path in $(/usr/bin/which -a "$SCM_FILE" 2>/dev/null)
do
if [[ "$path" != "/usr/bin/$SCM_FILE" ]]
then
safe_exec "$path" "$@"
fi
done

if executable "/usr/bin/xcode-select"
then
# xcode-select will return empty on no Xcode/CLT configuration.
# /usr/bin/<tool> will be a popup stub under such configuration.
# xcrun hangs if xcode-select is set to "/"
path = `/usr/bin/xcode-select -print-path 2>/dev/null`.chomp
popup_stub = path.empty?
if !popup_stub && path != "/"
path = `/usr/bin/xcrun -find #{F} 2>/dev/null`.chomp
exec path, *ARGV if executable? path
end
end
xcode_path="$(/usr/bin/xcode-select -print-path 2>/dev/null)"
[[ -n "$xcode_path" ]] || popup_stub=1
if [[ -z "$popup_stub" && "$xcode_path" != "/" ]]
then
path="$(/usr/bin/xcrun -find "$SCM_FILE" 2>/dev/null)"
safe_exec "$path" "$@"
fi
fi

path = "/Applications/Xcode.app/Contents/Developer/usr/bin/#{F}"
exec path, *ARGV if executable? path
path="/Applications/Xcode.app/Contents/Developer/usr/bin/$SCM_FILE"
safe_exec "$path" "$@"

path = "/usr/bin/#{F}"
exec path, *ARGV if !popup_stub && executable?(path)
path="/usr/bin/$SCM_FILE"
[[ -z "$popup_stub" ]] && safe_exec "$path" "$@"

abort "You must: brew install #{F}"
echo "You must: brew install $SCM_FILE" >&2
exit 1