Hiding annoying diffs on BitBucket pull request pages

Some shops man­date check­ing in long, auto­gen­er­at­ed files into source con­trol that clut­ter up Bit­Buck­et pull request pages with large diffs, leav­ing you to scroll epic dis­tances or depend on the top nav­i­ga­tion to vis­it files key to a code review.

Despite at least three dif­fer­ent tick­ets dat­ing back to 2014 ask­ing Atlass­ian to sup­port exclud­ing spe­cif­ic file diffs from pull request pages, a John Gar­cia explained why they would not imple­ment this fea­ture:

This response is rea­son­able for the ref­er­enced case, yet unhelp­ful to users as a whole. Even if the peo­ple ref­er­enced in the con­ver­sa­tion were using source con­trol inap­pro­pri­ate­ly by track­ing files that they should not track, oth­ers review­ing code might not be in a posi­tion to change that. Then there are big (as in, often has diffs that take up at least one screen) auto­gen­er­at­ed files that fre­quent­ly change, aren’t nor­mal­ly worth read­ing and should be in source con­trol, such as yarn.lock. Cit­ing a Stack­Over­flow answer address­ing one case with 11 votes at the time of this writ­ing does not make the fea­ture request unjus­ti­fied.

I had the same prob­lem as the peo­ple sup­port­ing the tick­et (with many yarn.lock files, no less) so I wrote a user­script cre­ative­ly named “Hide Annoy­ing Bit­Buck­et Diffs (HABD).” It’s set to exe­cute when you view a pull-request page pow­ered by Bit­Buck­et. The script does not sup­port bina­ry files. Instal­la­tion instruc­tions are at the bot­tom of this arti­cle.

In order for this script to func­tion, you will need to con­fig­ure a cou­ple of things. Thank­ful­ly it’s quick!

You can con­trol what diffs to remove by ref­er­enc­ing a plain text resource list­ing one reg­u­lar expres­sion per line (Point­ing to a raw Paste­bin helps here). A diff is removed from the pull request page if the file­name cor­re­spond­ing to that diff match­es ANY of the expres­sions in the resource. 

For exam­ple, this resource hides file diff blocks for package.json and yarn.lock, both of which are com­mon in Node projects and are checked into source con­trol.

package.json$
yarn.lock$

If we’re clear up to this point and you want to use the script, let’s get you set up. If you encounter any prob­lems, please report an issue.

Script installation for TamperMonkey

  1. While using Chrome, add Tam­per­Mon­key.
  2. Go to HABD’s page on OpenUserJs.org and click “Install.” You will see the source code and a con­fir­ma­tion. Con­firm the instal­la­tion.
  3. Click the Tam­per­mon­key icon in Chrome and click “Dash­board.” You will see a list of installed scripts.
  4. At the far right of the row where you see HABD, you will see a lit­tle notepad icon. Click that to edit the script.
  5. Change the line start­ing with “@resource” to point to a raw Paste­Bin or some oth­er clear text resource with one reg­u­lar expres­sion per line. The link already in the script points to an exam­ple. Remem­ber that the reg­u­lar expres­sions match against file­names. If ANY of the expres­sions match, the diff is removed from the page.
  6. (Option­al) Change the line start­ing with “@match” so that it match­es only the pages in which you review Bit­Buck­et pull requests.
  7. (Option­al) If you want to hide diffs on both pull request and com­mit pages, use the @include rule to add anoth­er URL match­ing pat­tern.

Script installation for GreaseMonkey

  1. While using Fire­fox (or some oth­er sup­port­ed brows­er), add Grease­Mon­key.
  2. Go to HABD’s page on OpenUserJs.org and click “Install.” You will see the source code and a con­fir­ma­tion. Con­firm the instal­la­tion.
  3. Click the arrow next to the Grease­Mon­key icon in Fire­fox and click “Man­age User Scripts.” You will see a list of installed scripts.
  4. On the row for “Hide Annoy­ing Bit­buck­et Diffs”, click “Pref­er­ences,” then “Edit This User Script.”
  5. Change the line start­ing with “@resource” to point to a raw Paste­Bin or some oth­er clear text resource with one reg­u­lar expres­sion per line. The link already in the script points to an exam­ple. Remem­ber that the reg­u­lar expres­sions match against file­names. If ANY of the expres­sions match, the diff is removed from the page.
  6. (Option­al) Change the line start­ing with “@match” so that it match­es only the pages in which you review Bit­Buck­et pull requests.
  7. (Option­al) If you want to hide diffs on both pull request and com­mit pages, use the @include rule to add anoth­er URL match­ing pat­tern.
Do NOT follow this link or you will be banned from the site!