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

feat: add pre-commit hooks #712

Merged
merged 1 commit into from
Nov 12, 2024

Conversation

danthorpe
Copy link
Contributor

@danthorpe danthorpe commented Apr 8, 2024

I took a shot at adding a pre-commit hooks files, according to this guide.

Fixes #676.


Once this is merged, it can be used in your other swift packages, by adding the following to the .pre-commit-config.yaml under the repos section, according to the guide.

  - repo: https://github.com/apple/swift-format
    rev: <commit hash / tag>
    hooks:
      - id: swift-format

@danthorpe danthorpe force-pushed the danthorpe/add_pre_commit_hooks branch from df58814 to 756fbb3 Compare April 10, 2024 13:06
@Feuermurmel
Copy link

Feuermurmel commented Apr 14, 2024

I'm test-driving the hook from this MR in one of my projects using this configuration:

repos:
  - repo: https://github.com/danthorpe/swift-format
    rev: danthorpe/add_pre_commit_hooks
    hooks:
      - id: swift-format

There are two things I noticed:

  • I think the hook should have require_serial: true set, because swift-format already processes the input files in parallel.

    When require_serial is set to true, only a single process is started for the hook, with all file names passed to it.

  • IMHO it would be nice if swift-format printed the names of files that have been changed. It gives a bit more context as to why the hook failed as well as how many files have been changed.

    I'm mainly a Python dev and I'm using black and isort in most of my projects. Compare their output to that of swift-format:

    image

    AFACIT, swift-format has no option to enable that right now.

In any case, happy to see this moving forward! 😊

@danthorpe
Copy link
Contributor Author

Thanks for the comments @Feuermurmel - I wasn't familiar with the require_serial option, I'll take a look into and update the branch/PR.

@danthorpe
Copy link
Contributor Author

👋 @ahoppen @MaxDesiatov any update on this one? Would be really handy to get merged so that people can use swift-format in their pre-commit hooks. 🙏

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Sorry for taking so long to get back to you on this @danthorpe.

Thanks you for providing the integration. The change looks good to me. Just a very minor comments:

  • Could you add a trailing newline to the YAML file?
  • Could you rebase your changes so they are a single commit without a merge from main into your branch? That generally makes for a nicer Git history

Also, pushing changes should trigger the new GitHub Actions CI, which is required to merge the changes.

@danthorpe
Copy link
Contributor Author

@ahoppen - done, thanks! 🎉

language: swift
types: [swift]
require_serial: true

Copy link
Member

Choose a reason for hiding this comment

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

The trailing newline has two spaces, could you remove them to make the YAML lint pass?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, done! 🤞

@danthorpe danthorpe force-pushed the danthorpe/add_pre_commit_hooks branch from a45febf to 2833b73 Compare October 30, 2024 10:46
language: swift
types: [swift]
require_serial: true

Copy link
Member

Choose a reason for hiding this comment

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

It looks like the file still has a space here on the last line (GitHub is showing the indicator that the file doesn’t have a trailing newline).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, yep I've removed the space. Not sure if there is a new trailing line now though.

Copy link
Member

Choose a reason for hiding this comment

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

Great. Looks good now.

@danthorpe danthorpe force-pushed the danthorpe/add_pre_commit_hooks branch from 2833b73 to 620f80b Compare October 30, 2024 20:02
@ahoppen
Copy link
Member

ahoppen commented Oct 31, 2024

@danthorpe Your changes look good, I’m investigating the unrelated CI failures.

@danthorpe
Copy link
Contributor Author

@ahoppen - just following the discussion in swiftlang/github-workflows#61 - and I'm happy to rebase my branch to get the latest changes from upstream.

I don't think it's an unreasonable request with OSS contributions. 😀

auto-merge was automatically disabled November 11, 2024 17:16

Head branch was pushed to by a user without write access

@danthorpe danthorpe force-pushed the danthorpe/add_pre_commit_hooks branch from 620f80b to 5e4caa8 Compare November 11, 2024 17:16
@ahoppen
Copy link
Member

ahoppen commented Nov 12, 2024

Thanks for rebasing, @danthorpe. swiftlang/github-workflows#61 diverged a little more than I expected 😄

@ahoppen ahoppen merged commit 0c05520 into swiftlang:main Nov 12, 2024
19 checks passed
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.

Request for official .pre-commit-hooks.yaml
3 participants