PSA - Elixir function declaration support in Git

I do not know how many of you have ever noticed that git diff shows current scope in the diff header. This is called hunk and can be useful in long diffs. The problem is that Elixir is not a language that is supported OOtB (no surprise there), but do not worry, with some Git-fu you can get it today. Installation is simple:

$ mkdir -p ~/.config/git
$ echo '*.ex diff=elixir' >> ~/.config/git/attributes
$ echo '*.exs diff=elixir' >> ~/.config/git/attributes
$ git config --global diff.elixir.xfuncname '^[ \t]*((def(macro|module|impl|guard|protocol)?p?|test)[ \t].*)$'

And change your diffs from this:

@@ -474,7 +472,7 @@
   """
   @spec reset_metadata(metadata) :: :ok
   def reset_metadata(keywords \\ []) do
-    :logger.set_process_metadata(%{})
+    :ok = :logger.set_process_metadata(%{})
     metadata(keywords)
   end
 

To this

@@ -474,7 +472,7 @@ def metadata() do
   """
   @spec reset_metadata(metadata) :: :ok
   def reset_metadata(keywords \\ []) do
-    :logger.set_process_metadata(%{})
+    :ok = :logger.set_process_metadata(%{})
     metadata(keywords)
   end
 

The difference is in first line of the snippets if you missed that.

8 Likes

@hauleth: For me it gives:

$ git config --global --set diff.elixir.xfuncname '^[ \t]*((def(macro|module|impl|guard|protocol)?p?|test)[ \t].*)$'
error: unknown option `set'
# here the git config usage is displayed

I think that it should be --add instead of --set.

My result:

$ git config --global --add diff.elixir.xfuncname '^[ \t]*((def(macro|module|impl|guard|protocol)?p?|test)[ \t].*)$'
$ git config --global --get diff.elixir.xfuncname
^[ \t]*((def(macro|module|impl|guard|protocol)?p?|test)[ \t].*)$
2 Likes

There should be no option at all (by default it is set). So this will work now with updated instruction.

4 Likes

This feature have landed in Git 2.25 which has just landed. So after update to newest Git it will be a built in.

3 Likes