Skip to content

Instantly share code, notes, and snippets.

@przemoc
Last active February 2, 2022 11:59
Show Gist options
  • Save przemoc/1275673 to your computer and use it in GitHub Desktop.
Save przemoc/1275673 to your computer and use it in GitHub Desktop.
Turn a github.com URL into a git.io URL.
#!/bin/sh
# SPDX-License-Identifier: MIT
## Copyright (C) 2011 Przemyslaw Pawelczyk <przemoc@gmail.com>
##
## This script is licensed under the terms of the MIT license.
## https://opensource.org/licenses/MIT
#
# Usage: gitio URL [CODE]
#
# Turns a github.com URL
# into a git.io URL
#
# Copies the git.io URL to your clipboard if xclip is available.
URL="$1"
CODE="$2"
if ! expr "${URL}" : "\(\(https\?://\)\?\(gist\.\)\?github.com/\)" >/dev/null; then
echo "* github.com URLs only" >&2
exit 1
fi
if ! expr "${URL}" : "http" >/dev/null; then
URL="https://${URL}"
fi
OUT="$(\
curl -si https://git.io -F "url=${URL}" ${CODE:+-F "code=${CODE}"} | \
sed '/^Status: /{s///;/^201/d;q};/^Location: /!d;s///'
)"
if expr "${OUT}" : "[0-9]\+" >/dev/null; then
echo "${OUT}" >&2
exit 1
fi
echo "${OUT}"
which xclip >/dev/null && echo "${OUT}" | xclip -selection clipboard
@rex
Copy link

rex commented Jan 28, 2015

I know I'm super late to this party, but...

Shouldn't you just update your Regex to match the one written by @defunkt in https://gist.github.com/defunkt/1209316#file-gitio-L12?

So this: \(\(https\?://\)\?\(gist\.\)\?github.com/\)
Becomes this: /^(https?:\/\/)?(gist\.)?github.com/ ?

It appears that there have been a number of updates to the original gist since you created this one.

@przemoc
Copy link
Author

przemoc commented Jan 31, 2015

@rex, being super late is fine, but you're also super wrong (sorry, I couldn't resist...).

@defunkt's gist has only 3 revisions and the last one is only a name change. All his revisions are from September 2011. My gist was created later (October 2011) and originally included regexp anchors (^), but it was useless (or even more like wrong as having different behavior in different implementations), because expr matching does anchored pattern matching anyway, so I removed them (November 2011). Regular expressions additionally differ, because ruby's RE flavor, which is Perl-like and closer to Extended Regular Expressions, is different from expr's one, which is Basic Regular Expressions.

And if you're talking about forks, I haven't checked them. I just looked at the forks of my script right now (only 2 introduced some changes) and I don't see any important stuff there. @rduenasf replaced xclip call with pbcopy (which is apparently MacOS thing) and @skopp introduced README.md and renamed the script twice (I simply don't get why people do such superfluous changes at all, but they're surely free to do whatever they want).

I always appreciate suggestions, comments and questions, but I don't like rushed and showy ones, because they usually lack even the most basic research regarding commented matters.

@thorade
Copy link

thorade commented Jul 9, 2015

http://git.io/gitio points here

@przemoc
Copy link
Author

przemoc commented Jan 30, 2018

I pushed very small update - https is used now (it should be used for quite some time already).

@przemoc
Copy link
Author

przemoc commented Feb 6, 2018

commit af12b6b48e6220daab23a92d69957407526aa0a7
Author: Przemyslaw Pawelczyk <przemoc@gmail.com>
Date:   2018-02-06 14:27:24 +0100

    gitio: Add copyright notice and MIT license notice.

commit 6a7f2ebe37ea089adf6762e5591e8e99fe95d4fe
Author: Przemyslaw Pawelczyk <przemoc@gmail.com>
Date:   2018-02-06 14:29:32 +0100

    Add SPDX License Identifier.

    The Software Package Data Exchange (SPDX) is a good initiative, it has
    matured over time and deserves accelerated adoption in open-source.

    https://spdx.org/learn
    https://spdx.org/using-spdx
    https://spdx.org/license-list

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