Frustrated at WXME files in git repositories? Set a filter!

Having WXME files in git repositories can be frustrating. They are not readable in other editors. Git can't display them in git diff and git show. But don't give up yet! Git supports custom filters that allows one to customize conversion from binary files to text, which can generate useful outputs when working with binary files. Some git implementation already includesuch configurations for PDFs. Let's add one for WXME files.

  1. Create this racket-wxme-astext script and make it available in your $PATH. When the specified file is identified as a binary file, it uses wxme-port->text-port to extract readable texts from WXME files. Remember to chmod u+x racket-wxme-astext.

    #!/bin/bash
    
    if [ $# -ne 1 ]; then
      echo Usage: racket-wxme-astext FILE >&2
      exit 1
    fi
    
    if ! file --mime "$1" | grep octet-stream > /dev/null; then
      cat "$1"
    else
      cat "$1" | racket -n -l racket/base -l wxme -l racket/port \
        -e '(copy-port (wxme-port->text-port (current-input-port)) (current-output-port))'
    fi
    
  2. Identify Racket files in GIT attributes. Add this line to either <PROJECT_ROOT>/.gitattributes, <PROJECT_ROOT>/.git/info/attributes or $XDG_CONFIG_HOME/git/attributes:

    *.rkt diff=wxme
    
  3. Hook racket-wxme-astext for wxme type in git:

    git config --local diff.wxme.textconv racket-wxme-astext
    

    Replace --local by --global if you want to enable racket-wxme-astext for all repositories (in which case you need to use the third git attribute file in the previous step).

    Instead of using git config, you can also stash the following content in either <PROJECT_ROOT>/.git/config, ~/.gitconfig or $XDG_CONFIG_HOME/git/config:

    [diff "wxme"]
    	textconv = racket-wxme-astext
    
4 Likes

Screenshot?

Filler to get to 20 characters.

Here is one! Actually, just running racket-wxme-astext FILE.rkt directly extracts the text content from FILE.rkt.

This screenshot contains a comment box and a fraction. Note that because the WXME library renders images as ., there's no way to show diff for images (not even hashes).

1 Like

Is it only locally you get the nice diffs - I mean - how does it look on Github?

It does not work on GitHub. As it turns out, diffing WXME actually depends on running Racket code.

1 Like