gitgit-diff

How can I ignore first x columns of the line with Git diff?


We have our COBOL-Sources now in GIT-repositories. During development nearly all devs are removing the cobol leading line squence numbers. So the git diff is showing all lines as changed.

I want that a diff of these files would show just the ====> CHANGED line:

000201 PROCEDURE DIVISION USING VKT1511
000202                          VKT1519
000203                          LI-DUMMY
000204                          LI-DUMMY
000205                          LI-KKONTO
000206                          BS0397-SEGMENT
000207                          TAB-KONTEN
000208                          TAB-LIMITE
000209                          TAB-KUNDE
000210                          BS0798.
000211*
000212*    == VORLAUF ==
000213*
       PROCEDURE DIVISION USING VKT1511
                                VKT1519
                                LI-DUMMY
                                LI-DUMMY
                                LI-KKONTO
                                BS0397-SEGMENT
                                TAB-KONTEN
                                TAB-LIMITE
                                TAB-KUNDE
                   ===========> CHANGED.
      *
      *    == VORLAUF ==
      *

I don't care if I have to use standard GIT DIFF or TortoiseDiff or whatever.


Solution

  • You can define a diff driver -- doc in git help gitattributes -- to run a specific program for your cobol files.

    A quick way to view a diff after stripping the 7 first columns:

    diff <(cut -c8- "$file1") <(cut -c8- "$file2")
    
    # you can actually use 'git diff' as a command line diff viewer:
    git diff --no-index <(cut -c8- "$file1") <(cut -c8- "$file2")
    

    You can stick that command in a script, and use that script as a diff driver:

    # ~/bin/cobol-diff:
    #!/bin/bash
    
    old_file=$2
    new_file=$5
    
    git diff --no-index <(cut -c8- "$old_file") <(cut -c8- "$new_file")
    
    # .git/config or global .gitconfig:
    [diff "cobol-diff"]
        command = path/to/cobol-diff
    
    # .gitattributes:
    .cobol diff=cobol-diff
    

    Obviously: you may adapt the script or use any other suitable diff viewer (including a graphical one)