Skip to content

Commit 41462b2

Browse files
committed
read only the first line of a file for owner annotation
1 parent c3cace6 commit 41462b2

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/project_file_builder.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use error_stack::Result;
22
use lazy_static::lazy_static;
33
use regex::Regex;
4+
use std::fs::File;
5+
use std::io::{BufRead, BufReader};
46
use std::path::{Path, PathBuf};
57

68
use crate::{
@@ -49,8 +51,8 @@ impl<'a> ProjectFileBuilder<'a> {
4951
}
5052

5153
pub(crate) fn build_project_file_without_cache(path: &PathBuf) -> ProjectFile {
52-
let content = match std::fs::read_to_string(path) {
53-
Ok(content) => content,
54+
let file = match File::open(path) {
55+
Ok(file) => file,
5456
Err(_) => {
5557
return ProjectFile {
5658
path: path.clone(),
@@ -59,13 +61,21 @@ pub(crate) fn build_project_file_without_cache(path: &PathBuf) -> ProjectFile {
5961
}
6062
};
6163

62-
let first_line = content.lines().next();
63-
let Some(first_line) = first_line else {
64-
return ProjectFile {
65-
path: path.clone(),
66-
owner: None,
67-
};
68-
};
64+
let mut reader = BufReader::new(file);
65+
let mut first_line = String::with_capacity(256);
66+
67+
match reader.read_line(&mut first_line) {
68+
Ok(0) | Err(_) => {
69+
return ProjectFile {
70+
path: path.clone(),
71+
owner: None,
72+
};
73+
}
74+
Ok(_) => {}
75+
}
76+
77+
// read_line includes the newline, but .lines() doesn't, so we need to trim
78+
let first_line = first_line.trim_end();
6979

7080
let owner = TEAM_REGEX
7181
.captures(first_line)

0 commit comments

Comments
 (0)