Skip to content

Instantly share code, notes, and snippets.

@rdnelson
Last active February 12, 2016 14:39
Show Gist options
  • Save rdnelson/8e596fd89232cebc0445 to your computer and use it in GitHub Desktop.
Save rdnelson/8e596fd89232cebc0445 to your computer and use it in GitHub Desktop.
Gerrit Changed Files Mover
// ==UserScript==
// @name Gerrit Change Mover
// @version 0.3
// @description Add keybindings to move through only modified files
// @author Robert Nelson
// @match http://gerrit/
// @updateUrl https://gist.githubusercontent.com/rdnelson/8e596fd89232cebc0445/raw/gerrit_move.js
// @downloadUrl https://gist.githubusercontent.com/rdnelson/8e596fd89232cebc0445/raw/gerrit_move.js
// @grant none
// ==/UserScript==
/* jshint -W097 */
'use strict';
var changeid;
var startRev;
var endRev;
var curFile;
var startHash = "";
var endHash = "";
var fileIdx = -1;
var changedFiles = [];
var disableInstall = false;
var changeObserver;
function findAncestor (el, cls) {
while ((el = el.parentElement) && !el.classList.contains(cls));
return el;
}
function ajax(url, callback) {
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
// Success!
var data = JSON.parse(request.responseText.substring(4));
if (callback) {
callback(data);
}
} else {
// We reached our target server, but it returned an error
}
};
request.onerror = function() {
// There was a connection error of some sort
};
request.send();
}
function moveFile(e) {
var active = document.activeElement;
var ignore = findAncestor(active, "CodeMirror-linewidget") !== null || findAncestor(active, "CodeMirror-dialog") !== null;
if (ignore) {
return;
}
if (e.keyCode == 102 || e.keyCode == 54) { // NUM 6
moveNext();
} else if (e.keyCode == 100 || e.keyCode == 52) { // NUM 4
movePrev();
}
}
function moveNext() {
if (changedFiles.length === 0) {
return;
}
fileIdx++;
loadFile();
}
function movePrev() {
if (changedFiles.length === 0) {
return;
}
fileIdx--;
loadFile();
}
function contentLoaded(){
var fileElements = document.getElementsByClassName("GKSE20JDHAB");
if (fileElements.length == 1) {
fileElements[0].innerHTML += " (" + fileIdx + "/" + changedFiles.length + ")";
}
}
function loadFile() {
var prefix = "/#/c/" + changeid + "/" + startRev + ".." + endRev;
if (fileIdx >= changedFiles.length) {
fileIdx = -1;
}
if (fileIdx == -1) {
disableInstall = true;
document.location.href = prefix;
return;
} else if (fileIdx < -1) {
fileIdx = changedFiles.length - 1;
}
disableInstall = true;
document.location.href = prefix + "/" + changedFiles[fileIdx] + ",cm";
var target = document.querySelector('.GKSE20JDB5');
// create an observer instance
changeObserver = new MutationObserver(function() {
contentLoaded();
});
// configuration of the observer:
var config = { childList: true };
// pass in the target node, as well as the observer options
changeObserver.observe(target, config);
}
function load(matches) {
if (matches !== null) {
changeid = matches[1];
startRev = matches[2];
endRev = matches[3];
curFile = matches[4];
if (curFile.endsWith(",cm")) {
curFile = curFile.substring(0, curFile.length - 3);
}
changedFiles = [];
ajax("/changes/" + changeid + "/detail?O=404", function(data) {
for(var rev in data.revisions) {
if (data.revisions[rev]._number == endRev) {
endHash = rev;
} else if (data.revisions[rev]._number == startRev) {
startHash = rev;
}
if (startHash !== "" && endHash !== "") {
break;
}
}
ajax("/changes/" + changeid + "/revisions/" + endHash + "/files?base=" + startHash, function(files) {
for(var file in files) {
changedFiles.push(file);
if (file == curFile) {
fileIdx = changedFiles.length - 1;
}
}
document.addEventListener("keydown", moveFile, false);
});
});
}
}
function install() {
if (disableInstall) {
disableInstall = false;
return;
}
console.log("Checking for Gerrit Range...");
var url = window.location.href;
var matches = /\/#\/c\/(\d+)\/(\d+)\.\.(\d+)\/?(.*)/.exec(url);
if (matches !== null) {
console.log("Found one!");
load(matches);
} else {
console.log("Didn't find one...");
}
}
window.addEventListener("hashchange", install);
install();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment