Home Explore Blog CI



git

11th chunk of `Documentation/gitattributes.adoc`
675b8ec29751c3182430a6dcefe418528f85100a4a8961a80000000100000fa2
 the option
`trustExitCode` to true.  It is then expected to return exit code 1 if
it finds significant changes and 0 if it doesn't.

Setting the internal diff algorithm
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The diff algorithm can be set through the `diff.algorithm` config key, but
sometimes it may be helpful to set the diff algorithm per path. For example,
one may want to use the `minimal` diff algorithm for .json files, and the
`histogram` for .c files, and so on without having to pass in the algorithm
through the command line each time.

First, in `.gitattributes`, assign the `diff` attribute for paths.

------------------------
*.json diff=<name>
------------------------

Then, define a "diff.<name>.algorithm" configuration to specify the diff
algorithm, choosing from `myers`, `patience`, `minimal`, or `histogram`.

----------------------------------------------------------------
[diff "<name>"]
  algorithm = histogram
----------------------------------------------------------------

This diff algorithm applies to user facing diff output like git-diff(1),
git-show(1) and is used for the `--stat` output as well. The merge machinery
will not use the diff algorithm set through this method.

NOTE: If `diff.<name>.command` is defined for path with the
`diff=<name>` attribute, it is executed as an external diff driver
(see above), and adding `diff.<name>.algorithm` has no effect, as the
algorithm is not passed to the external diff driver.

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\\{.*)$"
------------------------

Note.  A single level of backslashes are eaten by the
configuration file parser, so you would need to double the
backslashes; the pattern above picks a line that begins with a
backslash, and zero or more occurrences of `sub` followed by
`section` followed by open brace, to the end of line.

There are a few built-in patterns to make this easier, and `tex`
is one of them, so you do not have to write the above in your
configuration file (you still need to enable this with the
attribute mechanism, via `.gitattributes`).  The following built in
patterns are available:

- `ada` suitable for source code in the Ada language.

- `bash` suitable for source code in the Bourne-Again SHell language.
  Covers a superset of POSIX shell function definitions.

- `bibtex` suitable for files with BibTeX coded references.

- `cpp` suitable for source code in the C and C++ languages.

- `csharp` suitable for source code in the C# language.

- `css` suitable for cascading style sheets.

- `dts` suitable for devicetree (DTS) files.

- `elixir` suitable for source code in the Elixir language.

- `fortran` suitable for source code in the Fortran language.

- `fountain` suitable for Fountain documents.

- `golang` suitable for source code in the Go language.

- `html` suitable for HTML/XHTML documents.

- `java` suitable for source code in the Java language.

- `kotlin` suitable for source code in the Kotlin language.

- `markdown` suitable for Markdown documents.

- `matlab` suitable for source code in the MATLAB and Octave languages.

- `objc`

Title: Customizing Git Diff Output
Summary
Git allows customizing diff output through various configuration options, including setting the diff algorithm per path, defining custom hunk headers, and using built-in patterns for specific programming languages, enabling users to tailor diff output to their needs and improve code review efficiency.