Skip to content

Commit

Permalink
Detect the line endings convention of Cargo.lock (CRLF or LF) and pre…
Browse files Browse the repository at this point in the history
…serve it

Fixes rust-lang#2076
  • Loading branch information
gkoz committed Oct 30, 2015
1 parent 5f3f796 commit da4b8af
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/cargo/ops/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ pub fn write_lockfile(dst: &Path, resolve: &Resolve) -> CargoResult<()> {

// Load the original lockfile if it exists.
if let Ok(orig) = paths::read(dst) {
if has_crlf_line_endings(&orig) {
out = out.replace("\n", "\r\n");
}
if out == orig {
// The lockfile contents haven't changed so don't rewrite it.
// This is helpful on read-only filesystems.
Expand All @@ -81,6 +84,15 @@ pub fn write_lockfile(dst: &Path, resolve: &Resolve) -> CargoResult<()> {
Ok(())
}

fn has_crlf_line_endings(s: &str) -> bool {
// Only check the first line.
if let Some(lf) = s.find('\n') {
s[..lf].ends_with('\r')
} else {
false
}
}

fn emit_package(dep: &toml::Table, out: &mut String) {
out.push_str(&format!("name = {}\n", lookup(dep, "name")));
out.push_str(&format!("version = {}\n", lookup(dep, "version")));
Expand Down

0 comments on commit da4b8af

Please sign in to comment.