“Pin” link to commit reference from generic tree reference on GitHub

github

Let's say I'm on a GitHub page for a file in a repository, like so:

https://github.com/apple/swift/blob/master/stdlib/public/SwiftShims/HeapObject.h#L33

Note that this links to line 33 and branch master. If I want to reference this is in an issue or pull request, I'd want a link to the current commit, so that updates to the file don't invalidate my link.

Is there a one-click way to do this?

Obviously, I can click on the commit at the top, copy the SHA, and paste it over master, but that's inconvenient.

Best Answer

Here's a bookmarklet that can grab the commit URL for you. Just copy the code below into a bookmark in your browser, then when you're viewing a page like the one you linked to above, click the bookmark. A box will appear at the bottom of the screen like this one, containing the URL for the commit page:

URL copy box

Press CTRLC to copy the link (which will already be highlighted) and from there you can paste it wherever you need it.

Make sure that when you copy the code into a bookmark that javascript: appears at the beginning. (In some browsers, it doesn't always copy over.)

javascript:
(function() {
  var commitLink = document.getElementsByClassName("message")[0].href;
  var linkModal = document.createElement('input');
  linkModal.type = "text";
  linkModal.innerText = commitLink;
  linkModal.contentEditable = false;
  linkModal.size = commitLink.length * 1.1;
  linkModal.style.zIndex = 10000;
  linkModal.style.border = "2px solid red";
  linkModal.style.borderRadius = "4px";
  linkModal.style.fontFamily = "sans-serif";
  linkModal.style.fontSize = "1em";
  linkModal.style.backgroundColor = "white";
  linkModal.style.color = "black";
  linkModal.style.padding = "3px 7px 4px 7px";
  linkModal.style.position = "fixed";
  linkModal.style.left = "37%";
  linkModal.style.bottom = "0%";
  document.body.appendChild(linkModal);
  linkModal.focus();
  linkModal.select();
})();