An open API service indexing awesome lists of open source software.

https://github.com/austenstone/migrate-submodules

Migrate submodules recursively using sed
https://github.com/austenstone/migrate-submodules

git git-submodules submodules

Last synced: about 2 months ago
JSON representation

Migrate submodules recursively using sed

Awesome Lists containing this project

README

        

# Migrate Submodules

Git migrations tools like [GitHub Importer](https://docs.github.com/en/get-started/importing-your-projects-to-github/importing-source-code-to-github/importing-a-repository-with-github-importer) and even [ghe-migrator](https://docs.github.com/en/[email protected]/admin/user-management/migrating-data-to-and-from-your-enterprise/migrating-data-to-your-enterprise) are not able to update [submodule urls](https://git-scm.com/docs/gitmodules#Documentation/gitmodules.txt-submoduleltnamegturl). This small script solves that problem by updating all your `.gitmodules` files recursively with a simple [sed](https://linux.die.net/man/1/sed) command.

[Install](https://github.com/austenstone/migrate-submodules#install-%EF%B8%8F)

You can run this script in any git repository and it will recursively itterate submodules and perform the following operations:
1. Executes the sed command provided via `-s` on .gitmodules
2. Synchronizes submodules' remote URL configuration setting to the value specified in .gitmodules
3. Updates(pulls) all submodules.
4. Commits the changes to the branch provided via `-b` with the message provided via `-m`

#### NOTES
- You must have read/write permission on the repositories you are modifying
- This script will not import repositories for you and assumes all required repositories have already been imported to your target. This means if you are replacing the url of a submodule, the new URL must be pointing to a valid repository.

## Usage 🏃‍♂️
This example is migrating the url from `bitbucket.org` to `github.com`.

Clone your repo that contains submodules and cd inside:
```bash
git clone https://[email protected]/austenstone/main-test1.git
cd main-test1
```

Run the script:
```bash
migrate-submodules.sh -s 's/bitbucket.org/github.com/g' -b 'master'
```
Congratulations. Your submodules have been updated! 🎉

Check them out 👀
```bash
cat .gitmodules && git submodule foreach --recursive '[ -f .gitmodules ] && cat .gitmodules || true'
```

If you are happy with the results, push your changes to remote:
```bash
git submodule foreach --recursive git push origin master
git push origin master
```

TIP: You can automatically sync all branches with master by running the following command:
```bash
for BRANCH in `ls .git/refs/heads`; do git rebase master $BRANCH; done
git push --all origin
```

To see this example in action read the [GitHub Actions workflow](https://github.com/austenstone/migrate-submodules/blob/main/.github/workflows/blank.yml) and also checkout the [previous runs](https://github.com/austenstone/migrate-submodules/actions/workflows/blank.yml).

## Install ⬇️
Simply add the script to your PATH.
```bash
cd /usr/local/bin
curl https://raw.githubusercontent.com/austenstone/migrate-submodules/main/migrate-submodules.sh > migrate-submodules.sh
chmod +x migrate-submodules.sh
```
Script tested on Ubuntu and MacOS.

## Test Repo Hierarchy
[bitbucket.org/austenstone/main-test1](https://bitbucket.org/austenstone/main-test1) is the root of a small set of repos and is used to test the functionality.

[github.com/austenstone/main-test1](https://github.com/austenstone/main-test1) is the migrated repo.
```mermaid
graph TD;
main-test1-->child-test1;
child-test1-->child-test1-child ;
main-test1-->child-test2;
```

## Possible Improvements 🚧
- Use `set-url [--] ` instead of sed.
- Sync all branches in the script
- Dynamically get the default branch using `git remote show origin | sed -n '/HEAD branch/s/.*: //p'`

## Inspiration ✨
- [Enteee/git-submodule-url-rewrite](https://github.com/Enteee/git-submodule-url-rewrite)