Last active
May 23, 2020 12:37
-
-
Save tueda/7741614 to your computer and use it in GitHub Desktop.
A script to give executable file permissions to files in a Gist.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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 | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The git clone command in line 71 should change from
git clone ssh://gist.github.com/$1.git
to
for ssh, or else it's giving me
error.
Also, in line 66 the
can be changed to
to avoid error when the tmp folder does not exists.
But eitherway thanks for the nice script :)