Skip to content

Instantly share code, notes, and snippets.

@goozbach
Last active January 19, 2017 07:28
Show Gist options
  • Select an option

  • Save goozbach/1b1aa6c780c71e9bc052e9f286a9c39b to your computer and use it in GitHub Desktop.

Select an option

Save goozbach/1b1aa6c780c71e9bc052e9f286a9c39b to your computer and use it in GitHub Desktop.

Revisions

  1. goozbach revised this gist Jan 19, 2017. 1 changed file with 50 additions and 19 deletions.
    69 changes: 50 additions & 19 deletions gitcreate.sh
    Original file line number Diff line number Diff line change
    @@ -1,35 +1,66 @@
    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/
    # `~/.gitcreate_secret` format:
    #
    # GITHUB_AUTH=<username>:<personal_access_token>
    #
    # Generate a personal access token here: https://github.com/settings/tokens
    source ~/.gitcreate_secret || return

    REPONAME=${1}
    PUSH=${2}

    # This function only works in a git repo.
    if [[ ! -d .git ]]
    then
    echo "This needs to be ran in the top-level directory of an already existing git repo."
    return
    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
    REPONAME=${1}
    PUSH=${2}
    curl -s -u ${GITHUB_AUTH} https://api.github.com/user/repos -d "{\"name\":\"${REPONAME}\"}" > .git/github-info.json

    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 "Automatically pushing..."
    echo -e "\nAutomatically pushing..."
    git remote add origin ${CLONEURL}
    git push -u origin master
    else
    # gotta keep this indentation cause HEREDOCs suck at indenting
    cat <<-EOF
    Created REPO ${1}, to push manually:
    git remote add origin ${CLONEURL}
    git push -u origin master
    EOF
    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}"
  2. goozbach revised this gist Jan 19, 2017. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gitcreate.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    gitcreate() {
    # Seed of this idea came from http://www.karan.org/blog/2017/01/13/create-a-new-github-com-repo-from-the-cli/
    # `~/.gitcreate_secret` format:
    #
    # GITHUB_AUTH=<username>:<personal_access_token>
  3. goozbach revised this gist Jan 19, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gitcreate.sh
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ gitcreate() {
    # GITHUB_AUTH=<username>:<personal_access_token>
    #
    # Generate a personal access token here: https://github.com/settings/tokens
    source ~/.gitcreate_secret
    source ~/.gitcreate_secret || return
    if [[ ! -d .git ]]
    then
    echo "This needs to be ran in the top-level directory of an already existing git repo."
  4. goozbach created this gist Jan 19, 2017.
    35 changes: 35 additions & 0 deletions gitcreate.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    gitcreate() {
    # `~/.gitcreate_secret` format:
    #
    # GITHUB_AUTH=<username>:<personal_access_token>
    #
    # Generate a personal access token here: https://github.com/settings/tokens
    source ~/.gitcreate_secret
    if [[ ! -d .git ]]
    then
    echo "This needs to be ran in the top-level directory of an already existing git repo."
    return
    fi
    REPONAME=${1}
    PUSH=${2}
    curl -s -u ${GITHUB_AUTH} https://api.github.com/user/repos -d "{\"name\":\"${REPONAME}\"}" > .git/github-info.json

    CLONEURL=$(jq -r '.clone_url' .git/github-info.json)

    if [[ ${PUSH} == '--push' ]]
    then
    echo "Automatically pushing..."
    git remote add origin ${CLONEURL}
    git push -u origin master
    else
    # gotta keep this indentation cause HEREDOCs suck at indenting
    cat <<-EOF
    Created REPO ${1}, to push manually:
    git remote add origin ${CLONEURL}
    git push -u origin master
    EOF
    fi
    HTMLURL=$(jq -r '.html_url' .git/github-info.json)
    echo -e "\nVisit your new repo here: ${HTMLURL}"
    }