use super::*;
pub fn stash_push(root: &Path, message: Option<&str>) -> Result<String, String> {
stash_push_args(root, &[], message)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum StashKind {
Tracked,
IncludeUntracked,
Staged,
}
pub(super) fn stash_push_args(
root: &Path,
extra: &[&str],
message: Option<&str>,
) -> Result<String, String> {
let mut args = vec!["stash", "push"];
args.extend_from_slice(extra);
if let Some(message) = message.map(str::trim).filter(|m| !m.is_empty()) {
args.extend(["-m", message]);
}
run_mutation(root, &args)
}
pub fn stash_pop(root: &Path) -> Result<String, String> {
run_mutation_keeping_conflicts(root, &["stash", "pop"])
}
pub fn stash_push_untracked(root: &Path, message: Option<&str>) -> Result<String, String> {
stash_push_args(root, &["-u"], message)
}
pub fn stash_push_staged(root: &Path, message: Option<&str>) -> Result<String, String> {
stash_push_args(root, &["--staged"], message)
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StashInfo {
pub index: usize,
pub message: String,
pub age: String,
pub files: Vec<String>,
}
impl StashInfo {
pub fn label(&self) -> String {
let mut label = format!("stash@{{{}}}: {}", self.index, self.message);
if !self.age.is_empty() {
label.push_str(&format!(" \u{b7} {}", self.age));
}
if !self.files.is_empty() {
let shown: Vec<&str> = self.files.iter().take(3).map(String::as_str).collect();
label.push_str(&format!(" \u{b7} {}", shown.join(", ")));
if self.files.len() > 3 {
label.push_str(&format!(" +{}", self.files.len() - 3));
}
}
label
}
}
pub fn list_stashes(root: &Path) -> Result<Vec<StashInfo>, String> {
let raw = run_git(
root,
&[
"stash",
"list",
"-m",
"--name-only",
"--format=%x1e%gd%x1f%cr%x1f%P%x1f%gs",
],
)
.map_err(|e| e.to_string())?;
let mut out = Vec::new();
let mut untracked_commits: Vec<(usize, String)> = Vec::new();
for record in raw.split('\x1e') {
let mut lines = record.lines();
let Some(header) = lines.next().filter(|h| !h.is_empty()) else {
continue;
};
let mut parts = header.split('\x1f');
let reflog = parts.next().unwrap_or("");
let age = parts.next().unwrap_or("").to_string();
let parents = parts.next().unwrap_or("");
let message = parts.next().unwrap_or("").to_string();
let index = reflog
.trim_start_matches("stash@{")
.trim_end_matches('}')
.parse::<usize>()
.unwrap_or(out.len());
if let Some(untracked) = parents.split_whitespace().nth(2) {
untracked_commits.push((out.len(), untracked.to_string()));
}
let files = lines
.map(str::trim_end)
.filter(|l| !l.is_empty())
.map(str::to_string)
.collect();
out.push(StashInfo {
index,
message,
age,
files,
});
}
if !untracked_commits.is_empty() {
let mut args = vec!["show", "--root", "--name-only", "--format=%x1e%H"];
args.extend(untracked_commits.iter().map(|(_, hash)| hash.as_str()));
if let Ok(raw) = run_git(root, &args) {
let mut by_hash: std::collections::HashMap<&str, Vec<String>> =
std::collections::HashMap::new();
for record in raw.split('\x1e') {
let mut lines = record.lines();
let Some(hash) = lines.next().filter(|h| !h.is_empty()) else {
continue;
};
let names = lines
.map(str::trim_end)
.filter(|l| !l.is_empty())
.map(str::to_string)
.collect();
by_hash.insert(hash.trim(), names);
}
for (pos, hash) in &untracked_commits {
if let Some(names) = by_hash.remove(hash.as_str()) {
let files = &mut out[*pos].files;
files.extend(names);
files.sort();
files.dedup();
}
}
}
}
Ok(out)
}
pub fn show_stash(root: &Path, index: usize) -> Result<String, String> {
diff_text(
root,
&[
"stash",
"show",
"--no-color",
"--no-ext-diff",
"--stat",
"--patch",
"--include-untracked",
&format!("stash@{{{index}}}"),
],
)
}
pub fn stash_apply(root: &Path, index: usize) -> Result<String, String> {
run_mutation_keeping_conflicts(root, &["stash", "apply", &format!("stash@{{{index}}}")])
}
pub fn stash_pop_at(root: &Path, index: usize) -> Result<String, String> {
run_mutation_keeping_conflicts(root, &["stash", "pop", &format!("stash@{{{index}}}")])
}
pub fn stash_drop(root: &Path, index: usize) -> Result<String, String> {
run_mutation(root, &["stash", "drop", &format!("stash@{{{index}}}")])
}
pub fn stash_oid(root: &Path, index: usize) -> Result<String, String> {
run_git(
root,
&[
"rev-parse",
"--verify",
"--quiet",
&format!("stash@{{{index}}}"),
],
)
.map_err(|e| e.to_string())
}
pub fn stash_drop_oid(root: &Path, oid: &str) -> Result<String, String> {
stash_drop(root, stash_index_of(root, oid)?)
}
pub fn stash_pop_oid(root: &Path, oid: &str) -> Result<String, String> {
stash_pop_at(root, stash_index_of(root, oid)?)
}
pub(super) fn stash_index_of(root: &Path, oid: &str) -> Result<usize, String> {
let list = run_git(root, &["stash", "list", "--format=%H"]).map_err(|e| e.to_string())?;
list.lines()
.position(|hash| hash.trim() == oid)
.ok_or_else(|| String::from("that stash is no longer in the stash list"))
}