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

8

38

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.

Grimalkin answered 21/11, 2013 at 12:0 Comment(2)
I'm interested in an answer for this question as well.Claudy
are you sure those files shouldn't be in .gitignore in the first place?Lofty
M
32

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

Mindszenty answered 18/11, 2019 at 21:48 Comment(4)
Thnx! For a folder recursive - folder/**/* linguist-generated=true.Photogrammetry
How would all filetypes work? It doesn't seem that the classic wildcard works, i.e. *.filetypeDiploma
For me all the files in the directory marked as "linguist-generated" pop up with empty content(with the "Load diff" button). Is this the expected behavior or is it expected for the files to not come up in the PR?Bookkeeping
I'm seeing the same behaviour as @Bookkeeping the files are still shown in the diff, but you have to click the "Load Diff" button to see the content. This isn't helpful for my purposes as the files I want to exclude are large, so they were displayed like this anyway, and I also want them excluded from the count.Examen
S
9

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.

Simply answered 17/1, 2019 at 11:36 Comment(0)
W
6

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.

Woods answered 18/11, 2019 at 16:19 Comment(1)
This was amazingly helpful to mark thousands of files in vendor folder in PR.Stormproof
H
4

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

Hypoglycemia answered 3/2, 2017 at 20:55 Comment(0)
M
3

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://static.mcmap.net/file/mcmap/ZG-AbGLDKwfoXVYeKmcpcFh0Wi2jaRA/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.

Melanson answered 4/12, 2018 at 22:4 Comment(3)
This does not work for file extensions that are stacked, like *.freezed.dart.Reprisal
@Reprisal That is unfortunate. Did you find any workaround for that use case?Melanson
https://mcmap.net/q/406497/-ignoring-specific-files-file-types-or-folders-in-a-pull-request-diff Wrote a little script yesterday!Reprisal
O
1

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.

Obsidian answered 22/6, 2015 at 4:27 Comment(0)
R
1

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());
});
Reprisal answered 23/4 at 16:0 Comment(0)
P
0

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
Poteen answered 23/1, 2020 at 17:19 Comment(1)
Please consider posting more of the answer here instead of linking to it as this is the preferred method on stack overflow.Coralline

© 2022 - 2024 — McMap. All rights reserved.