Skip to content

Commit 782e28f

Browse files
committed
feat(git): detect renames in diff extraction and preserve old_path
1 parent d570a26 commit 782e28f

1 file changed

Lines changed: 107 additions & 4 deletions

File tree

‎src/git.rs‎

Lines changed: 107 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use anyhow::{Context, Result};
22
use chrono::{DateTime, Local, Utc};
33
use chrono_english::{parse_date_string, Dialect};
4-
use git2::{Commit as Git2Commit, Delta, DiffOptions, Oid, Repository};
4+
use git2::{Commit as Git2Commit, Delta, DiffFindOptions, DiffOptions, Oid, Repository};
55
use globset::{Glob, GlobSet, GlobSetBuilder};
66
use rand::RngExt;
77
use std::cell::RefCell;
@@ -173,6 +173,15 @@ fn matches_date_filter(
173173
Ok(true)
174174
}
175175

176+
fn detect_file_moves(diff: &mut git2::Diff, for_untracked: bool) {
177+
let mut options = DiffFindOptions::new();
178+
options.renames(true);
179+
if for_untracked {
180+
options.for_untracked(true);
181+
}
182+
let _ = diff.find_similar(Some(&mut options));
183+
}
184+
176185
pub struct GitRepository {
177186
repo: Repository,
178187
commit_cache: RefCell<Option<Vec<Oid>>>,
@@ -603,14 +612,15 @@ impl GitRepository {
603612
let mut diff_opts = DiffOptions::new();
604613
diff_opts.context_lines(3);
605614

606-
let diff = match repo.diff_tree_to_tree(
615+
let mut diff = match repo.diff_tree_to_tree(
607616
parent_tree.as_ref(),
608617
Some(&commit_tree),
609618
Some(&mut diff_opts),
610619
) {
611620
Ok(d) => d,
612621
Err(_) => return Ok(Vec::new()), // Skip if diff fails
613622
};
623+
detect_file_moves(&mut diff, false);
614624

615625
let mut changes = Vec::new();
616626

@@ -816,10 +826,11 @@ impl GitRepository {
816826
let mut diff_opts = DiffOptions::new();
817827
diff_opts.context_lines(3);
818828

819-
let diff = self
829+
let mut diff = self
820830
.repo
821831
.diff_tree_to_index(head_tree.as_ref(), Some(&index), Some(&mut diff_opts))
822832
.context("Failed to diff tree to index")?;
833+
detect_file_moves(&mut diff, false);
823834

824835
self.extract_changes_from_diff(&diff, head_tree.as_ref(), None)
825836
}
@@ -835,10 +846,11 @@ impl GitRepository {
835846
diff_opts.context_lines(3);
836847
diff_opts.include_untracked(true);
837848

838-
let diff = self
849+
let mut diff = self
839850
.repo
840851
.diff_index_to_workdir(Some(&index), Some(&mut diff_opts))
841852
.context("Failed to diff index to workdir")?;
853+
detect_file_moves(&mut diff, true);
842854

843855
// For unstaged, "old" content comes from index, "new" from workdir
844856
self.extract_changes_from_diff_workdir(&diff, &index)
@@ -1362,6 +1374,35 @@ mod tests {
13621374
index.write().unwrap();
13631375
self.commit_index(author_name, author_email, timestamp, message)
13641376
}
1377+
1378+
fn stage_rename(&self, from: &str, to: &str) {
1379+
let from_path = self.path.join(from);
1380+
let to_path = self.path.join(to);
1381+
to_path
1382+
.parent()
1383+
.map(std::fs::create_dir_all)
1384+
.transpose()
1385+
.unwrap();
1386+
std::fs::rename(from_path, &to_path).unwrap();
1387+
1388+
let mut index = self.repo.index().unwrap();
1389+
index.remove_path(std::path::Path::new(from)).unwrap();
1390+
index.add_path(std::path::Path::new(to)).unwrap();
1391+
index.write().unwrap();
1392+
}
1393+
1394+
fn rename_file(
1395+
&self,
1396+
from: &str,
1397+
to: &str,
1398+
author_name: &str,
1399+
author_email: &str,
1400+
timestamp: i64,
1401+
message: &str,
1402+
) -> String {
1403+
self.stage_rename(from, to);
1404+
self.commit_index(author_name, author_email, timestamp, message)
1405+
}
13651406
}
13661407

13671408
fn large_diff_fixture(label: &str) -> String {
@@ -2042,6 +2083,68 @@ mod tests {
20422083
assert!(deleted_change.new_content.is_none());
20432084
}
20442085

2086+
#[test]
2087+
fn test_get_commit_detects_renamed_files_and_keeps_old_path() {
2088+
let test_repo = TestRepo::new();
2089+
test_repo.commit_file(
2090+
"src/original.rs",
2091+
"pub fn renamed() {}\n",
2092+
"Alice Example",
2093+
2094+
1_700_006_500,
2095+
"base commit",
2096+
);
2097+
let renamed_hash = test_repo.rename_file(
2098+
"src/original.rs",
2099+
"src/renamed.rs",
2100+
"Bob Example",
2101+
2102+
1_700_006_560,
2103+
"rename file",
2104+
);
2105+
2106+
let repo = GitRepository::open(&test_repo.path).unwrap();
2107+
let change = repo
2108+
.get_commit(&renamed_hash)
2109+
.unwrap()
2110+
.changes
2111+
.into_iter()
2112+
.find(|change| change.path == "src/renamed.rs")
2113+
.unwrap();
2114+
2115+
assert_eq!(change.status, FileStatus::Renamed);
2116+
assert_eq!(change.old_path.as_deref(), Some("src/original.rs"));
2117+
assert_eq!(change.old_content.as_deref(), Some("pub fn renamed() {}\n"));
2118+
assert_eq!(change.new_content.as_deref(), Some("pub fn renamed() {}\n"));
2119+
}
2120+
2121+
#[test]
2122+
fn test_working_tree_diff_detects_staged_renames() {
2123+
let test_repo = TestRepo::new();
2124+
test_repo.commit_file(
2125+
"src/original.rs",
2126+
"pub fn renamed() {}\n",
2127+
"Alice Example",
2128+
2129+
1_700_006_620,
2130+
"base commit",
2131+
);
2132+
test_repo.stage_rename("src/original.rs", "src/renamed.rs");
2133+
2134+
let repo = GitRepository::open(&test_repo.path).unwrap();
2135+
let change = repo
2136+
.get_working_tree_diff(DiffMode::Staged)
2137+
.unwrap()
2138+
.changes[0]
2139+
.clone();
2140+
2141+
assert_eq!(change.status, FileStatus::Renamed);
2142+
assert_eq!(change.path, "src/renamed.rs");
2143+
assert_eq!(change.old_path.as_deref(), Some("src/original.rs"));
2144+
assert_eq!(change.old_content.as_deref(), Some("pub fn renamed() {}\n"));
2145+
assert_eq!(change.new_content.as_deref(), Some("pub fn renamed() {}\n"));
2146+
}
2147+
20452148
#[test]
20462149
fn test_working_tree_diff_excludes_lockfiles_and_preserves_context_lines() {
20472150
let test_repo = TestRepo::new();

0 commit comments

Comments
 (0)