12

When diffing with Git, for most languages, it provides a function name in the chunk header, allowing to get a better grasp on the context, e.g.,

@@ -7,5 +7,6 @@ int main() {
   if(condition()) {
     task4();
     task5();
+    task6();
   }
 }

For Emacs Lisp, git diff seems to use some sort of fall-back mode, where apparently the closest line without indentation is used; which usually is part of a docstring, not something like (defun ...

@@ -9,4 +9,5 @@ Last docstring line."
   (run-task3)
   (when (run-condition)
     (run-task4)
-    (run-task5)))
+    (run-task5)
+    (run-task6)))

Is it possible to make Git aware of how to identify a useful line to use for the @@ line?

Complete example source

For reference I attach the full source used to produce the examples.

/// run.c
int main() {
// foo
  task1();
  task2();
  task3();
  if(condition()) {
    task4();
    task5();
    task6();
  }
}
;;; run.el
(defun run-main ()
  "1st docstring line.
2nd docstring line.
Last docstring line."
  (run-task1)
  (run-task2)
  (run-task3)
  (when (run-condition)
    (run-task4)
    (run-task5)
    (run-task6)))
kdb
  • 1,561
  • 12
  • 24

1 Answers1

13

Is it possible to make Git aware of how to identify a useful line to use for the @@ line?

Yes, quoth section "Defining a custom hunk-header" in man 5 gitattributes:

Defining a custom hunk-header
    Each group of changes (called a "hunk") in the textual diff output
    is prefixed with a line of the form:
    @@ -k,l +n,m @@ TEXT

This is called a hunk header. The "TEXT" portion is by default a
line that begins with an alphabet, an underscore or a dollar sign;
this matches what GNU diff -p output uses. This default selection
however is not suited for some contents, and you can use a
customized pattern to make a selection.

First, in .gitattributes, you would assign the diff attribute for
paths.

    *.tex   diff=tex

Then, you would define a "diff.tex.xfuncname" configuration to
specify a regular expression that matches a line that you would
want to appear as the hunk header "TEXT". Add a section to your
$GIT_DIR/config file (or $HOME/.gitconfig file) like this:

    [diff "tex"]
            xfuncname = "^(\\\\(sub)*section\\{.*)$"

So, to teach Git about Elisp files, you could first add the following to your $XDG_CONFIG_HOME/git/attributes file (which defaults to ~/.config/git/attributes):

*.el diff=elisp

Then, you can configure the exact header-matching regexp in your $XDG_CONFIG_HOME/git/config file (which defaults to ~/.config/git/config), e.g. as follows:

[diff "elisp"]
  xfuncname = ^\\([^[:space:]]+[[:space:]]+([^()[:space:]]+)

This is the relatively liberal regexp that I use globally. The Emacs repository itself uses a slightly more restricted one:

[diff "elisp"]
  xfuncname = ^\\([^[:space:]]*def[^[:space:]]+[[:space:]]+([^()[:space:]]+)

You can find it by searching for xfuncname in the script autogen.sh.

Basil
  • 12,383
  • 43
  • 69
  • 1
    See also https://github.com/magit/magit/issues/3706 – phils Jun 10 '21 at 11:38
  • quoth: "(archaic or literary, now *defective) simple past tense of quethe; said"* and "2. ...Quoth is considered a *defective* verb because it is now the only recognizable form of the verb quethe, all other forms of which are obsolete". Perhaps rephrase? – Peter Mortensen Jun 11 '21 at 01:23
  • @PeterMortensen quoth: "old use or humorous" ;) Either way, I don't think it affects the answer. – Basil Jun 11 '21 at 07:30
  • @PeterMortensen Thanks for the review and suggestion though. I value good writing and truly appreciate help with improving my own for style and readability. I just meant to say that my use of quoth was intentional, as a quirky personal favourite. This is under the assumption, of course, that its use is not actively detrimental - in that case I wouldn't hesitate to rephrase. – Basil Jun 11 '21 at 07:47
  • (OT) @PeterMortensen I think you misunderstand the meaning of "defective". In linguistics, it just means a verb which is not (or no longer) used in all possible forms. It doesn't mean there's anything wrong or bad about the verb. Some very useful verbs in English are defective: I am, you am, she ams. – Reign of Error Jun 11 '21 at 11:05
  • @Basil As a non-native speaker, I didn't know the form. Here its also not clear from the context, what exactly it means (I guessed at something with "quote"), but it didn't affect the readability of the content of the answer. I actually only even noticed the word after reading the comments. (And when we're at it: Thanks for the reply, it solved the issue for me.) – kdb Jun 14 '21 at 07:43