use std::path::Path;
use gitpoulpi_core::domain::{DiscardTarget, HunkId, LineSelector, RepoRef, StageTarget};
use gitpoulpi_core::{Error, Result};
use crate::patch::{self, Direction, PatchHunk};
use crate::process::{run, run_with_stdin};
pub fn stage(repo: &RepoRef, target: &StageTarget) -> Result<()> {
match target {
StageTarget::File(path) => {
run(&repo.root, &["add", "--", &path_str(path)])?;
}
StageTarget::Folder(path) => {
run(&repo.root, &["add", "--", &path_str(path)])?;
}
StageTarget::Files(paths) => {
if !paths.is_empty() {
let mut args = vec!["add".to_string(), "--".to_string()];
args.extend(paths.iter().map(|p| path_str(p)));
let args: Vec<&str> = args.iter().map(String::as_str).collect();
run(&repo.root, &args)?;
}
}
StageTarget::All => {
run(&repo.root, &["add", "-A"])?;
}
StageTarget::Hunk(path, hunk_id) => {
if let Some(whole) = untracked_hunk(repo, path)? {
if hunk_id.0 != 0 {
return Err(hunk_not_found(path, *hunk_id));
}
return apply_new_file(repo, path, &whole);
}
let hunk = find_hunk(repo, Side::Worktree, path, *hunk_id)?;
apply(repo, path, &hunk, &["apply", "--cached", "-"])?;
}
StageTarget::Line(path, selector) => {
if let Some(whole) = untracked_hunk(repo, path)? {
if selector.hunk.0 != 0 {
return Err(hunk_not_found(path, selector.hunk));
}
let picked =
patch::select_lines(&whole, &selector.line_indices, Direction::Forward);
if !picked.has_changes() {
return Err(Error::Backend(format!(
"the selection covers no line of {}",
path.display()
)));
}
return apply_new_file(repo, path, &picked);
}
let hunk = select(repo, Side::Worktree, path, selector, Direction::Forward)?;
apply(repo, path, &hunk, &["apply", "--cached", "-"])?;
}
}
Ok(())
}
fn hunk_not_found(path: &Path, id: HunkId) -> Error {
Error::Backend(format!("hunk {} not found in {}", id.0, path.display()))
}
fn untracked_hunk(repo: &RepoRef, path: &Path) -> Result<Option<PatchHunk>> {
let spec = format!(":(literal){}", path_str(path));
if !run(&repo.root, &["ls-files", "-z", "--", &spec])?.is_empty() {
return Ok(None);
}
let full = repo.root.join(path);
if !full.is_file() {
return Ok(None);
}
let bytes = std::fs::read(&full)
.map_err(|e| Error::Backend(format!("could not read {}: {e}", path.display())))?;
let text = String::from_utf8(bytes).map_err(|_| {
Error::Backend(format!(
"{} is not text: stage or discard it as a whole file",
path.display()
))
})?;
Ok(patch::whole_file_hunk(&text))
}
fn apply_new_file(repo: &RepoRef, path: &Path, hunk: &PatchHunk) -> Result<()> {
let text = patch::render_new_file(&path_str(path), file_mode(&repo.root.join(path)), hunk);
run_with_stdin(&repo.root, &["apply", "--cached", "-"], &text)?;
Ok(())
}
#[cfg(unix)]
fn file_mode(path: &Path) -> &'static str {
use std::os::unix::fs::PermissionsExt;
match std::fs::metadata(path) {
Ok(m) if m.permissions().mode() & 0o111 != 0 => "100755",
_ => "100644",
}
}
#[cfg(not(unix))]
fn file_mode(_path: &Path) -> &'static str {
"100644"
}
fn discard_untracked(
repo: &RepoRef,
path: &Path,
whole: &PatchHunk,
target: &DiscardTarget,
) -> Result<()> {
let full = repo.root.join(path);
let (id, indices) = match target {
DiscardTarget::Hunk(_, id) => (*id, None),
DiscardTarget::Line(_, selector) => (selector.hunk, Some(&selector.line_indices)),
};
if id.0 != 0 {
return Err(hunk_not_found(path, id));
}
let Some(indices) = indices else {
return std::fs::remove_file(&full)
.map_err(|e| Error::Backend(format!("could not delete {}: {e}", path.display())));
};
if !indices.iter().any(|i| (*i as usize) < whole.lines.len()) {
return Err(Error::Backend(format!(
"the selection covers no line of {}",
path.display()
)));
}
let mut out = String::new();
for (i, line) in whole.lines.iter().enumerate() {
if indices.contains(&(i as u32)) {
continue;
}
out.push_str(&line.text);
if !line.no_eol {
out.push('\n');
}
}
std::fs::write(&full, out)
.map_err(|e| Error::Backend(format!("could not write {}: {e}", path.display())))
}
pub fn unstage(repo: &RepoRef, target: &StageTarget) -> Result<()> {
match target {
StageTarget::File(path) => {
unstage_paths(repo, &path_str(path))?;
}
StageTarget::Files(paths) => {
if !paths.is_empty() {
let specs: Vec<String> = paths.iter().map(|p| path_str(p)).collect();
let specs: Vec<&str> = specs.iter().map(String::as_str).collect();
unstage_specs(repo, &specs)?;
}
}
StageTarget::Folder(path) => {
unstage_all_but_unmerged(repo, &path_str(path))?;
}
StageTarget::All => {
unstage_all_but_unmerged(repo, ".")?;
}
StageTarget::Hunk(path, hunk_id) => {
let hunk = find_hunk(repo, Side::Index, path, *hunk_id)?;
apply(repo, path, &hunk, &["apply", "--cached", "--reverse", "-"])?;
}
StageTarget::Line(path, selector) => {
let hunk = select(repo, Side::Index, path, selector, Direction::Reverse)?;
apply(repo, path, &hunk, &["apply", "--cached", "--reverse", "-"])?;
}
}
Ok(())
}
fn unstage_paths(repo: &RepoRef, spec: &str) -> Result<()> {
unstage_specs(repo, &[spec])
}
fn unstage_specs(repo: &RepoRef, specs: &[&str]) -> Result<()> {
let mut restore = vec!["restore", "--staged", "--"];
restore.extend_from_slice(specs);
match run(&repo.root, &restore) {
Err(Error::Git { message: msg, .. })
if msg.contains("could not resolve HEAD")
|| msg.contains("could not resolve 'HEAD'") =>
{
let mut rm = vec!["rm", "-r", "-q", "--cached", "--ignore-unmatch", "--"];
rm.extend_from_slice(specs);
run(&repo.root, &rm)?;
Ok(())
}
other => other.map(|_| ()),
}
}
fn unstage_all_but_unmerged(repo: &RepoRef, spec: &str) -> Result<()> {
let unmerged = match crate::index_scan::has_unmerged(&crate::conflicts::git_dir(&repo.root)) {
Some(false) => Vec::new(),
_ => unmerged_under(repo, spec),
};
if unmerged.is_empty() {
return unstage_paths(repo, spec);
}
let staged = run(
&repo.root,
&[
"diff-index",
"--cached",
"--name-only",
"--no-renames",
"-z",
"HEAD",
"--",
spec,
],
)?;
let mut args: Vec<String> = ["restore", "--staged", "--"]
.iter()
.map(|s| s.to_string())
.collect();
args.extend(
staged
.split('\0')
.filter(|p| !p.is_empty() && !unmerged.iter().any(|u| u == p))
.map(|p| format!(":(literal){p}")),
);
if args.len() == 3 {
return Ok(());
}
let refs: Vec<&str> = args.iter().map(String::as_str).collect();
run(&repo.root, &refs).map(|_| ())
}
pub fn discard_part(repo: &RepoRef, target: &DiscardTarget) -> Result<()> {
let (DiscardTarget::Hunk(path, _) | DiscardTarget::Line(path, _)) = target;
if let Some(whole) = untracked_hunk(repo, path)? {
return discard_untracked(repo, path, &whole, target);
}
let (path, hunk) = match target {
DiscardTarget::Hunk(path, hunk_id) => {
(path, find_hunk(repo, Side::Worktree, path, *hunk_id)?)
}
DiscardTarget::Line(path, selector) => (
path,
select(repo, Side::Worktree, path, selector, Direction::Reverse)?,
),
};
apply(repo, path, &hunk, &["apply", "--reverse", "-"])
}
#[derive(Clone, Copy)]
enum Side {
Worktree,
Index,
}
fn unmerged_under(repo: &RepoRef, spec: &str) -> Vec<String> {
let Ok(out) = run(&repo.root, &["ls-files", "-u", "-z", "--", spec]) else {
return Vec::new();
};
let mut paths: Vec<String> = out
.split('\0')
.filter_map(|record| record.split_once('\t').map(|(_, p)| p.to_string()))
.collect();
paths.dedup();
paths
}
fn path_str(path: &Path) -> String {
path.to_string_lossy().to_string()
}
fn file_hunks(repo: &RepoRef, side: Side, path: &Path) -> Result<Vec<PatchHunk>> {
let spec = format!(":(literal){}", path_str(path));
let mut args = vec![
"diff",
"--no-color",
"--no-ext-diff",
"--no-textconv",
"-U3",
];
if matches!(side, Side::Index) {
args.push("--cached");
}
args.extend(["--", &spec]);
let raw = run(&repo.root, &args)?;
let hunks = patch::parse_hunks(&raw);
if hunks.is_empty() {
return Err(Error::Backend(format!(
"no diff found for {}",
path.display()
)));
}
Ok(hunks)
}
fn find_hunk(repo: &RepoRef, side: Side, path: &Path, id: HunkId) -> Result<PatchHunk> {
file_hunks(repo, side, path)?
.into_iter()
.nth(id.0 as usize)
.ok_or_else(|| Error::Backend(format!("hunk {} not found in {}", id.0, path.display())))
}
fn select(
repo: &RepoRef,
side: Side,
path: &Path,
selector: &LineSelector,
direction: Direction,
) -> Result<PatchHunk> {
let hunk = find_hunk(repo, side, path, selector.hunk)?;
let selected = patch::select_lines(&hunk, &selector.line_indices, direction);
if !selected.has_changes() {
return Err(Error::Backend(format!(
"the selection covers no added or removed line of hunk {} in {}",
selector.hunk.0,
path.display()
)));
}
Ok(selected)
}
fn apply(repo: &RepoRef, path: &Path, hunk: &PatchHunk, args: &[&str]) -> Result<()> {
let text = patch::render(&path_str(path), hunk);
run_with_stdin(&repo.root, args, &text)?;
Ok(())
}