use std::collections::{BTreeMap, BTreeSet};
use super::{short, JournalEntry};
use crate::domain::{
EntryState, EntrySummary, FileHash, Oid, PathHash, RestoreItem, RestoreItemKind, RestoreOffer,
UndoRefusal, UNDO_PIN_PREFIX,
};
#[derive(Debug, Clone, Default)]
pub struct RestoreFacts {
pub refs: BTreeMap<String, Oid>,
pub symrefs: BTreeMap<String, String>,
pub config: BTreeMap<String, Vec<(String, String)>>,
pub stashes: Vec<Oid>,
pub files: Vec<PathHash>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RestoreAction {
CreateRef {
name: String,
oid: Oid,
kind: RestoreItemKind,
},
SetSymref { name: String, target: String },
SetConfig {
section: String,
to: Vec<(String, String)>,
from: Vec<(String, String)>,
},
StashStore { commit: Oid, message: String },
RestorePaths { paths: Vec<String>, snapshot: Oid },
SideWrite {
path: String,
dest: String,
file: FileHash,
},
ReAddLines {
path: String,
before: Option<FileHash>,
after: Option<FileHash>,
},
}
impl RestoreAction {
pub fn describe(&self) -> String {
match self {
RestoreAction::CreateRef { name, oid, .. } => {
format!("create {} at {}", display_ref(name), short(*oid))
}
RestoreAction::SetSymref { name, target } => {
format!("point {name} at {target}")
}
RestoreAction::SetConfig { section, to, .. } => {
format!("restore the configuration section {section} ({} keys)", to.len())
}
RestoreAction::StashStore { commit, message } => {
format!("put the stash \"{message}\" ({}) on top of the stash list", short(*commit))
}
RestoreAction::RestorePaths { paths, .. } => format!(
"write back {} (they are as the operation left them)",
super::paths_in_words(paths)
),
RestoreAction::SideWrite { path, dest, .. } => format!(
"{path} changed since: write the kept version next to it as {dest}, {path} is not touched"
),
RestoreAction::ReAddLines { path, .. } => {
format!("add the missing lines back to {path}")
}
}
}
pub fn written_paths(&self) -> Vec<String> {
match self {
RestoreAction::RestorePaths { paths, .. } => paths.clone(),
RestoreAction::SideWrite { dest, .. } => vec![dest.clone()],
RestoreAction::ReAddLines { path, .. } => vec![path.clone()],
_ => Vec::new(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RestorePlan {
pub entry: EntrySummary,
pub label: String,
pub actions: Vec<RestoreAction>,
}
impl RestorePlan {
pub fn describe(&self) -> Vec<String> {
self.actions.iter().map(RestoreAction::describe).collect()
}
}
fn display_ref(name: &str) -> String {
name.strip_prefix("refs/heads/")
.map(str::to_string)
.or_else(|| name.strip_prefix("refs/tags/").map(|t| format!("tag {t}")))
.or_else(|| name.strip_prefix("refs/remotes/").map(str::to_string))
.unwrap_or_else(|| name.to_string())
}
fn is_rule_file(path: &str) -> bool {
let base = path.rsplit('/').next().unwrap_or(path);
base == ".gitignore" || base == ".gitattributes"
}
pub fn readd_lines(before: &str, after: &str, current: &str) -> Option<String> {
let lines = |text: &str| -> Vec<String> {
text.lines()
.map(|l| l.trim_end().to_string())
.filter(|l| !l.is_empty())
.collect()
};
let (b, a, c) = (lines(before), lines(after), lines(current));
let mut wanted: Vec<String> = b.iter().filter(|l| !a.contains(l)).cloned().collect();
if wanted.is_empty() {
wanted = a.iter().filter(|l| !b.contains(l)).cloned().collect();
}
let mut missing: Vec<String> = Vec::new();
for line in wanted {
if !c.contains(&line) && !missing.contains(&line) {
missing.push(line);
}
}
if missing.is_empty() {
return None;
}
let mut out = current.to_string();
if !out.is_empty() && !out.ends_with('\n') {
out.push('\n');
}
for line in missing {
out.push_str(&line);
out.push('\n');
}
Some(out)
}
pub fn valid_ref_name(name: &str) -> bool {
!name.is_empty()
&& !name.starts_with('-')
&& !name.starts_with('/')
&& !name.ends_with('/')
&& !name.ends_with('.')
&& !name.ends_with(".lock")
&& !name.contains("..")
&& !name.contains("//")
&& !name.contains("@{")
&& !name
.chars()
.any(|c| c.is_whitespace() || c.is_control() || "~^:?*[\\".contains(c))
}
#[derive(Debug, Default)]
struct Candidates<'a> {
deleted: Vec<(&'a str, Oid, RestoreItemKind)>,
old_tips: Vec<(&'a str, Oid)>,
config: Vec<&'a crate::journal::ConfigChange>,
symrefs: Vec<&'a crate::journal::SymrefChange>,
stashes: Vec<&'a crate::journal::StashRecord>,
files: Vec<FileCandidate<'a>>,
}
#[derive(Debug)]
struct FileCandidate<'a> {
path: &'a str,
kept: Option<FileHash>,
after: Option<PathHash>,
after_worktree: Option<FileHash>,
}
fn candidates(entry: &JournalEntry) -> Candidates<'_> {
let mut c = Candidates::default();
for change in &entry.refs {
if change.name.starts_with(UNDO_PIN_PREFIX) {
continue;
}
match (change.old, change.new) {
(Some(old), None) => {
let kind = if change.name.starts_with("refs/tags/") {
RestoreItemKind::Tag
} else if change.name.starts_with("refs/heads/") {
RestoreItemKind::Branch
} else {
RestoreItemKind::RemoteRef
};
c.deleted.push((change.name.as_str(), old, kind));
}
(Some(old), Some(new)) if old != new && change.name.starts_with("refs/heads/") => {
c.old_tips.push((change.name.as_str(), old));
}
_ => {}
}
}
c.config = entry
.config
.iter()
.filter(|ch| sorted(&ch.old) != sorted(&ch.new))
.collect();
c.symrefs = entry
.symrefs
.iter()
.filter(|s| s.old.is_some() && s.old != s.new)
.collect();
if let Some(st) = &entry.stash {
c.stashes = st.removed.iter().collect();
}
if let Some(f) = &entry.files {
for path in &f.paths {
let hash = |snap: &crate::domain::Snapshot| {
snap.hashes.iter().find(|h| &h.path == path).cloned()
};
let before = hash(&f.before);
let after = hash(&f.after);
let kept = before.and_then(|b| b.worktree);
let after_worktree = after.as_ref().and_then(|a| a.worktree);
if after_worktree == kept || (kept.is_none() && !is_rule_file(path)) {
continue;
}
c.files.push(FileCandidate {
path,
kept,
after,
after_worktree,
});
}
}
c
}
fn sorted(pairs: &[(String, String)]) -> Vec<(String, String)> {
let mut v = pairs.to_vec();
v.sort();
v
}
fn not_restorable_reason(state: EntryState) -> &'static str {
match state {
EntryState::Purged => "its old history was purged, so there is nothing left to restore",
_ => "the operation never completed, so it is not known what it removed",
}
}
impl JournalEntry {
pub fn restore_offer(&self) -> RestoreOffer {
if self.state != EntryState::Done {
return RestoreOffer {
items: Vec::new(),
not_restorable: Some(not_restorable_reason(self.state).into()),
};
}
let c = candidates(self);
let mut items = Vec::new();
for (name, oid, kind) in &c.deleted {
items.push(RestoreItem {
kind: *kind,
label: format!("{} ({})", display_ref(name), short(*oid)),
});
}
for (name, oid) in &c.old_tips {
items.push(RestoreItem {
kind: RestoreItemKind::OldTip,
label: format!("old tip of {} ({})", display_ref(name), short(*oid)),
});
}
for r in &c.stashes {
items.push(RestoreItem {
kind: RestoreItemKind::Stash,
label: format!("stash \"{}\" ({})", r.message, short(r.commit)),
});
}
for f in &c.files {
items.push(RestoreItem {
kind: if is_rule_file(f.path) {
RestoreItemKind::Lines
} else {
RestoreItemKind::File
},
label: f.path.to_string(),
});
}
for ch in &c.config {
if ch.section.starts_with("branch.") && !ch.old.is_empty() && !c.deleted.is_empty() {
continue;
}
items.push(RestoreItem {
kind: RestoreItemKind::Config,
label: ch.section.clone(),
});
}
let not_restorable = items.is_empty().then(|| self.nothing_reason());
RestoreOffer {
items,
not_restorable,
}
}
fn nothing_reason(&self) -> String {
if self.index.is_some() || self.opstate.is_some() {
"it only changed the index (staging), nothing was removed".into()
} else if self.files.is_some() {
"the files it changed held nothing that is lost".into()
} else if self.refs.is_empty() && self.head.is_some() {
"it only moved HEAD; the reflog lists the earlier positions".into()
} else if !self.refs.is_empty() || self.stash.is_some() {
"it only added things, nothing was removed".into()
} else {
"it removed nothing that can be brought back without guessing".into()
}
}
}
fn unique(base: &str, taken: &dyn Fn(&str) -> bool) -> String {
if !taken(base) {
return base.to_string();
}
(2..)
.map(|n| format!("{base}-{n}"))
.find(|c| !taken(c))
.expect("an unbounded range has a free name")
}
fn unique_side(path: &str, taken: &dyn Fn(&str) -> bool) -> String {
let first = format!("{path}.restored");
if !taken(&first) {
return first;
}
(2..)
.map(|n| format!("{path}.restored.{n}"))
.find(|c| !taken(c))
.expect("an unbounded range has a free name")
}
pub fn plan_restore(
entry: &JournalEntry,
facts: &RestoreFacts,
name: Option<&str>,
path_exists: &dyn Fn(&str) -> bool,
) -> Result<RestorePlan, UndoRefusal> {
if entry.state != EntryState::Done {
return Err(UndoRefusal::NotRestorable {
reason: not_restorable_reason(entry.state).into(),
});
}
let c = candidates(entry);
if c.deleted.is_empty()
&& c.old_tips.is_empty()
&& c.config.is_empty()
&& c.stashes.is_empty()
&& c.files.is_empty()
{
return Err(UndoRefusal::NotRestorable {
reason: entry.nothing_reason(),
});
}
let nameable = c
.deleted
.iter()
.filter(|(_, _, k)| matches!(k, RestoreItemKind::Branch | RestoreItemKind::Tag))
.count()
+ c.old_tips.len();
if let Some(n) = name {
if nameable != 1 {
return Err(UndoRefusal::NotRestorable {
reason: "--name needs an operation that brings back exactly one branch or tag"
.into(),
});
}
if !valid_ref_name(n) {
return Err(UndoRefusal::NotRestorable {
reason: format!("{n} is not a valid branch or tag name"),
});
}
}
let ref_taken = |full: &str| facts.refs.contains_key(full);
let mut actions: Vec<RestoreAction> = Vec::new();
let mut already = false;
let mut returning: BTreeSet<String> = BTreeSet::new();
for (full, oid, kind) in &c.deleted {
let prefix = match kind {
RestoreItemKind::Tag => "refs/tags/",
RestoreItemKind::Branch => "refs/heads/",
_ => "",
};
let target = match (name, prefix.is_empty()) {
(Some(n), false) => format!("{prefix}{n}"),
_ => (*full).to_string(),
};
match facts.refs.get(&target) {
Some(now) if now == oid => already = true,
Some(_) => {
let short_name = target.strip_prefix(prefix).unwrap_or(&target).to_string();
let suggested = unique(&format!("{short_name}-restored"), &|n| {
ref_taken(&format!("{prefix}{n}"))
});
return Err(UndoRefusal::NameTaken {
name: short_name,
suggested,
});
}
None => {
if let (Some(orig), true) = (full.strip_prefix("refs/heads/"), target == *full) {
returning.insert(orig.to_string());
}
actions.push(RestoreAction::CreateRef {
name: target,
oid: *oid,
kind: *kind,
});
}
}
}
for (full, oid) in &c.old_tips {
let branch = full.strip_prefix("refs/heads/").unwrap_or(full);
let target = match name {
Some(n) => format!("refs/heads/{n}"),
None => {
let base = format!("restore/{branch}-{}", short(*oid));
format!(
"refs/heads/{}",
unique(&base, &|n| ref_taken(&format!("refs/heads/{n}")))
)
}
};
match facts.refs.get(&target) {
Some(now) if now == oid => already = true,
Some(_) => {
let n = target.strip_prefix("refs/heads/").unwrap_or(&target);
return Err(UndoRefusal::NameTaken {
name: n.to_string(),
suggested: unique(&format!("{n}-restored"), &|c| {
ref_taken(&format!("refs/heads/{c}"))
}),
});
}
None => actions.push(RestoreAction::CreateRef {
name: target,
oid: *oid,
kind: RestoreItemKind::OldTip,
}),
}
}
for s in &c.symrefs {
let (Some(old), true) = (&s.old, !facts.symrefs.contains_key(&s.name)) else {
continue;
};
let target_back = facts.refs.contains_key(old)
|| actions
.iter()
.any(|a| matches!(a, RestoreAction::CreateRef { name, .. } if name == old));
if target_back {
actions.push(RestoreAction::SetSymref {
name: s.name.clone(),
target: old.clone(),
});
}
}
for ch in &c.config {
let now = facts.config.get(&ch.section).cloned().unwrap_or_default();
if sorted(&now) == sorted(&ch.old) {
already = true;
continue;
}
if let Some(branch) = ch.section.strip_prefix("branch.") {
let exists = facts.refs.contains_key(&format!("refs/heads/{branch}"));
if !exists && !returning.contains(branch) {
continue;
}
}
if sorted(&now) != sorted(&ch.new) {
return Err(UndoRefusal::ConfigChanged {
section: ch.section.clone(),
});
}
actions.push(RestoreAction::SetConfig {
section: ch.section.clone(),
to: ch.old.clone(),
from: now,
});
}
for r in &c.stashes {
if facts.stashes.contains(&r.commit) {
already = true;
} else {
actions.push(RestoreAction::StashStore {
commit: r.commit,
message: r.message.clone(),
});
}
}
let history_moving = entry.effect == super::Effect::Files;
let mut in_place: Vec<String> = Vec::new();
let mut snapshot = None;
let mut dests: BTreeSet<String> = BTreeSet::new();
for f in &c.files {
let now = facts.files.iter().find(|h| h.path == f.path);
let now_wt = now.and_then(|h| h.worktree);
if is_rule_file(f.path) {
actions.push(RestoreAction::ReAddLines {
path: f.path.to_string(),
before: f.kept,
after: f.after_worktree,
});
continue;
}
let Some(kept) = f.kept else { continue };
if now_wt == Some(kept) {
already = true;
continue;
}
let as_left = {
let (a_wt, a_ix) = f
.after
.as_ref()
.map(|a| (a.worktree, a.index))
.unwrap_or((None, None));
let (n_wt, n_ix) = now.map(|n| (n.worktree, n.index)).unwrap_or((None, None));
(a_wt, a_ix) == (n_wt, n_ix)
};
if as_left && !history_moving {
in_place.push(f.path.to_string());
snapshot = entry.files.as_ref().map(|p| p.before.tree);
} else {
let dest = unique_side(f.path, &|d| path_exists(d) || dests.contains(d));
dests.insert(dest.clone());
actions.push(RestoreAction::SideWrite {
path: f.path.to_string(),
dest,
file: kept,
});
}
}
if let (false, Some(snapshot)) = (in_place.is_empty(), snapshot) {
actions.push(RestoreAction::RestorePaths {
paths: in_place,
snapshot,
});
}
if actions.is_empty() {
return Err(if already {
UndoRefusal::AlreadyRestored
} else {
UndoRefusal::NotRestorable {
reason: entry.nothing_reason(),
}
});
}
Ok(RestorePlan {
entry: entry.summary(),
label: format!("Restore \"{}\"", entry.label),
actions,
})
}