Write a Script to add a Submodule to Repo
Add Submodule Script
This script is designed to add a submodule to a Git super project by passing the link to the submodule and the location where it should be added. It also pushes the new branch to GitHub and creates a pull request.
Usage
Prerequisites
- Ensure you have Git installed and configured on your system.
- Ensure you have the GitHub CLI (
gh
) installed and authenticated. You can install it following the instructions here.
Running the Script
Save the script to a file, e.g.,
add_submodule.sh
.Make the script executable:
chmod +x add_submodule.sh
Run the script with the submodule link and location as arguments:
./add_submodule.sh <submodule-link> <location>
Example
To add a submodule with the link https://github.com/username/repo.git
to the content/posts
directory, run:
./add_submodule.sh https://github.com/username/repo.git content/posts
Script Details
add_submodule.sh
#!/bin/bash
# Get the submodule link from the first argument
submodule_link=$1
# Get the location from the second argument
location=$2
# Check if the submodule link was provided
if [ -z "$submodule_link" ]; then
echo "Error: No submodule link provided."
echo "Usage: $0 <submodule-link> <location>"
exit 1
fi
# Check if the location was provided
if [ -z "$location" ]; then
echo "Error: No location provided."
echo "Usage: $0 <submodule-link> <location>"
exit 1
fi
# Extract the repo name from the submodule link
repo_name=$(basename -s .git "$submodule_link")
# Check if the repo name was extracted successfully
if [ -z "$repo_name" ]; then
echo "Error: Could not extract repo name from the link."
exit 1
fi
# Checkout a new branch with the repo name
git checkout -b "$repo_name"
# Add the submodule
git submodule add "$submodule_link" "$location/$repo_name"
# Commit the changes
git add .
git commit -m "Add submodule $repo_name"
# Push the new branch to GitHub
git push -u origin "$repo_name"
# Create a pull request using GitHub CLI
gh pr create --title "Add submodule $repo_name" --body "This PR adds the submodule $repo_name to $location" --base main --head "$repo_name"
echo "Submodule $repo_name added successfully to $location/$repo_name"
echo "Branch $repo_name pushed to GitHub and pull request created."
How It Works
- Get the submodule link: The script takes the submodule link as the first argument.
- Get the location: The script takes the location as the second argument.
- Check if the submodule link and location are provided: If either is not provided, the script shows an error message and exits.
- Extract the repo name: The script extracts the repository name from the provided link.
- Check if the repo name was extracted successfully: If not, the script shows an error message and exits.
- Checkout a new branch: It creates a new branch with the repository name.
- Add the submodule: Finally, it adds the submodule to the specified location.
- Commit the changes: Adds and commits the changes to the new branch.
- Push the new branch to GitHub: Pushes the new branch to GitHub.
- Create a pull request: Uses the GitHub CLI to create a pull request with the specified title and body.
License
This script is provided “as is” without any warranty. You can modify and distribute it as needed.