Advertisement
Guest User

Untitled

a guest
Mar 30th, 2016
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.72 KB | None | 0 0
  1. brew() {
  2.   "$HOMEBREW_BREW_FILE" "$@"
  3. }
  4.  
  5. git() {
  6.   "$HOMEBREW_LIBRARY/ENV/scm/git" "$@"
  7. }
  8.  
  9. git_init_if_necessary() {
  10.   if [[ ! -d ".git" ]]
  11.   then
  12.     set -e
  13.     trap '{ rm -rf .git; exit 1; }' EXIT
  14.     git init
  15.     git config --bool core.autocrlf false
  16.     git config remote.origin.url https://github.com/Homebrew/homebrew.git
  17.     git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
  18.     git fetch --force --depth=1 origin refs/heads/master:refs/remotes/origin/master
  19.     git reset --hard origin/master
  20.     SKIP_FETCH_HOMEBREW_REPOSITORY=1
  21.     set +e
  22.     trap - EXIT
  23.     return
  24.   fi
  25.  
  26.   if [[ "$(git remote show origin -n)" = *"mxcl/homebrew"* ]]
  27.   then
  28.     git remote set-url origin https://github.com/Homebrew/homebrew.git &&
  29.     git remote set-url --delete origin ".*mxcl\/homebrew.*"
  30.   fi
  31. }
  32.  
  33. rename_taps_dir_if_necessary() {
  34.   local tap_dir
  35.   local tap_dir_basename
  36.   local user
  37.   local repo
  38.  
  39.   for tap_dir in "$HOMEBREW_LIBRARY"/Taps/*
  40.   do
  41.     [[ -d "$tap_dir/.git" ]] || continue
  42.     tap_dir_basename="${tap_dir##*/}"
  43.     if [[ "$tap_dir_basename" = *"-"* ]]
  44.     then
  45.       # only replace the *last* dash: yes, tap filenames suck
  46.       user="$(echo "${tap_dir_basename%-*}" | tr "[:upper:]" "[:lower:]")"
  47.       repo="$(echo "${tap_dir_basename:${#user}+1}" | tr "[:upper:]" "[:lower:]")"
  48.       mkdir -p "$HOMEBREW_LIBRARY/Taps/$user"
  49.       mv "$tap_dir", "$HOMEBREW_LIBRARY/Taps/$user/homebrew-$repo"
  50.  
  51.       if [[ ${#${tap_dir_basename//[^\-]}} -gt 1 ]]
  52.       then
  53.         echo "Homebrew changed the structure of Taps like <someuser>/<sometap>." >&2
  54.         echo "So you may need to rename $HOMEBREW_LIBRARY/Taps/$user/homebrew-$repo manually." >&2
  55.       fi
  56.     else
  57.       echo "Homebrew changed the structure of Taps like <someuser>/<sometap>. " >&2
  58.       echo "$tap_dir is an incorrect Tap path." >&2
  59.       echo "So you may need to rename it to $HOMEBREW_LIBRARY/Taps/<someuser>/homebrew-<sometap> manually." >&2
  60.     fi
  61.   done
  62. }
  63.  
  64. repo_var() {
  65.   local repo_var
  66.  
  67.   repo_var="$1"
  68.   if [[ "$repo_var" = "$HOMEBREW_REPOSITORY" ]]
  69.   then
  70.     repo_var=""
  71.   else
  72.     repo_var="${repo_var#"$HOMEBREW_LIBRARY/Taps"}"
  73.     repo_var="$(echo -n "$repo_var" | tr -C "A-Za-z0-9" "_" | tr "[:lower:]" "[:upper:]")"
  74.   fi
  75.   echo "$repo_var"
  76. }
  77.  
  78. upstream_branch() {
  79.   local upstream_branch
  80.  
  81.   upstream_branch="$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null)"
  82.   upstream_branch="${upstream_branch#refs/remotes/origin/}"
  83.   [[ -z "$upstream_branch" ]] && upstream_branch="master"
  84.   echo "$upstream_branch"
  85. }
  86.  
  87. read_current_revision() {
  88.   git rev-parse -q --verify HEAD
  89. }
  90.  
  91. pop_stash() {
  92.   [[ -z "$STASHED" ]] && return
  93.   git stash pop "${QUIET_ARGS[@]}"
  94.   if [[ -n "$HOMEBREW_VERBOSE" ]]
  95.   then
  96.     echo "Restoring your stashed changes to $DIR:"
  97.     git status --short --untracked-files
  98.   fi
  99.   unset STASHED
  100. }
  101.  
  102. pop_stash_message() {
  103.   [[ -z "$STASHED" ]] && return
  104.   echo "To restore the stashed changes to $DIR run:"
  105.   echo "  'cd $DIR && git stash pop'"
  106.   unset STASHED
  107. }
  108.  
  109. reset_on_interrupt() {
  110.   if [[ "$INITIAL_BRANCH" != "$UPSTREAM_BRANCH" && -n "$INITIAL_BRANCH" ]]
  111.   then
  112.     git checkout "$INITIAL_BRANCH"
  113.   fi
  114.  
  115.   if [[ -n "$INITIAL_REVISION" ]]
  116.   then
  117.     git reset --hard "$INITIAL_REVISION" "${QUIET_ARGS[@]}"
  118.   fi
  119.  
  120.   if [[ "$INITIAL_BRANCH" != "$UPSTREAM_BRANCH" && -n "$INITIAL_BRANCH" ]]
  121.   then
  122.     pop_stash
  123.   else
  124.     pop_stash_message
  125.   fi
  126.  
  127.   exit 130
  128. }
  129.  
  130. pull() {
  131.   local DIR
  132.   local TAP_VAR
  133.  
  134.   DIR="$1"
  135.   cd "$DIR" || return
  136.   TAP_VAR=$(repo_var "$DIR")
  137.   unset STASHED
  138.  
  139.   # The upstream repository's default branch may not be master;
  140.   # check refs/remotes/origin/HEAD to see what the default
  141.   # origin branch name is, and use that. If not set, fall back to "master".
  142.   INITIAL_BRANCH="$(git symbolic-ref --short HEAD 2>/dev/null)"
  143.   UPSTREAM_BRANCH="$(upstream_branch)"
  144.  
  145.   # Used for testing purposes, e.g., for testing formula migration after
  146.   # renaming it in the currently checked-out branch. To test run
  147.   # "brew update --simulate-from-current-branch"
  148.   if [[ -n "$HOMEBREW_SIMULATE_FROM_CURRENT_BRANCH" ]]
  149.   then
  150.     INITIAL_REVISION="$(git rev-parse -q --verify "$UPSTREAM_BRANCH")"
  151.     CURRENT_REVISION="$(read_current_revision)"
  152.     export HOMEBREW_UPDATE_BEFORE"$TAP_VAR"="$INITIAL_REVISION"
  153.     export HOMEBREW_UPDATE_AFTER"$TAP_VAR"="$CURRENT_REVISION"
  154.     if ! git merge-base --is-ancestor "$INITIAL_REVISION" "$CURRENT_REVISION"
  155.     then
  156.       odie "Your $DIR HEAD is not a descendant of $UPSTREAM_BRANCH!"
  157.     fi
  158.     return
  159.   fi
  160.  
  161.   trap reset_on_interrupt SIGINT
  162.  
  163.   if [[ -n "$(git status --untracked-files=all --porcelain 2>/dev/null)" ]]
  164.   then
  165.     if [[ -n "$HOMEBREW_VERBOSE" ]]
  166.     then
  167.       echo "Stashing uncommitted changes to $DIR."
  168.       git status --short --untracked-files=all
  169.     fi
  170.     git merge --abort &>/dev/null
  171.     git -c "user.email=brew-update@localhost" \
  172.         -c "user.name=brew update" \
  173.         stash save --include-untracked "${QUIET_ARGS[@]}"
  174.     git reset --hard "${QUIET_ARGS[@]}"
  175.     STASHED="1"
  176.   fi
  177.  
  178.   if [[ "$INITIAL_BRANCH" != "$UPSTREAM_BRANCH" && -n "$INITIAL_BRANCH" ]]
  179.   then
  180.     # Recreate and check out `#{upstream_branch}` if unable to fast-forward
  181.     # it to `origin/#{@upstream_branch}`. Otherwise, just check it out.
  182.     if git merge-base --is-ancestor "$UPSTREAM_BRANCH" "origin/$UPSTREAM_BRANCH" &>/dev/null
  183.     then
  184.       git checkout --force "$UPSTREAM_BRANCH" "${QUIET_ARGS[@]}"
  185.     else
  186.       git checkout --force -B "$UPSTREAM_BRANCH" "origin/$UPSTREAM_BRANCH" "${QUIET_ARGS[@]}"
  187.     fi
  188.   fi
  189.  
  190.   INITIAL_REVISION="$(read_current_revision)"
  191.   export HOMEBREW_UPDATE_BEFORE"$TAP_VAR"="$INITIAL_REVISION"
  192.  
  193.   # ensure we don't munge line endings on checkout
  194.   git config core.autocrlf false
  195.  
  196.   if [[ -n "$HOMEBREW_REBASE" ]]
  197.   then
  198.     git rebase "${QUIET_ARGS[@]}" "origin/$UPSTREAM_BRANCH"
  199.   else
  200.     git merge --no-edit --ff "${QUIET_ARGS[@]}" "origin/$UPSTREAM_BRANCH" \
  201.       --strategy=recursive \
  202.       --strategy-option=ours \
  203.       --strategy-option=ignore-all-space
  204.   fi
  205.  
  206.   export HOMEBREW_UPDATE_AFTER"$TAP_VAR"="$(read_current_revision)"
  207.  
  208.   trap '' SIGINT
  209.  
  210.   pop_stash_message
  211.  
  212.   trap - SIGINT
  213. }
  214.  
  215. homebrew-update() {
  216.   local option
  217.   local DIR
  218.   local UPSTREAM_BRANCH
  219.  
  220.   for option in "$@"
  221.   do
  222.     case "$option" in
  223.       # TODO: - `brew update --help` should display update subcommand help
  224.       --help) brew --help; exit $? ;;
  225.       --verbose) HOMEBREW_VERBOSE=1 ;;
  226.       --debug) HOMEBREW_DEBUG=1;;
  227.       --rebase) HOMEBREW_REBASE=1 ;;
  228.       --simulate-from-current-branch) HOMEBREW_SIMULATE_FROM_CURRENT_BRANCH=1 ;;
  229.       --*) ;;
  230.       -*)
  231.         [[ "$option" = *v* ]] && HOMEBREW_VERBOSE=1;
  232.         [[ "$option" = *d* ]] && HOMEBREW_DEBUG=1;
  233.         ;;
  234.       *)
  235.         odie <<-EOS
  236. This command updates brew itself, and does not take formula names.
  237. Use 'brew upgrade <formula>'.
  238. EOS
  239.         ;;
  240.     esac
  241.   done
  242.  
  243.   if [[ -n "$HOMEBREW_DEBUG" ]]
  244.   then
  245.     set -x
  246.   fi
  247.  
  248.   # check permissions
  249.   if [[ "$HOMEBREW_PREFIX" = "/usr/local" && ! -w /usr/local ]]
  250.   then
  251.     odie "/usr/local must be writable!"
  252.   fi
  253.  
  254.   if [[ ! -w "$HOMEBREW_REPOSITORY" ]]
  255.   then
  256.     odie "$HOMEBREW_REPOSITORY must be writable!"
  257.   fi
  258.  
  259.   if ! git --version >/dev/null 2>&1
  260.   then
  261.     brew install git
  262.     if ! git --version >/dev/null 2>&1
  263.     then
  264.       odie "Git must be installed and in your PATH!"
  265.     fi
  266.   fi
  267.   export GIT_TERMINAL_PROMPT="0"
  268.   export GIT_ASKPASS="false"
  269.   export GIT_SSH_COMMAND="ssh -oBatchMode=yes"
  270.  
  271.   if [[ -z "$HOMEBREW_VERBOSE" ]]
  272.   then
  273.     QUIET_ARGS=(-q)
  274.   else
  275.     QUIET_ARGS=()
  276.   fi
  277.  
  278.   # ensure GIT_CONFIG is unset as we need to operate on .git/config
  279.   unset GIT_CONFIG
  280.  
  281.   chdir "$HOMEBREW_REPOSITORY"
  282.   git_init_if_necessary
  283.   # rename Taps directories
  284.   # this procedure will be removed in the future if it seems unnecessary
  285.   rename_taps_dir_if_necessary
  286.  
  287.   # kill all of subprocess on interrupt
  288.   trap '{ pkill -P $$; wait; exit 130; }' SIGINT
  289.  
  290.   for DIR in "$HOMEBREW_REPOSITORY" "$HOMEBREW_LIBRARY"/Taps/*/*
  291.   do
  292.     [[ -d "$DIR/.git" ]] || continue
  293.     [[ -n "$SKIP_FETCH_HOMEBREW_REPOSITORY" && "$DIR" = "$HOMEBREW_REPOSITORY" ]] && continue
  294.     cd "$DIR" || continue
  295.     UPSTREAM_BRANCH="$(upstream_branch)"
  296.     # the refspec ensures that the default upstream branch gets updated
  297.     (
  298.       UPSTREAM_REPOSITORY_URL="$(git config remote.origin.url)"
  299.       if [[ "$UPSTREAM_REPOSITORY_URL" = "https://github.com/"* ]]
  300.       then
  301.         UPSTREAM_REPOSITORY="${UPSTREAM_REPOSITORY_URL#https://github.com/}"
  302.         UPSTREAM_REPOSITORY="${UPSTREAM_REPOSITORY%.git}"
  303.         UPSTREAM_BRANCH_LOCAL_SHA="$(git rev-parse "refs/remotes/origin/$UPSTREAM_BRANCH")"
  304.         # Only try to `git fetch` when the upstream branch is at a different SHA
  305.         # (so the API does not return 304: unmodified).
  306.         UPSTREAM_SHA_HTTP_CODE="$(curl --silent '--max-time' 3 \
  307.           --output /dev/null --write-out "%{http_code}" \
  308.           --user-agent "Homebrew $HOMEBREW_VERSION" \
  309.           --header "Accept: application/vnd.github.chitauri-preview+sha" \
  310.           --header "If-None-Match: \"$UPSTREAM_BRANCH_LOCAL_SHA\"" \
  311.           "https://api.github.com/repos/$UPSTREAM_REPOSITORY/commits/$UPSTREAM_BRANCH")"
  312.         [[ "$UPSTREAM_SHA_HTTP_CODE" = "304" ]] && exit
  313.       fi
  314.  
  315.       git fetch --force "${QUIET_ARGS[@]}" origin \
  316.         "refs/heads/$UPSTREAM_BRANCH:refs/remotes/origin/$UPSTREAM_BRANCH" || \
  317.           odie "Fetching $DIR failed!"
  318.     ) &
  319.   done
  320.  
  321.   wait
  322.   trap - SIGINT
  323.  
  324.   for DIR in "$HOMEBREW_REPOSITORY" "$HOMEBREW_LIBRARY"/Taps/*/*
  325.   do
  326.     [[ -d "$DIR/.git" ]] || continue
  327.     pull "$DIR"
  328.   done
  329.  
  330.   chdir "$HOMEBREW_REPOSITORY"
  331.   brew update-report "$@"
  332.   return $?
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement