3

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.

DJ SymBiotiX
  • 187
  • 2
  • 12

1 Answers1

0

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"))

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Hmm good idea. I took a try at implementing a clean/smudge solution this morning but I'm still running into a bit of an issue. The diffs and commits looks great now, as it does what I want it to. The weird thing is that the local file doesn't seem to be modified at all, but I would expect it to look the same as what was committed. Am I doing something wrong? Here is a copy of my clean/smudge script I am using https://pastebin.com/mCPg7rbE – DJ SymBiotiX Feb 08 '19 at 16:55
  • @DJSymBiotiX The goal is to *generate* a private local file, not to modify an existing file. – VonC Feb 08 '19 at 17:09
  • hmm well I guess then this isn't quite what I'm looking for, based on my initial question/problem. – DJ SymBiotiX Feb 08 '19 at 17:32
  • @DJSymBiotiX You can modifiy an existing file, but you have to make sure your smudge/clean scripts are "symetrical": the changes added by one are removed by the other. Then `touch yourFile` and `git checkout -- .` to force the smudge script to re-apply. – VonC Feb 08 '19 at 17:38
  • Well that seems counter intuitive. You can't really "undo" a linting function. – DJ SymBiotiX Feb 08 '19 at 17:56
  • Right, In your case a smudge would be enough. No need to reverse the modification – VonC Feb 08 '19 at 18:01