Skip to content

Instantly share code, notes, and snippets.

@soraxas
Last active August 23, 2023 04:02
Show Gist options
  • Save soraxas/507cbff5fab0a10f71cdc4b9179e3e36 to your computer and use it in GitHub Desktop.
Save soraxas/507cbff5fab0a10f71cdc4b9179e3e36 to your computer and use it in GitHub Desktop.
script:Add executable permissions to Gist files. (based on https://gist.github.com/tueda/7741614)
#! /bin/sh
#
# @file gist+x.sh
#
# Gives executable file permissions to files in a Gist.
#
set -e
prog=`basename "$0"`
# Command line options.
opt_ammnd=false
# Get raw gist id (i.e. works for both d21231acb8f or )
# from https://unix.stackexchange.com/questions/325490/how-to-get-last-part-of-http-link-in-bash/325492
GIST_ID="${1##*/}"
# Wraps "mktemp -d ${GIST_ID}XXXXXXXXXX"
mktemp_d() {(
umask 077
{
# Use mktemp if available.
dir=`mktemp -d "${GIST_ID}XXXXXXXXXX" 2>/dev/null` && [ -d "$dir" ]
} || {
# Fall back on mkdir.
dir=
i=0
while [ $i -lt 100 ]; do
next_dir=${GIST_ID}$$$i$RANDOM$RANDOM
if mkdir "$next_dir" >/dev/null 2>&1; then
dir=$next_dir
break
fi
i=`expr $i + 1`
done
[ "x$dir" != x ] && [ -d "$dir" ]
} || {
echo "$prog: error: failed to create a temporary directory" >&2
exit 1
}
echo "$dir"
)}
# Parse the command line options.
for opt_arg do
case $opt_arg in
--amend)
opt_amend=:
;;
--)
shift
break
;;
-*)
echo "$prog: error: unknown option \"$opt_arg\"" >&2
exit 1
;;
*)
break
;;
esac
shift
done
if [ $# -eq 0 ]; then
echo "Usage $prog <id>" >&2
exit
fi
# Create a working directory.
tmpdir=`mktemp_d "${TMPDIR:-/tmp}/tmp"`
trap 'rm -rf "$tmpdir 2> /dev/null"' 0 1 2 13 15
(
cd "$tmpdir"
git clone git@gist.github.com:${GIST_ID}.git || \
git clone https://gist.github.com/${GIST_ID}.git
cd ${GIST_ID}
git config user.name "`git log -1 --pretty=%an`"
git config user.email "`git log -1 --pretty=%ae`"
for f in *; do
if [ -f "$f" ]; then
chmod +x "$f"
fi
done
git add -u
if $opt_amend; then
git commit --amend --allow-empty-message -m ''
git push -f origin #master
else
git commit -m 'Change file permission'
git push origin #master
fi
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment