| 98286406 Andy McCall ↶ |
1 |
|
| 2 |
|
| 3 |
use super::*; |
| 4 |
|
| 5 |
#[derive(Debug, Default, Clone, PartialEq, Eq)] |
| 6 |
pub struct CheckoutReport { |
| 7 |
pub written: usize, |
| 8 |
pub removed: usize, |
| 9 |
pub conflicted: bool, |
| 10 |
} |
| 11 |
|
| 12 |
impl Repo { |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
pub fn checkout(&self, heads: &[ChangeHash], discard: bool, command: &str) -> Result<CheckoutReport> { |
| 17 |
let snap = self.snapshot(command)?; |
| 18 |
if !discard && (!snap.wc.tree_ops.is_empty() || !snap.wc.edits.is_empty()) { |
| 19 |
return fail("the working copy has changes; commit them first or pass --discard"); |
| 20 |
} |
| 21 |
let target_state = self.load_state(heads)?; |
| 22 |
let heads = target_state.maximal(heads); |
| 23 |
let target = target_state.materialize(); |
| 24 |
let current = self.walk_working_copy()?; |
| 25 |
let mut report = CheckoutReport::default(); |
| 26 |
|
| 27 |
|
| 28 |
let mut dirs_to_prune: BTreeSet<PathBuf> = BTreeSet::new(); |
| 29 |
for path in current.keys() { |
| 30 |
if !target.files.contains_key(path) { |
| 31 |
let full = self.safe_path(path)?; |
| 32 |
fs::remove_file(&full)?; |
| 33 |
report.removed += 1; |
| 34 |
if let Some(parent) = full.parent() { |
| 35 |
dirs_to_prune.insert(parent.to_path_buf()); |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
for (path, f) in &target.files { |
| 41 |
match &f.content { |
| 42 |
FileContent::Dir => continue, |
| 43 |
FileContent::Text { bytes, .. } => { |
| 44 |
let same = current.get(path).is_some_and(|c| c.kind == NodeKind::File && &c.content == bytes && c.exec == f.mode.exec); |
| 45 |
if !same { |
| 46 |
self.write_file(path, bytes, f.mode.exec)?; |
| 47 |
report.written += 1; |
| 48 |
} |
| 49 |
} |
| 50 |
FileContent::Opaque { blobs } => { |
| 51 |
let Some(blob) = blobs.first() else { continue }; |
| 52 |
let bytes = self.read_blob(blob)?; |
| 53 |
if f.kind == NodeKind::Symlink { |
| 54 |
let same = current.get(path).is_some_and(|c| c.kind == NodeKind::Symlink && c.content == bytes); |
| 55 |
if !same { |
| 56 |
self.write_symlink(path, &bytes)?; |
| 57 |
report.written += 1; |
| 58 |
} |
| 59 |
} else { |
| 60 |
let same = current.get(path).is_some_and(|c| c.kind == NodeKind::File && c.content == bytes && c.exec == f.mode.exec); |
| 61 |
if !same { |
| 62 |
self.write_file(path, &bytes, f.mode.exec)?; |
| 63 |
report.written += 1; |
| 64 |
} |
| 65 |
} |
| 66 |
if blobs.len() > 1 { |
| 67 |
for (i, b) in blobs.iter().enumerate().skip(1) { |
| 68 |
self.write_file(&format!("{path}.oxy-conflict-{}", i + 1), &self.read_blob(b)?, f.mode.exec)?; |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
for d in dirs_to_prune.iter().rev() { |
| 76 |
let mut d = d.clone(); |
| 77 |
while d != self.root && fs::read_dir(&d).map(|mut i| i.next().is_none()).unwrap_or(false) { |
| 78 |
fs::remove_dir(&d)?; |
| 79 |
let Some(p) = d.parent() else { break }; |
| 80 |
d = p.to_path_buf(); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
let (op_ref, op) = self.head_op()?; |
| 85 |
let wc = self.new_working_copy(heads, ""); |
| 86 |
let wc_hash = self.store.put_change(&wc)?; |
| 87 |
let mut view = op.view.clone(); |
| 88 |
view.workspaces.insert(self.workspace, wc_hash); |
| 89 |
self.record_op(&[op_ref], command, view)?; |
| 90 |
report.conflicted = target.is_conflicted(); |
| 135212de Andy McCall ↶ |
91 |
self.write_manifest(report.conflicted, false)?; |
| 98286406 Andy McCall ↶ |
92 |
Ok(report) |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
fn safe_path(&self, rel: &str) -> Result<PathBuf> { |
| 98 |
let mut full = self.root.clone(); |
| 99 |
let comps: Vec<&str> = rel.split('/').collect(); |
| 100 |
for (i, comp) in comps.iter().enumerate() { |
| 101 |
if comp.is_empty() || *comp == "." || *comp == ".." || comp.contains('\\') { |
| 102 |
return fail(format!("refusing unsafe path {rel:?}")); |
| 103 |
} |
| 104 |
if i == 0 && *comp == OXY_DIR { |
| 105 |
return fail(format!("refusing to write inside {OXY_DIR}: {rel:?}")); |
| 106 |
} |
| 107 |
if i + 1 < comps.len() { |
| 108 |
full.push(comp); |
| 109 |
if let Ok(m) = fs::symlink_metadata(&full) { |
| 110 |
if m.file_type().is_symlink() { |
| 111 |
return fail(format!("refusing to write through symlink {}", full.display())); |
| 112 |
} |
| 113 |
} |
| 114 |
} else { |
| 115 |
full.push(comp); |
| 116 |
} |
| 117 |
} |
| 118 |
Ok(full) |
| 119 |
} |
| 120 |
|
| 121 |
fn write_file(&self, rel: &str, bytes: &[u8], exec: bool) -> Result<()> { |
| 122 |
let full = self.safe_path(rel)?; |
| 123 |
if let Some(parent) = full.parent() { |
| 124 |
fs::create_dir_all(parent)?; |
| 125 |
} |
| 126 |
if fs::symlink_metadata(&full).is_ok_and(|m| m.file_type().is_symlink() || m.is_dir()) { |
| 127 |
remove_any(&full)?; |
| 128 |
} |
| 129 |
fs::write(&full, bytes)?; |
| 130 |
set_executable(&full, exec)?; |
| 131 |
Ok(()) |
| 132 |
} |
| 133 |
|
| 134 |
fn write_symlink(&self, rel: &str, target: &[u8]) -> Result<()> { |
| 135 |
let full = self.safe_path(rel)?; |
| 136 |
if let Some(parent) = full.parent() { |
| 137 |
fs::create_dir_all(parent)?; |
| 138 |
} |
| 139 |
if fs::symlink_metadata(&full).is_ok() { |
| 140 |
remove_any(&full)?; |
| 141 |
} |
| 142 |
#[cfg(unix)] |
| 143 |
{ |
| 144 |
std::os::unix::fs::symlink(String::from_utf8_lossy(target).as_ref(), &full)?; |
| 145 |
Ok(()) |
| 146 |
} |
| 147 |
#[cfg(not(unix))] |
| 148 |
{ |
| 149 |
|
| 150 |
fs::write(&full, target)?; |
| 151 |
Ok(()) |
| 152 |
} |
| 153 |
} |
| 154 |
} |