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

Allow pre-commit to run in subpath #2007

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

LecrisUT
Copy link
Contributor

@LecrisUT LecrisUT commented Apr 19, 2023

Took some mucking around, but I've finally got something half-decent approach. It almost works other than #2006. Here is an example for testing:

  - repo: https://github.com/LecrisUT/tmt.git
    rev: pre-commit
    hooks:
      - id: tmt-lint
        args:
          - "--root"
          - "path/to/fmf"

Fixes: #2004

@lukaszachy
Copy link
Collaborator

/packit test

@LecrisUT
Copy link
Contributor Author

LecrisUT commented Apr 19, 2023

Well at least the tests are happy so far, although there are no tests written for this. Can I leave writing these tests to the others? I'll just push a quick fix for RHEL8 builds when the tests are done.

Ok so pre-commit test failed 🤔

[15:05:25] [W] [worker_3] [stderr] [CentOS-Stream-9:x86_64:/plans/features/advanced]             00:00:27 fail /tests/precommit [7/16]

@LecrisUT
Copy link
Contributor Author

LecrisUT commented Apr 19, 2023

@lukaszachy I will need some help on this. Locally when I test I get appropriate failures, but tmt seems to detect a fmf root from somewhere. And it is not like I am using #2008 here. Is it because I am not using local file url for the context url?

@LecrisUT
Copy link
Contributor Author

LecrisUT commented May 5, 2023

/packit test

@LecrisUT
Copy link
Contributor Author

LecrisUT commented May 5, 2023

@happz There are no other PRs that could help with the tests for this right? The failed test is very bizarre to me, i.e. where is the fmf root detected over there?

@happz
Copy link
Collaborator

happz commented May 5, 2023

@happz There are no other PRs that could help with the tests for this right? The failed test is very bizarre to me, i.e. where is the fmf root detected over there?

I'm not aware of any lint-related PRs other than #1851.

WRT the failed test, no idea, TBH, it's the first time I see the pre-commit hooks in action. Maybe @lukaszachy could help.

@LecrisUT
Copy link
Contributor Author

LecrisUT commented May 5, 2023

Anyway, I have added the tests for the fmf_root. Can someone trigger the tests again?

@lukaszachy
Copy link
Collaborator

/packit test

@lukaszachy
Copy link
Collaborator

I'm sorry, not that much time for pre-commit stuff.

The idea behind that test is that it is common to forgot to include .fmf/version in the commit and then be suprised why no plans or tests are detected.

The test assumes fmf root == git root. For some use cases this is required (e.g. fedora/centos where plans are detected from git root) for others (e.g. this PR) not so much.

So we will need additional test to check that pre-commit finds issues if fmf-root option is used and no such root exists.

@LecrisUT
Copy link
Contributor Author

LecrisUT commented May 5, 2023

Makes sense, so the issue is that my wrapper here still detects the current path's .fmf/version even though it is not added. I don't get how the the previous version checked that:

git ls-files --error-unmatch $(python3 -c 'import tmt; print(tmt.Tree(logger=tmt.Logger.create(), path=\".\").root)')/.fmf/version && tmt lint --source $@"

Maybe I just run git ls-files within the wrapper relative to that path will work?

@lukaszachy
Copy link
Collaborator

lukaszachy commented May 5, 2023

Makes sense, so the issue is that my wrapper here still detects the current path's .fmf/version even though it is not added. I don't get how the the previous version checked that:

git ls-files --error-unmatch $(python3 -c 'import tmt; print(tmt.Tree(logger=tmt.Logger.create(), path=\".\").root)')/.fmf/version && tmt lint --source $@"

The python script outpus absolute path to the fmf root so git ls-files --error-unmatch /path/.fmf/version is called. Error exit code means the file is not recognized.

Maybe I just run git ls-files within the wrapper relative to that path will work?

I'm afraid that it can be false positive - If the 'path' is set to say gitroot/foo/bar but fmf root is only in gitroot/foo the fmf.Tree call will find and report the gitroot/foo and the check might pass. However what the pre-commit should check instead is that the gitroot/foo/bar is known to git.

But we might have easier life: Use git rev-parse --show-toplevel to get absolute path to the git root and
call git ls-files --error-unmatch $(git rev-parse --show-toplevel)/$path/.fmf/version ? With 'path' empty (the default) it will keep checking for fmfroot==gitroot. With 'path' set it will make sure the supplied path is really fmf root

@LecrisUT
Copy link
Contributor Author

LecrisUT commented May 5, 2023

I'm afraid that it can be false positive - If the 'path' is set to say gitroot/foo/bar but fmf root is only in gitroot/foo the fmf.Tree call will find and report the gitroot/foo and the check might pass. However what the pre-commit should check instead is that the gitroot/foo/bar is known to git.

I don't get this one, maybe we are missing each other point. What I was thinking of doing is effectively:

$ git ls-files ${root_path}/.fmf/version
$ tmt --root ${root_path} lint --source ...

(being wrapped into def pre_commit function and ${root_path} is the variable passed to the cli.)

But we might have easier life: Use git rev-parse --show-toplevel

Thanks I was just thinking about how to get git root to pass it there in the above.

@lukaszachy
Copy link
Collaborator

I don't get this one, maybe we are missing each other point. What I was thinking of doing is effectively:

$ git ls-files ${root_path}/.fmf/version
$ tmt --root ${root_path} lint --source ...

Cool, this should work. I got confused because in fmf id the 'path' is part between git root to the fmf root. Too many 'path's

@LecrisUT
Copy link
Contributor Author

LecrisUT commented May 5, 2023

Ok, 7104337 should fix the issue. Am I missing anything else?

@LecrisUT
Copy link
Contributor Author

LecrisUT commented May 5, 2023

/packit test

@LecrisUT
Copy link
Contributor Author

LecrisUT commented May 5, 2023

Yey, tests are passing :)

tmt/cli.py Outdated Show resolved Hide resolved
tmt/cli.py Outdated Show resolved Hide resolved
@LecrisUT LecrisUT force-pushed the pre-commit branch 2 times, most recently from 1af2b6b to d10f735 Compare May 9, 2023 09:27
@LecrisUT
Copy link
Contributor Author

LecrisUT commented May 2, 2024

/packit test

These days I think /packit build and test must be done separately. At least I've seen that for manual triggers

@thrix
Copy link
Collaborator

thrix commented May 2, 2024

/packit build

tmt/cli.py Show resolved Hide resolved
@psss psss modified the milestones: 1.33, 1.34 May 6, 2024
@psss psss changed the title Allow pre-commit to run in subpath Allow pre-commit to run in subpath May 6, 2024
tmt.spec Outdated Show resolved Hide resolved
@happz happz modified the milestones: 1.34, 1.35 Jun 11, 2024
@lukaszachy lukaszachy self-requested a review June 18, 2024 12:04
Copy link
Collaborator

@lukaszachy lukaszachy left a comment

Choose a reason for hiding this comment

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

Just hide the --pre-check option, lgtm otherwise. It would be nice to have a dedicated tmt subcommand but as you mentioned it would require much more refactoring which is IMO not worth it.

@happz happz added the full test Pull request is ready for the full test execution label Jun 18, 2024
@martinhoyer
Copy link
Collaborator

/packit build

@happz
Copy link
Collaborator

happz commented Jul 4, 2024

@LecrisUT when you have a moment, rebase, please, there seem to be conflicts.

Signed-off-by: Cristian Le <cristian.le@mpsd.mpg.de>
Signed-off-by: Cristian Le <cristian.le@mpsd.mpg.de>
Signed-off-by: Cristian Le <cristian.le@mpsd.mpg.de>
@happz
Copy link
Collaborator

happz commented Jul 4, 2024

/packit build

@happz happz added the ready for merge The only missing piece is to do the rebase the current 'main' and let the CI finish. label Jul 4, 2024
.pre-commit-hooks.yaml Outdated Show resolved Hide resolved
Signed-off-by: Cristian Le <cristian.le@mpsd.mpg.de>
@happz
Copy link
Collaborator

happz commented Jul 5, 2024

/packit build

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
full test Pull request is ready for the full test execution pre-commit ready for merge The only missing piece is to do the rebase the current 'main' and let the CI finish.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pre-commit fmf subpath
7 participants