From a4a3cf01369fdd19304caad15ecb5f91d3b2c084 Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Sat, 6 Aug 2022 15:42:41 -0700 Subject: [PATCH] Create a sync-repos action. (#1863) This will allow us to automatically create and maintain specific repos based on the main repository here, pulling key files like the license and other infrastructure and pushing them systematically to a narrow repo. Things like editor plugins that are best packaged and installed from a separate repos can still be developed in a central place, even potentially sharing common things like grammars where useful. Currently this will maintain a Vim plugin repository out of the `utils/vim` directory, but can be easily expanded for other systems. --- .github/workflows/sync_repos.yaml | 31 +++++++++++++ scripts/sync_repos.sh | 77 +++++++++++++++++++++++++++++++ utils/vim/README.md | 16 ++++++- 3 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/sync_repos.yaml create mode 100755 scripts/sync_repos.sh diff --git a/.github/workflows/sync_repos.yaml b/.github/workflows/sync_repos.yaml new file mode 100644 index 000000000000..10a6df8514d7 --- /dev/null +++ b/.github/workflows/sync_repos.yaml @@ -0,0 +1,31 @@ +# Part of the Carbon Language project, under the Apache License v2.0 with LLVM +# Exceptions. See /LICENSE for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +name: sync-repos + +on: + push: + branches: [trunk] + paths: + # Minimize where we run this to changes to the top-level files and + # specific trees that we sync to other repositories. + - '*' + - 'utils/**' + # Also run if the action itself is updated. + - '.github/workflows/sync_repos.yaml' + - 'scripts/sync_repos.sh' + +jobs: + sync-repos: + runs-on: ubuntu-latest + steps: + # Checkout our main repository. + - name: Checkout the main repository + uses: actions/checkout@v2 + + # Run the sync script. + - name: Sync to other repositories + env: + API_TOKEN_GITHUB: ${{ secrets.SYNC_REPOS_API_TOKEN_GITHUB }} + run: ./scripts/sync_repos.sh diff --git a/scripts/sync_repos.sh b/scripts/sync_repos.sh new file mode 100755 index 000000000000..dda70f6af6f7 --- /dev/null +++ b/scripts/sync_repos.sh @@ -0,0 +1,77 @@ +#!/bin/bash -eux +# +# Part of the Carbon Language project, under the Apache License v2.0 with LLVM +# Exceptions. See /LICENSE for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# +# Sync directories in the main Carbon repository into dedicated child +# repositories to better match repository-oriented installing and tooling. + +ORIGIN_DIR="$PWD" +COMMIT_SHA="$(git rev-parse --short $GITHUB_SHA)" +COMMIT_SUMMARY="Original $(git show -s --pretty=full "$COMMIT_SHA") + +$(git diff --summary "${COMMIT_SHA}^!") +" + +# Setup global git configuration. +GIT_USERNAME="CarbonInfraBot" +git config --global user.email "carbon-external-infra@google.com" +git config --global user.name "$GIT_USERNAME" + +declare -A MIRRORS +MIRRORS["utils/vim"]="vim-carbon-lang" + +for dir in "${!MIRRORS[@]}"; do + SRC_DIR="$dir" + DEST_REPO="${MIRRORS[$SRC_DIR]}" + DEST_REPO_URL="https://$GIT_USERNAME:$API_TOKEN_GITHUB@github.com/carbon-language/$DEST_REPO.git" + DEST_CLONE_DIR="$(mktemp -d)" + + git clone --single-branch "$DEST_REPO_URL" "$DEST_CLONE_DIR" + cd "$DEST_CLONE_DIR" + + # Print out the destination repository to help with debugging failures in + # GitHub's actions. + ls -al + + # Remove all the existing files to rebuild it from scratch. We ignore when + # this matches no files to handle freshly created repositories. Also print the + # status afterward for debugging. + git rm --ignore-unmatch -r . + git status + + # Copy the basic framework from the origin repository. + cp "$ORIGIN_DIR/.gitignore" \ + "$ORIGIN_DIR/CODE_OF_CONDUCT.md" \ + "$ORIGIN_DIR/LICENSE" \ + . + + # Copy the mirrored directory. We use `rsync` to get a more reliable way of + # handling the mirroring of the contents of a directory. We also make this + # verbose to help with debugging action failures on GitHub. + rsync -av "$ORIGIN_DIR/$SRC_DIR/" . + + # Add back all the files now, and print the status for debugging. + git add -A + git status + + # See if there is anything to commit and push. This works the same way as + # diff(1) and so exits zero when there are no changes. + if ! git diff --cached --quiet; then + # Commit the new state. + git commit -F- < -For Carbon devs using Vim or Neovim, this plugin provides syntax highlighting -for .carbon files found throughout `explorer/testdata` +For Carbon developers using Vim or Neovim, this plugin provides syntax +highlighting for .carbon files found throughout `explorer/testdata` + +## Repository and contributing + +This code is developed as +[part](https://github.com/carbon-language/carbon-lang/tree/trunk/utils/vim) of +the [Carbon Language](https://github.com/carbon-language/carbon-lang) project. +Everything is then automatically mirrored into a dedicated +[repository](https://github.com/carbon-language/vim-carbon-lang). + +If you would like to contribute, please follow the normal +[Carbon contributing guide](https://github.com/carbon-language/carbon-lang/blob/trunk/CONTRIBUTING.md) +and submit pull requests to the main repository. ## Manual Installation