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

Req: apply the check selectively on just the changed files #83

Open
ns-osmolsky opened this issue Jan 19, 2022 · 11 comments
Open

Req: apply the check selectively on just the changed files #83

ns-osmolsky opened this issue Jan 19, 2022 · 11 comments

Comments

@ns-osmolsky
Copy link

Context: a sizeable code base into which I had added clang-format. People use it... but only a subset of source lines is up to the spec. Obviously, I can run clang-format on all files... but that will pollute Git history.

Ask: apply clang-format just to the delta that is being merged.

Justification: this way the large code base will slowly and inevitably converge.

@jidicula
Copy link
Owner

A couple of approaches I can think of:

  • Does the check-path argument help with this? For example, if some directories are formatted but others are not, you could set up a matrix option for check-path and progressively add directories as they become clean.

  • Alternatively, you could use the exclude-regex to ensure this action doesn't check the noncompliant files.

Otherwise, if your request is to run clang-format checks on just a few lines that have changed (and not whole files), I'm not sure that's possible.

If I were in your shoes I'd still bite the bullet and just run it repo-wide, git history pollution be damned (that way all the formatting changes are isolated to a single commit). Then, if I need to use git blame for archaeological reasons, I'd ignore specific commits with the --ignore-rev option.

@ns-osmolsky
Copy link
Author

A couple of approaches I can think of:

  • Does the check-path argument help with this? For example, if some directories are formatted but others are not, you could set up a matrix option for check-path and progressively add directories as they become clean.
  • Alternatively, you could use the exclude-regex to ensure this action doesn't check the noncompliant files.

Unfortunately, in this codebase, very few files are fully clang-format-compliant. I am trying to figure out a way to improve on that.

Otherwise, if your request is to run clang-format checks on just a few lines that have changed (and not whole files), I'm not sure that's possible.

Well, here is a tool that people use in their pre-commit hooks: https://github.com/llvm/llvm-project/blob/main/clang/tools/clang-format/clang-format-diff.py

I run it in my local repo via another helper script... but it comes down to this:

git diff -U0 --no-color --relative HEAD^ | clang-format-diff.py -p1 -i

I thought the Action may be able to do something similar. Basically we'd have to get a diff to the top of the destination branch, filter the source files and then check the resulting delta.

@ns-osmolsky
Copy link
Author

I've just tried running the action and it simply scans files in the predefined directories. So, here are two feature requests:

  1. The Action should just scan the changed file set, as scanning the whole tree is only feasible for projects that are already fully compliant. Also, scanning the whole tree is too costly when you have thousands on .cc/.cpp files...
  2. It would be nice to have an option to scan just the changed regions of the files. The mechanics of such a mode are described above and this mode is predicated on the "selective" scan in Item (1).

@ns-osmolsky ns-osmolsky changed the title Q: is there a way to run the action only on the changed lines? Req: apply the check selectively on just the changed files Jan 22, 2022
@r-barnes
Copy link

I would also find this useful.

@jidicula
Copy link
Owner

jidicula commented Oct 1, 2022

Implementation caveat: need to be careful about differences in clang-format-py in older versions of clang-format - at some point, the tool switched from Python 2 to 3.

Possible solution: build a separate set of Docker images in the GHCR for this repo and base them on Ubuntu Python images for 2 or 3, depending on the major version of clang-format. Build definitions would be similar to those in https://github.com/jidicula/clang-format-action/blob/main/.github/workflows/clang-format-image.yml

@linuxmaniac
Copy link

clang-format-${CLANG_FORMAT_VERSION} package should provide already /usr/bin/clang-format-diff- ${CLANG_FORMAT_VERSION}
For instance: https://packages.ubuntu.com/jammy/amd64/clang-format-14/filelist
and the package already depends on the proper Python version, see: https://packages.ubuntu.com/jammy/clang-format-14

@jidicula
Copy link
Owner

clang-format-${CLANG_FORMAT_VERSION} package should provide already /usr/bin/clang-format-diff- ${CLANG_FORMAT_VERSION}
For instance: https://packages.ubuntu.com/jammy/amd64/clang-format-14/filelist
and the package already depends on the proper Python version, see: https://packages.ubuntu.com/jammy/clang-format-14

Neat, and it's available for all versions this action supports! https://packages.ubuntu.com/bionic/amd64/clang-format-3.9/filelist

linuxmaniac added a commit to linuxmaniac/clang-format-action that referenced this issue May 17, 2023
Possible solution for jidicula#83
@linuxmaniac
Copy link

@jidicula what about linuxmaniac@91849f4 as a base for getting this done?

Can you please take a look?

@tnoczyns-volue
Copy link

@linuxmaniac +1 for the feature.
@jidicula any chances of integrating this in the near future?

@moran-inadvantage
Copy link

+1. This feature would cut our pipeline down significantly.

moran-inadvantage pushed a commit to Innovative-Advantage/ia-clang-format-action that referenced this issue Nov 22, 2023
Possible solution for jidicula#83
@danra
Copy link

danra commented Apr 17, 2024

@moran-inadvantage How about PRing your branch?

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

No branches or pull requests

7 participants