| 1 | #!/bin/bash |
| 2 | # Create a gist from the current directory |
| 3 | # |
| 4 | # Creates a gist on an OpenGist instance from the current directory. |
| 5 | # It will take care of initializing the git repo, committing all files, and |
| 6 | # pushing to the configured endpoint. On completion, it will update the remote |
| 7 | # URL to the created gist. |
| 8 | # |
| 9 | # The script requires that you create the file $HOME/.config/create-gist/url, |
| 10 | # and it should have the endpoint to an OpenGist creation URL (e.g. |
| 11 | # https://gist.example.org/init) |
| 12 | |
| 13 | set -euo pipefail |
| 14 | |
| 15 | if [[ ! -f $HOME/.config/create-gist/url ]]; then |
| 16 | echo "In order to use this utility, you must define the file \$HOME/.config/create-gist/url" >2 |
| 17 | echo "and the contents of that file must be an endpoint to an OpenGist installation" >2 |
| 18 | echo "(e.g. https://gist.example.org/init)" >2 |
| 19 | exit 1 |
| 20 | fi |
| 21 | |
| 22 | gist_init_url=$(cat "$HOME/.config/create-gist/url" | tr -d '') |
| 23 | gist_dir="$PWD" |
| 24 | |
| 25 | git init -b main |
| 26 | git add . |
| 27 | git commit -m 'Creating gist' |
| 28 | git remote add origin "$gist_init_url" |
| 29 | |
| 30 | push_result=$(git push -u origin main 2>&1) |
| 31 | echo "$push_result" |
| 32 | remote_url=$(echo "$push_result" | grep -Po "git remote set-url origin https:\/\/\S+" | grep -Po "https:\/\/\S+") |
| 33 | if [[ "$remote_url" =~ ^https:// ]]; then |
| 34 | git remote set-url origin "$remote_url" |
| 35 | echo "Remote origin updated to ${remote_url}" |
| 36 | echo "Visit the gist online: ${remote_url}" |
| 37 | else |
| 38 | echo "Unable to determine gist URL; creation may have failed" |
| 39 | fi |
| 40 |
matthew / create-gist
Last active 1 week ago
Simple script for creating a gist on an OpenGist installation