git.bash 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/usr/bin/env bash
  2. # Copyright Materialize, Inc. and contributors. All rights reserved.
  3. #
  4. # Use of this software is governed by the Business Source License
  5. # included in the LICENSE file at the root of this repository.
  6. #
  7. # As of the Change Date specified in that file, in accordance with
  8. # the Business Source License, use of this software will be governed
  9. # by the Apache License, Version 2.0.
  10. set -euo pipefail
  11. . misc/shlib/shlib.bash
  12. MZ_REPO_REF="${BUILDKITE_REPO_REF:-origin}"
  13. MZ_REPO_PULL_REQUEST_BASE_BRANCH="${BUILDKITE_PULL_REQUEST_BASE_BRANCH:-main}"
  14. if [[ "${BUILDKITE:-}" != "true" ]]; then
  15. # when running locally, our origin may point to a fork of the repo but we want the mz repo
  16. MZ_REPO_REF=$(git remote -v | grep -i "MaterializeInc/materialize" | grep "fetch" | head -n1 | cut -f1)
  17. fi
  18. configure_git_user_if_in_buildkite() {
  19. if [[ "${BUILDKITE:-}" == "true" ]]; then
  20. ci_collapsed_heading "Configure git"
  21. run git config --global user.email "buildkite@materialize.com"
  22. run git config --global user.name "Buildkite"
  23. fi
  24. }
  25. fetch_pr_target_branch() {
  26. ci_collapsed_heading "Fetch target branch"
  27. run git fetch "$MZ_REPO_REF" "$MZ_REPO_PULL_REQUEST_BASE_BRANCH"
  28. }
  29. merge_pr_target_branch() {
  30. configure_git_user_if_in_buildkite
  31. ci_collapsed_heading "Merge target branch"
  32. run git merge "$MZ_REPO_REF"/"$MZ_REPO_PULL_REQUEST_BASE_BRANCH" --message "Merge"
  33. }
  34. get_common_ancestor_commit_of_pr_and_target() {
  35. run git merge-base HEAD "$MZ_REPO_REF"/"$MZ_REPO_PULL_REQUEST_BASE_BRANCH"
  36. }