|
#!/bin/sh |
|
|
|
# The MIT License (MIT) |
|
# |
|
# Copyright (c) 2013 https://gist.github.com/rbf |
|
# |
|
# Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
# this software and associated documentation files (the "Software"), to deal in |
|
# the Software without restriction, including without limitation the rights to |
|
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
|
# the Software, and to permit persons to whom the Software is furnished to do so, |
|
# subject to the following conditions: |
|
# |
|
# The above copyright notice and this permission notice shall be included in all |
|
# copies or substantial portions of the Software. |
|
# |
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
|
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
|
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
|
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
|
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
|
|
|
|
### File info: |
|
|
|
GUR_VERSION="v2.1-SNAPSHOT" |
|
GUR_SOURCE="https://gist.github.com/rbf/6117917/raw/gist-raw-url" |
|
GUR_DOC="https://gist.github.com/rbf/6117917" |
|
GUR_INSTALL_PATH="/usr/local/bin/" |
|
|
|
### |
|
|
|
GUR_COMMAND_DEFAULT_NAME=$(basename "${GUR_SOURCE}") |
|
GUR_COMMAND_NAME="${0#${GUR_INSTALL_PATH}}" |
|
GUR_COMMAND_NAME="${GUR_COMMAND_NAME##/dev/fd/*}" # When using it in a piped mode, the pipe filename tends to be '/dev/fd/63' |
|
GUR_COMMAND_NAME="${GUR_COMMAND_NAME:-${GUR_COMMAND_DEFAULT_NAME}}" |
|
|
|
if [ "${1}" == "--help" ] || [ "${1}" == "-h" ] || [ "${1}" == "-?" ] |
|
then |
|
echo "Returns the permalink URL to the raw version of a gist's file at a given commit." |
|
echo "Usage: ${GUR_COMMAND_NAME} <filename> [<committish> [<remote>]]" |
|
echo " <filename> The file to generate the permalink URL for." |
|
echo " <committish> The committish object name (like e.g. a commit hash or a tag name) of the target file version." |
|
echo " Defaults to last annotated tag, or last commit if no tag found. Using 'HEAD' as a '<committish>' object, " |
|
echo " generates a permalik URL that will point always to the last committed version of the file." |
|
echo " <remote> The remote to get the URL from. In most cases this doesn't need to be specified, since it will be often 'origin'." |
|
echo " Defaults to 'origin'." |
|
echo "For more info visit ${GUR_DOC}" |
|
exit 0 |
|
elif [ "${1}" == "--version" ] || [ "${1}" == "-v" ] |
|
then |
|
echo "${GUR_COMMAND_NAME} ${GUR_VERSION}" |
|
exit 0 |
|
fi |
|
|
|
[ -z "$(which git)" ] && echo "It doesn't seem to be git installed in this system, or at least is not reachable in the PATH." && exit 0 |
|
|
|
[ -e .git/config ] && gistid="$(git config --get remote.${3:-origin}.url)" |
|
gistid="${gistid#https://gist.github.com/}" |
|
gistid="${gistid#git://github.com/}" # Anonymously cloned gist |
|
gistid="${gistid#git@gist.github.com:}" |
|
gistid="${gistid#git@github.com:}" |
|
gistid="${gistid%.git}" |
|
|
|
[ -z "${gistid##*[!0-9]*}" ] && echo "It doesn't seem to be a gist repo here." && exit 0 |
|
|
|
if [ ! "$1" ] |
|
then |
|
echo "Specify the file to get the permalink for. Try e.g. with following command:" |
|
echo " ${GUR_COMMAND_NAME} $(git ls-tree --name-only HEAD | head -1)" |
|
echo "For more info type:" |
|
echo " ${GUR_COMMAND_NAME} --help" |
|
exit 1 |
|
fi |
|
|
|
commit=${2:-$(git describe --always --abbrev=0)} |
|
gitblob="$(git ls-tree ${commit} "${1}")" |
|
[ ! $? -eq 0 ] && echo "'${commit}' is not a committish object name, like e.g. a commit hash or a tag name." && exit 1 |
|
|
|
[ "$(git tag)" ] && committype="last annotated tag" || committype="last commit" && commit=${commit:0:7} |
|
|
|
GUR_GIST_API_URL="https://api.github.com/gists/${gistid}" |
|
GUR_GIST_API_JSON="$(curl -siL ${GUR_GIST_API_URL})" |
|
GUR_GIST_API_CALL_EXIT_CODE="$?" |
|
|
|
if [ "${GUR_GIST_API_CALL_EXIT_CODE}" != "0" ] |
|
then |
|
# Possibly no internet connection |
|
echo "GitHub API couldn't be reached at ${GUR_GIST_API_URL}" |
|
exit 1 |
|
elif [ -z "$(echo "${GUR_GIST_API_JSON}" | grep "Status.*20*")" ] |
|
then |
|
# Possibly wrong gist id fetched |
|
echo "Oups! Something went wrong while trying to find out more about the gist ${gistid} on the URL ${GUR_GIST_API_URL}" |
|
exit 1 |
|
fi |
|
|
|
GUR_BASE_GIST_URL="$( echo "${GUR_GIST_API_JSON}" | grep "raw_url" | head -1 | sed -e "s|.*\(http.*raw\).*|\1|")" |
|
|
|
url=$(echo "${gitblob}" | sed "s|.* .* \(.*\)[[:space:]]${1}|${GUR_BASE_GIST_URL}/\1/${1}|" | sed "s| |%20|g") |
|
if [ "${url}" ] |
|
then |
|
if [ "${2}" == "HEAD" ] |
|
then |
|
echo "${GUR_BASE_GIST_URL}/${1}" |
|
exit 0 |
|
fi |
|
[ ! $2 ] && echo "Permalink to file '${1}' at ${committype} '${commit}':" |
|
echo "${url}" |
|
else |
|
firstseencommit=$(git log --reverse --format="%H" -- "${1}" | head -1) |
|
lastseencommit=$(git log -1 --format="%H" -- "${1}") |
|
[ "$(git ls-tree --name-only HEAD "${1}")" ] && currentlypresent=true |
|
if [ ${lastseencommit} ] |
|
then |
|
firstseentag=$(git describe --always --abbrev=7 ${firstseencommit}) |
|
lastseentag=$(git describe --always --abbrev=7 ${lastseencommit}^) |
|
echo "File '${1}' not found at $([ ${2} ] && echo "commit" || echo "${committype}") '${commit}'." |
|
echo "Try the following command to get a list of candidate files at this point:" |
|
echo " git ls-tree --name-only ${commit}" |
|
echo "The file was first seen at commit '${firstseentag}' $(git log -1 --format="%cr" "${firstseentag}")," |
|
if [ $currentlypresent ] |
|
then |
|
echo "was last modified at commit '${lastseentag}' $(git log -1 --format="%cr" "${lastseentag}"), and is currently still present at HEAD." |
|
else |
|
echo "and last seen at commit '${lastseentag}' $(git log -1 --format="%cr" "${lastseentag}")." |
|
fi |
|
echo "Try the following command to get a the URL to the raw file at this first and last commit points:" |
|
echo " ${GUR_COMMAND_NAME} ${1} ${firstseentag}" |
|
echo " ${GUR_COMMAND_NAME} ${1} ${lastseentag}" |
|
else |
|
echo "File '${1}' not found in this repository's history." |
|
echo "Try the following command to get a list of candidate files at the $([ ${2} ] && echo "commit" || echo "${committype}") '${commit}':" |
|
echo " git ls-tree --name-only ${commit}" |
|
fi |
|
fi |