-
Notifications
You must be signed in to change notification settings - Fork 55
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 ability to use as a pre-commit hook #2
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @Pacu2 for this addition! Could you explain this in the README and link to the relevant projects and their documentation for how to make use of this feature?
Also please add your information to the |
This new feature deserves a mention in the change log ( |
@Carreau @Mystic-Mirage @CorreyL are you familiar enough with pre-commit to tell whether this patch is good to be merged? |
My old team is actually looking to use |
I could be mistaken about my conclusion, so someone please step-in and correct me if I'm wrong. The idea behind a I tried to run # See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/Pacu2/darker
rev: master
hooks:
- id: darker
files: \.py$ I intentionally added a change that I expect (venv) λ git add -p
diff --git a/inputs.py b/inputs.py
index 1e4e51f..2654cbe 100644
--- a/inputs.py
+++ b/inputs.py
@@ -5,7 +5,7 @@ def get_year_input(rates):
tries = 0
while tries < max_tries:
print(
- "What year would you like to get your currency rate from?\n"
+ 'What year would you like to get your currency rate from?\n'
f"Supported years include: {rates.get_supported_years()}"
)
year_input = input() With the change staged, here is what happens with
Notice how This makes sense in the bigger context of what As a point of comparison, here is # See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/psf/black
rev: 19.3b0
hooks:
- id: black Here is what happened: C:\Users\Correy\projects\personal\sassa-git-workshop (feature/pre-commit-hook)
(venv) λ git add -p
diff --git a/inputs.py b/inputs.py
index 1e4e51f..2654cbe 100644
--- a/inputs.py
+++ b/inputs.py
@@ -5,7 +5,7 @@ def get_year_input(rates):
tries = 0
while tries < max_tries:
print(
- "What year would you like to get your currency rate from?\n"
+ 'What year would you like to get your currency rate from?\n'
f"Supported years include: {rates.get_supported_years()}"
)
year_input = input()
(1/1) Stage this hunk [y,n,q,a,d,e,?]? y
C:\Users\Correy\projects\personal\sassa-git-workshop (feature/pre-commit-hook)
(venv) λ git commit
black....................................................................Failed
- hook id: black
- files were modified by this hook
reformatted inputs.py
All done! \u2728 \U0001f370 \u2728
1 file reformatted. Notice how As a second experiment, here is another
This is what the reformat did: Notice how it only modified the Overall, I believe we would want https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks#_committing_workflow_hooks But I definitely stand to be corrected, perhaps there's a way to configure the Python |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@CorreyL, if darker
had a switch for comparing staging/index to HEAD (instead of worktree to HEAD), would that solve your issue and allow the pre-commit hook to work as expected?
I believe this would work. To ensure we're on the same page, if this feature were added, the workflow I envision would look something like: C:\Users\Correy\projects\personal\sassa-git-workshop (feature/pre-commit-hook)
(venv) λ git add -p
diff --git a/inputs.py b/inputs.py
index 1e4e51f..2654cbe 100644
--- a/inputs.py
+++ b/inputs.py
@@ -5,7 +5,7 @@ def get_year_input(rates):
tries = 0
while tries < max_tries:
print(
- "What year would you like to get your currency rate from?\n"
+ 'What year would you like to get your currency rate from?\n'
f"Supported years include: {rates.get_supported_years()}"
)
year_input = input()
(1/1) Stage this hunk [y,n,q,a,d,e,?]? y
C:\Users\Correy\projects\personal\sassa-git-workshop (feature/pre-commit-hook)
(venv) λ git commit
darker....................................................................Failed
- hook id: darker
- files were modified by this hook
reformatted inputs.py
All done! \u2728 \U0001f370 \u2728
1 file reformatted.
# darker would have reverted the changes made to `inputs.py` Though I do foresee a couple of potential gotchas still:
# Carrying on from my above example
C:\Users\Correy\projects\personal\sassa-git-workshop (feature/pre-commit-hook)
(venv) λ git commit
darker....................................................................Failed
- hook id: darker
- files were modified by this hook
reformatted inputs.py
All done! \u2728 \U0001f370 \u2728
1 file reformatted.
# darker would have reverted the changes made to `inputs.py`
C:\Users\Correy\projects\personal\sassa-git-workshop (feature/pre-commit-hook)
λ git diff --staged
diff --git a/inputs.py b/inputs.py
index 1e4e51f..2654cbe 100644
--- a/inputs.py
+++ b/inputs.py
@@ -5,7 +5,7 @@ def get_year_input(rates):
tries = 0
while tries < max_tries:
print(
- "What year would you like to get your currency rate from?\n"
+ 'What year would you like to get your currency rate from?\n'
f"Supported years include: {rates.get_supported_years()}"
)
year_input = input() This is what happened when I used I'm not sure if |
Well, darker does already interact with Git (albeit only for obtaining information currently) Currently it
With e.g. a
For me it doesn't sound like pushing darker's responsibilities too far yet.
Do you mean providing the By the way, darker also currently requires at least one file or directory path argument on the command line. Perhaps it could default to the current directory? |
Uh-oh @CorreyL, I think I get your point about staging only some chunks in a file. If darker only reformats a chunk in the staging index, the unformatted version of that chunk in the working tree will later look like an intended change from the developer to revert formatting. Having darker take the diff from reformatting the staging index and apply it also to the working tree should work in most cases, but I can imagine complex situations where it would simply fail or do the wrong thing. And, the changes in the staging index don't necessarily even correspond to the working tree – staging might have been followed by a working tree edit which falls inside an already staged chunk. Would it then actually be most appropriate to run darker twice: once for the working tree and once for the staging index? Would that ensure that the two are as much in sync as possible after the pre-commit hook? |
For editing files in the staging index, I found these resources:
But is it really so that Or is it just that pre-commit hooks aren't supposed to modify files, but just report violations? |
We rebased this PR on top of current master (see #91). It seems the default behavior is now what pre-commit expects – compare files on disk (whether in staging index or not) to @samoylovfp and Sean (whatever your GitHub handle is), please review #91. |
@akaihola thanks for picking this up. Looking forward to using darker as a precommit in my project. Any help needed let me know. |
Closed in favor of #91. |
This should allow using
darker
as a pre-commit hook inpre-commit
.