Support finding the latest commit for a file#1583
Conversation
…er information Signed-off-by: Ruizhi Huang <231220075@smail.nju.edu.cn>
There was a problem hiding this comment.
Pull Request Overview
This PR extends the get_latest_commit API to support finding the latest commit for both directories and files. Previously, the function only handled directory paths; now it falls back to file path resolution when a directory is not found.
Key Changes:
- Restructured the function to try directory lookup first, then fall back to file lookup
- Added file path parsing logic to extract filename and parent directory
- Implemented blob item matching in the parent directory's commit map to find file-specific commits
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| let mut matched_commit: Option<git_internal::internal::object::commit::Commit> = None; | ||
| for (item, commit_opt) in map.into_iter() { | ||
| if item.mode == TreeItemMode::Blob && item.name == file_name { | ||
| matched_commit = commit_opt; | ||
| break; | ||
| } | ||
| } |
There was a problem hiding this comment.
[nitpick] Replace the manual loop with iterator methods for better readability. Use find_map to directly return the commit: let matched_commit = map.into_iter().find_map(|(item, commit_opt)| if item.mode == TreeItemMode::Blob && item.name == file_name { commit_opt } else { None });
| let mut matched_commit: Option<git_internal::internal::object::commit::Commit> = None; | |
| for (item, commit_opt) in map.into_iter() { | |
| if item.mode == TreeItemMode::Blob && item.name == file_name { | |
| matched_commit = commit_opt; | |
| break; | |
| } | |
| } | |
| let matched_commit = map.into_iter().find_map(|(item, commit_opt)| { | |
| if item.mode == TreeItemMode::Blob && item.name == file_name { | |
| commit_opt | |
| } else { | |
| None | |
| } | |
| }); |
| let Some(_parent_tree) = self.search_tree_by_path(parent).await? else { | ||
| return Err(GitError::CustomError( | ||
| "can't find target parent tree under latest commit".to_string(), | ||
| )); | ||
| }; |
There was a problem hiding this comment.
[nitpick] The _parent_tree variable is retrieved but not used. Since only existence is checked, consider using is_some() instead: if self.search_tree_by_path(parent).await?.is_none() { return Err(...); }
| let Some(_parent_tree) = self.search_tree_by_path(parent).await? else { | |
| return Err(GitError::CustomError( | |
| "can't find target parent tree under latest commit".to_string(), | |
| )); | |
| }; | |
| if self.search_tree_by_path(parent).await?.is_none() { | |
| return Err(GitError::CustomError( | |
| "can't find target parent tree under latest commit".to_string(), | |
| )); | |
| } |
Signed-off-by: Ruizhi Huang <231220075@smail.nju.edu.cn> fix reviews Signed-off-by: Ruizhi Huang <231220075@smail.nju.edu.cn>
|
link #1562 |
Extend the functionality of the get_latest_commit API to support finding the latest commit for a file