Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include deletions in modified files mask #718

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

jakeleveroni
Copy link
Contributor

@jakeleveroni jakeleveroni commented May 7, 2024

Problem:

The git.getModifiedFiles method does not include removed files.

Solution:

The diff filter being used does not include the deletions (D) mask. Changing the diff-filter from ACMR to ACMRD includes deleted files in the results of the git.getModifiedFiles method.

Related issues:

Fixes #706

Checklist:

  • Added or updated tests
    • unsure of how to test this, suggestions are appreciated!
  • Added or updated documentation
  • Ensured the pre-commit hooks ran successfully

By opening this pull request, you agree that this submission can be released under the same License that covers the project.

@jakeleveroni jakeleveroni changed the title fixes #706 Include deletions in modified files mask Include deletions in modified files mask May 7, 2024
Copy link

netlify bot commented May 7, 2024

Deploy Preview for onerepo canceled.

Name Link
🔨 Latest commit a13484a
🔍 Latest deploy log https://app.netlify.com/sites/onerepo/deploys/66512c185761630008e4756b

Copy link
Owner

@paularmstrong paularmstrong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As implemented, this will break any command that relies on getFilepaths and passes them through for operating on.

This is easy to see with eslint. In the oneRepo repository:

  1. Delete prettier.config.cjs
  2. Run one lint
one lint
 ┌ Filtering ignored files
 └ ✔ 43ms
 ┌ Lint files
 │ ERR Oops! Something went wrong! :(
 │ ERR
 │ ERR ESLint: 8.57.0
 │ ERR
 │ ERR No files matching the pattern "prettier.config.cjs" were found.
 │ ERR Please check for typing mistakes in the pattern.
 └ ✘ 1178ms
 ■ ✘ Completed with errors 1242ms

I don't think it's a good idea to change how getFilepaths works, as that would result in a breaking change here. So, in order to preserve that behavior, we should figure out which is best between:

  1. Add a flag to getModifiedFiles to include deleted or not, eg getModifiedFiles({ includeDeleted: true })

  2. OR – create a separate getModifiedFilesByStatus that returns an object of the shape:

    type ModifiedByStatus = {
      added: Array<string>;
      copied: Array<string>;
      modified: Array<string>;
      deleted: Array<string>;
      copied: Array<string>;
      updated: Array<string>;
    }

    This is likely a better long-term solution that would open more possibilities for other plugins and integrations, but may be slightly more work.

@jakeleveroni
Copy link
Contributor Author

As implemented, this will break any command that relies on getFilepaths and passes them through for operating on.

This is easy to see with eslint. In the oneRepo repository:

  1. Delete prettier.config.cjs
  2. Run one lint
one lint
 ┌ Filtering ignored files
 └ ✔ 43ms
 ┌ Lint files
 │ ERR Oops! Something went wrong! :(
 │ ERR
 │ ERR ESLint: 8.57.0
 │ ERR
 │ ERR No files matching the pattern "prettier.config.cjs" were found.
 │ ERR Please check for typing mistakes in the pattern.
 └ ✘ 1178ms
 ■ ✘ Completed with errors 1242ms

I don't think it's a good idea to change how getFilepaths works, as that would result in a breaking change here. So, in order to preserve that behavior, we should figure out which is best between:

  1. Add a flag to getModifiedFiles to include deleted or not, eg getModifiedFiles({ includeDeleted: true })

  2. OR – create a separate getModifiedFilesByStatus that returns an object of the shape:

    type ModifiedByStatus = {
      added: Array<string>;
      copied: Array<string>;
      modified: Array<string>;
      deleted: Array<string>;
      copied: Array<string>;
      updated: Array<string>;
    }

    This is likely a better long-term solution that would open more possibilities for other plugins and integrations, but may be slightly more work.

Thanks for the info, I also like the second option you mentioned. Ill try to get this updated this week or next week when i have some time. thanks for the feedback!

@jakeleveroni
Copy link
Contributor Author

jakeleveroni commented May 13, 2024

@paularmstrong I updated the original getModifiedFiles method to take an additional parameter to specify whether or not to include deletions. But i also implemented the getModifiedFileByStatus method cause it seemed like it might be useful at some point. I updated the tasks command to use the new method but it could just use the existing getModifiedFiles and pass true to the includeDeletions param. Not sure which way you prefer so lmk.

Additionally im not really sure how to write good tests around this since it requires git state mgmt, any pointers on how i can write meaningful tests for this?

GH Actions were down so i closed and opened the pr to kick them off again since they were stuck

Copy link
Owner

@paularmstrong paularmstrong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. Almost there. I'm happy to take over on this if you don't have time.

modules/git/src/index.ts Outdated Show resolved Hide resolved
modules/git/src/index.ts Outdated Show resolved Hide resolved
@paularmstrong
Copy link
Owner

Additionally im not really sure how to write good tests around this since it requires git state mgmt, any pointers on how i can write meaningful tests for this?

@paularmstrong I updated the original getModifiedFiles method to take an additional parameter to specify whether or not to include deletions. But i also implemented the getModifiedFileByStatus method cause it seemed like it might be useful at some point. I updated the tasks command to use the new method but it could just use the existing getModifiedFiles and pass true to the includeDeletions param. Not sure which way you prefer so lmk.

I think what you've done here is good!

Additionally im not really sure how to write good tests around this since it requires git state mgmt, any pointers on how i can write meaningful tests for this?

I don't think I would write tests… there's a lot more setup necessary that's not really worth the payoff.

@jakeleveroni
Copy link
Contributor Author

jakeleveroni commented May 15, 2024

Nice. Almost there. I'm happy to take over on this if you don't have time.

Thanks for the offer! But I will have time tomorrow to wrap up those changes.

@paularmstrong
Copy link
Owner

@jakeleveroni I like what you did – and your changes helped me realize it could be simplified down to reuse the getModifiedFiles function just by including a single option. Take a look at the commit and let me know what you think.

@jakeleveroni
Copy link
Contributor Author

jakeleveroni commented May 24, 2024

@jakeleveroni I like what you did – and your changes helped me realize it could be simplified down to reuse the getModifiedFiles function just by including a single option. Take a look at the commit and let me know what you think.

Very nice, thats a clever implementation. I like it thanks for the assist! One thing im not quite understanding is the need for the template type ByStatusi see you use it to derive the reutrn type conditionally but does that mean you would need to call the method like so to get the modified by status response: getModifiedFiles<true>({ byStatus: true })?

@paularmstrong
Copy link
Owner

does that mean you would need to call the method like so to get the modified by status response: getModifiedFiles({ byStatus: true })?

Nope! It infers it internally. The getters for getFilepaths, for example, automatically knows the return type is an array of strings because byStatus is not set / false.

I need to dig a little bit more to figure out why I had to drop in the // @ts-expect-error though, so not quite done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Workspace is not included as an affected workspace if only change is a file deletion
2 participants