Skip to content

Use binary search to find last commit #287

@reddevilmidzy

Description

@reddevilmidzy

What's your idea?

위치가 이동된 파일을 찾기 위해 현재 커밋 하나하나 일일이 찾고 있다.

pub fn find_last_commit_id<'a>(
target_file: &str,
repo: &'a Repository,
) -> Result<Commit<'a>, git2::Error> {
let mut revwalk = repo.revwalk()?;
revwalk.push_head()?;
for commit_id in revwalk {
let commit_id = commit_id?;
let commit = repo.find_commit(commit_id)?;
// Ignore merge commits(2+ Parents) because that's what 'git whatchenged' does.
// Ignore commit with 0 parent (initial commit) because there's nothing to diff against.
if commit.parent_count() == 1 {
let prev_commit = commit.parent(0)?;
let tree = commit.tree()?;
let prev_tree = prev_commit.tree()?;
let diff = repo.diff_tree_to_tree(Some(&prev_tree), Some(&tree), None)?;
for delta in diff.deltas() {
if let Some(file_path) = delta.new_file().path()
&& let Some(file_path_str) = file_path.to_str()
&& file_path_str == target_file
{
return Ok(commit);
}
}
}
}

알고리즘 배워서 어따 써먹겠나 여기에 이분탐색 발라 먹자.

related: https://github.com/rust-lang/cargo-bisect-rustc/

Metadata

Metadata

Assignees

No one assigned

    Labels

    refactorRefactoring a method or structure

    Projects

    Status

    Opening

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions