Skip to content

Instantly share code, notes, and snippets.

@defunkt
Created September 11, 2011 08:11
Show Gist options
  • Select an option

  • Save defunkt/1209316 to your computer and use it in GitHub Desktop.

Select an option

Save defunkt/1209316 to your computer and use it in GitHub Desktop.
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

mrb commented Sep 11, 2011

Copy link
Copy Markdown

Sweet.

@NARKOZ

NARKOZ commented Sep 17, 2011

Copy link
Copy Markdown

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

@defunkt

defunkt commented Sep 17, 2011

Copy link
Copy Markdown
Author

@NARKOZ Updated, thanks!

@shurizzle

Copy link
Copy Markdown

@errordeveloper

Copy link
Copy Markdown

@shurizzle, thanks :)

@shurizzle

Copy link
Copy Markdown

np

@przemoc

przemoc commented Oct 10, 2011

Copy link
Copy Markdown

@saik0

saik0 commented Nov 4, 2011

Copy link
Copy Markdown

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

Copy link
Copy Markdown

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

@rhwy

rhwy commented Jun 22, 2012

Copy link
Copy Markdown

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

hauleth commented May 6, 2013

Copy link
Copy Markdown

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

@mitchellvanw

Copy link
Copy Markdown

👍

ghost commented Oct 16, 2013

Copy link
Copy Markdown

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

@jasonm23

Copy link
Copy Markdown

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

@rsvp

rsvp commented Mar 29, 2015

Copy link
Copy Markdown

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

ghost commented Jan 14, 2016

Copy link
Copy Markdown

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

Copy link
Copy Markdown

@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

Copy link
Copy Markdown

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

Copy link
Copy Markdown

@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

Copy link
Copy Markdown

@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