Skip to content

Commit 52dedba

Browse files
committed
Accept LICENCE... as license file name
1 parent a64ad56 commit 52dedba

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

src/info.rs

+17-11
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use crate::{AsciiArt, CommitInfo, Configuration, Error, InfoFieldOn};
1414

1515
type Result<T> = std::result::Result<T, crate::Error>;
1616

17+
const LICENSE_FILES: [&str; 3] = ["LICENSE", "LICENCE", "COPYING"];
18+
1719
pub struct Info {
1820
git_version: String,
1921
git_username: String,
@@ -37,7 +39,7 @@ pub struct Info {
3739
bold_enabled: bool,
3840
no_color_blocks: bool,
3941
custom_image: Option<DynamicImage>,
40-
image_backend: Option<Box<dyn ImageBackend>>
42+
image_backend: Option<Box<dyn ImageBackend>>,
4143
}
4244

4345
impl std::fmt::Display for Info {
@@ -649,22 +651,26 @@ impl Info {
649651
}
650652

651653
fn get_project_license(dir: &str) -> Result<String> {
654+
fn is_license_file<S: AsRef<str>>(file_name: S) -> bool {
655+
LICENSE_FILES
656+
.iter()
657+
.any(|&name| file_name.as_ref().starts_with(name))
658+
}
659+
652660
let detector = Detector::new()?;
653661

654662
let mut output = fs::read_dir(dir)
655663
.map_err(|_| Error::ReadDirectory)?
656664
.filter_map(std::result::Result::ok)
657665
.map(|entry| entry.path())
658-
.filter(
659-
|entry| {
660-
entry.is_file()
661-
&& entry
662-
.file_name()
663-
.map(OsStr::to_string_lossy)
664-
.iter()
665-
.any(|x| x.starts_with("LICENSE") || x.starts_with("COPYING"))
666-
}, // TODO: multiple prefixes, like COPYING?
667-
)
666+
.filter(|entry| {
667+
entry.is_file()
668+
&& entry
669+
.file_name()
670+
.map(OsStr::to_string_lossy)
671+
.map(is_license_file)
672+
.unwrap_or_default()
673+
})
668674
.filter_map(|entry| {
669675
let contents = fs::read_to_string(entry).unwrap_or_default();
670676
detector.analyze(&contents)

0 commit comments

Comments
 (0)