XpLoDWilD | 244b805 | 2014-01-05 15:21:57 +0100 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # Tool for forking a GitHub repository to another GitHub org, |
| 4 | # by creating the repo on GitHub and Gerrit, and pushing |
| 5 | # the source repo to Gerrit. |
| 6 | # The name of the repository can be changed before forking, |
| 7 | # and if the regular branch isn't found, you can choose |
| 8 | # in the list of available branches. |
| 9 | # Usage: ./fork.sh |
| 10 | |
| 11 | ## |
| 12 | # Configuration |
| 13 | ## |
maxwen | 95841aa | 2016-03-08 23:43:18 +0100 | [diff] [blame] | 14 | USERNAME=<INSERT USER> |
Vachounet | 4842681 | 2020-10-23 18:26:07 +0200 | [diff] [blame] | 15 | TOKEN=<INSERT TOKEN> |
maxwen | 0b8f56b | 2023-10-07 15:32:48 +0200 | [diff] [blame^] | 16 | BRANCH=android-14.0 |
XpLoDWilD | 244b805 | 2014-01-05 15:21:57 +0100 | [diff] [blame] | 17 | GERRIT=gerrit.omnirom.org |
| 18 | GITHUB_ORG=omnirom |
| 19 | |
| 20 | ## |
| 21 | # Script |
| 22 | ## |
| 23 | |
| 24 | # Read the source GitHub URL |
| 25 | read -p "GitHub URL (no trailing slash): " github_url |
| 26 | |
| 27 | # Extract the repo name and prompt for changes |
| 28 | repo_name=${github_url##*/} |
| 29 | original_repo_name=$repo_name |
| 30 | |
| 31 | read -e -i "$repo_name" -p "Final repo name: " repo_name |
| 32 | |
| 33 | # Clone the repo locally |
| 34 | git clone $github_url |
| 35 | |
| 36 | # Create the new repository on the organization |
| 37 | echo Creating $repo_name on GitHub |
| 38 | |
Vachounet | 4842681 | 2020-10-23 18:26:07 +0200 | [diff] [blame] | 39 | curl -H 'Authorization: token '$TOKEN'' --data "{\"name\":\"$repo_name\"}" https://api.github.com/orgs/$GITHUB_ORG/repos |
XpLoDWilD | 244b805 | 2014-01-05 15:21:57 +0100 | [diff] [blame] | 40 | |
| 41 | # Create the repository on Gerrit |
| 42 | echo Creating $repo_name on Gerrit |
| 43 | |
maxwen | 3327f8d | 2016-10-06 23:59:02 +0200 | [diff] [blame] | 44 | ssh -p 29418 $USERNAME@$GERRIT gerrit create-project $repo_name |
XpLoDWilD | 244b805 | 2014-01-05 15:21:57 +0100 | [diff] [blame] | 45 | |
| 46 | # Push the repository |
| 47 | cd $original_repo_name |
| 48 | |
| 49 | git checkout $BRANCH |
| 50 | git show-ref --verify --quiet refs/heads/$BRANCH |
| 51 | |
| 52 | if [ $? != 0 ]; then |
| 53 | echo "Branch $BRANCH doesn't exist in the original repository" |
| 54 | echo "Here are the branches:" |
| 55 | git branch -a |
| 56 | echo "--------------------------------------" |
| 57 | read -p "Branch to clone from: " origin_branch |
| 58 | git checkout $origin_branch |
| 59 | git branch $BRANCH |
| 60 | fi |
| 61 | |
maxwen | 95841aa | 2016-03-08 23:43:18 +0100 | [diff] [blame] | 62 | git push ssh://$USERNAME@gerrit.omnirom.org:29418/$repo_name $BRANCH |
XpLoDWilD | 244b805 | 2014-01-05 15:21:57 +0100 | [diff] [blame] | 63 | |
| 64 | # If pushing failed, we might want to forcepush the repository |
| 65 | # to overwite what was previously there. |
| 66 | if [ $? != 0 ]; then |
| 67 | echo "Unable to push!" |
| 68 | read -p "Try with -f? [y/n]: " forcepush |
| 69 | if [ "$forcepush" = "y" ]; then |
XpLoDWilD | f3b8276 | 2014-01-22 08:56:47 +0100 | [diff] [blame] | 70 | git push -f ssh://gerrit.omnirom.org:29418/$repo_name $BRANCH |
XpLoDWilD | 244b805 | 2014-01-05 15:21:57 +0100 | [diff] [blame] | 71 | fi |
| 72 | fi |
| 73 | |
| 74 | # Cleanup our local copy |
| 75 | cd .. |
| 76 | rm -rf $original_repo_name |
| 77 | |
| 78 | # Done! |
| 79 | echo "Fork done!" |