Skip to content

Instantly share code, notes, and snippets.

@mklaber
Last active December 19, 2018 01:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mklaber/ca9ad1a5fe865939428bf3a197b1bdc4 to your computer and use it in GitHub Desktop.
Save mklaber/ca9ad1a5fe865939428bf3a197b1bdc4 to your computer and use it in GitHub Desktop.
GitHub Pull Request against the current branch

README

GitHub helpfully prompts you to create a pull request after pushing a new branch. Unhelpfully, it always suggests PRing against master (or develop or whatever your main branch is). This Tampermonkey/ Greasemonkey script adds a "Compare & pull request (current branch)" button that will bring you to the PR creation page already comparing against the branch you're currently on.

image

How To:

  1. Install the Tampermonkey extension for Chrome or Greasemonkey extension for Firefox
  2. Restart your browser
  3. Click on the "Raw" button of the github-pr-against-current-br.user.js file below
  4. Tampermonkey/Greasemonkey should pick up the fact that it's a *.user.js file and prompt you to install. Click "Install"
  5. Push an update to a branch (or push a new branch)
  6. On GitHub.com, navigate to the tree you want to PR against using the "Branch" dropdown (if you're already on the branch, refresh the page)
  7. Click on "Pull request (current branch)"
  8. Save time

TODO:

  • Get it to listen to changes to the DOM so that it can add itself when GitHub detects a branch (currently requires the branch to be available on page load)
  • Create a Chrome extension
  • Create a Firefox extension
  • Encourage GitHub to add this functionality natively
// ==UserScript==
// @name github-pr-against-current-br
// @namespace mklaber
// @description Adds a Compare & pull request button that compares to the current branch
// @include https://github.com/*/*/tree/*
// @version 1.1
// @grant none
// ==/UserScript==
function log(str) {
if (typeof debug !== 'undefined') { debug(str); }
if (typeof GM_log !== 'undefined') { GM_log(str); return true; }
else if (typeof console !== 'undefined' && console.log) { console.log(str); return true; }
return false;
}
function logError(category, x) {
msg = "Error (" + category + ") - " + x.name + ' - ' + x.message + ' in file <' + x.fileName + '> on line ' + x.lineNumber + ' while viewing ' + page;
log(msg);
}
function $q(selector, root, multi) {
root = root || document;
if (multi) {
return Array.from(root.querySelectorAll(selector));
} else {
return root.querySelector(selector);
}
}
function getBranch() {
return $q('#branch').value;
}
function getRecentBranches() {
return $q('.RecentBranches .RecentBranches-item', document, true);
}
function addButtonToBranch(branch) {
var a = $q('a.btn', branch);
var currentBranchName = getBranch();
log(currentBranchName);
var newBtn = a.cloneNode();
newBtn.innerHTML = a.innerHTML.replace('pull request', 'pull request (current branch)');
newBtn.style.marginLeft = '5px';
newBtn.href = newBtn.href.replace(/\/compare\//, '/compare/' + currentBranchName + '...');
branch.insertBefore(newBtn, a);
}
function getRecentBranchUpdatePanel() {
return $q('.js-updatable-content[data-url$="recently_touched_branches_list"] .RecentBranches');
}
function findAndUpdateBranches() {
var branches = getRecentBranches();
branches.forEach(addButtonToBranch);
}
try {
setTimeout(function() {
findAndUpdateBranches();
}, 1000);
} catch(err) {
logError('github', err);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment