Skip to content

Instantly share code, notes, and snippets.

@tueda
Last active May 23, 2020 12:37
Show Gist options
  • Save tueda/7741614 to your computer and use it in GitHub Desktop.
Save tueda/7741614 to your computer and use it in GitHub Desktop.
A script to give executable file permissions to files in a Gist.
#! /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
# Wraps "mktemp -d $1XXXXXXXXXX"
mktemp_d() {(
umask 077
{
# Use mktemp if available.
dir=`mktemp -d "$1XXXXXXXXXX" 2>/dev/null` && [ -d "$dir" ]
} || {
# Fall back on mkdir.
dir=
i=0
while [ $i -lt 100 ]; do
next_dir=$1$$$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"' 0 1 2 13 15
(
cd "$tmpdir"
git clone ssh://gist.github.com/$1.git || \
git clone https://gist.github.com/$1.git
cd $1
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
)
@soraxas
Copy link

soraxas commented May 7, 2020

The git clone command in line 71 should change from

git clone ssh://gist.github.com/$1.git

to

git clone git@gist.github.com:$1.git

for ssh, or else it's giving me

me@gist.github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

error.

Also, in line 66 the

trap 'rm -rf "$tmpdir"' 0 1 2 13 15

can be changed to

trap 'rm -rf "$tmpdir  2> /dev/null"' 0 1 2 13 15

to avoid error when the tmp folder does not exists.

But eitherway thanks for the nice script :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment