aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/shims/scm/git
blob: bfb779c2593442254a916662f013ed414cdbbc90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/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.

set +o posix

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

absdir() {
  quiet_safe_cd "${1%/*}/" && pwd -P
}

dirbasepath() {
  local dir="$1"
  local base="${2##*/}"
  echo "$dir/$base"
}

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

  dir="$(absdir "$path")"
  path="$(dirbasepath "$dir" "$path")"

  while [[ -L "$path" ]]
  do
    dest="$(readlink "$path")"
    if [[ "$dest" = "/"* ]]
    then
      path="$dest"
    else
      path="$dir/$dest"
    fi
    dir="$(absdir "$path")"
    path="$(dirbasepath "$dir" "$path")"
  done

  echo "$path"
}

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
  if [[ "$(lowercase "$arg0")" = "$SCM_FILE" || "$(realpath "$arg0")" = "$SCM_REAL" ]]
  then
    return
  fi
  if [[ "$HOMEBREW" = "print-path" ]]
  then
    local dir="$(quiet_safe_cd "${arg0%/*}/" && pwd)"
    local path="$(dirbasepath "$dir" "$arg0")"
    echo "$path"
    exit
  fi
  exec "$@"
}

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

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

case "$(lowercase "$SCM_FILE")" in
  git)
    [[ -n "$HOMEBREW_GIT" ]] && safe_exec "$(which "$HOMEBREW_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
unset IFS

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 "/"
  xcode_path="$(/usr/bin/xcode-select -print-path 2>/dev/null)"
  if [[ -z "$xcode_path" ]]
  then
    if [[ -z "$HOMEBREW_MACOS_VERSION" ]]
    then
      HOMEBREW_MACOS_VERSION="$(/usr/bin/sw_vers -productVersion)"
    fi
    printf -v macos_version_numeric "%02d%02d%02d" ${HOMEBREW_MACOS_VERSION//./ }
    if [[ "$macos_version_numeric" -ge "100900" ]]
    then
      popup_stub=1
    fi
  fi
  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/$SCM_FILE"
safe_exec "$path" "$@"

path="/usr/bin/$SCM_FILE"
[[ -z "$popup_stub" ]] && safe_exec "$path" "$@"

echo "You must: brew install $SCM_FILE" >&2
exit 1