blob: 535d4c9134a88ae87c55b45ff6460db793f5cc56 [file] [log] [blame]
XpLoDWilD244b8052014-01-05 15:21:57 +01001#!/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##
maxwen95841aa2016-03-08 23:43:18 +010014USERNAME=<INSERT USER>
Vachounet48426812020-10-23 18:26:07 +020015TOKEN=<INSERT TOKEN>
Humberto Borba7ea79de2016-12-07 20:19:29 -020016BRANCH=android-7.1
XpLoDWilD244b8052014-01-05 15:21:57 +010017GERRIT=gerrit.omnirom.org
18GITHUB_ORG=omnirom
19
20##
21# Script
22##
23
24# Read the source GitHub URL
25read -p "GitHub URL (no trailing slash): " github_url
26
27# Extract the repo name and prompt for changes
28repo_name=${github_url##*/}
29original_repo_name=$repo_name
30
31read -e -i "$repo_name" -p "Final repo name: " repo_name
32
33# Clone the repo locally
34git clone $github_url
35
36# Create the new repository on the organization
37echo Creating $repo_name on GitHub
38
Vachounet48426812020-10-23 18:26:07 +020039curl -H 'Authorization: token '$TOKEN'' --data "{\"name\":\"$repo_name\"}" https://api.github.com/orgs/$GITHUB_ORG/repos
XpLoDWilD244b8052014-01-05 15:21:57 +010040
41# Create the repository on Gerrit
42echo Creating $repo_name on Gerrit
43
maxwen3327f8d2016-10-06 23:59:02 +020044ssh -p 29418 $USERNAME@$GERRIT gerrit create-project $repo_name
XpLoDWilD244b8052014-01-05 15:21:57 +010045
46# Push the repository
47cd $original_repo_name
48
49git checkout $BRANCH
50git show-ref --verify --quiet refs/heads/$BRANCH
51
52if [ $? != 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
60fi
61
maxwen95841aa2016-03-08 23:43:18 +010062git push ssh://$USERNAME@gerrit.omnirom.org:29418/$repo_name $BRANCH
XpLoDWilD244b8052014-01-05 15:21:57 +010063
64# If pushing failed, we might want to forcepush the repository
65# to overwite what was previously there.
66if [ $? != 0 ]; then
67 echo "Unable to push!"
68 read -p "Try with -f? [y/n]: " forcepush
69 if [ "$forcepush" = "y" ]; then
XpLoDWilDf3b82762014-01-22 08:56:47 +010070 git push -f ssh://gerrit.omnirom.org:29418/$repo_name $BRANCH
XpLoDWilD244b8052014-01-05 15:21:57 +010071 fi
72fi
73
74# Cleanup our local copy
75cd ..
76rm -rf $original_repo_name
77
78# Done!
79echo "Fork done!"