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 a clang-format format check in github pull request actions. #661

Merged
merged 4 commits into from
Feb 3, 2021
Merged

Add a clang-format format check in github pull request actions. #661

merged 4 commits into from
Feb 3, 2021

Conversation

hzeller
Copy link
Collaborator

@hzeller hzeller commented Feb 2, 2021

Issues #653

Signed-off-by: Henner Zeller h.zeller@acm.org

Issues #653

Signed-off-by: Henner Zeller <h.zeller@acm.org>
@googlebot googlebot added the cla: yes All contributors in pull request have signed the CLA with Google. label Feb 2, 2021
@mithro mithro requested a review from umarcor February 2, 2021 21:36
@mithro
Copy link
Collaborator

mithro commented Feb 2, 2021

@hzeller -- You might want to add a commit which deliberately break formatting to test it does the right thing?

@hzeller
Copy link
Collaborator Author

hzeller commented Feb 2, 2021

I did that in my local test repository: https://github.com/hzeller/verible/runs/1817888890

VerifyFormatting:
runs-on: ubuntu-20.04

steps:
Copy link
Contributor

Choose a reason for hiding this comment

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

Unless you want to have a separate status report and shield/badge for showing the status of this specific test/job independently from others, I would suggest not to create this new workflow. Instead, just copy this job (L7-L19) into the existing workflow.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done. I mostly started the new file while experimenting with the new flow, but merging it now makes it simpler.

# See the License for the specific language governing permissions and
# limitations under the License.

sudo apt-get install clang-format
Copy link
Contributor

Choose a reason for hiding this comment

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

Unless this is used somewhere else, I would not recommend adding it as a separate file. Just put it in the workflow. In the current state, this script is platform specific, so not really very useful outside of that specific CI job. If/when it is modified for supporting installation on different environments, then it'll make sense to have it be a file.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sounds good. Done.

Copy link
Member

@tgorochowik tgorochowik left a comment

Choose a reason for hiding this comment

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

LGTM in general

@@ -0,0 +1,31 @@
#!/bin/bash
# Copyright 2020 The Verible Authors.
Copy link
Member

Choose a reason for hiding this comment

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

2021 😉

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

FORMAT_OUT=${TMPDIR:-/tmp}/clang-format-diff.out

# TODO(hzeller): only run on the files that are checked out
clang-format -i --style=Google $(find . -name "*.h" -o -name "*.cc")
Copy link
Member

Choose a reason for hiding this comment

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

We could probably use something like:
$(git ls-tree HEAD -r --name-only | grep --color=never -e '\.cc$\|\.h$')

But assuming that this is run before building and before anything is generated I think find should be okay too

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Both these assume that the files are local changed. Though I assume within the pull request environment it will already have applied the patch already as we have a clean head without diffs.
So maybe I need something like

git diff --name-only --diff-filter=AM HEAD~1

? Will have to try.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So I did some experiments in a testing branch, trying to see how the git repository looks like when we're called in an action in a pull request

It looks weird: it looks like a git repository with a single branch (named like the branch from which is pushed) ,but no master branch. There is also only a single commit on it. So it looks like it clones everything, applies all the previous patches in the pull request, uses that as 'git init' baseline in a fresh repository with a single branch and then applies a single patch, which is the last push in that pull request.

Anyway, so this means, not even a git diff --name-only --diff-filter=AM -r origin/master will work.
Apparently, there are github actions scripts on the marketplace that can find all changed files; will have to look into that.

So I suggest for now, we leave it with the find all the files and apply clang-format to them. Which is fine, as we expect the code-base to be clean all the time so that we can afford running the format over all the files, even the ones not touched in the pull request.

Signed-off-by: Henner Zeller <h.zeller@acm.org>
Signed-off-by: Henner Zeller <h.zeller@acm.org>
@mithro
Copy link
Collaborator

mithro commented Feb 3, 2021

You can change how the git repository is cloned.

@mithro
Copy link
Collaborator

mithro commented Feb 3, 2021

You can change how the git repository is cloned.

https://github.com/actions/checkout

    # Number of commits to fetch. 0 indicates all history for all branches and tags.
    # Default: 1
    fetch-depth: ''

    # Whether to checkout submodules: `true` to checkout submodules or `recursive` to
    # recursively checkout submodules.
    #
    # When the `ssh-key` input is not provided, SSH URLs beginning with
    # `git@github.com:` are converted to HTTPS.
    #
    # Default: false
    submodules: ''

@mithro
Copy link
Collaborator

mithro commented Feb 3, 2021

@hzeller
Copy link
Collaborator Author

hzeller commented Feb 3, 2021

Aha, bingo, this seems to be the ticket. Here the output of a run in my test repository: https://github.com/hzeller/verible/runs/1825048661
Thanks @mithro

Signed-off-by: Henner Zeller <h.zeller@acm.org>
@hzeller
Copy link
Collaborator Author

hzeller commented Feb 3, 2021

Not sure why it didn't run the actions now after the last commit, but it should work now :)
In my test-repo, I did a run that only affected one file, and the github action picked only that up.

@hzeller hzeller merged commit 79e0a3c into chipsalliance:master Feb 3, 2021
@umarcor
Copy link
Contributor

umarcor commented Feb 3, 2021

FTR, the actions/checkout is both clever and annoying. Precisely, depth: 0 was added in May, after I reported problems because of the weird checkout procedure: actions/checkout#250. However, it's nice cause it allow reusing the config easily: actions/checkout#300.

@umarcor
Copy link
Contributor

umarcor commented Feb 3, 2021

Not sure why it didn't run the actions now after the last commit, but it should work now :)
In my test-repo, I did a run that only affected one file, and the github action picked only that up.

@hzeller, when merging both workflows, the indentation was not the same. At the same time, a | symbol was missing. That's why CI was not running. See #664.

@hzeller
Copy link
Collaborator Author

hzeller commented Feb 3, 2021

Ah, interesting, thanks for hunting down the syntax issues.
Where does github show if actions are not running due to syntax issues ?

@umarcor
Copy link
Contributor

umarcor commented Feb 4, 2021

You go to the Actions tab above. You see, e.g.:

Fix env-MODE: got unindented in last commit (#665) Signed-off-by: Henner Zeller h.zeller@acm.org
verible-ci #272: Commit 898d507 pushed by hzeller

That's correct, because "verible-ci" is shown in the second line.

When the workflow is defective, you see .github/workflows/verible-ci.yml in there. That is, the path to the file, instead of the name of the workflow. The same happens with the items in the sidebar.

Unfortunately, all that is dynamically generated. Therefore, as soon as we fixed it, those failing items are not shown anymore. That's why I cannot point you to an specific example ATM.

Anyway, the error just points to a line in the workflow. In this case, it was not intuitive to know that the problem was indentation. I knew it because I had hit that problem before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes All contributors in pull request have signed the CLA with Google.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants