Last active
August 29, 2015 14:26
-
-
Save nilshartmann/230a0303e72e04ab47fd to your computer and use it in GitHub Desktop.
Bash function that creates a private BitBucket Repository for your current local Git repository
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Creates a private BitBucket repository with one call from bash | |
# -------------------------------------------------------------------------------- | |
# | |
# Uses the BitBucket REST API to create a new private, non-forkable repository | |
# for the local Git repository your currently staying in. | |
# The content of your local repository gets pushed after the repository is created. | |
# | |
# Prequisites: | |
# - you must have curl installed | |
# - your local repo must not have a remote already connect to a bitbucket repo | |
# - your local repo must not have an origin 'remote' | |
# - has been tested on MacOs and Linux | |
# | |
# Install | |
# - Add this method to your .bash_profile or .bash_rc and source the file (or login again) | |
# | |
# Usage | |
# 1. Switch to a directory that is a git repository (i.e. have a '.git'-folder inside) | |
# 2. run 'add_bb' from your bash prompt (you will be asked for your BitBucket username and password) | |
# | |
# NOTE | |
# The name of the BitBucket repository will be determined from the last part of the directory | |
# you are currently in. | |
# (Path '/home/nils/repos/My First Git Repo' would become my_first_git_repo in BitBucket) | |
# If that's not good for you, you can specify a repository name as first parameter to the | |
# method call: add_bb another_repo_name | |
# | |
add_bb() { | |
REPO_NAME=$1 | |
if [ -z "$REPO_NAME" ]; then | |
# Remove spaces from repository name and convert to lowercase | |
REPO_NAME=$(basename `pwd | sed 's/ /_/g'|tr A-Z a-z`) | |
fi; | |
if [ ! -d .git/ ]; then | |
echo "No Git repository inside this directory. Please init with 'git init' first"; | |
return; | |
fi | |
# Check, if bitbucket is already registered as remote | |
git remote -v|grep bitbucket.org | |
if [ "$?" == "0" ]; then | |
echo "Repository already has a BitBucket remote" | |
return; | |
fi; | |
# Check if there is already a remote called 'origin' | |
git remote|grep origin | |
if [ "$?" == "0" ]; then | |
echo "Repository alread has an 'origin' remote" | |
return; | |
fi | |
if [ -z "$BB_USER" ]; then | |
local BB_USER | |
echo "Enter BitBucket User name (avoid this by setting BB_USER env variable" | |
read BB_USER | |
fi; | |
if [ -z "$BB_PASSWORD" ]; then | |
local BB_PASSWORD | |
echo "Enter BitBucket password (WARNING: will be displayed in clear text)" | |
echo "Avoid this by setting the BB_PASSWORD env variable" | |
read BB_PASSWORD | |
fi | |
echo Creating Repository $REPO_NAME | |
read -p "To confirm press any key, otherwise quit with 'n' or ctrl+c" -n 1 confirmation | |
echo | |
if [ "$confirmation" == "n" ]; then | |
echo "cancelled." | |
return; | |
fi; | |
# REST Call | |
curl -f -X POST -v -u $BB_USER:$BB_PASSWORD -H "Content-Type: application/json" \ | |
https://api.bitbucket.org/2.0/repositories/$BB_USER/$REPO_NAME \ | |
-d '{"scm": "git", "is_private": "true", "fork_policy": "no_public_forks" }' | |
if [ "$?" == "22" ]; then | |
echo "REST Call to BitBucket failed. Repository has not been created." | |
return; | |
fi | |
echo Add remote 'origin' and push to $REPO_NAME | |
git remote add origin https://[email protected]/$BB_USER/$REPO_NAME.git | |
git push -u origin --all | |
echo "Done. You can find your new repository here: https://bitbucket.org/$BB_USER/$REPO_NAME" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment