Last active
April 7, 2016 20:51
-
-
Save csham/4791d47160a627e54d3f193b508cdd8d to your computer and use it in GitHub Desktop.
For github.com, adds compare buttons next to commits on pull request page so you can easily compare between two commits. Just run contents through a bookmarklet tool like http://chriszarate.github.io/bookmarkleter/ and save as bookmark.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function() { | |
| var comparisonList = []; | |
| var onCompareButtonClick = function() { | |
| var $button = $(this); | |
| if ($button.hasClass("btn-outline")) { | |
| $button.addClass("btn-primary") | |
| .removeClass("btn-outline"); | |
| var sha = $button.data("sha"); | |
| var shaIndex = $button.data("shaIndex"); | |
| comparisonList.push({"sha": sha, "shaIndex": shaIndex}); | |
| if (comparisonList.length === 2) { | |
| $(".compareShaButtons").removeClass("btn-primary") | |
| .addClass("btn-outline"); | |
| openComparison(); | |
| } | |
| } else { | |
| $button.addClass("btn-outline") | |
| .removeClass("btn-primary"); | |
| comparisonList = []; | |
| } | |
| }; | |
| var openComparison = function() { | |
| var baseURL = window.location.href.replace("/commits", ""); | |
| comparisonList.sort(function(a, b) { | |
| if (a.shaIndex < b.shaIndex) { | |
| return 1; | |
| } else if (a.shaIndex > b.shaIndex) { | |
| return -1; | |
| } else { | |
| return 0; | |
| } | |
| }) | |
| var comparisonURL = baseURL + "/files/" + comparisonList[0].sha + ".." + comparisonList[1].sha; | |
| window.open(comparisonURL, "_blank"); | |
| comparisonList = []; | |
| }; | |
| var getShaKeyForRow = function($row) { | |
| var shaURL = $(".sha, .commit-id", $row).attr("href"); | |
| var shaURLParsed = shaURL.split("/commits/"); | |
| return shaURLParsed[1]; | |
| }; | |
| $(".sha, .commit-id").parents(".commit").each(function(index, $commitRow) { | |
| var $compareButtonContainer = null; | |
| if ($(".commit-sig-status", $commitRow).length > 0) { | |
| $compareButtonContainer = $(".commit-sig-status", $commitRow); | |
| } else if ($(".commit-links-group", $commitRow).length > 0) { | |
| $compareButtonContainer = $(".commit-links-group", $commitRow); | |
| } | |
| if ($compareButtonContainer) { | |
| var sha = getShaKeyForRow($commitRow); | |
| var $compareButton = $("<button />") | |
| .addClass("btn") | |
| .addClass("btn-outline") | |
| .addClass("compareShaButtons") | |
| .text("Compare"); | |
| $compareButton.on("click", onCompareButtonClick); | |
| $compareButton.data("sha", sha); | |
| $compareButton.data("shaIndex", index); | |
| $compareButtonContainer.append($compareButton); | |
| } | |
| }); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment