Skip to content

Instantly share code, notes, and snippets.

View alvis's full-sized avatar
✴️
building stuff for future

Alvis Tang alvis

✴️
building stuff for future
View GitHub Profile
@alvis
alvis / resolve_current_file_and_dir.sh
Created May 12, 2020 04:38
A procedure for getting the resolved absolute path of current file and its containing directory
#!/bin/sh
CWD=`pwd -P`
TARGET_FILE=$0
cd `dirname $TARGET_FILE`
TARGET_FILE=`basename $TARGET_FILE`
# iterate down a (possible) chain of symlinks
while [ -L "$TARGET_FILE" ]
@alvis
alvis / git-mv-all.sh
Last active March 22, 2020 07:11
git: move a folder with all histories rewritten
#!/bin/sh
FROM=$1
TO=$2
TEMP=$(mktemp -d)
echo Moving "$FROM" to "$TO"
git filter-branch -f --tree-filter "if [[ -e '$FROM' ]]; then mkdir -p "$(dirname $TO)"; mv '$FROM/' '$TEMP'; mv '$TEMP/' '$TO/'; fi" --tag-name-filter cat --prune-empty -- --all
@alvis
alvis / base64url.sh
Created October 17, 2019 09:45
bash: base64url encode and decode
# base64url encode
function base64url_encode {
(if [ -z "$1" ]; then cat -; else echo -n "$1"; fi) |
openssl base64 -e -A |
sed s/\\+/-/g |
sed s/\\//_/g |
sed -E s/=+$//
}
# base64url decode