use crate::core::context::{ChangeType, RecentCommit, StagedFile};
use crate::git::utils::is_binary_diff;
use anyhow::{Context, Result};
use git2::{DiffOptions, Repository, StatusOptions};
use log::debug;
use std::fs;
use std::path::Path;
use super::ignore_matcher::GitIgnoreMatcher;
#[derive(Debug)]
pub struct RepoFilesInfo {
pub branch: String,
pub recent_commits: Vec<RecentCommit>,
pub staged_files: Vec<StagedFile>,
pub file_paths: Vec<String>,
}
pub fn get_file_statuses(
repo: &Repository,
gitignore_matcher: &GitIgnoreMatcher,
) -> Result<Vec<StagedFile>> {
debug!("Getting file statuses");
let mut staged_files = Vec::new();
let mut opts = StatusOptions::new();
opts.include_untracked(true);
let statuses = repo.statuses(Some(&mut opts))?;
for entry in statuses.iter() {
let path = entry.path().context("Could not get path")?;
let status = entry.status();
if status.is_index_new() || status.is_index_modified() || status.is_index_deleted() {
let change_type = if status.is_index_new() {
ChangeType::Added
} else if status.is_index_modified() {
ChangeType::Modified
} else {
ChangeType::Deleted
};
let should_exclude = gitignore_matcher.should_exclude(path);
let diff = if should_exclude {
String::from("[Content excluded]")
} else {
get_diff_for_file(repo, path)?
};
let content =
if should_exclude || change_type != ChangeType::Modified || is_binary_diff(&diff) {
None
} else {
let path_obj = Path::new(path);
if path_obj.exists() {
Some(fs::read_to_string(path_obj)?)
} else {
None
}
};
staged_files.push(StagedFile {
path: path.to_string(),
change_type,
diff,
content,
content_excluded: should_exclude,
});
}
}
debug!("Found {} staged files", staged_files.len());
Ok(staged_files)
}
pub fn get_diff_for_file(repo: &Repository, path: &str) -> Result<String> {
debug!("Getting diff for file: {}", path);
let mut diff_options = DiffOptions::new();
diff_options.pathspec(path);
let tree = match repo.head() {
Ok(head) => Some(head.peel_to_tree()?),
Err(_) => {
None
}
};
let diff = repo.diff_tree_to_workdir_with_index(tree.as_ref(), Some(&mut diff_options))?;
let mut diff_string = String::new();
diff.print(git2::DiffFormat::Patch, |_delta, _hunk, line| {
let origin = match line.origin() {
'+' | '-' | ' ' => line.origin(),
_ => ' ',
};
diff_string.push(origin);
diff_string.push_str(&String::from_utf8_lossy(line.content()));
true
})?;
if is_binary_diff(&diff_string) {
Ok("[Binary file changed]".to_string())
} else {
debug!("Generated diff for {} ({} bytes)", path, diff_string.len());
Ok(diff_string)
}
}
pub fn get_unstaged_file_statuses(
repo: &Repository,
gitignore_matcher: &GitIgnoreMatcher,
) -> Result<Vec<StagedFile>> {
debug!("Getting unstaged file statuses");
let mut unstaged_files = Vec::new();
let mut opts = StatusOptions::new();
opts.include_untracked(true);
let statuses = repo.statuses(Some(&mut opts))?;
for entry in statuses.iter() {
let path = entry.path().context("Could not get path")?;
let status = entry.status();
if status.is_wt_new() || status.is_wt_modified() || status.is_wt_deleted() {
let change_type = if status.is_wt_new() {
ChangeType::Added
} else if status.is_wt_modified() {
ChangeType::Modified
} else {
ChangeType::Deleted
};
let should_exclude = gitignore_matcher.should_exclude(path);
let diff = if should_exclude {
String::from("[Content excluded]")
} else {
get_diff_for_unstaged_file(repo, path)?
};
let content =
if should_exclude || change_type != ChangeType::Modified || is_binary_diff(&diff) {
None
} else {
let path_obj = Path::new(path);
if path_obj.exists() {
Some(fs::read_to_string(path_obj)?)
} else {
None
}
};
unstaged_files.push(StagedFile {
path: path.to_string(),
change_type,
diff,
content,
content_excluded: should_exclude,
});
}
}
debug!("Found {} unstaged files", unstaged_files.len());
Ok(unstaged_files)
}
pub fn get_diff_for_unstaged_file(repo: &Repository, path: &str) -> Result<String> {
debug!("Getting unstaged diff for file: {}", path);
let mut diff_options = DiffOptions::new();
diff_options.pathspec(path);
let diff = repo.diff_index_to_workdir(None, Some(&mut diff_options))?;
let mut diff_string = String::new();
diff.print(git2::DiffFormat::Patch, |_delta, _hunk, line| {
let origin = match line.origin() {
'+' | '-' | ' ' => line.origin(),
_ => ' ',
};
diff_string.push(origin);
diff_string.push_str(&String::from_utf8_lossy(line.content()));
true
})?;
if is_binary_diff(&diff_string) {
Ok("[Binary file changed]".to_string())
} else {
debug!(
"Generated unstaged diff for {} ({} bytes)",
path,
diff_string.len()
);
Ok(diff_string)
}
}