Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: git attrs calculation #173

Merged
merged 4 commits into from
Feb 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 0 additions & 150 deletions fmt/src/error.rs

This file was deleted.

89 changes: 50 additions & 39 deletions fmt/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use std::collections::hash_map::Entry;
use std::collections::BTreeSet;
use std::collections::HashMap;
use std::convert::Infallible;
use std::path::Path;
use std::path::PathBuf;

Expand Down Expand Up @@ -108,51 +107,63 @@ pub fn resolve_file_attrs(
let workdir = repo.work_dir().expect("workdir cannot be absent");
let workdir = workdir.canonicalize()?;

let mode = gix::diff::blob::pipeline::Mode::ToGit;
let mut cache = repo.diff_resource_cache(mode, Default::default())?;
let mut do_insert_attrs =
|change: gix::diff::tree_with_rewrites::Change, time: gix::date::Time, author: &str| {
let filepath = gix::path::from_bstr(change.location());
let filepath = workdir.join(filepath);
match attrs.entry(filepath) {
Entry::Occupied(mut ent) => {
let attrs: &mut GitFileAttrs = ent.get_mut();
attrs.created_time = time.min(attrs.created_time);
attrs.modified_time = time.max(attrs.modified_time);
attrs.authors.insert(author.to_string());
}
Entry::Vacant(ent) => {
ent.insert(GitFileAttrs {
created_time: time,
modified_time: time,
authors: {
let mut authors = BTreeSet::new();
authors.insert(author.to_string());
authors
},
});
}
}
};

let option = {
let mut option = gix::diff::Options::default();
option.track_path();
option
};

let head = repo.head_commit()?;
let mut prev_commit = head.clone();
let mut next_commit = head.clone();

for info in head.ancestors().all()? {
let info = info?;
let this_commit = info.object()?;
let time = this_commit.time()?;
let author = this_commit.author()?.name.to_string();

let tree = this_commit.tree()?;
let mut changes = tree.changes()?;
changes
.options(|opts| {
opts.track_path();
})
.for_each_to_obtain_tree_with_cache(&prev_commit.tree()?, &mut cache, |change| {
let filepath = gix::path::from_bstr(change.location());
let filepath = workdir.join(filepath);
match attrs.entry(filepath) {
Entry::Occupied(mut ent) => {
let attrs: &mut GitFileAttrs = ent.get_mut();
attrs.created_time = time.min(attrs.created_time);
attrs.modified_time = time.max(attrs.modified_time);
attrs.authors.insert(author.clone());
}
Entry::Vacant(ent) => {
ent.insert(GitFileAttrs {
created_time: time,
modified_time: time,
authors: {
let mut authors = BTreeSet::new();
authors.insert(author.clone());
authors
},
});
}
}
let time = next_commit.time()?;
let author = next_commit.author()?.name.to_string();

let this_tree = this_commit.tree()?;
let next_tree = next_commit.tree()?;

let changes = repo.diff_tree_to_tree(Some(&this_tree), Some(&next_tree), Some(option))?;
for change in changes {
do_insert_attrs(change, time, author.as_str());
}
next_commit = this_commit;
Copy link
Member Author

@tisonkun tisonkun Feb 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This patch is a follow-up to #150 and GitoxideLabs/gitoxide#1395.

@Byron I'm figuring out how to walk the first commit tree. That said, the current logic iterate all the commits and calculate the changes between a next commit and a current commit. The iteration will exit on the very first (root) commit, then all the changes in the root commit won't be handled.

I'm not quite sure how to get a full change list (or just iterate all the files in the root commit). Investigating...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is to have an empty source tree to compare with if a commit has no parents.
There is an alternative API now, https://docs.rs/gix/latest/gix/struct.Repository.html#method.diff_tree_to_tree, which might make this more obvious and/or easier to use.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Byron Thanks a lot for your information! I use it in 272eee0 and everything looks correct now.

It was the right choice to ask you first :D

}

Ok::<_, Infallible>(Default::default())
})?;
prev_commit = this_commit;
cache.clear_resource_cache();
// process the root commit
let time = next_commit.time()?;
let author = next_commit.author()?.name.to_string();
let next_tree = next_commit.tree()?;
let changes = repo.diff_tree_to_tree(None, Some(&next_tree), Some(option))?;
for change in changes {
do_insert_attrs(change, time, author.as_str());
}

Ok(attrs)
Expand Down
Loading