|
| 1 | +#!/bin/sh |
| 2 | +# |
| 3 | +# Sync changes from upstream and open a pull request |
| 4 | +# |
| 5 | +# Usage: script/sync-upstream [remote] |
| 6 | +# |
| 7 | +# TODO: |
| 8 | +# - [ ] handle merge conflicts with `git merge --continue` |
| 9 | + |
| 10 | +set -e |
| 11 | + |
| 12 | +which -s hub || (echo "This script requires the 'hub' command: http://hub.github.com/" && exit 1) |
| 13 | + |
| 14 | +# If there's not an upstream remote, set it. |
| 15 | +if ! git remote show | grep -q upstream; then |
| 16 | + git remote add upstream https://github.com/github/opensource.guide.git |
| 17 | +fi |
| 18 | + |
| 19 | +REMOTE=${1:-"origin"} |
| 20 | +BASE="$REMOTE/gh-pages" |
| 21 | +# FIXME: switch to upstream/gh-pages before this is merged: |
| 22 | +# https://github.com/github/open-source-guide/pull/295 |
| 23 | +HEAD="upstream/i18n" |
| 24 | + |
| 25 | +git fetch upstream |
| 26 | +git fetch $REMOTE |
| 27 | + |
| 28 | +BASE_SHA=$(git rev-parse $BASE) |
| 29 | +HEAD_SHA=$(git rev-parse $HEAD) |
| 30 | +BRANCH="sync-${HEAD_SHA:0:8}" |
| 31 | + |
| 32 | +TRANSLATABLE_FILES=" |
| 33 | + _articles |
| 34 | + _data/locale/en-US.yml |
| 35 | + _config |
| 36 | +" |
| 37 | + |
| 38 | +## Create a new branch and merge in the changes from upstream. |
| 39 | +echo "Creating branch $BRANCH based off $BASE" |
| 40 | +git checkout -B $BRANCH $BASE |
| 41 | +echo "Merging in latest changes from $HEAD" |
| 42 | +git merge -q --no-edit $HEAD |
| 43 | +git push $REMOTE $BRANCH |
| 44 | + |
| 45 | +CHANGED_FILES=$(git diff --name-only $BASE_SHA $HEAD_SHA -- $TRANSLATABLE_FILES) |
| 46 | + |
| 47 | +TEMPLATE="Sync changes from upstream\n\n" |
| 48 | + |
| 49 | +if [ -z "$CHANGED_FILES" ]; then |
| 50 | + TEMPLATE="$TEMPLATE\nNo files that need translation were changed upstream." |
| 51 | +else |
| 52 | + TEMPLATE="$TEMPLATE\nCheck the changes in each of these files and update the translation accordingly:\n" |
| 53 | + |
| 54 | + for file in $CHANGED_FILES; do |
| 55 | + TEMPLATE="$TEMPLATE\n- [ ] \`$file\`" |
| 56 | + done |
| 57 | +fi |
| 58 | + |
| 59 | +OWNER=$(git config --get remote.$REMOTE.url | sed -Ene 's#.*github.com[/:]([^/]*)/.*#\1#p') |
| 60 | + |
| 61 | +echo $TEMPLATE | hub pull-request -o -F - -b $OWNER:gh-pages -h $OWNER:$BRANCH |
0 commit comments