Skip to content

Commit b0f20f1

Browse files
authored
fix git diff when core.autocrlf is enabled (helix-editor#4995)
1 parent 1e31bc3 commit b0f20f1

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

helix-vcs/src/git.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@ impl Git {
1717
// custom open options
1818
let mut git_open_opts_map = git::sec::trust::Mapping::<git::open::Options>::default();
1919

20-
// don't use the global git configs (not needed)
20+
// On windows various configuration options are bundled as part of the installations
21+
// This path depends on the install location of git and therefore requires some overhead to lookup
22+
// This is basically only used on windows and has some overhead hence it's disabled on other platforms.
23+
// `gitoxide` doesn't use this as default
2124
let config = git::permissions::Config {
22-
system: false,
23-
git: false,
24-
user: false,
25+
system: true,
26+
git: true,
27+
user: true,
2528
env: true,
2629
includes: true,
27-
git_binary: false,
30+
git_binary: cfg!(windows),
2831
};
2932
// change options for config permissions without touching anything else
3033
git_open_opts_map.reduced = git_open_opts_map.reduced.permissions(git::Permissions {
@@ -61,7 +64,29 @@ impl DiffProvider for Git {
6164
let file_oid = find_file_in_commit(&repo, &head, file)?;
6265

6366
let file_object = repo.find_object(file_oid).ok()?;
64-
Some(file_object.detach().data)
67+
let mut data = file_object.detach().data;
68+
// convert LF to CRLF if configured to avoid showing every line as changed
69+
if repo
70+
.config_snapshot()
71+
.boolean("core.autocrlf")
72+
.unwrap_or(false)
73+
{
74+
let mut normalized_file = Vec::with_capacity(data.len());
75+
let mut at_cr = false;
76+
for &byte in &data {
77+
if byte == b'\n' {
78+
// if this is a LF instead of a CRLF (last byte was not a CR)
79+
// insert a new CR to generate a CRLF
80+
if !at_cr {
81+
normalized_file.push(b'\r');
82+
}
83+
}
84+
at_cr = byte == b'\r';
85+
normalized_file.push(byte)
86+
}
87+
data = normalized_file
88+
}
89+
Some(data)
6590
}
6691
}
6792

0 commit comments

Comments
 (0)