-
Hey I'm trying to use #[derive(Debug)]
struct Change {
author: String,
message: String,
filenames: HashSet<String>,
}
fn id_to_commit(id: Id) -> Result<Commit> {
let object = id.try_object()?;
let object = object.expect("empty");
let commit = object.try_into_commit()?;
Ok(commit)
}
fn load_history(path: impl AsRef<Path>) -> Result<History> {
let repo = git_repository::discover(path)?;
let rev_walk = repo.rev_walk(Some(repo.head_id()?.detach()));
let mut changes = rev_walk
.all()?
.filter_map(Result::ok)
.map(id_to_commit)
.filter_map(Result::ok);
let mut last = changes.next().unwrap();
let mut result = Vec::new();
for next in changes {
result.push(change_from_commit(&last, Some(&next))?);
last = next;
}
// TODO: once again with last
// result.push(change_from_commit(&last, None));
Ok(History(result))
}
fn change_from_commit(last: &Commit, next: Option<&Commit>) -> Result<Change> {
let author = last.author()?;
let author = format!("{} <{}>", author.name, author.email);
let message = last.message()?;
let message = format!(
"{}",
message.title.to_os_str()?.to_string_lossy().trim_end()
);
let tree = if let Some(next) = next {
next.tree()?
} else {
todo!("empty tree?")
};
let mut changes = tree.changes();
let changes = changes.track_path();
let last_tree = last.tree().unwrap();
let mut filenames = HashSet::new();
changes.for_each_to_obtain_tree(&last_tree, |change| {
let is_file_change = match change.event {
Event::Addition { entry_mode, .. } => entry_mode == EntryMode::Blob,
Event::Modification { entry_mode, .. } => entry_mode == EntryMode::Blob,
Event::Deletion { .. } => false,
};
if is_file_change {
let path = change.location.to_os_str()?.to_string_lossy();
filenames.insert(format!("{}", path));
}
Ok::<Action, Utf8Error>(Action::Continue)
})?;
Ok(Change {
author,
message,
filenames,
})
} This works fine until the initial commit where I can't get a |
Beta Was this translation helpful? Give feedback.
Answered by
Byron
Nov 20, 2022
Replies: 1 comment
-
Here is how an empty tree of the correct type can be obtained by hand: repo.find_object(git::hash::ObjectId::empty_tree(repo.object_hash()))
.expect("always present")
.into_tree() Just now I also added a new method, |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Byron
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is how an empty tree of the correct type can be obtained by hand:
Just now I also added a new method,
Repository::empty_tree()
to do exactly that.I hope that helps.