Skip to main content

gitkraft_core/features/diff/
ops.rs

1//! Diff operations — working-directory, staged, and per-commit diffs.
2
3use anyhow::{Context, Result};
4use git2::{Diff, DiffFormat, DiffOptions, Repository};
5
6use super::types::{DiffHunk, DiffInfo, DiffLine, FileStatus};
7
8// ── Public API ────────────────────────────────────────────────────────────────
9
10/// Return the diff of unstaged (working-directory) changes against the index.
11///
12/// Includes untracked files.
13pub 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
24/// Return the diff of staged (index) changes against HEAD.
25///
26/// For an initial commit (no HEAD yet), diffs the full index as all-new files.
27pub 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        // No HEAD yet (empty repo) — diff the full index as "new"
36        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
45/// Return the diff introduced by a specific commit (compared to its first parent).
46///
47/// For a root commit (no parents), diffs against an empty tree.
48pub 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
70/// Return just the list of changed files for a commit — no hunk / line parsing.
71///
72/// This is much faster than [`get_commit_diff`] because it only reads the
73/// tree-level delta metadata.  The GUI uses this to instantly populate the
74/// file sidebar when a commit is selected.
75pub 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
115/// Return the diff for a **single file** within a commit.
116///
117/// Uses `pathspec` filtering so that git2 only walks the hunks / lines for the
118/// requested file — much faster than parsing the entire commit diff.
119pub 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
148// ── Helpers ───────────────────────────────────────────────────────────────────
149
150/// Walk every delta / hunk / line in a `git2::Diff` and produce our domain
151/// `Vec<DiffInfo>`.
152fn parse_diff(diff: &Diff<'_>) -> Result<Vec<DiffInfo>> {
153    let num_deltas = diff.deltas().len();
154    let mut infos: Vec<DiffInfo> = Vec::with_capacity(num_deltas);
155
156    // Pre-populate DiffInfo shells for each delta so the print callback can
157    // index into them.
158    for delta in diff.deltas() {
159        let old_file = delta
160            .old_file()
161            .path()
162            .map(|p| p.to_string_lossy().into_owned())
163            .unwrap_or_default();
164        let new_file = delta
165            .new_file()
166            .path()
167            .map(|p| p.to_string_lossy().into_owned())
168            .unwrap_or_default();
169        let status = FileStatus::from_delta(delta.status());
170        infos.push(DiffInfo {
171            old_file,
172            new_file,
173            status,
174            hunks: Vec::new(),
175        });
176    }
177
178    // Walk through the diff with the print callback which gives us
179    // file / hunk / line events in order.
180    let mut current_delta_idx: usize = 0;
181
182    diff.print(DiffFormat::Patch, |delta, maybe_hunk, line| {
183        // Identify which delta we are currently processing by matching paths.
184        let delta_new = delta
185            .new_file()
186            .path()
187            .map(|p| p.to_string_lossy().into_owned())
188            .unwrap_or_default();
189        let delta_old = delta
190            .old_file()
191            .path()
192            .map(|p| p.to_string_lossy().into_owned())
193            .unwrap_or_default();
194
195        // Find the matching DiffInfo — usually at current_delta_idx or later.
196        let found_idx = infos[current_delta_idx..]
197            .iter()
198            .position(|info| info.new_file == delta_new && info.old_file == delta_old)
199            .map(|pos| pos + current_delta_idx)
200            .or_else(|| {
201                // Also search from the beginning in case deltas are reordered
202                infos[..current_delta_idx]
203                    .iter()
204                    .position(|info| info.new_file == delta_new && info.old_file == delta_old)
205            });
206
207        let found = found_idx.is_some();
208        if let Some(idx) = found_idx {
209            current_delta_idx = idx;
210        }
211        if !found {
212            return true; // skip unknown delta
213        }
214
215        let info = &mut infos[current_delta_idx];
216
217        // If we have a hunk header, potentially create a new hunk.
218        if let Some(hunk) = maybe_hunk {
219            let header = String::from_utf8_lossy(hunk.header())
220                .trim_end()
221                .to_string();
222
223            // Only create a new hunk if the header differs from the current one.
224            let needs_new = match info.hunks.last() {
225                Some(h) => h.header != header,
226                None => true,
227            };
228            if needs_new {
229                info.hunks.push(DiffHunk {
230                    header: header.clone(),
231                    lines: vec![DiffLine::HunkHeader(header)],
232                });
233            }
234        }
235
236        // Map the line origin to our DiffLine type and append to the current hunk.
237        if let Some(hunk) = info.hunks.last_mut() {
238            let content = String::from_utf8_lossy(line.content())
239                .trim_end_matches('\n')
240                .trim_end_matches('\r')
241                .to_string();
242
243            let diff_line = match line.origin() {
244                '+' | '>' => DiffLine::Addition(content),
245                '-' | '<' => DiffLine::Deletion(content),
246                ' ' => DiffLine::Context(content),
247                // File-level headers ('F'), binary notices ('B'), hunk header origin ('H')
248                // — we skip these as they are handled above or are informational.
249                _ => return true,
250            };
251            hunk.lines.push(diff_line);
252        }
253
254        true
255    })
256    .context("failed to walk diff")?;
257
258    Ok(infos)
259}
260
261#[cfg(test)]
262mod tests {
263    use super::*;
264    use std::fs;
265
266    fn init_repo_with_commit(dir: &std::path::Path) -> git2::Repository {
267        let repo = git2::Repository::init(dir).unwrap();
268        {
269            let file_path = dir.join("hello.txt");
270            fs::write(&file_path, "Hello, world!\n").unwrap();
271
272            let mut index = repo.index().unwrap();
273            index.add_path(std::path::Path::new("hello.txt")).unwrap();
274            index.write().unwrap();
275
276            let tree_oid = index.write_tree().unwrap();
277            let tree = repo.find_tree(tree_oid).unwrap();
278            let sig = git2::Signature::now("Test", "test@test.com").unwrap();
279            repo.commit(Some("HEAD"), &sig, &sig, "initial commit", &tree, &[])
280                .unwrap();
281        }
282        repo
283    }
284
285    #[test]
286    fn working_dir_diff_shows_changes() {
287        let tmp = tempfile::tempdir().unwrap();
288        let repo = init_repo_with_commit(tmp.path());
289
290        // Modify the file
291        fs::write(tmp.path().join("hello.txt"), "Hello, modified!\n").unwrap();
292
293        let diffs = get_working_dir_diff(&repo).unwrap();
294        assert_eq!(diffs.len(), 1);
295        assert_eq!(diffs[0].new_file, "hello.txt");
296        assert_eq!(diffs[0].status, FileStatus::Modified);
297        assert!(!diffs[0].hunks.is_empty());
298    }
299
300    #[test]
301    fn staged_diff_shows_staged_changes() {
302        let tmp = tempfile::tempdir().unwrap();
303        let repo = init_repo_with_commit(tmp.path());
304
305        // Modify and stage the file
306        fs::write(tmp.path().join("hello.txt"), "Hello, staged!\n").unwrap();
307        let mut index = repo.index().unwrap();
308        index.add_path(std::path::Path::new("hello.txt")).unwrap();
309        index.write().unwrap();
310
311        let diffs = get_staged_diff(&repo).unwrap();
312        assert_eq!(diffs.len(), 1);
313        assert_eq!(diffs[0].new_file, "hello.txt");
314        assert_eq!(diffs[0].status, FileStatus::Modified);
315    }
316
317    #[test]
318    fn commit_diff_shows_initial_commit() {
319        let tmp = tempfile::tempdir().unwrap();
320        let repo = init_repo_with_commit(tmp.path());
321
322        let head_oid = repo.head().unwrap().target().unwrap().to_string();
323        let diffs = get_commit_diff(&repo, &head_oid).unwrap();
324        assert_eq!(diffs.len(), 1);
325        assert_eq!(diffs[0].new_file, "hello.txt");
326        assert_eq!(diffs[0].status, FileStatus::New);
327    }
328
329    #[test]
330    fn working_dir_diff_untracked_file() {
331        let tmp = tempfile::tempdir().unwrap();
332        let repo = init_repo_with_commit(tmp.path());
333
334        // Create a new untracked file
335        fs::write(tmp.path().join("new_file.txt"), "I am new!\n").unwrap();
336
337        let diffs = get_working_dir_diff(&repo).unwrap();
338        assert_eq!(diffs.len(), 1);
339        assert_eq!(diffs[0].new_file, "new_file.txt");
340        assert_eq!(diffs[0].status, FileStatus::Untracked);
341    }
342
343    #[test]
344    fn commit_file_list_returns_entries() {
345        let tmp = tempfile::tempdir().unwrap();
346        let repo = init_repo_with_commit(tmp.path());
347        let head_oid = repo.head().unwrap().target().unwrap().to_string();
348        let files = get_commit_file_list(&repo, &head_oid).unwrap();
349        assert_eq!(files.len(), 1);
350        assert_eq!(files[0].new_file, "hello.txt");
351        assert_eq!(files[0].status, FileStatus::New);
352        assert_eq!(files[0].display_path(), "hello.txt");
353    }
354
355    #[test]
356    fn single_file_diff_returns_correct_file() {
357        let tmp = tempfile::tempdir().unwrap();
358        let repo = init_repo_with_commit(tmp.path());
359        let head_oid = repo.head().unwrap().target().unwrap().to_string();
360        let diff = get_single_file_diff(&repo, &head_oid, "hello.txt").unwrap();
361        assert_eq!(diff.new_file, "hello.txt");
362        assert_eq!(diff.status, FileStatus::New);
363        assert!(!diff.hunks.is_empty());
364    }
365
366    #[test]
367    fn single_file_diff_not_found() {
368        let tmp = tempfile::tempdir().unwrap();
369        let repo = init_repo_with_commit(tmp.path());
370        let head_oid = repo.head().unwrap().target().unwrap().to_string();
371        let result = get_single_file_diff(&repo, &head_oid, "nonexistent.txt");
372        assert!(result.is_err());
373    }
374}