Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zmarkan/503789d31acf385e44f13b0b3c3c7af4 to your computer and use it in GitHub Desktop.
Save zmarkan/503789d31acf385e44f13b0b3c3c7af4 to your computer and use it in GitHub Desktop.
Bookmarklet: Compare GitHub commit with Master

Usage

  1. Create a new bookmark in your bookmarks bar with the following contents:
javascript: (function(){
            let location = window.location.href;
            let validatorRegex = /https:\/\/github.com\/[a-z][a-zA-z0-9\-\_]+\/[a-z][a-zA-z0-9\-\_]+\/commit\/[a-f0-9]+/g;
            let match = location.match(validatorRegex);
            if(!match){
                console.log('No match. You must be viewing a commit.');
                alert('Use this bookmarklet when viewing a commit.');
            }
            else{
                console.log('Location matches a commit - redirecting to diff view.');        
                let repoLocationRegex =  /https:\/\/github.com\/[a-z][a-zA-z0-9\-\_]+\/[a-z][a-zA-z0-9\-\_]+/g;
                let commitRegex = /\/commit\/[a-f0-9]+/g;
                let repoLocation = location.match(repoLocationRegex)[0];
                let commitId = location.match(commitRegex)[0].slice(8);
                console.log(repoLocation);
                console.log(commitId);
                let compareLocation = `${repoLocation}/compare/${commitId}...master `;
                window.location.href = compareLocation;
            }})();
  1. Browse to a commit you wish to compare with its current master branch, and press the bookmarklet.
(function(){
let location = window.location.href;
let validatorRegex = /https:\/\/github.com\/[a-z][a-zA-z0-9\-\_]+\/[a-z][a-zA-z0-9\-\_]+\/commit\/[a-f0-9]+/g;
let match = location.match(validatorRegex);
if(!match){
console.log('No match. You must be viewing a commit.');
alert('Use this bookmarklet when viewing a commit.');
}
else{
console.log('Location matches a commit - redirecting to diff view.');
let repoLocationRegex = /https:\/\/github.com\/[a-z][a-zA-z0-9\-\_]+\/[a-z][a-zA-z0-9\-\_]+/g;
let commitRegex = /\/commit\/[a-f0-9]+/g;
let repoLocation = location.match(repoLocationRegex)[0];
let commitId = location.match(commitRegex)[0].slice(8);
console.log(repoLocation);
console.log(commitId);
let compareLocation = `${repoLocation}/compare/${commitId}...master `;
window.location.href = compareLocation;
}})();
@TFWol
Copy link

TFWol commented Jun 3, 2023

Thanks, this is useful. I had to change master to main for it to work.

javascript: (function(){
            let location = window.location.href;
            let validatorRegex = /https:\/\/github.com\/[a-z][a-zA-z0-9\-\_]+\/[a-z][a-zA-z0-9\-\_]+\/commit\/[a-f0-9]+/g;
            let match = location.match(validatorRegex);
            if(!match){
                console.log('No match. You must be viewing a commit.');
                alert('Use this bookmarklet when viewing a commit.');
            }
            else{
                console.log('Location matches a commit - redirecting to diff view.');        
                let repoLocationRegex =  /https:\/\/github.com\/[a-z][a-zA-z0-9\-\_]+\/[a-z][a-zA-z0-9\-\_]+/g;
                let commitRegex = /\/commit\/[a-f0-9]+/g;
                let repoLocation = location.match(repoLocationRegex)[0];
                let commitId = location.match(commitRegex)[0].slice(8);
                console.log(repoLocation);
                console.log(commitId);
                let compareLocation = `${repoLocation}/compare/${commitId}...main`;
                window.location.href = compareLocation;
            }})();

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