Skip to content

Instantly share code, notes, and snippets.

@chrisross
Last active August 18, 2019 01:04
Show Gist options
  • Save chrisross/5676065 to your computer and use it in GitHub Desktop.
Save chrisross/5676065 to your computer and use it in GitHub Desktop.
Functions to init/delete a remote repo using bitbucket or github, using cURL and respective APIs.
#!/usr/bin/env bash
function gitsetup {
# Collect parameters
local params=()
while [[ $# -gt 0 ]]; do
case "${1}" in
-h|-\?|--help)
echo "Usage: gitsetup -H bitbucket -u username -d 'A description' -t repo_title"
return 0
;;
-H|--host) local remote_host="${2}" && shift 2 ;;
-u|--username) local username="${2}" && shift 2 ;;
-t|--title) local repo_title="${2}" && shift 2 ;;
-d|--description) local repo_desc="${2}" && shift 2 ;;
*) params+=("${1}") && shift ;;
esac
done
# Compile and echo invalid parameters
if [[ "${#params[@]}" -gt 0 ]]; then
local comp=""
for x in "${params[@]}"; do comp+="${comp}, ${x}"; done
echo "Unrecognised parameters passed in: ${comp}" >&2
return 2
fi
# No host provided
if [[ -z "${remote_host}" ]]; then
echo "Missing host parameter: use 'bitbucket' OR 'github'" >&2
return 2
fi
# Bitbucket
case "${remote_host}" in
bitbucket)
local API="https://api.bitbucket.org/1.0/repositories/"
local DATA="name=${repo_title}&description=${repo_desc}&is_private=true&scm=git"
local REMOTE="ssh://[email protected]/${username}/${repo_title}"
;;
github)
local API="https://api.github.com/user/repos"
local DATA="{\"name\":\"${repo_title}\",\"description\":\"${repo_desc}\"}"
local REMOTE="[email protected]:${username}/${repo_title}"
;;
*)
echo "Invalid remote host: use 'bitbucket' OR 'github'" >&2
return 2
;;
esac
# Create remote repository
curl -X POST -u "${username}" "${API}" -d "${DATA}"
# If git not already inited then init and push etc.
# This allows you to retry gitsetup if you provided an incorrect password the
# first time or if you simply wish to setup the remote and push to it from an
# existing local git repo
if [[ ! -e ".git" ]]; then
git init
git add .
git commit -m "Created repo"
git remote add origin "${REMOTE}"
git push -u origin master
fi
}
function gitkill {
# Collect parameters
local params=()
while [[ $# -gt 0 ]]; do
case "${1}" in
-h|-\?|--help)
echo "Usage: gitkill -H hostname -t repo_title -u username"
shift
;;
-H|--host) local remote_host="${2}" && shift 2 ;;
-u|--username) local username="${2}" && shift 2 ;;
-t|--title) local repo_title="${2}" && shift 2 ;;
*) params+=("${1}") && shift ;;
esac
done
# Compile and echo invalid parameters
if [[ "${#params[@]}" -gt 0 ]]; then
local comp=""
for x in "${params[@]}"; do comp+="${comp}, ${x}"; done
echo "Unrecognised parameters passed in: ${comp}" >&2
return 2
fi
case "${remote_host}" in
bitbucket)
local API="https://api.bitbucket.org/1.0/repositories/${2}/${3}"
;;
github)
local API="https://api.github.com/repos/${2}/${3}"
;;
*)
echo "Invalid remote host: use 'bitbucket' OR 'github'" >&2
return 2
;;
esac
curl -X DELETE -u "${username}" "${API}"
}
@prsshini
Copy link

prsshini commented Apr 26, 2018

Hi,

curl -X POST -u "${username}" "${API}" -d "${DATA}" this is run without any errors.

But when I set my remote origin and try to push -u it shows as remote repo doesnt exist.

curl --user username https://api.bitbucket.org/1.0/repositories/ --data name="test" --data is_private='true'
git remote add origin https://bitbucket.****.com/username/subha.git
git push -u origin --all

C:\Users**********t>git push -u origin --all
fatal: repository 'https://bitbucket.*****.com/username/test.git/' not found
git push -u origin --tags

I am expecting that curl will create a remote repo so it helps me to avoid a step to go into bitbucket GUI and create it manually for everytime When I need to create a new one and push a new repo from my local.

But its not behaving so.

Do you know why?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment