Push a new stash from the dirty working copy. Returns `Ok(None)` when the working copy is clean (never an error).
(&mut self, options: StashPushOptions)
| 234 | /// Push a new stash from the dirty working copy. Returns `Ok(None)` |
| 235 | /// when the working copy is clean (never an error). |
| 236 | pub fn stash_push(&mut self, options: StashPushOptions) -> Result<Option<StashEntry>, String> { |
| 237 | use crate::status::StatusOptions; |
| 238 | let working_copy = self.require_working_copy_id().map_err(|e| e.to_string())?; |
| 239 | let status = self |
| 240 | .status(working_copy, StatusOptions::default()) |
| 241 | .map_err(|e| e.to_string())?; |
| 242 | if status.is_clean() && !(options.include_untracked && status.untracked().next().is_some()) |
| 243 | { |
| 244 | return Ok(None); |
| 245 | } |
| 246 | |
| 247 | let source_view = self |
| 248 | .desired_view_name(working_copy) |
| 249 | .map_err(|e| e.to_string())?; |
| 250 | let stash_message = options |
| 251 | .message |
| 252 | .unwrap_or_else(|| DEFAULT_STASH_MESSAGE.to_string()); |
| 253 | |
| 254 | // Stash view name with embedded metadata (verbatim from the CLI). |
| 255 | let timestamp = Utc::now().timestamp_millis(); |
| 256 | let safe_source = source_view.replace('/', "-"); |
| 257 | let safe_message = stash_message |
| 258 | .chars() |
| 259 | .take(30) |
| 260 | .filter(|c| c.is_alphanumeric() || *c == '-' || *c == '_' || *c == ' ') |
| 261 | .collect::<String>() |
| 262 | .replace(' ', "-"); |
| 263 | let stash_name = format!( |
| 264 | "{}{}_{}_{}", |
| 265 | STASH_PREFIX, safe_source, timestamp, safe_message |
| 266 | ); |
| 267 | |
| 268 | self.create_view(&stash_name).map_err(|e| e.to_string())?; |
| 269 | |
| 270 | // Save the exact bytes of every dirty file to the sidecar dir — |
| 271 | // a pure working-copy snapshot, no graph interaction. |
| 272 | let stash_dir = self.dot_dir().join("stashes").join(&stash_name); |
| 273 | std::fs::create_dir_all(&stash_dir) |
| 274 | .map_err(|e| format!("failed to create stash dir: {e}"))?; |
| 275 | |
| 276 | let deleted = status |
| 277 | .deleted() |
| 278 | .map(|entry| { |
| 279 | let path = entry.path().to_string_lossy().to_string(); |
| 280 | let content = self |
| 281 | .get_file_content_on_view(entry.path(), &source_view) |
| 282 | .map_err(|e| format!("cannot read deleted file {path}: {e}"))? |
| 283 | .ok_or_else(|| format!("no recorded content for deleted file {path}"))?; |
| 284 | Ok(StashedDeletion { |
| 285 | path, |
| 286 | recorded_hash: Hash::of(&content).to_base32(), |
| 287 | }) |
| 288 | }) |
| 289 | .collect::<Result<Vec<_>, String>>()?; |
| 290 | let mut stashed_paths: Vec<String> = Vec::new(); |
| 291 | let snapshot_path = |p: String| -> Result<(), String> { |
| 292 | let abs = stash_entry_path(self.root(), &p)?; |
| 293 | let destination = stash_entry_path(&stash_dir, &p)?; |