Add scripts for interacting with GitHub (#2476)
These scripts automate some common maintainer tasks. They use the
[gh cli](https://github.com/cli/cli).
diff --git a/scripts/git/bazel_to_cmake.sh b/scripts/git/bazel_to_cmake.sh
new file mode 100755
index 0000000..5b8234f
--- /dev/null
+++ b/scripts/git/bazel_to_cmake.sh
@@ -0,0 +1,66 @@
+#!/bin/bash
+
+# Copyright 2020 Google LLC
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Creates a PR based on the specified BASE_BRANCH (default "google") to fix
+# CMake files based on Bazel BUILD files.
+#
+# - Requries the gh CLI (https://github.com/cli/cli) to create a PR.
+# - Will force push to the configured PR_BRANCH (default "bazel-to-cmake-fix")
+# on the configured FORK_REMOTE (default "fork")
+# - Requires that local BASE_BRANCH branch is a pristine (potentially stale)
+# copy of the same branch on the configured UPSTREAM_REMOTE
+# (default "upstream").
+# - Requires that the working directory be clean. Will abort otherwise.
+
+set -e
+set -x
+set -o pipefail
+
+BASE_BRANCH="${1:-google}"
+PR_BRANCH="bazel-to-cmake-fix"
+UPSTREAM_REMOTE="${UPSTREAM_REMOTE:-upstream}"
+FORK_REMOTE="${FORK_REMOTE:-fork}"
+
+if [[ -n "$(git status --porcelain)" ]]; then
+ echo "Working directory not clean. Aborting"
+ git status
+ exit 1
+fi
+if ! git symbolic-ref -q HEAD; then
+ echo "In a detached HEAD state. Aborting"
+ git status
+ exit 1
+fi
+git checkout "${BASE_BRANCH?}"
+git pull "${UPSTREAM_REMOTE?}" "${BASE_BRANCH?}" --ff-only
+git submodule update --init
+if [[ -n "$(git status --porcelain)" ]]; then
+ echo "Working directory not clean after sync. Aborting"
+ git status
+ exit 1
+fi
+git checkout -B "${PR_BRANCH?}"
+./build_tools/bazel_to_cmake/bazel_to_cmake.py
+
+TITLE="Run Bazel -> CMake"
+BODY='Updates CMake files to match Bazel BUILD
+
+\`./build_tools/bazel_to_cmake/bazel_to_cmake.py\`
+'
+
+git commit -am "${TITLE?}"
+git push -f "${FORK_REMOTE}" "${PR_BRANCH?}"
+gh pr create --title="${TITLE?}" --body="${BODY?}" --base="${BASE_BRANCH?}"
diff --git a/scripts/git/google_to_main.sh b/scripts/git/google_to_main.sh
new file mode 100755
index 0000000..bb64413
--- /dev/null
+++ b/scripts/git/google_to_main.sh
@@ -0,0 +1,71 @@
+#!/bin/bash
+
+# Copyright 2020 Google LLC
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Checks out a branch at the specified green commit (default "google") and
+# creates a PR from that branch based on main.
+#
+# - Requries the gh CLI (https://github.com/cli/cli) to create a PR.
+# - Will force push to the configured PR_BRANCH (default "google-to-main") on
+# the configured FORK_REMOTE (default "fork")
+# - Requires that local "google" branch is a pristine (potentially stale) copy
+# of the "google" branch on the configured UPSTREAM_REMOTE
+# (default "upstream").
+# - Requires that the working directory be clean. Will abort otherwise.
+
+set -x
+set -e
+set -o pipefail
+
+GREEN_COMMIT="${1:-google}"
+
+PR_BRANCH="${PR_BRANCH:-google-to-main}"
+UPSTREAM_REMOTE="${UPSTREAM_REMOTE:-upstream}"
+FORK_REMOTE="${FORK_REMOTE:-fork}"
+
+if [[ -n "$(git status --porcelain)" ]]; then
+ echo "Working directory not clean. Aborting"
+ git status
+ exit 1
+fi
+if ! git symbolic-ref -q HEAD; then
+ echo "In a detached HEAD state. Aborting"
+ git status
+ exit 1
+fi
+git checkout google
+git pull "${UPSTREAM_REMOTE?}" google --ff-only
+git submodule update --init
+if [[ -n "$(git status --porcelain)" ]]; then
+ echo "Working directory not clean after sync. Aborting"
+ git status
+ exit 1
+fi
+if [[ "${GREEN_COMMIT}" != "google" ]]; then
+ git checkout "${GREEN_COMMIT?}"
+ git submodule update
+fi
+git checkout -B "${PR_BRANCH?}"
+git push -f "${FORK_REMOTE?}" "${PR_BRANCH?}"
+if [[ -z "$(which gh)" ]]; then
+ echo "gh not found on path."
+ echo "Have you installed the GitHub CLI (https://github.com/cli/cli)?"
+ echo "Cannot create PR. Branch ${BRANCH?} pushed, but aborting."
+ exit 1
+fi
+gh pr create \
+ --base main \
+ --title="Merge google -> main" \
+ --body="$(git log main.. --decorate=no --pretty='format:* %h %<(80,trunc)%s')"
diff --git a/scripts/git/main_to_google.sh b/scripts/git/main_to_google.sh
new file mode 100755
index 0000000..4de198a
--- /dev/null
+++ b/scripts/git/main_to_google.sh
@@ -0,0 +1,71 @@
+#!/bin/bash
+
+# Copyright 2020 Google LLC
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Checks out a branch at the specified green commit (default "main") and
+# creates a PR from that branch based on google.
+#
+# - Requries the gh CLI (https://github.com/cli/cli) to create a PR.
+# - Will force push to the configured PR_BRANCH (default "main-to-google") on
+# the configured FORK_REMOTE (default "fork")
+# - Requires that local "main" branch is a pristine (potentially stale) copy
+# of the "main" branch on the configured UPSTREAM_REMOTE
+# (default "upstream").
+# - Requires that the working directory be clean. Will abort otherwise.
+
+set -x
+set -e
+set -o pipefail
+
+GREEN_COMMIT="${1:-main}"
+
+PR_BRANCH="${PR_BRANCH:-main-to-google}"
+UPSTREAM_REMOTE="${UPSTREAM_REMOTE:-upstream}"
+FORK_REMOTE="${FORK_REMOTE:-fork}"
+
+if [[ -n "$(git status --porcelain)" ]]; then
+ echo "Working directory not clean. Aborting"
+ git status
+ exit 1
+fi
+if ! git symbolic-ref -q HEAD; then
+ echo "In a detached HEAD state. Aborting"
+ git status
+ exit 1
+fi
+git checkout main
+git pull "${UPSTREAM_REMOTE?}" main --ff-only
+git submodule update --init
+if [[ -n "$(git status --porcelain)" ]]; then
+ echo "Working directory not clean after sync. Aborting"
+ git status
+ exit 1
+fi
+if [[ "${GREEN_COMMIT}" != "main" ]]; then
+ git checkout "${GREEN_COMMIT?}"
+ git submodule update
+fi
+git checkout -B "${PR_BRANCH?}"
+git push -f "${FORK_REMOTE?}" "${PR_BRANCH?}"
+if [[ -z "$(which gh)" ]]; then
+ echo "gh not found on path."
+ echo "Have you installed the GitHub CLI (https://github.com/cli/cli)?"
+ echo "Cannot create PR. Branch ${BRANCH?} pushed, but aborting."
+ exit 1
+fi
+gh pr create \
+ --base google \
+ --title="Merge main -> google" \
+ --body="$(git log google.. --decorate=no --pretty='format:* %h %<(80,trunc)%s')"
diff --git a/scripts/git/update_tf_submodule.sh b/scripts/git/update_tf_submodule.sh
new file mode 100755
index 0000000..0d6f776
--- /dev/null
+++ b/scripts/git/update_tf_submodule.sh
@@ -0,0 +1,86 @@
+#!/bin/bash
+
+# Copyright 2020 Google LLC
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Creates a PR based on the specified BASE_BRANCH (default "google") to update
+# the TF submodule to the configured TENSORFLOW_COMMIT (default REMOTE for
+# current HEAD).
+#
+# - Requries the gh CLI (https://github.com/cli/cli) to create a PR.
+# - Will force push to the configured PR_BRANCH (default "tf-submodule-update")
+# on the configured FORK_REMOTE (default "fork")
+# - Requires that local BASE_BRANCH branch is a pristine (potentially stale)
+# copy of the same branch on the configured UPSTREAM_REMOTE
+# (default "upstream").
+# - Requires that the working directory be clean. Will abort otherwise.
+# - An optional TF_COMMIT_NICKNAME nickname can be given to the commit for the
+# PR description. Otherwise, it will default to "current HEAD" if
+# TENSORFLOW_COMMIT is REMOTE and the trimmed commit sha otherwise.
+
+set -e
+set -x
+set -o pipefail
+
+TENSORFLOW_COMMIT="${1:-REMOTE}"
+PR_BRANCH="tf-submodule-update"
+BASE_BRANCH="${1:-google}"
+UPSTREAM_REMOTE="${UPSTREAM_REMOTE:-upstream}"
+FORK_REMOTE="${FORK_REMOTE:-fork}"
+TF_COMMIT_NICKNAME=""
+
+if [[ -n "$(git status --porcelain)" ]]; then
+ echo "Working directory not clean. Aborting"
+ git status
+ exit 1
+fi
+if ! git symbolic-ref -q HEAD; then
+ echo "In a detached HEAD state. Aborting"
+ git status
+ exit 1
+fi
+git checkout "${BASE_BRANCH?}"
+git pull "${UPSTREAM_REMOTE?}" "${BASE_BRANCH?}" --ff-only
+git submodule update --init
+if [[ -n "$(git status --porcelain)" ]]; then
+ echo "Working directory not clean after sync. Aborting"
+ git status
+ exit 1
+fi
+git checkout -B "${PR_BRANCH?}"
+
+CMD="./scripts/git/update_tf_llvm_submodules.py --llvm_commit=KEEP --update_build_files=true --tensorflow_commit=${TENSORFLOW_COMMIT?}"
+
+bash -c "${CMD?}"
+
+TF_SHA="$(git submodule status third_party/tensorflow | awk '{print $1}' | cut -c -12)"
+
+if [[ -z "${TF_COMMIT_NICKNAME?}" && "${TENSORFLOW_COMMIT?}" == "REMOTE" ]]; then
+ TF_COMMIT_NICKNAME="current HEAD"
+fi
+TF_COMMIT_NICKNAME="${TF_COMMIT_NICKNAME:-${TF_SHA?}}"
+
+TITLE="Integrate TF at tensorflow/tensorflow@${TF_SHA?}"
+BODY="$(cat <<-EOF
+Updates TF to
+[${TF_COMMIT_NICKNAME?}](https://github.com/tensorflow/tensorflow/commit/${TF_SHA?})
+and copies over the LLVM BUILD files.
+
+\`${CMD?}\`
+EOF
+)"
+
+git commit -am "${TITLE?}"
+git push -f fork "${PR_BRANCH?}"
+gh pr create --title="${TITLE?}" --body="${BODY?}" --base="${BASE_BRANCH?}"