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

Add .clang-format file which defines coding style as GNU #2851

Closed
wants to merge 1 commit into from

Conversation

ericcurtin
Copy link
Collaborator

@ericcurtin ericcurtin commented Apr 28, 2023

This is particularly useful for those that work on multiple projects
with different styles so they can easily auto-format their code before
submission. Luckily libostree looks like it's pretty standard GNU style
so this file is quite small.

@openshift-ci
Copy link

openshift-ci bot commented Apr 28, 2023

Hi @ericcurtin. Thanks for your PR.

I'm waiting for a ostreedev member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@ericcurtin
Copy link
Collaborator Author

Example of code formatted using git clang-format/clang-format and this file:

#2844

I find this useful, because when different codebases use different style, you can just write it in whatever style you like and auto-format in the end before submission. Before I was spending a lot of time manually editing before submission to ostree. Didn't realize clang-format had GNU these days baked in, nice!

@ericcurtin
Copy link
Collaborator Author

ericcurtin commented Apr 28, 2023

If we wanted we could hook this up to the "Code style" CI build so that all new changes are enforced to use this style, that can be a good compromise instead of re-style the whole codebase. I did that with inotify-tools and never regretted it.

But I'd be happy if we could just to have this checked in here for my own personal use and others that like to autoformat with clang-format.

@ericcurtin
Copy link
Collaborator Author

I think the "Debian Testing" failure is just flakey CI

@ericcurtin ericcurtin force-pushed the clang-format branch 2 times, most recently from 7d8bb8d to d85e5fc Compare April 28, 2023 10:34
@ericcurtin
Copy link
Collaborator Author

I since realised there's a PR for this already, reworked to be just a partial version of:

#2565

Just the .clang-format file part so people can start using it.

@ericcurtin
Copy link
Collaborator Author

ericcurtin commented Apr 28, 2023

I also addressed this comment by @dbnicholson from #2565:

The other part that seems important to me is to have a workflow that enforces it. Without that people will forget to do it and we'll continue to get commits fixing up formatting.

Although I didn't use a plugin to do it, I try to avoid CI plugins because if you ever have to migrate off GitHub or somebody stops maintaining the plugin, it's a pain.

But if you guys would prefer to use a pre-baked plugin, that would work too.

@ericcurtin ericcurtin force-pushed the clang-format branch 19 times, most recently from 21383fa to 8c4a3b9 Compare April 29, 2023 19:55
@ericcurtin ericcurtin force-pushed the clang-format branch 4 times, most recently from 2875603 to e33391c Compare April 29, 2023 20:16
This is particularly useful for those that work on multiple projects
with different styles so they can easily auto-format their code before
submission. Luckily libostree looks like it's pretty standard GNU style
so this file is quite small. I also added a CI check so all new code
follows the style defined in the .clang-format file.
Copy link
Member

@cgwalters cgwalters left a comment

Choose a reason for hiding this comment

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

Not opposed but I think it'd actually be better to just try to land the PR to restyle everything at once.

@@ -1,7 +1,27 @@
#!/usr/bin/env bash
# Tests that validate structure of the source code;
# can be run without building it.
set -euo pipefail
set -eo pipefail
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer we keep this and instead...

set -euo pipefail
set -eo pipefail

if command -v clang-format >/dev/null && [ -n "$GITHUB_ACTOR" ]; then
Copy link
Member

Choose a reason for hiding this comment

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

...use [ -n "${GITHUB_ACTOR:-}" ]

git rebase $THAT_COMMIT

echo -n "Formatting with git clang-format... "
if ! git clang-format $THAT_COMMIT; then
Copy link
Member

Choose a reason for hiding this comment

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

What makes this a git subcommand?

@@ -27,7 +27,7 @@ set -euo pipefail
set -x

# First, basic static analysis
./ci/codestyle.sh
./ci/codestyle.sh clang-format
Copy link
Member

Choose a reason for hiding this comment

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

What does this argument do?

Copy link
Collaborator Author

@ericcurtin ericcurtin Apr 30, 2023

Choose a reason for hiding this comment

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

I can remove the "clang-format" arg if needs be. I added it because I felt the ci/codestyle.sh shouldn't do git operations like fetch, rebase, etc. and apply reformats by default. But it's the only way I could thing of, of getting git clang-format to just look at the changes in a PR. If we reformat the whole codebase though, you can simply call clang-format to check the whole codebase.

Another thing I've seen happen, just to be mindful of it if we start seeing it, is sometimes you have to do version checks eventually like only check if clang-format version is greater than 12. Because sometimes how it reformats between versions can change slightly, and you get into the situation where so some builds thing the style is ok, and some don't depending on clang-format version.

This is the way I did it in inotify-tools CI, is we only support clang-format greater than 11 because earlier versions reformat a little differently:

https://github.com/inotify-tools/inotify-tools/blob/8367bdd1eda71c10ab0a49c694846a622e543807/build_and_test.sh#L80

I didn't reformat the whole codebase in inotify-tools. Like I am proposing here, all new changes to inotify-tools are mandated to correspond with the .clang-format style. It avoids the "git blame" being confusing problem.

@ericcurtin
Copy link
Collaborator Author

Not opposed but I think it'd actually be better to just try to land the PR to restyle everything at once.

Yeah happy to close this

@cgwalters
Copy link
Member

Closing in favor of #2565 which has #2854 as a preparatory fix series

@cgwalters cgwalters closed this May 1, 2023
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.

None yet

2 participants