简体   繁体   中英

Is there a way to get git commit --verbose to show an updated diff when using pre-commit hooks?

So I'm currently setting up a git pre-commit hook to lint my python files with iSort, and python Black, the issue I'm running into is that when I use git commit --verbose the diff that shows up in the commit editor hasn't actually taken the modifications to the staged files into account.

For example, lets say I have a python file that looks like so:

import re

from os import path

def x():
    v = re.compile(r"1")
    print(3, v)

def y(v=3):
    z = path.join("a", "b")
    thing = "a string"
    print(thing, z)

Based on the iSort and black settings I have configured, my pre-commit script will change the file to look like so:

import re
from os import path


def x():
    v = re.compile(r"1")
    print(3, v)


def y(v=3):
    z = path.join("a", "b")
    thing = "a string"
    print(thing, z)

Unfortunately in the git commit editor it still shows the non modified diff. Is there some way to get the editor to have the correct output?

Theoretically I guess it doesn't matter, but it would be nice to see what the diff would actually be.

Instead of a pre-commit hook, try instead a content filter driver, with a smudge/clean script which can:

  • on checkout make your script one way
  • on commit (or on git diff) make your script the other way

See an example here or (for clean) here

https://i.stack.imgur.com/tumAc.png
(image from "Customizing Git - Git Attributes" from " Pro Git book "))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM