Last active 1729716271

Simple script for creating a gist on an OpenGist installation

Revision 80722c2075dadc8d4844d10b98308438f79a81e8

create-gist.sh Raw
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
13set -euo pipefail
14
15if [[ ! -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
20fi
21
22gist_init_url=$(cat "$HOME/.config/create-gist/url" | tr -d '')
23gist_dir="$PWD"
24
25git init -b main
26git add .
27git commit -m 'Creating gist'
28git remote add origin "$gist_init_url"
29
30push_result=$(git push -u origin main 2>&1)
31echo "$push_result"
32remote_url=$(echo "$push_result" | grep -Po "git remote set-url origin https:\/\/\S+" | grep -Po "https:\/\/\S+")
33if [[ "$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}"
37else
38 echo "Unable to determine gist URL; creation may have failed"
39fi
40