sync_repos.sh 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env bash
  2. #
  3. # Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  4. # Exceptions. See /LICENSE for license information.
  5. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. #
  7. # Sync directories in the main Carbon repository into dedicated child
  8. # repositories to better match repository-oriented installing and tooling.
  9. set -eux
  10. ORIGIN_DIR="$PWD"
  11. COMMIT_SHA="$(git rev-parse --short $GITHUB_SHA)"
  12. COMMIT_SUMMARY="Original $(git show -s --pretty=full "$COMMIT_SHA")
  13. $(git diff --summary "${COMMIT_SHA}^!")
  14. "
  15. # Setup global git configuration.
  16. GIT_USERNAME="CarbonInfraBot"
  17. git config --global user.email "carbon-external-infra@google.com"
  18. git config --global user.name "$GIT_USERNAME"
  19. declare -A MIRRORS
  20. MIRRORS["utils/vim"]="vim-carbon-lang"
  21. for dir in "${!MIRRORS[@]}"; do
  22. SRC_DIR="$dir"
  23. DEST_REPO="${MIRRORS[$SRC_DIR]}"
  24. DEST_REPO_URL="https://$GIT_USERNAME:$API_TOKEN_GITHUB@github.com/carbon-language/$DEST_REPO.git"
  25. DEST_CLONE_DIR="$(mktemp -d)"
  26. git clone --single-branch "$DEST_REPO_URL" "$DEST_CLONE_DIR"
  27. cd "$DEST_CLONE_DIR"
  28. # Print out the destination repository to help with debugging failures in
  29. # GitHub's actions.
  30. ls -al
  31. # Remove all the existing files to rebuild it from scratch. We ignore when
  32. # this matches no files to handle freshly created repositories. Also print the
  33. # status afterward for debugging.
  34. git rm --ignore-unmatch -r .
  35. git status
  36. # Copy the basic framework from the origin repository.
  37. cp "$ORIGIN_DIR/.gitignore" \
  38. "$ORIGIN_DIR/CODE_OF_CONDUCT.md" \
  39. "$ORIGIN_DIR/LICENSE" \
  40. .
  41. # Copy the mirrored directory. We use `rsync` to get a more reliable way of
  42. # handling the mirroring of the contents of a directory. We also make this
  43. # verbose to help with debugging action failures on GitHub.
  44. rsync -av "$ORIGIN_DIR/$SRC_DIR/" .
  45. # Add back all the files now, and print the status for debugging.
  46. git add -A
  47. git status
  48. # See if there is anything to commit and push. This works the same way as
  49. # diff(1) and so exits zero when there are no changes.
  50. if ! git diff --cached --quiet; then
  51. # Commit the new state.
  52. git commit -F- <<EOF
  53. Sync $DEST_REPO to carbon-language/carbon-lang@$COMMIT_SHA
  54. $COMMIT_SUMMARY
  55. EOF
  56. git log
  57. # Push the new commit.
  58. git push
  59. fi
  60. # Cleanup.
  61. cd "$ORIGIN_DIR"
  62. rm -rf "$DEST_CLONE_DIR"
  63. done