Skip to content

Commit

Permalink
speed up
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun committed Jun 11, 2024
1 parent c9d3ab6 commit 82d3999
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
10 changes: 6 additions & 4 deletions fmt/src/document/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::{
};

use snafu::ResultExt;
use time::format_description;
use time::{format_description, format_description::FormatItem};

use crate::{
config::Mapping, document::Document, error::CreateDocumentSnafu, git::GitFileAttrs,
Expand All @@ -36,6 +36,7 @@ pub struct DocumentFactory {

keywords: Vec<String>,
git_file_attrs: HashMap<String, GitFileAttrs>,
year_formatter: Vec<FormatItem<'static>>,
}

impl DocumentFactory {
Expand All @@ -46,12 +47,14 @@ impl DocumentFactory {
keywords: Vec<String>,
git_file_attrs: HashMap<String, GitFileAttrs>,
) -> Self {
let year_formatter = format_description::parse("[year]").expect("cannot parse format");
Self {
mapping,
definitions,
properties,
keywords,
git_file_attrs,
year_formatter,
}
}

Expand Down Expand Up @@ -83,18 +86,17 @@ impl DocumentFactory {
.to_string();
properties.insert("hawkeye.core.filename".to_string(), filename);

let year_formatter = format_description::parse("[year]").expect("cannot parse format");
if let Some(attrs) = self
.git_file_attrs
.get(filepath.to_str().expect("path is never empty"))
{
properties.insert(
"hawkeye.git.fileCreatedYear".to_string(),
attrs.created_time.format(year_formatter.as_slice()),
attrs.created_time.format(self.year_formatter.as_slice()),
);
properties.insert(
"hawkeye.git.fileModifiedYear".to_string(),
attrs.modified_time.format(year_formatter.as_slice()),
attrs.modified_time.format(self.year_formatter.as_slice()),
);
}

Expand Down
3 changes: 2 additions & 1 deletion fmt/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ pub fn resolve_file_attrs(repo: &Repository) -> anyhow::Result<HashMap<String, G
for info in head.ancestors().all()? {
let info = info?;
let this_commit = info.object()?;
let time = this_commit.time()?;

let tree = this_commit.tree()?;
let mut changes = tree.changes()?;
changes.track_path().for_each_to_obtain_tree_with_cache(
Expand All @@ -120,7 +122,6 @@ pub fn resolve_file_attrs(repo: &Repository) -> anyhow::Result<HashMap<String, G
let filepath = workdir.join(change.location.to_string());
let filepath = filepath.display().to_string();

let time = this_commit.time().expect("commit always has time");
match attrs.entry(filepath) {
Entry::Occupied(mut ent) => {
let attrs: &GitFileAttrs = ent.get();
Expand Down

0 comments on commit 82d3999

Please sign in to comment.