blob: 4e60ec03260c7367cef395b959ecb29fe64f4d5f [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##
14USERNAME=xplodwild
15BRANCH=android-4.4
16GERRIT=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
43ssh -p 29418 $GERRIT gerrit create-project --name $repo_name
44
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
61git push ssh://gerrit.omnirom.org:29418/$repo_name $BRANCH
62
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
69 git psuh -f ssh://gerrit.omnirom.org:29418/$repo_name $BRANCH
70 fi
71fi
72
73# Cleanup our local copy
74cd ..
75rm -rf $original_repo_name
76
77# Done!
78echo "Fork done!"