Skip to content

Instantly share code, notes, and snippets.

@goozbach
Last active January 19, 2017 07:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goozbach/1b1aa6c780c71e9bc052e9f286a9c39b to your computer and use it in GitHub Desktop.
Save goozbach/1b1aa6c780c71e9bc052e9f286a9c39b to your computer and use it in GitHub Desktop.
gitcreate function
gitcreate() {
# usage: gitcreate <reponame> [--push]
# requires 'jq' to be installed
# Seed of this idea came from http://www.karan.org/blog/2017/01/13/create-a-new-github-com-repo-from-the-cli/
REPONAME=${1}
PUSH=${2}
# This function only works in a git repo.
if [[ ! -d .git ]]
then
echo "Error: This needs to be ran in the top-level directory of an already existing git repo."
return 2
fi
# We need to have a secret token to access github API.
if [[ ! -r ~/.gitcreate_secret ]]
then
echo "Error: You must generate a personal access token and place it in the file '~/.gitcreate_secret'"
echo
echo " '~/.gitcreate_secret' format:"
echo
echo " GITHUB_AUTH=<username>:<personal_access_token>"
echo
echo "Generate a 'personal access token' here: https://github.com/settings/tokens"
return 2
fi
source ~/.gitcreate_secret
# Actual API call
TEMPJSON=$(mktemp .git/github-info-XXXX)
curl -s -u ${GITHUB_AUTH} https://api.github.com/user/repos -d "{\"name\":\"${REPONAME}\"}" > ${TEMPJSON}
# Check status of request and print error messages
if grep -qs 'error' ${TEMPJSON}
then
REQMSG=$(jq -r '.message' ${TEMPJSON})
ERRSTR=$(jq -r '.errors[0].message' ${TEMPJSON})
echo
echo "Error creating repo: ${REQMSG}"
echo " Message is: ${ERRSTR}"
echo
rm -f ${TEMPJSON}
return 2
else
echo -e "\nRepo creation SUCCESS!"
mv -f ${TEMPJSON} .git/github-info.json
fi
CLONEURL=$(jq -r '.clone_url' .git/github-info.json)
# you can pass the trailing flag of '--push' to automatically push the repo.
if [[ ${PUSH} == '--push' ]]
then
echo -e "\nAutomatically pushing..."
git remote add origin ${CLONEURL}
git push -u origin master
else
echo "Created REPO ${1}, to push manually:"
echo
echo " git remote add origin ${CLONEURL}"
echo " git push -u origin master"
fi
HTMLURL=$(jq -r '.html_url' .git/github-info.json)
echo -e "\nVisit your new repo here: ${HTMLURL}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment