Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Turn a github.com URL into a git.io URL.
#!/usr/bin/env ruby
# Usage: gitio URL [CODE]
#
# Turns a github.com URL
# into a git.io URL
#
# Copies the git.io URL to your clipboard.
url = ARGV[0]
code = ARGV[1]
if url !~ /^(https?:\/\/)?(gist\.)?github.com/
abort "* github.com URLs only"
end
if url !~ /^http/
url = "https://#{url}"
end
if code
code = "-F code=#{code}"
end
output = `curl -i http://git.io -F 'url=#{url}' #{code} 2> /dev/null`
if output =~ /Location: (.+)\n?/
puts $1
`echo #$1 | pbcopy`
else
puts output
end
@mrb

This comment has been minimized.

Copy link

@mrb mrb commented Sep 11, 2011

Sweet.

@NARKOZ

This comment has been minimized.

Copy link

@NARKOZ NARKOZ commented Sep 17, 2011

Change the url pattern to /^(https?:\/\/)?(gist\.)?github.com/ if you want to gitio gist urls too.

@defunkt

This comment has been minimized.

Copy link
Owner Author

@defunkt defunkt commented Sep 17, 2011

@NARKOZ Updated, thanks!

@shurizzle

This comment has been minimized.

Copy link

@shurizzle shurizzle commented Sep 17, 2011

@errordeveloper

This comment has been minimized.

Copy link

@errordeveloper errordeveloper commented Oct 5, 2011

@shurizzle, thanks :)

@shurizzle

This comment has been minimized.

Copy link

@shurizzle shurizzle commented Oct 5, 2011

np

@przemoc

This comment has been minimized.

Copy link

@przemoc przemoc commented Oct 10, 2011

@saik0

This comment has been minimized.

Copy link

@saik0 saik0 commented Nov 4, 2011

I get 503 if a URL has already been shortened, if they are unique is there any way to retrieve an shorturl for a given longurl?

@bakotaco

This comment has been minimized.

Copy link

@bakotaco bakotaco commented Feb 16, 2012

Made a small fix to make this not report an error on non-os x environments: https://gist.github.com/1844374

@rhwy

This comment has been minimized.

Copy link

@rhwy rhwy commented Jun 22, 2012

Maybe you should change the pattern again in order to get the raw version of the gists (useful from a shell perspective ;-)
->
if url !~ /^(https?://)?((gist|raw).)?github.com/

@hauleth

This comment has been minimized.

Copy link

@hauleth hauleth commented May 6, 2013

It is great if you name it git-io in your path. Then you can use it with git io.

@mitchellvanw

This comment has been minimized.

Copy link

@mitchellvanw mitchellvanw commented Aug 16, 2013

👍

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Oct 16, 2013

I notice you can pass codes, where can I find out what codes can be passed and what they do?

@jasonm23

This comment has been minimized.

Copy link

@jasonm23 jasonm23 commented Nov 15, 2014

Any chance that githubusercontent.com will be handled by git.io too?

@rsvp

This comment has been minimized.

Copy link

@rsvp rsvp commented Mar 29, 2015

This will handle raw code: https://git.io/git.io and insure uniqueness -- written in Bash though.

@branderxero

This comment has been minimized.

Copy link

@branderxero branderxero commented Jan 14, 2016

the urls has changed. it's now a post to git.io/create but the code option no longer seems to work 😭 no more vanity urls

@basicallydan

This comment has been minimized.

Copy link

@basicallydan basicallydan commented Jan 31, 2016

@branderxero are you sure /create works? Has the request structure changed in any other way? I can't get it to work. Just keeps giving me a 301 pointing to whatever URL I use.

@basicallydan

This comment has been minimized.

Copy link

@basicallydan basicallydan commented Jan 31, 2016

Okay, I've had some success. /create doesn't make any difference, and you can still use the code option but the request has to be secure. https://git.io

@dwijnand

This comment has been minimized.

Copy link

@dwijnand dwijnand commented Feb 21, 2016

@basicallydan Are you sure you can still use the code option? Can you provide a complete example? Because I'm finding that I can't..

@tanepiper

This comment has been minimized.

Copy link

@tanepiper tanepiper commented Feb 21, 2016

@basicallydan @dwijnand - Yep seems code no longer works. A bug was reported in my library (https://github.com/tanepiper/node-gitio) and I can't get it to work with new urls (test pass because it's older urls that were set)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.