BombTodley
BombTodley

Reputation: 567

Ignoring specific files, file types or folders in a pull request diff

We frequently use the Files Changed tab on a pull request to peer review the work we have done on a branch, unfortunately a major part of our development process is regenerating Flex services so when viewing the files changed 99% of the changes are irrelevant. This makes it very easy to miss important changes that should be reviewed.

We know the folder that these regenerated services live in, and we could commit all the regen changes in one commit if that would help.

Does anyone have any suggestions how we can improve this? Ideally we would exclude a folder from the pull request diff.

Upvotes: 40

Views: 20643

Answers (8)

Flawn
Flawn

Reputation: 61

As it seems, you cannot remove files completely from the Pull Request diff. I wrote a little JS Script which you can add with TamperMonkey or similar tools to be applied to GitHub. It removes the files from the sidebar and the bigger file elements from the middle:

document.addEventListener('DOMContentLoaded', () => {

// Select all elements with role="treeitem" - Removes items from the sidebar
const treeItems = document.querySelectorAll('[data-tree-entry-type="file"]');
// Iterate over each treeitem element
treeItems.forEach(item => {
  // Check if the treeitem's text contains the substring "freezed.dart"
  if (item.textContent.includes('freezed.dart')) { // Change here file extensions you want to match.
    item.parentElement.remove()
    // Perform any desired actions with the matched treeitem element
  }
});

// Function to check if an element has the data-file-path attribute containing a substring
function hasFilePathSubstring(element, substring) {
  const filePath = element.getAttribute('data-file-path');
  return filePath && filePath.includes(substring);
}

// Get all elements with the data-file-path attribute containing the substring "freezed.dart" - Removes from the bigger file diff view
const diffItems = Array.from(document.querySelectorAll('[data-file-path]')).filter(element => 
  element.getAttribute('data-file-path') && element.getAttribute('data-file-path').includes('freezed.dart') // Change here file extensions you want to match.
).forEach(item => item.remove());
});

Upvotes: 1

jlyon
jlyon

Reputation: 411

Here's the URL to create a bookmarklet generated on https://caiorss.github.io/bookmarklet-maker/ from Vinicius Brasil's snippet above:

javascript:(function()%7Bconst%20fileElements%20%3D%20document.querySelectorAll(%22*%5Bdata-path*%3D'vendor'%5D%22)%0AfileElements.forEach(el%20%3D%3E%20el.parentElement.remove())%7D)()%3B

Upvotes: 0

Paul Peelen
Paul Peelen

Reputation: 10329

I know this is an old question, but wanted to share my solution anyways. As an iOS developer I can experience this problem as well when updating libraries fetched with Carthage. So, before I perform a review on a pull request I run the following script:

document.querySelectorAll("*[data-path*='Carthage']").forEach( el => {
    const item = el.querySelectorAll("input[type=checkbox][name=viewed]:not(:checked)")[0]
    if ( typeof item !== "undefined" ) {
        setTimeout(function () {
            item.click()
        }, 1000)
    }
})

Run it in you console (DevTools), on Safari on a Mac: CMD + Option + U. It'll find all the "Viewed" unchecked checkboxes for files having Carthage in their path and "click" them. Thereafter, select the filter to not show files that are viewed.

Upvotes: 6

prograhammer
prograhammer

Reputation: 20630

Github supports this now with a .gitattributes file.

  1. Create a .gitattributes file in the root of the repository.

  2. Use the linguist-generated attribute to mark or unmark paths that you would like to be ignored for the repository's language statistics and hidden by default in diffs.

For example, to mark search/index.json as a generated file, add this line to .gitattributes:

search/index.json linguist-generated=true

Reference: https://help.github.com/en/github/administering-a-repository/customizing-how-changed-files-appear-on-github

Upvotes: 34

justapilgrim
justapilgrim

Reputation: 6882

To exclude a certain folder, I've made a tricky script that you can execute via the DevTools.

const fileElements = document.querySelectorAll("*[data-path*='vendor']")
fileElements.forEach(el => el.parentElement.remove())

This will remove any file diffs that matches %vendor%, in my case.

Upvotes: 9

VonC
VonC

Reputation: 1329072

While you cannot exclude files from a Pull Request, you can (since Dec. 2018) filter them.

See "Pull request file filter":

In the “Files changed” tab of a pull request you can now:

  • filter by file type or
  • hide all deleted files in order to stay focused on the diffs that you care about.

See the documentation "Filtering files in a pull request by file type"

https://help.github.com/assets/images/help/pull_requests/file-filter-menu.png

You can see an animated version of that new feature in this tweet.

And don't forget you can already show only file names, with a collapse all/show all toggle, with Alt+Click on the arrow in the diff view.

Note: the filter does not support regexes for now. For that, you would still need a Chrome extension.

Upvotes: 3

Blue
Blue

Reputation: 758

Github now has a little more functionality to navigate a pull request.

You can filter and jump to specific files in a pull request. Pressing t gives you access to this functionality anywhere in the pull request.

You can also, as you mentioned, keep the files you don't want to review in a separate commit. Then you can take advantage of the commit filter feature, which lets you view the changes from just one commit rather than the entire pull request. Pressing c brings up this selector, and p and n allow you to move to the previous and next commit, respectively.

? brings up the list of keyboard shortcuts.

Source: https://github.com/blog/2123-more-code-review-tools

Upvotes: 4

bitoiu
bitoiu

Reputation: 7484

Currently GitHub does not support a way to exclude files or folders from the pull request.

If I had this problem while sending pull requests and it was something that was causing pain to my development team I can only think of the following:

The goal would be to exclude the folders and files from the difference but at the same time you don't want to merge the changes of the services before the whole pull request is ready. This solution is not ideal but you could:

  1. Commit and push the folders you're not interested in reviewing - branch A
  2. Branch out and commit the files you really plan to compare - branch B
  3. Open a pull request from B to A and you'll only see what you are interested on.

A few things I don't like with this suggestion:

  • You would need to automate this in some way, or else it would be too much manual work (a bash script?)
  • If you need to change your code as part of the review you would have to repeat the process, probably because you want those files re-generated. This would defeat the good conversation value of the pull request, where you see a history of changes

Maybe someone has a better flow for this, but I had the same issue with distribution files which I pushed upstream and I dealt with it by just passing them completely. However I imagine your use case is much trickier than mine.

Hope this helps or it gives other users something to start on.

Upvotes: 1

Related Questions