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> |
| 15 | BRANCH=android-6.0 |
XpLoDWilD | 244b805 | 2014-01-05 15:21:57 +0100 | [diff] [blame] | 16 | GERRIT=gerrit.omnirom.org |
| 17 | GITHUB_ORG=omnirom |
| 18 | |
| 19 | ## |
| 20 | # Script |
| 21 | ## |
| 22 | |
| 23 | # Read the source GitHub URL |
| 24 | read -p "GitHub URL (no trailing slash): " github_url |
| 25 | |
| 26 | # Extract the repo name and prompt for changes |
| 27 | repo_name=${github_url##*/} |
| 28 | original_repo_name=$repo_name |
| 29 | |
| 30 | read -e -i "$repo_name" -p "Final repo name: " repo_name |
| 31 | |
| 32 | # Clone the repo locally |
| 33 | git clone $github_url |
| 34 | |
| 35 | # Create the new repository on the organization |
| 36 | echo Creating $repo_name on GitHub |
| 37 | |
| 38 | curl --user $USERNAME --data "{\"name\":\"$repo_name\"}" https://api.github.com/orgs/$GITHUB_ORG/repos |
| 39 | |
| 40 | # Create the repository on Gerrit |
| 41 | echo Creating $repo_name on Gerrit |
| 42 | |
maxwen | 95841aa | 2016-03-08 23:43:18 +0100 | [diff] [blame^] | 43 | ssh -p 29418 $USERNAME@$GERRIT gerrit create-project --name $repo_name |
XpLoDWilD | 244b805 | 2014-01-05 15:21:57 +0100 | [diff] [blame] | 44 | |
| 45 | # Push the repository |
| 46 | cd $original_repo_name |
| 47 | |
| 48 | git checkout $BRANCH |
| 49 | git show-ref --verify --quiet refs/heads/$BRANCH |
| 50 | |
| 51 | if [ $? != 0 ]; then |
| 52 | echo "Branch $BRANCH doesn't exist in the original repository" |
| 53 | echo "Here are the branches:" |
| 54 | git branch -a |
| 55 | echo "--------------------------------------" |
| 56 | read -p "Branch to clone from: " origin_branch |
| 57 | git checkout $origin_branch |
| 58 | git branch $BRANCH |
| 59 | fi |
| 60 | |
maxwen | 95841aa | 2016-03-08 23:43:18 +0100 | [diff] [blame^] | 61 | git push ssh://$USERNAME@gerrit.omnirom.org:29418/$repo_name $BRANCH |
XpLoDWilD | 244b805 | 2014-01-05 15:21:57 +0100 | [diff] [blame] | 62 | |
| 63 | # If pushing failed, we might want to forcepush the repository |
| 64 | # to overwite what was previously there. |
| 65 | if [ $? != 0 ]; then |
| 66 | echo "Unable to push!" |
| 67 | read -p "Try with -f? [y/n]: " forcepush |
| 68 | if [ "$forcepush" = "y" ]; then |
XpLoDWilD | f3b8276 | 2014-01-22 08:56:47 +0100 | [diff] [blame] | 69 | git push -f ssh://gerrit.omnirom.org:29418/$repo_name $BRANCH |
XpLoDWilD | 244b805 | 2014-01-05 15:21:57 +0100 | [diff] [blame] | 70 | fi |
| 71 | fi |
| 72 | |
| 73 | # Cleanup our local copy |
| 74 | cd .. |
| 75 | rm -rf $original_repo_name |
| 76 | |
| 77 | # Done! |
| 78 | echo "Fork done!" |