13

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

1 Answer 1

14

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.

6
  • 1
    See also github.com/magit/magit/issues/3706
    – phils
    Commented Jun 10, 2021 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? Commented Jun 11, 2021 at 1:23
  • @PeterMortensen quoth: "old use or humorous" ;) Either way, I don't think it affects the answer.
    – Basil
    Commented Jun 11, 2021 at 7: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
    Commented Jun 11, 2021 at 7: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. Commented Jun 11, 2021 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.