Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions scripts/import-remark-definition-links.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/bin/bash

set -x
set -e
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To make the script more robust, I suggest two improvements:

  1. Use set -u: This will cause the script to exit if it tries to use an uninitialized variable.
  2. Use a trap for cleanup: This ensures that the temporary directories ($SOURCE_REPO_DIR and $DEST_REPO_DIR) are removed when the script exits, whether it succeeds or fails.

With the trap in place, you can then remove the rm -rf commands on lines 26-27.

Suggested change
set -e
set -e
set -u
trap 'rm -rf "$SOURCE_REPO_DIR" "$DEST_REPO_DIR"' EXIT


# In this repo
SOURCE_GIT="git@github.com:mcansh/remark-definition-links.git"
# Cloned locally to this folder
SOURCE_REPO_DIR="temp-remark-definition-links"
# Copy files as of this ref
SOURCE_REF="main"
# Using a temp branch named
SOURCE_WORKING_BRANCH="migration-src"

# And re-play it in this repo
DEST_GIT="git@github.com:mcansh/packages.git"
# Cloned locally to this folder
DEST_REPO_DIR="temp-packages"
# Onto this branch
DEST_BRANCH="dev"
# Using a temp branch named
DEST_WORKING_BRANCH="import-remark-definition-links"
# Using the source repo as this remote
TEMP_SOURCE_REMOTE_NAME="rtw"

rm -rf "$SOURCE_REPO_DIR"
rm -rf "$DEST_REPO_DIR"

# Clone fresh copies of the repos
git clone "$SOURCE_GIT" "$SOURCE_REPO_DIR"
git clone "$DEST_GIT" "$DEST_REPO_DIR"

# Remove origins so we can't accidentally mess them up
cd "$SOURCE_REPO_DIR"
git remote rm origin
cd ..
cd "$DEST_REPO_DIR"
git checkout $DEST_BRANCH
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The variable $DEST_BRANCH should be quoted (e.g., "$DEST_BRANCH") to prevent potential issues with word splitting and globbing if the branch name were to contain spaces or special characters. This is a shell scripting best practice that should be applied to all unquoted variable expansions in this script for improved robustness (e.g., on lines 42, 44, 64-66, 68-69).

Suggested change
git checkout $DEST_BRANCH
git checkout "$DEST_BRANCH"

git remote rm origin
cd ..

cd "$SOURCE_REPO_DIR"

git checkout -b $SOURCE_WORKING_BRANCH $SOURCE_REF
# git-filter-repo --force \
# --path .env.example \
# --path .github \
# --path .gitignore \
# --path .prettierrc \
# --path .vscode \
# --path CONTRIBUTING.md \
# --path LICENSE \
# --path package.json \
# --path packages \
# --path pnpm-lock.yaml \
# --path pnpm-workspace.yaml \
# --path scripts
Comment on lines +45 to +57
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This large block of commented-out code related to git-filter-repo should be removed if it's not intended to be used. It reduces clarity and makes the script harder to understand and maintain.


git reset --hard
git gc --aggressive
git prune
cd ..

cd $DEST_REPO_DIR
git checkout -b $DEST_WORKING_BRANCH
git remote add $TEMP_SOURCE_REMOTE_NAME ../$SOURCE_REPO_DIR
# Only fetch the migration branch, don't include tags
git fetch $TEMP_SOURCE_REMOTE_NAME $SOURCE_WORKING_BRANCH
git merge $TEMP_SOURCE_REMOTE_NAME/$SOURCE_WORKING_BRANCH --allow-unrelated-histories --no-edit
cd ..

set +x
set +e

echo "✅ Migration complete!"
echo ""
echo "To finish:"
echo " cd ${DEST_REPO_DIR}"
echo " git remote add origin ${DEST_GIT}"
echo " git push --set-upstream origin $DEST_WORKING_BRANCH"
echo ""
echo "Then open a PR to the ${DEST_BRANCH} branch and perform a **NORMAL MERGE**"
echo " https://github.com/mcansh/packages/compare/${DEST_BRANCH}...${DEST_WORKING_BRANCH}?expand=1"
echo ""
Loading