1
0
mirror of https://github.com/j178/prek.git synced 2026-04-25 02:11:36 +02:00

Use line-by-line reading in check-merge-conflict (#910)

Follow up https://github.com/j178/prek/pull/907
This commit is contained in:
Jo
2025-10-17 11:27:48 +08:00
committed by GitHub
parent 67d956e8a0
commit bb9cd2d30a
@@ -3,6 +3,7 @@ use std::path::Path;
use anyhow::Result;
use clap::Parser;
use futures::StreamExt;
use tokio::io::AsyncBufReadExt;
use crate::git::get_git_dir;
use crate::hook::Hook;
@@ -71,28 +72,16 @@ async fn is_in_merge() -> Result<bool> {
}
async fn check_file(file_base: &Path, filename: &Path) -> Result<(i32, Vec<u8>)> {
let content = fs_err::tokio::read(file_base.join(filename)).await?;
let file_path = file_base.join(filename);
let file = fs_err::tokio::File::open(&file_path).await?;
let mut reader = tokio::io::BufReader::new(file);
let mut code = 0;
let mut output = Vec::new();
let mut line = Vec::new();
let mut line_number = 1;
let mut i = 0;
while i < content.len() {
// Find the start of this line
let line_start = i;
// Find the end of this line (including line ending)
let mut line_end = i;
while line_end < content.len() && content[line_end] != b'\n' {
line_end += 1;
}
if line_end < content.len() && content[line_end] == b'\n' {
line_end += 1; // Include the \n
}
let line = &content[line_start..line_end];
while reader.read_until(b'\n', &mut line).await? != 0 {
// Check all patterns
for pattern in CONFLICT_PATTERNS {
if line.starts_with(pattern) {
@@ -117,8 +106,8 @@ async fn check_file(file_base: &Path, filename: &Path) -> Result<(i32, Vec<u8>)>
}
}
line.clear();
line_number += 1;
i = line_end;
}
Ok((code, output))