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