Last active
November 25, 2020 20:53
-
-
Save alanxoc3/b0d340b1b00a0b02dfb4d780f66c2632 to your computer and use it in GitHub Desktop.
A cli based approach to sharing files on your git hosting service.
This file contains 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/bash | |
# By: @alanxoc3 | |
if [ "$#" -ne 1 ]; then | |
echo "Usage: repolink FILENAME" | |
exit 1; | |
fi | |
if ! git rev-parse --git-dir &> /dev/null; then | |
echo "Error: Must be in a git repository." | |
exit 2; | |
fi | |
REPO_ARR=$(git remote get-url origin \ | |
| sed 's/^ssh:\/\///' \ | |
| sed 's/^git@//' \ | |
| sed 's/^https:\/\///' \ | |
| sed 's/:[0-9]*\//\//' \ | |
| sed 's/:/\//' \ | |
| sed 's/\.git$//' \ | |
| sed 's/\// /g') | |
HOST=$(echo $REPO_ARR | awk '{print $1;}') | |
GROUP=$(echo $REPO_ARR | awk '{print $2;}') | |
REPO=$(echo $REPO_ARR | awk '{print $3;}') | |
LOCAL_REPO_PATH=$(realpath $(git rev-parse --show-toplevel)) | |
LOCAL_FILE_PATH=$(realpath $1) | |
FILE_PATH_RELATIVE_TO_GIT=${LOCAL_FILE_PATH#$LOCAL_REPO_PATH} | |
if echo "$HOST" | grep 'stash' &> /dev/null; then | |
echo "https://$HOST/projects/$GROUP/repos/$REPO/browse$FILE_PATH_RELATIVE_TO_GIT" | |
elif echo "$HOST" | grep 'github' &> /dev/null; then | |
echo "https://$HOST/$GROUP/$REPO/blob/main$FILE_PATH_RELATIVE_TO_GIT" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment