gitkraft_core/features/diff/
ops.rs1use anyhow::{Context, Result};
4use git2::{Diff, DiffFormat, DiffOptions, Repository};
5
6use super::types::{DiffHunk, DiffInfo, DiffLine, FileStatus};
7
8pub fn get_working_dir_diff(repo: &Repository) -> Result<Vec<DiffInfo>> {
14 let mut opts = DiffOptions::new();
15 opts.include_untracked(true);
16 opts.recurse_untracked_dirs(true);
17
18 let diff = repo
19 .diff_index_to_workdir(None, Some(&mut opts))
20 .context("failed to diff working directory against index")?;
21 parse_diff(&diff)
22}
23
24pub fn get_staged_diff(repo: &Repository) -> Result<Vec<DiffInfo>> {
28 let head_tree = match repo.head() {
29 Ok(reference) => {
30 let commit = reference
31 .peel_to_commit()
32 .context("HEAD does not point to a commit")?;
33 Some(commit.tree().context("commit has no tree")?)
34 }
35 Err(_) => None,
37 };
38
39 let diff = repo
40 .diff_tree_to_index(head_tree.as_ref(), None, None)
41 .context("failed to diff index against HEAD tree")?;
42 parse_diff(&diff)
43}
44
45pub fn get_commit_diff(repo: &Repository, oid_str: &str) -> Result<Vec<DiffInfo>> {
49 let oid =
50 git2::Oid::from_str(oid_str).with_context(|| format!("invalid OID string: {oid_str}"))?;
51 let commit = repo
52 .find_commit(oid)
53 .with_context(|| format!("commit {oid_str} not found"))?;
54 let commit_tree = commit.tree().context("commit has no tree")?;
55
56 let parent_tree = if commit.parent_count() > 0 {
57 let parent = commit.parent(0).context("failed to read parent commit")?;
58 Some(parent.tree().context("parent commit has no tree")?)
59 } else {
60 None
61 };
62
63 let mut opts = DiffOptions::new();
64 let diff = repo
65 .diff_tree_to_tree(parent_tree.as_ref(), Some(&commit_tree), Some(&mut opts))
66 .context("failed to diff commit against parent")?;
67 parse_diff(&diff)
68}
69
70pub fn get_commit_file_list(
76 repo: &Repository,
77 oid_str: &str,
78) -> Result<Vec<super::types::DiffFileEntry>> {
79 let oid =
80 git2::Oid::from_str(oid_str).with_context(|| format!("invalid OID string: {oid_str}"))?;
81 let commit = repo
82 .find_commit(oid)
83 .with_context(|| format!("commit {oid_str} not found"))?;
84 let commit_tree = commit.tree().context("commit has no tree")?;
85
86 let parent_tree = if commit.parent_count() > 0 {
87 let parent = commit.parent(0).context("failed to read parent commit")?;
88 Some(parent.tree().context("parent commit has no tree")?)
89 } else {
90 None
91 };
92
93 let diff = repo
94 .diff_tree_to_tree(parent_tree.as_ref(), Some(&commit_tree), None)
95 .context("failed to diff commit against parent")?;
96
97 Ok(diff
98 .deltas()
99 .map(|delta| super::types::DiffFileEntry {
100 old_file: delta
101 .old_file()
102 .path()
103 .map(|p| p.to_string_lossy().into_owned())
104 .unwrap_or_default(),
105 new_file: delta
106 .new_file()
107 .path()
108 .map(|p| p.to_string_lossy().into_owned())
109 .unwrap_or_default(),
110 status: FileStatus::from_delta(delta.status()),
111 })
112 .collect())
113}
114
115pub fn get_single_file_diff(repo: &Repository, oid_str: &str, file_path: &str) -> Result<DiffInfo> {
120 let oid =
121 git2::Oid::from_str(oid_str).with_context(|| format!("invalid OID string: {oid_str}"))?;
122 let commit = repo
123 .find_commit(oid)
124 .with_context(|| format!("commit {oid_str} not found"))?;
125 let commit_tree = commit.tree().context("commit has no tree")?;
126
127 let parent_tree = if commit.parent_count() > 0 {
128 let parent = commit.parent(0).context("failed to read parent commit")?;
129 Some(parent.tree().context("parent commit has no tree")?)
130 } else {
131 None
132 };
133
134 let mut opts = DiffOptions::new();
135 opts.pathspec(file_path);
136
137 let diff = repo
138 .diff_tree_to_tree(parent_tree.as_ref(), Some(&commit_tree), Some(&mut opts))
139 .context("failed to diff commit against parent for single file")?;
140
141 let infos = parse_diff(&diff)?;
142 infos
143 .into_iter()
144 .next()
145 .ok_or_else(|| anyhow::anyhow!("file '{}' not found in commit diff", file_path))
146}
147
148pub fn diff_file_commit_vs_workdir(
154 repo: &Repository,
155 oid_str: &str,
156 file_path: &str,
157) -> Result<DiffInfo> {
158 let oid =
159 git2::Oid::from_str(oid_str).with_context(|| format!("invalid OID string: {oid_str}"))?;
160 let commit = repo
161 .find_commit(oid)
162 .with_context(|| format!("commit {oid_str} not found"))?;
163 let commit_tree = commit.tree().context("commit has no tree")?;
164
165 let mut opts = DiffOptions::new();
166 opts.pathspec(file_path);
167
168 let diff = repo
170 .diff_tree_to_workdir_with_index(Some(&commit_tree), Some(&mut opts))
171 .context("failed to diff commit tree against working directory")?;
172
173 let infos = parse_diff(&diff)?;
174
175 if let Some(info) = infos.into_iter().next() {
176 return Ok(info);
177 }
178
179 let in_commit = commit_tree
181 .get_path(std::path::Path::new(file_path))
182 .is_ok();
183
184 let workdir = repo.workdir().context("bare repository")?;
186 let in_workdir = workdir.join(file_path).exists();
187
188 match (in_commit, in_workdir) {
189 (true, true) => {
190 Ok(DiffInfo {
192 old_file: file_path.to_string(),
193 new_file: file_path.to_string(),
194 status: FileStatus::Modified,
195 hunks: vec![DiffHunk {
196 header: "@@ No changes — file is identical @@".to_string(),
197 lines: vec![DiffLine::HunkHeader(
198 "@@ No changes — file is identical to working tree @@".to_string(),
199 )],
200 }],
201 })
202 }
203 (true, false) => {
204 let blob_entry = commit_tree.get_path(std::path::Path::new(file_path))?;
206 let mut hunks = Vec::new();
207 if let Ok(blob) = repo.find_blob(blob_entry.id()) {
208 let content = String::from_utf8_lossy(blob.content());
209 let lines: Vec<DiffLine> = std::iter::once(DiffLine::HunkHeader(format!(
210 "@@ File deleted since commit {} @@",
211 &oid_str[..7.min(oid_str.len())]
212 )))
213 .chain(content.lines().map(|l| DiffLine::Deletion(l.to_string())))
214 .collect();
215
216 hunks.push(DiffHunk {
217 header: lines
218 .first()
219 .map(|l| match l {
220 DiffLine::HunkHeader(h) => h.clone(),
221 _ => String::new(),
222 })
223 .unwrap_or_default(),
224 lines,
225 });
226 }
227
228 Ok(DiffInfo {
229 old_file: file_path.to_string(),
230 new_file: String::new(),
231 status: FileStatus::Deleted,
232 hunks,
233 })
234 }
235 (false, true) => {
236 Err(anyhow::anyhow!(
238 "file '{}' did not exist at commit {} — it was added later",
239 file_path,
240 &oid_str[..7.min(oid_str.len())]
241 ))
242 }
243 (false, false) => Err(anyhow::anyhow!(
244 "file '{}' not found in commit {} or working tree — it may have been renamed",
245 file_path,
246 &oid_str[..7.min(oid_str.len())]
247 )),
248 }
249}
250
251fn parse_diff(diff: &Diff<'_>) -> Result<Vec<DiffInfo>> {
256 let num_deltas = diff.deltas().len();
257 let mut infos: Vec<DiffInfo> = Vec::with_capacity(num_deltas);
258
259 for delta in diff.deltas() {
262 let old_file = delta
263 .old_file()
264 .path()
265 .map(|p| p.to_string_lossy().into_owned())
266 .unwrap_or_default();
267 let new_file = delta
268 .new_file()
269 .path()
270 .map(|p| p.to_string_lossy().into_owned())
271 .unwrap_or_default();
272 let status = FileStatus::from_delta(delta.status());
273 infos.push(DiffInfo {
274 old_file,
275 new_file,
276 status,
277 hunks: Vec::new(),
278 });
279 }
280
281 let mut current_delta_idx: usize = 0;
284
285 diff.print(DiffFormat::Patch, |delta, maybe_hunk, line| {
286 let delta_new = delta
288 .new_file()
289 .path()
290 .map(|p| p.to_string_lossy().into_owned())
291 .unwrap_or_default();
292 let delta_old = delta
293 .old_file()
294 .path()
295 .map(|p| p.to_string_lossy().into_owned())
296 .unwrap_or_default();
297
298 let found_idx = infos[current_delta_idx..]
300 .iter()
301 .position(|info| info.new_file == delta_new && info.old_file == delta_old)
302 .map(|pos| pos + current_delta_idx)
303 .or_else(|| {
304 infos[..current_delta_idx]
306 .iter()
307 .position(|info| info.new_file == delta_new && info.old_file == delta_old)
308 });
309
310 let found = found_idx.is_some();
311 if let Some(idx) = found_idx {
312 current_delta_idx = idx;
313 }
314 if !found {
315 return true; }
317
318 let info = &mut infos[current_delta_idx];
319
320 if let Some(hunk) = maybe_hunk {
322 let header = String::from_utf8_lossy(hunk.header())
323 .trim_end()
324 .to_string();
325
326 let needs_new = match info.hunks.last() {
328 Some(h) => h.header != header,
329 None => true,
330 };
331 if needs_new {
332 info.hunks.push(DiffHunk {
333 header: header.clone(),
334 lines: vec![DiffLine::HunkHeader(header)],
335 });
336 }
337 }
338
339 if let Some(hunk) = info.hunks.last_mut() {
341 let content = String::from_utf8_lossy(line.content())
342 .trim_end_matches('\n')
343 .trim_end_matches('\r')
344 .to_string();
345
346 let diff_line = match line.origin() {
347 '+' | '>' => DiffLine::Addition(content),
348 '-' | '<' => DiffLine::Deletion(content),
349 ' ' => DiffLine::Context(content),
350 _ => return true,
353 };
354 hunk.lines.push(diff_line);
355 }
356
357 true
358 })
359 .context("failed to walk diff")?;
360
361 Ok(infos)
362}
363
364#[cfg(test)]
365mod tests {
366 use super::*;
367 use std::fs;
368
369 fn init_repo_with_commit(dir: &std::path::Path) -> git2::Repository {
370 let repo = git2::Repository::init(dir).unwrap();
371 {
372 let file_path = dir.join("hello.txt");
373 fs::write(&file_path, "Hello, world!\n").unwrap();
374
375 let mut index = repo.index().unwrap();
376 index.add_path(std::path::Path::new("hello.txt")).unwrap();
377 index.write().unwrap();
378
379 let tree_oid = index.write_tree().unwrap();
380 let tree = repo.find_tree(tree_oid).unwrap();
381 let sig = git2::Signature::now("Test", "test@test.com").unwrap();
382 repo.commit(Some("HEAD"), &sig, &sig, "initial commit", &tree, &[])
383 .unwrap();
384 }
385 repo
386 }
387
388 #[test]
389 fn working_dir_diff_shows_changes() {
390 let tmp = tempfile::tempdir().unwrap();
391 let repo = init_repo_with_commit(tmp.path());
392
393 fs::write(tmp.path().join("hello.txt"), "Hello, modified!\n").unwrap();
395
396 let diffs = get_working_dir_diff(&repo).unwrap();
397 assert_eq!(diffs.len(), 1);
398 assert_eq!(diffs[0].new_file, "hello.txt");
399 assert_eq!(diffs[0].status, FileStatus::Modified);
400 assert!(!diffs[0].hunks.is_empty());
401 }
402
403 #[test]
404 fn staged_diff_shows_staged_changes() {
405 let tmp = tempfile::tempdir().unwrap();
406 let repo = init_repo_with_commit(tmp.path());
407
408 fs::write(tmp.path().join("hello.txt"), "Hello, staged!\n").unwrap();
410 let mut index = repo.index().unwrap();
411 index.add_path(std::path::Path::new("hello.txt")).unwrap();
412 index.write().unwrap();
413
414 let diffs = get_staged_diff(&repo).unwrap();
415 assert_eq!(diffs.len(), 1);
416 assert_eq!(diffs[0].new_file, "hello.txt");
417 assert_eq!(diffs[0].status, FileStatus::Modified);
418 }
419
420 #[test]
421 fn commit_diff_shows_initial_commit() {
422 let tmp = tempfile::tempdir().unwrap();
423 let repo = init_repo_with_commit(tmp.path());
424
425 let head_oid = repo.head().unwrap().target().unwrap().to_string();
426 let diffs = get_commit_diff(&repo, &head_oid).unwrap();
427 assert_eq!(diffs.len(), 1);
428 assert_eq!(diffs[0].new_file, "hello.txt");
429 assert_eq!(diffs[0].status, FileStatus::New);
430 }
431
432 #[test]
433 fn working_dir_diff_untracked_file() {
434 let tmp = tempfile::tempdir().unwrap();
435 let repo = init_repo_with_commit(tmp.path());
436
437 fs::write(tmp.path().join("new_file.txt"), "I am new!\n").unwrap();
439
440 let diffs = get_working_dir_diff(&repo).unwrap();
441 assert_eq!(diffs.len(), 1);
442 assert_eq!(diffs[0].new_file, "new_file.txt");
443 assert_eq!(diffs[0].status, FileStatus::Untracked);
444 }
445
446 #[test]
447 fn commit_file_list_returns_entries() {
448 let tmp = tempfile::tempdir().unwrap();
449 let repo = init_repo_with_commit(tmp.path());
450 let head_oid = repo.head().unwrap().target().unwrap().to_string();
451 let files = get_commit_file_list(&repo, &head_oid).unwrap();
452 assert_eq!(files.len(), 1);
453 assert_eq!(files[0].new_file, "hello.txt");
454 assert_eq!(files[0].status, FileStatus::New);
455 assert_eq!(files[0].display_path(), "hello.txt");
456 }
457
458 #[test]
459 fn single_file_diff_returns_correct_file() {
460 let tmp = tempfile::tempdir().unwrap();
461 let repo = init_repo_with_commit(tmp.path());
462 let head_oid = repo.head().unwrap().target().unwrap().to_string();
463 let diff = get_single_file_diff(&repo, &head_oid, "hello.txt").unwrap();
464 assert_eq!(diff.new_file, "hello.txt");
465 assert_eq!(diff.status, FileStatus::New);
466 assert!(!diff.hunks.is_empty());
467 }
468
469 #[test]
470 fn diff_file_commit_vs_workdir_shows_changes() {
471 let tmp = tempfile::tempdir().unwrap();
472 let repo = init_repo_with_commit(tmp.path());
473 let head_oid = repo.head().unwrap().target().unwrap().to_string();
474
475 std::fs::write(tmp.path().join("hello.txt"), "Modified content!\n").unwrap();
477
478 let diff = diff_file_commit_vs_workdir(&repo, &head_oid, "hello.txt").unwrap();
479 assert_eq!(diff.new_file, "hello.txt");
480 assert!(!diff.hunks.is_empty());
481 }
482
483 #[test]
484 fn single_file_diff_not_found() {
485 let tmp = tempfile::tempdir().unwrap();
486 let repo = init_repo_with_commit(tmp.path());
487 let head_oid = repo.head().unwrap().target().unwrap().to_string();
488 let result = get_single_file_diff(&repo, &head_oid, "nonexistent.txt");
489 assert!(result.is_err());
490 }
491}