From 3ac1c7b6533dc5cfe4e74155538ded8b2b1ee9ec Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Thu, 18 Feb 2016 10:41:09 +0000 Subject: [PATCH] update: use GitHub API to avoid unneeded fetches. Check to see if `HEAD` is the same as what we have locally. If it is: don't bother to `git fetch`. Closes Homebrew/homebrew#47888. Closes Homebrew/homebrew#49219. Signed-off-by: Mike McQuaid --- Library/Homebrew/cmd/update.sh | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Library/Homebrew/cmd/update.sh b/Library/Homebrew/cmd/update.sh index d9226cfb9a..4841c96bde 100644 --- a/Library/Homebrew/cmd/update.sh +++ b/Library/Homebrew/cmd/update.sh @@ -334,6 +334,22 @@ EOS UPSTREAM_BRANCH="$(upstream_branch)" # the refspec ensures that the default upstream branch gets updated ( + UPSTREAM_REPOSITORY_URL="$(git config remote.origin.url)" + if [[ "$UPSTREAM_REPOSITORY_URL" = "https://github.com/"* ]] + then + UPSTREAM_REPOSITORY="${UPSTREAM_REPOSITORY_URL#https://github.com/}" + UPSTREAM_REPOSITORY="${UPSTREAM_REPOSITORY%.git}" + UPSTREAM_BRANCH_LOCAL_SHA="$(git rev-parse "refs/remotes/origin/$UPSTREAM_BRANCH")" + # Only try to `git fetch` when the upstream branch is at a different SHA + # (so the API does not return 304: unmodified). + UPSTREAM_SHA_HTTP_CODE="$(curl --silent '--max-time' 3 \ + --output /dev/null --write-out "%{http_code}" \ + -H "Accept: application/vnd.github.chitauri-preview+sha" \ + -H "If-None-Match: \"$UPSTREAM_BRANCH_LOCAL_SHA\"" \ + "https://api.github.com/repos/$UPSTREAM_REPOSITORY/commits/$UPSTREAM_BRANCH")" + [[ "$UPSTREAM_SHA_HTTP_CODE" = "304" ]] && exit + fi + git fetch "${QUIET_ARGS[@]}" origin \ "refs/heads/$UPSTREAM_BRANCH:refs/remotes/origin/$UPSTREAM_BRANCH" || \ odie "Fetching $DIR failed!"