Skip to content

Commit

Permalink
Merge pull request Homebrew#1127 from MikeMcQuaid/update-check-github…
Browse files Browse the repository at this point in the history
…-api-tags

update.sh: use GitHub tags API.
  • Loading branch information
MikeMcQuaid committed Sep 24, 2016
2 parents 232078d + 87cf4f7 commit 96b1e23
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions Library/Homebrew/cmd/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ merge_or_rebase() {

if [[ "$DIR" = "$HOMEBREW_REPOSITORY" && -z "$HOMEBREW_NO_UPDATE_CLEANUP" ]]
then
UPSTREAM_TAG="$(git tag --list --sort=-version:refname | grep '^[0-9]*\.[0-9]*\.[0-9]*$' | head -n1)"
UPSTREAM_TAG="$(git tag --list --sort=-version:refname |
grep --max-count=1 '^[0-9]*\.[0-9]*\.[0-9]*$')"
else
UPSTREAM_TAG=""
fi
Expand All @@ -244,7 +245,7 @@ merge_or_rebase() {
stash save --include-untracked "${QUIET_ARGS[@]}"
then
odie <<EOS
Could not `git stash` in $DIR!
Could not 'git stash' in $DIR!
Please stash/commit manually if you need to keep your changes or, if not, run:
cd $DIR
git reset --hard origin/master
Expand Down Expand Up @@ -469,15 +470,30 @@ EOS
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_DIR")"
# Only try to `git fetch` when the upstream branch is at a different SHA
# (so the API does not return 304: unmodified).

if [[ "$DIR" = "$HOMEBREW_REPOSITORY" && -z "$HOMEBREW_NO_UPDATE_CLEANUP" ]]
then
# Only try to `git fetch` when the upstream tags have changed
# (so the API does not return 304: unmodified).
GITHUB_API_ETAG="$(sed -n 's/^ETag: "\([a-f0-9]\{32\}\)".*/\1/p' ".git/GITHUB_HEADERS")"
GITHUB_API_ACCEPT="application/vnd.github.v3+json"
GITHUB_API_ENDPOINT="tags"
else
# Only try to `git fetch` when the upstream branch is at a different SHA
# (so the API does not return 304: unmodified).
GITHUB_API_ETAG="$(git rev-parse "refs/remotes/origin/$UPSTREAM_BRANCH_DIR")"
GITHUB_API_ACCEPT="application/vnd.github.v3.sha"
GITHUB_API_ENDPOINT="commits/$UPSTREAM_BRANCH_DIR"
fi

UPSTREAM_SHA_HTTP_CODE="$("$HOMEBREW_CURL" --silent --max-time 3 \
--output /dev/null --write-out "%{http_code}" \
--dump-header "$DIR/.git/GITHUB_HEADERS" \
--user-agent "$HOMEBREW_USER_AGENT_CURL" \
--header "Accept: application/vnd.github.v3.sha" \
--header "If-None-Match: \"$UPSTREAM_BRANCH_LOCAL_SHA\"" \
"https://api.github.com/repos/$UPSTREAM_REPOSITORY/commits/$UPSTREAM_BRANCH_DIR")"
--header "Accept: $GITHUB_API_ACCEPT" \
--header "If-None-Match: \"$GITHUB_API_ETAG\"" \
"https://api.github.com/repos/$UPSTREAM_REPOSITORY/$GITHUB_API_ENDPOINT")"

# Touch FETCH_HEAD to confirm we've checked for an update.
[[ -f "$DIR/.git/FETCH_HEAD" ]] && touch "$DIR/.git/FETCH_HEAD"
[[ -z "$HOMEBREW_UPDATE_FORCE" ]] && [[ "$UPSTREAM_SHA_HTTP_CODE" = "304" ]] && exit
Expand Down

0 comments on commit 96b1e23

Please sign in to comment.