Skip to content

Instantly share code, notes, and snippets.

@reefdog
Last active December 6, 2017 23:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save reefdog/227f0b88c10ea798a2b9 to your computer and use it in GitHub Desktop.
Save reefdog/227f0b88c10ea798a2b9 to your computer and use it in GitHub Desktop.
Redirect a GitHub URL to the last-committed version of that thing.
/*!
* Petrify
* Redirect yourself to a permanent version of a GitHub blob or tree.
*
* Say you're looking at `https://github.com/documentcloud/documentcloud/blob/master/public/javascripts/embed/search_embed.js#L315-L317`
* and you want to link to it in an email or GitHub comment. You realize that,
* when people follow the link years or even days later, it's likely the file
* will have changed so much that those line references won't be the same, and
* people won't know what you were talking about.
*
* So you click your Petrify bookmarklet. It recognizes the last time that blob
* or tree were changed and redirects you to the permanent version of that URL:
* `https://github.com/documentcloud/documentcloud/blob/bb009c6875baedc7d58ed699e0b0ef02fc078ccd/public/javascripts/embed/search_embed.js#L315-L317`.
* You can safely share or link to this URL, knowing that years from now, your
* snoopy descendents can still know what you were talking about.
*
* To use, create a bookmark with the following address (sans backticks):
* `javascript:!function(){var t=window.location.href,o=t.match("^https://github.com/([^/]{1,})/([^/]{1,})/(blob|tree)/([^/]{1,})?/?(.*)$");if(o){var e=document.querySelector("a.commit-tease-sha");if(e&&e.href){var a=e.href,i=a.substr(a.lastIndexOf("/")+1),r=["https://github.com",o[1],o[2],o[3],i];o[5]&&r.push(o[5]);var h=r.join("/");h!=t&&(window.location=h)}}}();`
*
* TODO:
* - Copy URL into clipboard instead of redirecting (maybe? and let them know?)
* - Make a GH page with link people can drag to their bookmark bar
*
* @license (c) 2015 Justin Reese, DocumentCloud
* Petrify may be freely distributed under the MIT license.
*
*/
var current = window.location.href;
var matches = current.match('^https:\/\/github.com\/([^/]{1,})\/([^/]{1,})\/(blob|tree)\/([^/]{1,})?\/?(.*)$');
if (matches) {
/*
* matches[0]: Original URL
* matches[1]: Owner slug
* matches[2]: Repo slug
* matches[3]: `blob` or `tree`
* matches[4]: Branch or commit hash
* matches[5]: Anything following (optional)
*/
var lastCommitLink = document.querySelector('a.commit-tease-sha');
if (lastCommitLink && lastCommitLink.href) {
var lastCommitURL = lastCommitLink.href;
var lastHash = lastCommitURL.substr(lastCommitURL.lastIndexOf('/') + 1);
var components = ['https://github.com', matches[1], matches[2], matches[3], lastHash];
if (matches[5]) { components.push(matches[5]); }
var petrified = components.join('/');
if (petrified != current) {
window.location = petrified;
}
}
}
@reefdog
Copy link
Author

reefdog commented Dec 6, 2017

Hey dummy, GitHub does this natively with permanent links. Just hit y on any file page! Dummy!

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