Repository: sharkdp/bat (language: rust)

Issue:
Please support .debdiff files
Debian source diff files are often named `.debdiff`. They are normal diff files. Please support coloring them by just treating them as diff files.


Discussion / hints:
If nobody is picking this up, I might have a go.

Any hints on how it should be approached?
Hi, I believe the way to go is to add a syntax mapping for it, which is done with TOML files - see https://github.com/sharkdp/bat/tree/master/src/syntax_mapping/builtins
@bdrung Presumably you'd want `.nmudiff` too?
@jacg I cannot remember seeing `.nmudiff` in the wild. Looking at [a random NMU bug](https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1062650) the attached diff is named `.debdiff` there.
Are there any guidelines on what to use as a test case file?

Curiously enough, if I copy [`tests/syntax-tests/source/Diff/82e7786e747b1fcfac1f963ae6415a22ec9caae1.diff`](https://github.com/sharkdp/bat/blob/master/tests/syntax-tests/source/Diff/82e7786e747b1fcfac1f963ae6415a22ec9caae1.diff) into a file with the `.debdiff` extension, then `bat` applies syntax highlighting.

However, if I use the [the diff from the random NMU bug](https://bugs.debian.org/cgi-bin/bugreport.cgi?att=1;bug=1062650;filename=nmu_libterralib.debdiff;msg=5) mentioned above, then `bat` only applies syntax highlighting if the file extension is `.diff` but not `.debdiff`.

In other words, for *some* diff files, `bat` figures out that it's a diff even if the extension is `.debdiff`, for others it does not.

Adding a file `src/syntax_mapping/builtins/unix-family/50-diff.toml` with contents
```toml
# .debdiff is the extension used for diffs in Debian packaging
[mappings]
"Diff" = ["*.debdiff"]
```
seems to do the job.

However:

1. There are only 30 `.toml` files in `syntax_mapping/builtins`. Clearly these only account for a tiny part of the known syntaxes, not including diff.
2. `bat -L` shows `Diff    diff, patch`. The proposed solution above changes this to `Diff    diff, patch, *.debdiff`.

This makes me wonder whether it could/should be done using the same mechanism that was used to recognize the `diff` and `patch` extensions.

It can be done, yes, you'd need to patch the `Diff.sublime-syntax` file to add `debdiff` as a file extension associated with that syntax. See https://github.com/sharkdp/bat/blob/master/assets/patches/XML.sublime-syntax.patch for an example.

> However, if I use the [the diff from the random NMU bug](https://bugs.debian.org/cgi-bin/bugreport.cgi?att=1;bug=1062650;filename=nmu_libterralib.debdiff;msg=5) mentioned above, then `bat` only applies syntax highlighting if the file extension is `.diff` but not `.debdiff`.

Presumably [the `first_line_match` regex pattern](https://github.com/sublimehq/Packages/blob/759d6eed9b4beed87e602a23303a121c3a6c2fb3/Diff/Diff.sublime-syntax#L8) doesn't match the first line of that NMU diff, which is why it only gets picked up by a matching file extension.
Is either approach to be preferred over the other? (The toml file approach certainly looks simpler.)

As for testing, I started off by trying to generate a test failure, as a sanity check, by modifying the contents of `tests/syntax-tests/source/Diff/*.diff` and hoping that a mismatch with the corresponding file in `syntax_tests/highlighted` would be detected. But all tests pass. What am I missing?
I would think it's generally easier to maintain the TOML mappings than a series of patches. The TOML mappings were something added fairly recently, for reference. Previously mappings were maintained in Rust code, which wasn't so fun to modify.

If we want simple file extension globs to show up differently in `bat --list-languages` - to match the "standard supported file extensions" then we can certainly discuss that :)

I just tried changing `bat/tests/syntax-tests/source/Diff/82e7786e747b1fcfac1f963ae6415a22ec9caae1.diff` by adding a word and it was picked up as differing by `PATH="$PATH:$PWD/target/release/" ./tests/syntax-tests/regression_test.sh`

```
========== Diff/82e7786e747b1fcfac1f963ae6415a22ec9caae1.diff
--- /home/keith/repos/bat/tests/syntax-tests/highlighted/Diff/82e7786e747b1fcfac1f963ae6415a22ec9caae1.diff
+++ /tmp/tmp.6yGkWoBGns/Diff/82e7786e747b1fcfac1f963ae6415a22ec9caae1.diff
@@ -9,7 +9,7 @@
 +
 +- Add a new `--diff` option that can be used to only show lines surrounding
 +  Git changes, i.e. added, removed or modified lines. The amount of additional
-+  context can be controlled with `--diff-context=N`. See #23 and #940
++  context can be controlled with `--diff-context=N`. See #23 and #940. Blah
 +
  ## Bugfixes
  ## Other
```


Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e45f1bb10b..5326de598c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -114,6 +114,7 @@
 - Update `Julia` syntax, see #2553 (@dependabot)
 - add `NSIS` support, see #2577 (@idleberg)
 - Update `ssh-config`, see #2697 (@mrmeszaros)
+- Add syntax mapping `*.debdiff` => `diff`, see #2947 (@jacg)
 
 ## `bat` as a library
 
diff --git a/src/syntax_mapping/builtins/common/50-diff.toml b/src/syntax_mapping/builtins/common/50-diff.toml
new file mode 100644
index 0000000000..2998d9c586
--- /dev/null
+++ b/src/syntax_mapping/builtins/common/50-diff.toml
@@ -0,0 +1,3 @@
+# .debdiff is the extension used for diffs in Debian packaging
+[mappings]
+"Diff" = ["*.debdiff"]


Test patch: