1#![cfg(feature = "git")]
3use std::collections::HashMap;
8use git2::{DiffOptions, IntoCString, Repository};
10#[derive(Copy, Clone, Debug)]
18pub type LineChanges = HashMap<u32, LineChange>;
20pub fn get_git_diff(filename: &OsStr) -> Option<LineChanges> {
21 let repo = Repository::discover(&filename).ok()?;
23 let repo_path_absolute = fs::canonicalize(repo.workdir()?).ok()?;
25 let filepath_absolute = fs::canonicalize(&filename).ok()?;
26 let filepath_relative_to_repo = filepath_absolute.strip_prefix(&repo_path_absolute).ok()?;
28 let mut diff_options = DiffOptions::new();
29 let pathspec = filepath_relative_to_repo.into_c_string().ok()?;
30 diff_options.pathspec(pathspec);
31 diff_options.context_lines(0);
34 .diff_index_to_workdir(None, Some(&mut diff_options))
37 let mut line_changes: LineChanges = HashMap::new();
40 |line_changes: &mut LineChanges, start: u32, end: i32, change: LineChange| {
41 for line in start..=end as u32 {
42 line_changes.insert(line, change);
49 Some(&mut |delta, hunk| {
50 let path = delta.new_file().path().unwrap_or_else(|| Path::new(""));
52 if filepath_relative_to_repo != path {
56 let old_lines = hunk.old_lines();
57 let new_start = hunk.new_start();
58 let new_lines = hunk.new_lines();
59 let new_end = (new_start + new_lines) as i32 - 1;
61 if old_lines == 0 && new_lines > 0 {
62 mark_section(&mut line_changes, new_start, new_end, LineChange::Added);
63 } else if new_lines == 0 && old_lines > 0 {
65 mark_section(&mut line_changes, 1, 1, LineChange::RemovedAbove);
71 LineChange::RemovedBelow,
75 mark_section(&mut line_changes, new_start, new_end, LineChange::Modified);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list