Skip to content

Commit

Permalink
.gitattributes: set diff driver for Rust source code files
Browse files Browse the repository at this point in the history
Git supports a builtin Rust diff driver [1] since v2.23.0 (2019).

It improves the choice of hunk headers in some cases, such as
diffs within methods, since those are indented in Rust within
an `impl` block, and therefore the default diff driver would
pick the outer `impl` block instead (rather than the method
where the changed code is).

For instance, with the default diff driver:

    @@ -455,6 +455,8 @@ impl fmt::Write for RawFormatter {
             // Amount that we can copy. `saturating_sub` ensures we get 0 if `pos` goes past `end`.
             let len_to_copy = core::cmp::min(pos_new, self.end).saturating_sub(self.pos);

    +        test_diff_driver();
    +
             if len_to_copy > 0 {
                 // SAFETY: If `len_to_copy` is non-zero, then we know `pos` has not gone past `end`
                 // yet, so it is valid for write per the type invariants.

With the Rust diff driver:

    @@ -455,6 +455,8 @@ fn write_str(&mut self, s: &str) -> fmt::Result {
             // Amount that we can copy. `saturating_sub` ensures we get 0 if `pos` goes past `end`.
             let len_to_copy = core::cmp::min(pos_new, self.end).saturating_sub(self.pos);

    +        test_diff_driver();
    +
             if len_to_copy > 0 {
                 // SAFETY: If `len_to_copy` is non-zero, then we know `pos` has not gone past `end`
                 // yet, so it is valid for write per the type invariants.

Thus set the `rust` diff driver for `*.rs` source files.

The Rust repository also does so since 2020 [2].

Link: https://git-scm.com/docs/gitattributes#_defining_a_custom_hunk_header [1]
Link: rust-lang/rust#78882 [2]
Reviewed-by: Gary Guo <gary@garyguo.net>
Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com>
Reviewed-by: Benno Lossin <benno.lossin@proton.me>
Link: https://lore.kernel.org/r/20230418233048.335281-1-ojeda@kernel.org
[ Added link to Rust repository ]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
  • Loading branch information
ojeda authored and asahilina committed Jul 14, 2023
1 parent e4ef3dd commit 7160fed
Showing 1 changed file with 1 addition and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
*.[ch] diff=cpp
*.dts diff=dts
*.dts[io] diff=dts
*.rs diff=rust

0 comments on commit 7160fed

Please sign in to comment.