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

modified_files and modified_lines_in_file on CommitMsg #727

Merged
merged 6 commits into from
Sep 22, 2020
Merged

modified_files and modified_lines_in_file on CommitMsg #727

merged 6 commits into from
Sep 22, 2020

Conversation

Quintasan
Copy link
Contributor

As I promised – I did try my hand and I employed the dumbest approach that's almost not copy & paste*

Big problem

It's 2 in the morning so I'm probably cognitively impaired enough to not be able to figure out why rspec spec/overcommit/hook_context/commit_msg_spec.rb:636:705:764 are failing.

All those fails have amending in common so I suspect this is due to calling amendment? not from the Overcommit::HookContext::CommitMsg hook context but from the Overcommit::HookContext::PreCommit context which I created using Overcommit::HookContext::PreCommit.new(@config, @args, @input) but I am unable to pinpoint why would it behave differently.

Good points

  1. Reuses a big chunk of Overcommit::HookContext::PreCommit so in the future you'd have to modify only that

Bad points

  1. store_modified_times is copypasta
  2. restore_modified_times is copypasta
  3. modified_lines_in_file is quite close to being copypasta
  4. Test are copypasta – can be solved by converting into shared examples*

Fixing 1, 2, 3 would require some changes in the PreCommit hook context but I didn't want to throw a huge PR right away without asking first.

@sds Please let me know if this looks reasonable or I need another approach.

Closes #726

@sds
Copy link
Owner

sds commented Sep 20, 2020

Thanks for the PR, @Quintasan. Supportive of the general direction.

Looking over this quickly, I don't have an obvious explanation for the failing tests, but my guess would be we'd probably have an easier time extracting the shared logic into a module that we include in both Overcommit::HookContext::CommitMsg and Overcommit::HookContext::PreCommit.

I'm also OK having the tests be duplicated rather than using shared examples. There are often subtle differences between hooks and I don't think DRY tests in this context would be worth it, even if right now we expect the same behavior.

Appreciate you proposing this general direction. If we can extract the environment setup/cleanup logic into a module and get the tests passing that way, I'd be comfortable merging. By extracting into a module, perhaps we could finally (separate from this PR, of course) consider implementing a mechanism that doesn't use the stash (see #238).

@Quintasan
Copy link
Contributor Author

Great, I hope to have something you can work with in the upcoming week

This way, if another hook context needs this behavior it can be included
using Overcommit::HookContext::Helpers::StashUnstashedChanges.
Methods for determining which files were modified and on which lines can
be imported into hook context with
Overcommit::HookContext::Helpers::FileModifications.
@Quintasan
Copy link
Contributor Author

Quintasan commented Sep 20, 2020

@sds It turns out I couldn't wait this much to work on this.

I wasn't sure where to put the extracted modules so I went with Helpers module but I think SharedBehavior would work better.

Another thing I thought of was to rename setup_environment and cleanup_environment in the new module so we don't stealthily modify behavior just by including the module.

WDYT?

Copy link
Owner

@sds sds left a comment

Choose a reason for hiding this comment

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

Overall looks good. Some minor comments to address and then we'll happily merge. Thanks!


# Restores the file modification times for all modified files to make it
# appear like they never changed.
def restore_modified_times
Copy link
Owner

Choose a reason for hiding this comment

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

Let's have just one module that defines everything rather than splitting up into a separate module for stashing changes and handling file modification time. I don't see the value in separating them as they are both necessary to provide the functionality desired. Let me know if I'm misunderstanding the intent.

Copy link
Contributor Author

@Quintasan Quintasan Sep 21, 2020

Choose a reason for hiding this comment

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

@sds the intention here was to provide a way to reuse methods related to file modifications without assuming that the workflow will always be 'we must hide unstaged changes for the hook context'.

Gemfile Outdated Show resolved Hide resolved
@sds sds merged commit 1c8b179 into sds:master Sep 22, 2020
@sds
Copy link
Owner

sds commented Sep 22, 2020

Thanks!

@sds
Copy link
Owner

sds commented Sep 22, 2020

@Quintasan I'm seeing a warning once this commit is added. Can you have a look?

~/overcommit/lib/overcommit/hook/pre_commit/execute_permissions.rb:29: warning: Overcommit::Hook::PreCommit::Base#initial_commit? at ~/.rbenv/versions
/2.6.5/lib/ruby/2.6.0/forwardable.rb:158 forwarding to private method Overcommit::HookContext::PreCommit#initial_commit?

@sds
Copy link
Owner

sds commented Sep 25, 2020

Thank you for adding this functionality and taking it through to completion, @Quintasan.

Released in 0.56.0.

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

Successfully merging this pull request may close these issues.

modified_files and modified_lines_in_file on CommitMsg
2 participants