pub struct StashOptions {
pub include_untracked: bool,
pub include_all: bool,
pub keep_index: bool,
pub patch: bool,
pub staged_only: bool,
pub paths: Vec<PathBuf>,
}
Expand description
Options for creating stashes
Fields§
§include_untracked: bool
Include untracked files in the stash
include_all: bool
Include ignored files in the stash
keep_index: bool
Keep staged changes in the index
patch: bool
Create a patch-mode stash (interactive)
staged_only: bool
Only stash staged changes
paths: Vec<PathBuf>
Paths to specifically stash
Implementations§
Source§impl StashOptions
impl StashOptions
Sourcepub fn new() -> Self
pub fn new() -> Self
Create new default stash options
Examples found in repository?
examples/stash_operations.rs (line 122)
17fn main() -> Result<()> {
18 println!("Rustic Git - Stash Operations Example\n");
19
20 // Use a temporary directory for this example
21 let repo_path = env::temp_dir().join("rustic_git_stash_example");
22
23 // Clean up any previous run
24 if repo_path.exists() {
25 fs::remove_dir_all(&repo_path).expect("Failed to clean up previous example");
26 }
27
28 println!("Initializing repository at: {}", repo_path.display());
29
30 // Initialize repository and configure user
31 let repo = Repository::init(&repo_path, false)?;
32 repo.config()
33 .set_user("Stash Demo User", "stash@example.com")?;
34
35 // Create initial commit to have a base
36 println!("\nCreating initial commit...");
37 fs::write(
38 repo_path.join("README.md"),
39 "# Stash Demo Project\n\nDemonstrating Git stash operations.\n",
40 )?;
41 repo.add(&["README.md"])?;
42 let initial_commit = repo.commit("Initial commit: Add README")?;
43 println!("Created initial commit: {}", initial_commit.short());
44
45 // Create some work to stash
46 println!("\n=== Creating Work to Stash ===");
47
48 // Create tracked file modifications
49 fs::write(
50 repo_path.join("README.md"),
51 "# Stash Demo Project\n\nDemonstrating Git stash operations.\n\nAdded some new content!\n",
52 )?;
53
54 // Create new tracked files
55 fs::create_dir_all(repo_path.join("src"))?;
56 fs::write(
57 repo_path.join("src/main.rs"),
58 "fn main() {\n println!(\"Hello, stash!\");\n}\n",
59 )?;
60
61 // Create untracked files
62 fs::write(
63 repo_path.join("untracked.txt"),
64 "This file is not tracked by git\n",
65 )?;
66 fs::write(repo_path.join("temp.log"), "Temporary log file\n")?;
67
68 // Stage some changes
69 repo.add(&["src/main.rs"])?;
70
71 println!("Created various types of changes:");
72 println!(" - Modified tracked file (README.md)");
73 println!(" - Added new file and staged it (src/main.rs)");
74 println!(" - Created untracked files (untracked.txt, temp.log)");
75
76 // Check repository status before stashing
77 let status = repo.status()?;
78 println!("\nRepository status before stashing:");
79 println!(" Staged files: {}", status.staged_files().count());
80 println!(" Unstaged files: {}", status.unstaged_files().count());
81 println!(" Untracked files: {}", status.untracked_entries().count());
82
83 // Demonstrate stash creation
84 println!("\n=== Creating Stashes ===");
85
86 // 1. Simple stash save
87 println!("\n1. Creating simple stash:");
88 let simple_stash = repo.stash_save("WIP: working on main function")?;
89 println!("Created stash: {}", simple_stash);
90 println!(" Index: {}", simple_stash.index);
91 println!(" Branch: {}", simple_stash.branch);
92 println!(" Hash: {}", simple_stash.hash.short());
93
94 // Check status after stash
95 let status_after_stash = repo.status()?;
96 println!("\nStatus after simple stash:");
97 println!(
98 " Staged files: {}",
99 status_after_stash.staged_files().count()
100 );
101 println!(
102 " Unstaged files: {}",
103 status_after_stash.unstaged_files().count()
104 );
105 println!(
106 " Untracked files: {}",
107 status_after_stash.untracked_entries().count()
108 );
109
110 // 2. Make more changes and create stash with untracked files
111 println!("\n2. Creating stash with untracked files:");
112
113 // Modify file again
114 fs::write(
115 repo_path.join("README.md"),
116 "# Stash Demo Project\n\nDemonstrating Git stash operations.\n\nSecond round of changes!\n",
117 )?;
118
119 // Create more untracked files
120 fs::write(repo_path.join("config.json"), "{\"debug\": true}\n")?;
121
122 let untracked_options = StashOptions::new().with_untracked().with_keep_index();
123 let untracked_stash = repo.stash_push(
124 "WIP: config changes with untracked files",
125 untracked_options,
126 )?;
127 println!("Created stash with untracked files: {}", untracked_stash);
128
129 // 3. Create stash with specific paths
130 println!("\n3. Creating stash with specific paths:");
131
132 // Make changes to multiple files and add them to git
133 fs::write(repo_path.join("file1.txt"), "Content for file 1\n")?;
134 fs::write(repo_path.join("file2.txt"), "Content for file 2\n")?;
135 fs::write(repo_path.join("file3.txt"), "Content for file 3\n")?;
136
137 // Add all files so they're tracked
138 repo.add(&["file1.txt", "file2.txt", "file3.txt"])?;
139
140 // Now modify them so there are changes to stash
141 fs::write(repo_path.join("file1.txt"), "Modified content for file 1\n")?;
142 fs::write(repo_path.join("file2.txt"), "Modified content for file 2\n")?;
143 fs::write(repo_path.join("file3.txt"), "Modified content for file 3\n")?;
144
145 let path_options = StashOptions::new().with_paths(vec!["file1.txt".into(), "file2.txt".into()]);
146 let path_stash = repo.stash_push("WIP: specific files only", path_options)?;
147 println!("Created stash with specific paths: {}", path_stash);
148
149 // Demonstrate stash listing and filtering
150 println!("\n=== Stash Listing and Filtering ===");
151
152 let stashes = repo.stash_list()?;
153 println!("\nAll stashes ({} total):", stashes.len());
154 for stash in stashes.iter() {
155 println!(
156 " [{}] {} -> {}",
157 stash.index,
158 stash.message,
159 stash.hash.short()
160 );
161 println!(
162 " Branch: {} | Created: {}",
163 stash.branch,
164 stash.timestamp.format("%Y-%m-%d %H:%M:%S")
165 );
166 }
167
168 // Test filtering
169 println!("\nFiltering examples:");
170
171 // Find stashes containing specific text
172 let wip_stashes: Vec<_> = stashes.find_containing("WIP").collect();
173 println!("Stashes containing 'WIP': {} found", wip_stashes.len());
174 for stash in &wip_stashes {
175 println!(" - {}", stash.message);
176 }
177
178 let config_stashes: Vec<_> = stashes.find_containing("config").collect();
179 println!(
180 "Stashes containing 'config': {} found",
181 config_stashes.len()
182 );
183
184 // Get latest stash
185 if let Some(latest) = stashes.latest() {
186 println!("Latest stash: {}", latest.message);
187 }
188
189 // Get specific stash by index
190 if let Some(second_stash) = stashes.get(1) {
191 println!("Second stash: {}", second_stash.message);
192 }
193
194 // Demonstrate stash content viewing
195 println!("\n=== Viewing Stash Contents ===");
196
197 println!("\nShowing contents of latest stash:");
198 let stash_contents = repo.stash_show(0)?;
199 println!("{}", stash_contents);
200
201 // Demonstrate stash application
202 println!("\n=== Applying and Popping Stashes ===");
203
204 println!("\n1. Testing stash apply (keeps stash in list):");
205 let stashes_before_apply = repo.stash_list()?;
206 println!("Stashes before apply: {}", stashes_before_apply.len());
207
208 // Apply the latest stash
209 repo.stash_apply(0, StashApplyOptions::new())?;
210 println!("Applied stash@{{0}}");
211
212 let stashes_after_apply = repo.stash_list()?;
213 println!("Stashes after apply: {}", stashes_after_apply.len());
214
215 // Check what was restored
216 let status_after_apply = repo.status()?;
217 println!("Status after apply:");
218 println!(
219 " Staged files: {}",
220 status_after_apply.staged_files().count()
221 );
222 println!(
223 " Unstaged files: {}",
224 status_after_apply.unstaged_files().count()
225 );
226 println!(
227 " Untracked files: {}",
228 status_after_apply.untracked_entries().count()
229 );
230
231 println!("\n2. Testing stash pop (removes stash from list):");
232
233 // First, stash current changes again to have something to pop
234 repo.stash_save("Temporary stash for pop test")?;
235
236 let stashes_before_pop = repo.stash_list()?;
237 println!("Stashes before pop: {}", stashes_before_pop.len());
238
239 // Pop the latest stash
240 repo.stash_pop(0, StashApplyOptions::new().with_quiet())?;
241 println!("Popped stash@{{0}}");
242
243 let stashes_after_pop = repo.stash_list()?;
244 println!("Stashes after pop: {}", stashes_after_pop.len());
245
246 // Demonstrate advanced apply options
247 println!("\n3. Testing apply with index restoration:");
248
249 // Create a stash with staged changes
250 fs::write(repo_path.join("staged_file.txt"), "This will be staged\n")?;
251 repo.add(&["staged_file.txt"])?;
252
253 fs::write(
254 repo_path.join("unstaged_file.txt"),
255 "This will be unstaged\n",
256 )?;
257
258 repo.stash_save("Stash with staged and unstaged changes")?;
259
260 // Apply with index restoration
261 let apply_options = StashApplyOptions::new().with_index();
262 repo.stash_apply(0, apply_options)?;
263 println!("Applied stash with index restoration");
264
265 let final_status = repo.status()?;
266 println!("Final status after index restoration:");
267 println!(" Staged files: {}", final_status.staged_files().count());
268 println!(
269 " Unstaged files: {}",
270 final_status.unstaged_files().count()
271 );
272
273 // Demonstrate stash management
274 println!("\n=== Stash Management ===");
275
276 // Create a few test stashes
277 for i in 1..=3 {
278 fs::write(
279 repo_path.join(format!("test{}.txt", i)),
280 format!("Test content {}\n", i),
281 )?;
282 repo.stash_save(&format!("Test stash {}", i))?;
283 }
284
285 let management_stashes = repo.stash_list()?;
286 println!(
287 "\nCreated {} test stashes for management demo",
288 management_stashes.len()
289 );
290
291 // Drop a specific stash
292 println!("\n1. Dropping middle stash:");
293 println!("Before drop: {} stashes", management_stashes.len());
294
295 repo.stash_drop(1)?; // Drop second stash (index 1)
296 println!("Dropped stash@{{1}}");
297
298 let after_drop = repo.stash_list()?;
299 println!("After drop: {} stashes", after_drop.len());
300
301 // Show remaining stashes
302 println!("Remaining stashes:");
303 for stash in after_drop.iter() {
304 println!(" [{}] {}", stash.index, stash.message);
305 }
306
307 // Clear all stashes
308 println!("\n2. Clearing all stashes:");
309 repo.stash_clear()?;
310 println!("Cleared all stashes");
311
312 let final_stashes = repo.stash_list()?;
313 println!("Stashes after clear: {}", final_stashes.len());
314
315 // Demonstrate error handling
316 println!("\n=== Error Handling ===");
317
318 println!("\n1. Testing operations on empty stash list:");
319
320 // Try to apply non-existent stash
321 match repo.stash_apply(0, StashApplyOptions::new()) {
322 Ok(_) => println!("ERROR: Should have failed to apply non-existent stash"),
323 Err(e) => println!("Expected error applying non-existent stash: {}", e),
324 }
325
326 // Try to show non-existent stash
327 match repo.stash_show(0) {
328 Ok(_) => println!("ERROR: Should have failed to show non-existent stash"),
329 Err(e) => println!("Expected error showing non-existent stash: {}", e),
330 }
331
332 // Try to drop non-existent stash
333 match repo.stash_drop(0) {
334 Ok(_) => println!("ERROR: Should have failed to drop non-existent stash"),
335 Err(e) => println!("Expected error dropping non-existent stash: {}", e),
336 }
337
338 // Summary
339 println!("\n=== Summary ===");
340 println!("\nStash operations demonstrated:");
341 println!(" ✓ Basic stash save and push with options");
342 println!(" ✓ Stash with untracked files and keep-index");
343 println!(" ✓ Stash specific paths only");
344 println!(" ✓ Comprehensive stash listing and filtering");
345 println!(" ✓ Stash content viewing");
346 println!(" ✓ Apply vs pop operations");
347 println!(" ✓ Index restoration during apply");
348 println!(" ✓ Stash dropping and clearing");
349 println!(" ✓ Error handling for edge cases");
350
351 println!("\nStash options demonstrated:");
352 println!(" ✓ with_untracked() - Include untracked files");
353 println!(" ✓ with_keep_index() - Keep staged changes");
354 println!(" ✓ with_paths() - Stash specific files only");
355 println!(" ✓ with_index() - Restore staged state on apply");
356 println!(" ✓ with_quiet() - Suppress output messages");
357
358 println!("\nStash filtering demonstrated:");
359 println!(" ✓ find_containing() - Search by message content");
360 println!(" ✓ latest() - Get most recent stash");
361 println!(" ✓ get() - Get stash by index");
362 println!(" ✓ for_branch() - Filter by branch name");
363
364 // Clean up
365 println!("\nCleaning up example repository...");
366 fs::remove_dir_all(&repo_path)?;
367 println!("Stash operations example completed successfully!");
368
369 Ok(())
370}
Sourcepub fn with_untracked(self) -> Self
pub fn with_untracked(self) -> Self
Include untracked files in the stash
Examples found in repository?
examples/stash_operations.rs (line 122)
17fn main() -> Result<()> {
18 println!("Rustic Git - Stash Operations Example\n");
19
20 // Use a temporary directory for this example
21 let repo_path = env::temp_dir().join("rustic_git_stash_example");
22
23 // Clean up any previous run
24 if repo_path.exists() {
25 fs::remove_dir_all(&repo_path).expect("Failed to clean up previous example");
26 }
27
28 println!("Initializing repository at: {}", repo_path.display());
29
30 // Initialize repository and configure user
31 let repo = Repository::init(&repo_path, false)?;
32 repo.config()
33 .set_user("Stash Demo User", "stash@example.com")?;
34
35 // Create initial commit to have a base
36 println!("\nCreating initial commit...");
37 fs::write(
38 repo_path.join("README.md"),
39 "# Stash Demo Project\n\nDemonstrating Git stash operations.\n",
40 )?;
41 repo.add(&["README.md"])?;
42 let initial_commit = repo.commit("Initial commit: Add README")?;
43 println!("Created initial commit: {}", initial_commit.short());
44
45 // Create some work to stash
46 println!("\n=== Creating Work to Stash ===");
47
48 // Create tracked file modifications
49 fs::write(
50 repo_path.join("README.md"),
51 "# Stash Demo Project\n\nDemonstrating Git stash operations.\n\nAdded some new content!\n",
52 )?;
53
54 // Create new tracked files
55 fs::create_dir_all(repo_path.join("src"))?;
56 fs::write(
57 repo_path.join("src/main.rs"),
58 "fn main() {\n println!(\"Hello, stash!\");\n}\n",
59 )?;
60
61 // Create untracked files
62 fs::write(
63 repo_path.join("untracked.txt"),
64 "This file is not tracked by git\n",
65 )?;
66 fs::write(repo_path.join("temp.log"), "Temporary log file\n")?;
67
68 // Stage some changes
69 repo.add(&["src/main.rs"])?;
70
71 println!("Created various types of changes:");
72 println!(" - Modified tracked file (README.md)");
73 println!(" - Added new file and staged it (src/main.rs)");
74 println!(" - Created untracked files (untracked.txt, temp.log)");
75
76 // Check repository status before stashing
77 let status = repo.status()?;
78 println!("\nRepository status before stashing:");
79 println!(" Staged files: {}", status.staged_files().count());
80 println!(" Unstaged files: {}", status.unstaged_files().count());
81 println!(" Untracked files: {}", status.untracked_entries().count());
82
83 // Demonstrate stash creation
84 println!("\n=== Creating Stashes ===");
85
86 // 1. Simple stash save
87 println!("\n1. Creating simple stash:");
88 let simple_stash = repo.stash_save("WIP: working on main function")?;
89 println!("Created stash: {}", simple_stash);
90 println!(" Index: {}", simple_stash.index);
91 println!(" Branch: {}", simple_stash.branch);
92 println!(" Hash: {}", simple_stash.hash.short());
93
94 // Check status after stash
95 let status_after_stash = repo.status()?;
96 println!("\nStatus after simple stash:");
97 println!(
98 " Staged files: {}",
99 status_after_stash.staged_files().count()
100 );
101 println!(
102 " Unstaged files: {}",
103 status_after_stash.unstaged_files().count()
104 );
105 println!(
106 " Untracked files: {}",
107 status_after_stash.untracked_entries().count()
108 );
109
110 // 2. Make more changes and create stash with untracked files
111 println!("\n2. Creating stash with untracked files:");
112
113 // Modify file again
114 fs::write(
115 repo_path.join("README.md"),
116 "# Stash Demo Project\n\nDemonstrating Git stash operations.\n\nSecond round of changes!\n",
117 )?;
118
119 // Create more untracked files
120 fs::write(repo_path.join("config.json"), "{\"debug\": true}\n")?;
121
122 let untracked_options = StashOptions::new().with_untracked().with_keep_index();
123 let untracked_stash = repo.stash_push(
124 "WIP: config changes with untracked files",
125 untracked_options,
126 )?;
127 println!("Created stash with untracked files: {}", untracked_stash);
128
129 // 3. Create stash with specific paths
130 println!("\n3. Creating stash with specific paths:");
131
132 // Make changes to multiple files and add them to git
133 fs::write(repo_path.join("file1.txt"), "Content for file 1\n")?;
134 fs::write(repo_path.join("file2.txt"), "Content for file 2\n")?;
135 fs::write(repo_path.join("file3.txt"), "Content for file 3\n")?;
136
137 // Add all files so they're tracked
138 repo.add(&["file1.txt", "file2.txt", "file3.txt"])?;
139
140 // Now modify them so there are changes to stash
141 fs::write(repo_path.join("file1.txt"), "Modified content for file 1\n")?;
142 fs::write(repo_path.join("file2.txt"), "Modified content for file 2\n")?;
143 fs::write(repo_path.join("file3.txt"), "Modified content for file 3\n")?;
144
145 let path_options = StashOptions::new().with_paths(vec!["file1.txt".into(), "file2.txt".into()]);
146 let path_stash = repo.stash_push("WIP: specific files only", path_options)?;
147 println!("Created stash with specific paths: {}", path_stash);
148
149 // Demonstrate stash listing and filtering
150 println!("\n=== Stash Listing and Filtering ===");
151
152 let stashes = repo.stash_list()?;
153 println!("\nAll stashes ({} total):", stashes.len());
154 for stash in stashes.iter() {
155 println!(
156 " [{}] {} -> {}",
157 stash.index,
158 stash.message,
159 stash.hash.short()
160 );
161 println!(
162 " Branch: {} | Created: {}",
163 stash.branch,
164 stash.timestamp.format("%Y-%m-%d %H:%M:%S")
165 );
166 }
167
168 // Test filtering
169 println!("\nFiltering examples:");
170
171 // Find stashes containing specific text
172 let wip_stashes: Vec<_> = stashes.find_containing("WIP").collect();
173 println!("Stashes containing 'WIP': {} found", wip_stashes.len());
174 for stash in &wip_stashes {
175 println!(" - {}", stash.message);
176 }
177
178 let config_stashes: Vec<_> = stashes.find_containing("config").collect();
179 println!(
180 "Stashes containing 'config': {} found",
181 config_stashes.len()
182 );
183
184 // Get latest stash
185 if let Some(latest) = stashes.latest() {
186 println!("Latest stash: {}", latest.message);
187 }
188
189 // Get specific stash by index
190 if let Some(second_stash) = stashes.get(1) {
191 println!("Second stash: {}", second_stash.message);
192 }
193
194 // Demonstrate stash content viewing
195 println!("\n=== Viewing Stash Contents ===");
196
197 println!("\nShowing contents of latest stash:");
198 let stash_contents = repo.stash_show(0)?;
199 println!("{}", stash_contents);
200
201 // Demonstrate stash application
202 println!("\n=== Applying and Popping Stashes ===");
203
204 println!("\n1. Testing stash apply (keeps stash in list):");
205 let stashes_before_apply = repo.stash_list()?;
206 println!("Stashes before apply: {}", stashes_before_apply.len());
207
208 // Apply the latest stash
209 repo.stash_apply(0, StashApplyOptions::new())?;
210 println!("Applied stash@{{0}}");
211
212 let stashes_after_apply = repo.stash_list()?;
213 println!("Stashes after apply: {}", stashes_after_apply.len());
214
215 // Check what was restored
216 let status_after_apply = repo.status()?;
217 println!("Status after apply:");
218 println!(
219 " Staged files: {}",
220 status_after_apply.staged_files().count()
221 );
222 println!(
223 " Unstaged files: {}",
224 status_after_apply.unstaged_files().count()
225 );
226 println!(
227 " Untracked files: {}",
228 status_after_apply.untracked_entries().count()
229 );
230
231 println!("\n2. Testing stash pop (removes stash from list):");
232
233 // First, stash current changes again to have something to pop
234 repo.stash_save("Temporary stash for pop test")?;
235
236 let stashes_before_pop = repo.stash_list()?;
237 println!("Stashes before pop: {}", stashes_before_pop.len());
238
239 // Pop the latest stash
240 repo.stash_pop(0, StashApplyOptions::new().with_quiet())?;
241 println!("Popped stash@{{0}}");
242
243 let stashes_after_pop = repo.stash_list()?;
244 println!("Stashes after pop: {}", stashes_after_pop.len());
245
246 // Demonstrate advanced apply options
247 println!("\n3. Testing apply with index restoration:");
248
249 // Create a stash with staged changes
250 fs::write(repo_path.join("staged_file.txt"), "This will be staged\n")?;
251 repo.add(&["staged_file.txt"])?;
252
253 fs::write(
254 repo_path.join("unstaged_file.txt"),
255 "This will be unstaged\n",
256 )?;
257
258 repo.stash_save("Stash with staged and unstaged changes")?;
259
260 // Apply with index restoration
261 let apply_options = StashApplyOptions::new().with_index();
262 repo.stash_apply(0, apply_options)?;
263 println!("Applied stash with index restoration");
264
265 let final_status = repo.status()?;
266 println!("Final status after index restoration:");
267 println!(" Staged files: {}", final_status.staged_files().count());
268 println!(
269 " Unstaged files: {}",
270 final_status.unstaged_files().count()
271 );
272
273 // Demonstrate stash management
274 println!("\n=== Stash Management ===");
275
276 // Create a few test stashes
277 for i in 1..=3 {
278 fs::write(
279 repo_path.join(format!("test{}.txt", i)),
280 format!("Test content {}\n", i),
281 )?;
282 repo.stash_save(&format!("Test stash {}", i))?;
283 }
284
285 let management_stashes = repo.stash_list()?;
286 println!(
287 "\nCreated {} test stashes for management demo",
288 management_stashes.len()
289 );
290
291 // Drop a specific stash
292 println!("\n1. Dropping middle stash:");
293 println!("Before drop: {} stashes", management_stashes.len());
294
295 repo.stash_drop(1)?; // Drop second stash (index 1)
296 println!("Dropped stash@{{1}}");
297
298 let after_drop = repo.stash_list()?;
299 println!("After drop: {} stashes", after_drop.len());
300
301 // Show remaining stashes
302 println!("Remaining stashes:");
303 for stash in after_drop.iter() {
304 println!(" [{}] {}", stash.index, stash.message);
305 }
306
307 // Clear all stashes
308 println!("\n2. Clearing all stashes:");
309 repo.stash_clear()?;
310 println!("Cleared all stashes");
311
312 let final_stashes = repo.stash_list()?;
313 println!("Stashes after clear: {}", final_stashes.len());
314
315 // Demonstrate error handling
316 println!("\n=== Error Handling ===");
317
318 println!("\n1. Testing operations on empty stash list:");
319
320 // Try to apply non-existent stash
321 match repo.stash_apply(0, StashApplyOptions::new()) {
322 Ok(_) => println!("ERROR: Should have failed to apply non-existent stash"),
323 Err(e) => println!("Expected error applying non-existent stash: {}", e),
324 }
325
326 // Try to show non-existent stash
327 match repo.stash_show(0) {
328 Ok(_) => println!("ERROR: Should have failed to show non-existent stash"),
329 Err(e) => println!("Expected error showing non-existent stash: {}", e),
330 }
331
332 // Try to drop non-existent stash
333 match repo.stash_drop(0) {
334 Ok(_) => println!("ERROR: Should have failed to drop non-existent stash"),
335 Err(e) => println!("Expected error dropping non-existent stash: {}", e),
336 }
337
338 // Summary
339 println!("\n=== Summary ===");
340 println!("\nStash operations demonstrated:");
341 println!(" ✓ Basic stash save and push with options");
342 println!(" ✓ Stash with untracked files and keep-index");
343 println!(" ✓ Stash specific paths only");
344 println!(" ✓ Comprehensive stash listing and filtering");
345 println!(" ✓ Stash content viewing");
346 println!(" ✓ Apply vs pop operations");
347 println!(" ✓ Index restoration during apply");
348 println!(" ✓ Stash dropping and clearing");
349 println!(" ✓ Error handling for edge cases");
350
351 println!("\nStash options demonstrated:");
352 println!(" ✓ with_untracked() - Include untracked files");
353 println!(" ✓ with_keep_index() - Keep staged changes");
354 println!(" ✓ with_paths() - Stash specific files only");
355 println!(" ✓ with_index() - Restore staged state on apply");
356 println!(" ✓ with_quiet() - Suppress output messages");
357
358 println!("\nStash filtering demonstrated:");
359 println!(" ✓ find_containing() - Search by message content");
360 println!(" ✓ latest() - Get most recent stash");
361 println!(" ✓ get() - Get stash by index");
362 println!(" ✓ for_branch() - Filter by branch name");
363
364 // Clean up
365 println!("\nCleaning up example repository...");
366 fs::remove_dir_all(&repo_path)?;
367 println!("Stash operations example completed successfully!");
368
369 Ok(())
370}
Sourcepub fn with_keep_index(self) -> Self
pub fn with_keep_index(self) -> Self
Keep staged changes in the index after stashing
Examples found in repository?
examples/stash_operations.rs (line 122)
17fn main() -> Result<()> {
18 println!("Rustic Git - Stash Operations Example\n");
19
20 // Use a temporary directory for this example
21 let repo_path = env::temp_dir().join("rustic_git_stash_example");
22
23 // Clean up any previous run
24 if repo_path.exists() {
25 fs::remove_dir_all(&repo_path).expect("Failed to clean up previous example");
26 }
27
28 println!("Initializing repository at: {}", repo_path.display());
29
30 // Initialize repository and configure user
31 let repo = Repository::init(&repo_path, false)?;
32 repo.config()
33 .set_user("Stash Demo User", "stash@example.com")?;
34
35 // Create initial commit to have a base
36 println!("\nCreating initial commit...");
37 fs::write(
38 repo_path.join("README.md"),
39 "# Stash Demo Project\n\nDemonstrating Git stash operations.\n",
40 )?;
41 repo.add(&["README.md"])?;
42 let initial_commit = repo.commit("Initial commit: Add README")?;
43 println!("Created initial commit: {}", initial_commit.short());
44
45 // Create some work to stash
46 println!("\n=== Creating Work to Stash ===");
47
48 // Create tracked file modifications
49 fs::write(
50 repo_path.join("README.md"),
51 "# Stash Demo Project\n\nDemonstrating Git stash operations.\n\nAdded some new content!\n",
52 )?;
53
54 // Create new tracked files
55 fs::create_dir_all(repo_path.join("src"))?;
56 fs::write(
57 repo_path.join("src/main.rs"),
58 "fn main() {\n println!(\"Hello, stash!\");\n}\n",
59 )?;
60
61 // Create untracked files
62 fs::write(
63 repo_path.join("untracked.txt"),
64 "This file is not tracked by git\n",
65 )?;
66 fs::write(repo_path.join("temp.log"), "Temporary log file\n")?;
67
68 // Stage some changes
69 repo.add(&["src/main.rs"])?;
70
71 println!("Created various types of changes:");
72 println!(" - Modified tracked file (README.md)");
73 println!(" - Added new file and staged it (src/main.rs)");
74 println!(" - Created untracked files (untracked.txt, temp.log)");
75
76 // Check repository status before stashing
77 let status = repo.status()?;
78 println!("\nRepository status before stashing:");
79 println!(" Staged files: {}", status.staged_files().count());
80 println!(" Unstaged files: {}", status.unstaged_files().count());
81 println!(" Untracked files: {}", status.untracked_entries().count());
82
83 // Demonstrate stash creation
84 println!("\n=== Creating Stashes ===");
85
86 // 1. Simple stash save
87 println!("\n1. Creating simple stash:");
88 let simple_stash = repo.stash_save("WIP: working on main function")?;
89 println!("Created stash: {}", simple_stash);
90 println!(" Index: {}", simple_stash.index);
91 println!(" Branch: {}", simple_stash.branch);
92 println!(" Hash: {}", simple_stash.hash.short());
93
94 // Check status after stash
95 let status_after_stash = repo.status()?;
96 println!("\nStatus after simple stash:");
97 println!(
98 " Staged files: {}",
99 status_after_stash.staged_files().count()
100 );
101 println!(
102 " Unstaged files: {}",
103 status_after_stash.unstaged_files().count()
104 );
105 println!(
106 " Untracked files: {}",
107 status_after_stash.untracked_entries().count()
108 );
109
110 // 2. Make more changes and create stash with untracked files
111 println!("\n2. Creating stash with untracked files:");
112
113 // Modify file again
114 fs::write(
115 repo_path.join("README.md"),
116 "# Stash Demo Project\n\nDemonstrating Git stash operations.\n\nSecond round of changes!\n",
117 )?;
118
119 // Create more untracked files
120 fs::write(repo_path.join("config.json"), "{\"debug\": true}\n")?;
121
122 let untracked_options = StashOptions::new().with_untracked().with_keep_index();
123 let untracked_stash = repo.stash_push(
124 "WIP: config changes with untracked files",
125 untracked_options,
126 )?;
127 println!("Created stash with untracked files: {}", untracked_stash);
128
129 // 3. Create stash with specific paths
130 println!("\n3. Creating stash with specific paths:");
131
132 // Make changes to multiple files and add them to git
133 fs::write(repo_path.join("file1.txt"), "Content for file 1\n")?;
134 fs::write(repo_path.join("file2.txt"), "Content for file 2\n")?;
135 fs::write(repo_path.join("file3.txt"), "Content for file 3\n")?;
136
137 // Add all files so they're tracked
138 repo.add(&["file1.txt", "file2.txt", "file3.txt"])?;
139
140 // Now modify them so there are changes to stash
141 fs::write(repo_path.join("file1.txt"), "Modified content for file 1\n")?;
142 fs::write(repo_path.join("file2.txt"), "Modified content for file 2\n")?;
143 fs::write(repo_path.join("file3.txt"), "Modified content for file 3\n")?;
144
145 let path_options = StashOptions::new().with_paths(vec!["file1.txt".into(), "file2.txt".into()]);
146 let path_stash = repo.stash_push("WIP: specific files only", path_options)?;
147 println!("Created stash with specific paths: {}", path_stash);
148
149 // Demonstrate stash listing and filtering
150 println!("\n=== Stash Listing and Filtering ===");
151
152 let stashes = repo.stash_list()?;
153 println!("\nAll stashes ({} total):", stashes.len());
154 for stash in stashes.iter() {
155 println!(
156 " [{}] {} -> {}",
157 stash.index,
158 stash.message,
159 stash.hash.short()
160 );
161 println!(
162 " Branch: {} | Created: {}",
163 stash.branch,
164 stash.timestamp.format("%Y-%m-%d %H:%M:%S")
165 );
166 }
167
168 // Test filtering
169 println!("\nFiltering examples:");
170
171 // Find stashes containing specific text
172 let wip_stashes: Vec<_> = stashes.find_containing("WIP").collect();
173 println!("Stashes containing 'WIP': {} found", wip_stashes.len());
174 for stash in &wip_stashes {
175 println!(" - {}", stash.message);
176 }
177
178 let config_stashes: Vec<_> = stashes.find_containing("config").collect();
179 println!(
180 "Stashes containing 'config': {} found",
181 config_stashes.len()
182 );
183
184 // Get latest stash
185 if let Some(latest) = stashes.latest() {
186 println!("Latest stash: {}", latest.message);
187 }
188
189 // Get specific stash by index
190 if let Some(second_stash) = stashes.get(1) {
191 println!("Second stash: {}", second_stash.message);
192 }
193
194 // Demonstrate stash content viewing
195 println!("\n=== Viewing Stash Contents ===");
196
197 println!("\nShowing contents of latest stash:");
198 let stash_contents = repo.stash_show(0)?;
199 println!("{}", stash_contents);
200
201 // Demonstrate stash application
202 println!("\n=== Applying and Popping Stashes ===");
203
204 println!("\n1. Testing stash apply (keeps stash in list):");
205 let stashes_before_apply = repo.stash_list()?;
206 println!("Stashes before apply: {}", stashes_before_apply.len());
207
208 // Apply the latest stash
209 repo.stash_apply(0, StashApplyOptions::new())?;
210 println!("Applied stash@{{0}}");
211
212 let stashes_after_apply = repo.stash_list()?;
213 println!("Stashes after apply: {}", stashes_after_apply.len());
214
215 // Check what was restored
216 let status_after_apply = repo.status()?;
217 println!("Status after apply:");
218 println!(
219 " Staged files: {}",
220 status_after_apply.staged_files().count()
221 );
222 println!(
223 " Unstaged files: {}",
224 status_after_apply.unstaged_files().count()
225 );
226 println!(
227 " Untracked files: {}",
228 status_after_apply.untracked_entries().count()
229 );
230
231 println!("\n2. Testing stash pop (removes stash from list):");
232
233 // First, stash current changes again to have something to pop
234 repo.stash_save("Temporary stash for pop test")?;
235
236 let stashes_before_pop = repo.stash_list()?;
237 println!("Stashes before pop: {}", stashes_before_pop.len());
238
239 // Pop the latest stash
240 repo.stash_pop(0, StashApplyOptions::new().with_quiet())?;
241 println!("Popped stash@{{0}}");
242
243 let stashes_after_pop = repo.stash_list()?;
244 println!("Stashes after pop: {}", stashes_after_pop.len());
245
246 // Demonstrate advanced apply options
247 println!("\n3. Testing apply with index restoration:");
248
249 // Create a stash with staged changes
250 fs::write(repo_path.join("staged_file.txt"), "This will be staged\n")?;
251 repo.add(&["staged_file.txt"])?;
252
253 fs::write(
254 repo_path.join("unstaged_file.txt"),
255 "This will be unstaged\n",
256 )?;
257
258 repo.stash_save("Stash with staged and unstaged changes")?;
259
260 // Apply with index restoration
261 let apply_options = StashApplyOptions::new().with_index();
262 repo.stash_apply(0, apply_options)?;
263 println!("Applied stash with index restoration");
264
265 let final_status = repo.status()?;
266 println!("Final status after index restoration:");
267 println!(" Staged files: {}", final_status.staged_files().count());
268 println!(
269 " Unstaged files: {}",
270 final_status.unstaged_files().count()
271 );
272
273 // Demonstrate stash management
274 println!("\n=== Stash Management ===");
275
276 // Create a few test stashes
277 for i in 1..=3 {
278 fs::write(
279 repo_path.join(format!("test{}.txt", i)),
280 format!("Test content {}\n", i),
281 )?;
282 repo.stash_save(&format!("Test stash {}", i))?;
283 }
284
285 let management_stashes = repo.stash_list()?;
286 println!(
287 "\nCreated {} test stashes for management demo",
288 management_stashes.len()
289 );
290
291 // Drop a specific stash
292 println!("\n1. Dropping middle stash:");
293 println!("Before drop: {} stashes", management_stashes.len());
294
295 repo.stash_drop(1)?; // Drop second stash (index 1)
296 println!("Dropped stash@{{1}}");
297
298 let after_drop = repo.stash_list()?;
299 println!("After drop: {} stashes", after_drop.len());
300
301 // Show remaining stashes
302 println!("Remaining stashes:");
303 for stash in after_drop.iter() {
304 println!(" [{}] {}", stash.index, stash.message);
305 }
306
307 // Clear all stashes
308 println!("\n2. Clearing all stashes:");
309 repo.stash_clear()?;
310 println!("Cleared all stashes");
311
312 let final_stashes = repo.stash_list()?;
313 println!("Stashes after clear: {}", final_stashes.len());
314
315 // Demonstrate error handling
316 println!("\n=== Error Handling ===");
317
318 println!("\n1. Testing operations on empty stash list:");
319
320 // Try to apply non-existent stash
321 match repo.stash_apply(0, StashApplyOptions::new()) {
322 Ok(_) => println!("ERROR: Should have failed to apply non-existent stash"),
323 Err(e) => println!("Expected error applying non-existent stash: {}", e),
324 }
325
326 // Try to show non-existent stash
327 match repo.stash_show(0) {
328 Ok(_) => println!("ERROR: Should have failed to show non-existent stash"),
329 Err(e) => println!("Expected error showing non-existent stash: {}", e),
330 }
331
332 // Try to drop non-existent stash
333 match repo.stash_drop(0) {
334 Ok(_) => println!("ERROR: Should have failed to drop non-existent stash"),
335 Err(e) => println!("Expected error dropping non-existent stash: {}", e),
336 }
337
338 // Summary
339 println!("\n=== Summary ===");
340 println!("\nStash operations demonstrated:");
341 println!(" ✓ Basic stash save and push with options");
342 println!(" ✓ Stash with untracked files and keep-index");
343 println!(" ✓ Stash specific paths only");
344 println!(" ✓ Comprehensive stash listing and filtering");
345 println!(" ✓ Stash content viewing");
346 println!(" ✓ Apply vs pop operations");
347 println!(" ✓ Index restoration during apply");
348 println!(" ✓ Stash dropping and clearing");
349 println!(" ✓ Error handling for edge cases");
350
351 println!("\nStash options demonstrated:");
352 println!(" ✓ with_untracked() - Include untracked files");
353 println!(" ✓ with_keep_index() - Keep staged changes");
354 println!(" ✓ with_paths() - Stash specific files only");
355 println!(" ✓ with_index() - Restore staged state on apply");
356 println!(" ✓ with_quiet() - Suppress output messages");
357
358 println!("\nStash filtering demonstrated:");
359 println!(" ✓ find_containing() - Search by message content");
360 println!(" ✓ latest() - Get most recent stash");
361 println!(" ✓ get() - Get stash by index");
362 println!(" ✓ for_branch() - Filter by branch name");
363
364 // Clean up
365 println!("\nCleaning up example repository...");
366 fs::remove_dir_all(&repo_path)?;
367 println!("Stash operations example completed successfully!");
368
369 Ok(())
370}
Sourcepub fn with_patch(self) -> Self
pub fn with_patch(self) -> Self
Create an interactive patch-mode stash
Sourcepub fn with_staged_only(self) -> Self
pub fn with_staged_only(self) -> Self
Only stash staged changes
Sourcepub fn with_paths(self, paths: Vec<PathBuf>) -> Self
pub fn with_paths(self, paths: Vec<PathBuf>) -> Self
Specify paths to stash
Examples found in repository?
examples/stash_operations.rs (line 145)
17fn main() -> Result<()> {
18 println!("Rustic Git - Stash Operations Example\n");
19
20 // Use a temporary directory for this example
21 let repo_path = env::temp_dir().join("rustic_git_stash_example");
22
23 // Clean up any previous run
24 if repo_path.exists() {
25 fs::remove_dir_all(&repo_path).expect("Failed to clean up previous example");
26 }
27
28 println!("Initializing repository at: {}", repo_path.display());
29
30 // Initialize repository and configure user
31 let repo = Repository::init(&repo_path, false)?;
32 repo.config()
33 .set_user("Stash Demo User", "stash@example.com")?;
34
35 // Create initial commit to have a base
36 println!("\nCreating initial commit...");
37 fs::write(
38 repo_path.join("README.md"),
39 "# Stash Demo Project\n\nDemonstrating Git stash operations.\n",
40 )?;
41 repo.add(&["README.md"])?;
42 let initial_commit = repo.commit("Initial commit: Add README")?;
43 println!("Created initial commit: {}", initial_commit.short());
44
45 // Create some work to stash
46 println!("\n=== Creating Work to Stash ===");
47
48 // Create tracked file modifications
49 fs::write(
50 repo_path.join("README.md"),
51 "# Stash Demo Project\n\nDemonstrating Git stash operations.\n\nAdded some new content!\n",
52 )?;
53
54 // Create new tracked files
55 fs::create_dir_all(repo_path.join("src"))?;
56 fs::write(
57 repo_path.join("src/main.rs"),
58 "fn main() {\n println!(\"Hello, stash!\");\n}\n",
59 )?;
60
61 // Create untracked files
62 fs::write(
63 repo_path.join("untracked.txt"),
64 "This file is not tracked by git\n",
65 )?;
66 fs::write(repo_path.join("temp.log"), "Temporary log file\n")?;
67
68 // Stage some changes
69 repo.add(&["src/main.rs"])?;
70
71 println!("Created various types of changes:");
72 println!(" - Modified tracked file (README.md)");
73 println!(" - Added new file and staged it (src/main.rs)");
74 println!(" - Created untracked files (untracked.txt, temp.log)");
75
76 // Check repository status before stashing
77 let status = repo.status()?;
78 println!("\nRepository status before stashing:");
79 println!(" Staged files: {}", status.staged_files().count());
80 println!(" Unstaged files: {}", status.unstaged_files().count());
81 println!(" Untracked files: {}", status.untracked_entries().count());
82
83 // Demonstrate stash creation
84 println!("\n=== Creating Stashes ===");
85
86 // 1. Simple stash save
87 println!("\n1. Creating simple stash:");
88 let simple_stash = repo.stash_save("WIP: working on main function")?;
89 println!("Created stash: {}", simple_stash);
90 println!(" Index: {}", simple_stash.index);
91 println!(" Branch: {}", simple_stash.branch);
92 println!(" Hash: {}", simple_stash.hash.short());
93
94 // Check status after stash
95 let status_after_stash = repo.status()?;
96 println!("\nStatus after simple stash:");
97 println!(
98 " Staged files: {}",
99 status_after_stash.staged_files().count()
100 );
101 println!(
102 " Unstaged files: {}",
103 status_after_stash.unstaged_files().count()
104 );
105 println!(
106 " Untracked files: {}",
107 status_after_stash.untracked_entries().count()
108 );
109
110 // 2. Make more changes and create stash with untracked files
111 println!("\n2. Creating stash with untracked files:");
112
113 // Modify file again
114 fs::write(
115 repo_path.join("README.md"),
116 "# Stash Demo Project\n\nDemonstrating Git stash operations.\n\nSecond round of changes!\n",
117 )?;
118
119 // Create more untracked files
120 fs::write(repo_path.join("config.json"), "{\"debug\": true}\n")?;
121
122 let untracked_options = StashOptions::new().with_untracked().with_keep_index();
123 let untracked_stash = repo.stash_push(
124 "WIP: config changes with untracked files",
125 untracked_options,
126 )?;
127 println!("Created stash with untracked files: {}", untracked_stash);
128
129 // 3. Create stash with specific paths
130 println!("\n3. Creating stash with specific paths:");
131
132 // Make changes to multiple files and add them to git
133 fs::write(repo_path.join("file1.txt"), "Content for file 1\n")?;
134 fs::write(repo_path.join("file2.txt"), "Content for file 2\n")?;
135 fs::write(repo_path.join("file3.txt"), "Content for file 3\n")?;
136
137 // Add all files so they're tracked
138 repo.add(&["file1.txt", "file2.txt", "file3.txt"])?;
139
140 // Now modify them so there are changes to stash
141 fs::write(repo_path.join("file1.txt"), "Modified content for file 1\n")?;
142 fs::write(repo_path.join("file2.txt"), "Modified content for file 2\n")?;
143 fs::write(repo_path.join("file3.txt"), "Modified content for file 3\n")?;
144
145 let path_options = StashOptions::new().with_paths(vec!["file1.txt".into(), "file2.txt".into()]);
146 let path_stash = repo.stash_push("WIP: specific files only", path_options)?;
147 println!("Created stash with specific paths: {}", path_stash);
148
149 // Demonstrate stash listing and filtering
150 println!("\n=== Stash Listing and Filtering ===");
151
152 let stashes = repo.stash_list()?;
153 println!("\nAll stashes ({} total):", stashes.len());
154 for stash in stashes.iter() {
155 println!(
156 " [{}] {} -> {}",
157 stash.index,
158 stash.message,
159 stash.hash.short()
160 );
161 println!(
162 " Branch: {} | Created: {}",
163 stash.branch,
164 stash.timestamp.format("%Y-%m-%d %H:%M:%S")
165 );
166 }
167
168 // Test filtering
169 println!("\nFiltering examples:");
170
171 // Find stashes containing specific text
172 let wip_stashes: Vec<_> = stashes.find_containing("WIP").collect();
173 println!("Stashes containing 'WIP': {} found", wip_stashes.len());
174 for stash in &wip_stashes {
175 println!(" - {}", stash.message);
176 }
177
178 let config_stashes: Vec<_> = stashes.find_containing("config").collect();
179 println!(
180 "Stashes containing 'config': {} found",
181 config_stashes.len()
182 );
183
184 // Get latest stash
185 if let Some(latest) = stashes.latest() {
186 println!("Latest stash: {}", latest.message);
187 }
188
189 // Get specific stash by index
190 if let Some(second_stash) = stashes.get(1) {
191 println!("Second stash: {}", second_stash.message);
192 }
193
194 // Demonstrate stash content viewing
195 println!("\n=== Viewing Stash Contents ===");
196
197 println!("\nShowing contents of latest stash:");
198 let stash_contents = repo.stash_show(0)?;
199 println!("{}", stash_contents);
200
201 // Demonstrate stash application
202 println!("\n=== Applying and Popping Stashes ===");
203
204 println!("\n1. Testing stash apply (keeps stash in list):");
205 let stashes_before_apply = repo.stash_list()?;
206 println!("Stashes before apply: {}", stashes_before_apply.len());
207
208 // Apply the latest stash
209 repo.stash_apply(0, StashApplyOptions::new())?;
210 println!("Applied stash@{{0}}");
211
212 let stashes_after_apply = repo.stash_list()?;
213 println!("Stashes after apply: {}", stashes_after_apply.len());
214
215 // Check what was restored
216 let status_after_apply = repo.status()?;
217 println!("Status after apply:");
218 println!(
219 " Staged files: {}",
220 status_after_apply.staged_files().count()
221 );
222 println!(
223 " Unstaged files: {}",
224 status_after_apply.unstaged_files().count()
225 );
226 println!(
227 " Untracked files: {}",
228 status_after_apply.untracked_entries().count()
229 );
230
231 println!("\n2. Testing stash pop (removes stash from list):");
232
233 // First, stash current changes again to have something to pop
234 repo.stash_save("Temporary stash for pop test")?;
235
236 let stashes_before_pop = repo.stash_list()?;
237 println!("Stashes before pop: {}", stashes_before_pop.len());
238
239 // Pop the latest stash
240 repo.stash_pop(0, StashApplyOptions::new().with_quiet())?;
241 println!("Popped stash@{{0}}");
242
243 let stashes_after_pop = repo.stash_list()?;
244 println!("Stashes after pop: {}", stashes_after_pop.len());
245
246 // Demonstrate advanced apply options
247 println!("\n3. Testing apply with index restoration:");
248
249 // Create a stash with staged changes
250 fs::write(repo_path.join("staged_file.txt"), "This will be staged\n")?;
251 repo.add(&["staged_file.txt"])?;
252
253 fs::write(
254 repo_path.join("unstaged_file.txt"),
255 "This will be unstaged\n",
256 )?;
257
258 repo.stash_save("Stash with staged and unstaged changes")?;
259
260 // Apply with index restoration
261 let apply_options = StashApplyOptions::new().with_index();
262 repo.stash_apply(0, apply_options)?;
263 println!("Applied stash with index restoration");
264
265 let final_status = repo.status()?;
266 println!("Final status after index restoration:");
267 println!(" Staged files: {}", final_status.staged_files().count());
268 println!(
269 " Unstaged files: {}",
270 final_status.unstaged_files().count()
271 );
272
273 // Demonstrate stash management
274 println!("\n=== Stash Management ===");
275
276 // Create a few test stashes
277 for i in 1..=3 {
278 fs::write(
279 repo_path.join(format!("test{}.txt", i)),
280 format!("Test content {}\n", i),
281 )?;
282 repo.stash_save(&format!("Test stash {}", i))?;
283 }
284
285 let management_stashes = repo.stash_list()?;
286 println!(
287 "\nCreated {} test stashes for management demo",
288 management_stashes.len()
289 );
290
291 // Drop a specific stash
292 println!("\n1. Dropping middle stash:");
293 println!("Before drop: {} stashes", management_stashes.len());
294
295 repo.stash_drop(1)?; // Drop second stash (index 1)
296 println!("Dropped stash@{{1}}");
297
298 let after_drop = repo.stash_list()?;
299 println!("After drop: {} stashes", after_drop.len());
300
301 // Show remaining stashes
302 println!("Remaining stashes:");
303 for stash in after_drop.iter() {
304 println!(" [{}] {}", stash.index, stash.message);
305 }
306
307 // Clear all stashes
308 println!("\n2. Clearing all stashes:");
309 repo.stash_clear()?;
310 println!("Cleared all stashes");
311
312 let final_stashes = repo.stash_list()?;
313 println!("Stashes after clear: {}", final_stashes.len());
314
315 // Demonstrate error handling
316 println!("\n=== Error Handling ===");
317
318 println!("\n1. Testing operations on empty stash list:");
319
320 // Try to apply non-existent stash
321 match repo.stash_apply(0, StashApplyOptions::new()) {
322 Ok(_) => println!("ERROR: Should have failed to apply non-existent stash"),
323 Err(e) => println!("Expected error applying non-existent stash: {}", e),
324 }
325
326 // Try to show non-existent stash
327 match repo.stash_show(0) {
328 Ok(_) => println!("ERROR: Should have failed to show non-existent stash"),
329 Err(e) => println!("Expected error showing non-existent stash: {}", e),
330 }
331
332 // Try to drop non-existent stash
333 match repo.stash_drop(0) {
334 Ok(_) => println!("ERROR: Should have failed to drop non-existent stash"),
335 Err(e) => println!("Expected error dropping non-existent stash: {}", e),
336 }
337
338 // Summary
339 println!("\n=== Summary ===");
340 println!("\nStash operations demonstrated:");
341 println!(" ✓ Basic stash save and push with options");
342 println!(" ✓ Stash with untracked files and keep-index");
343 println!(" ✓ Stash specific paths only");
344 println!(" ✓ Comprehensive stash listing and filtering");
345 println!(" ✓ Stash content viewing");
346 println!(" ✓ Apply vs pop operations");
347 println!(" ✓ Index restoration during apply");
348 println!(" ✓ Stash dropping and clearing");
349 println!(" ✓ Error handling for edge cases");
350
351 println!("\nStash options demonstrated:");
352 println!(" ✓ with_untracked() - Include untracked files");
353 println!(" ✓ with_keep_index() - Keep staged changes");
354 println!(" ✓ with_paths() - Stash specific files only");
355 println!(" ✓ with_index() - Restore staged state on apply");
356 println!(" ✓ with_quiet() - Suppress output messages");
357
358 println!("\nStash filtering demonstrated:");
359 println!(" ✓ find_containing() - Search by message content");
360 println!(" ✓ latest() - Get most recent stash");
361 println!(" ✓ get() - Get stash by index");
362 println!(" ✓ for_branch() - Filter by branch name");
363
364 // Clean up
365 println!("\nCleaning up example repository...");
366 fs::remove_dir_all(&repo_path)?;
367 println!("Stash operations example completed successfully!");
368
369 Ok(())
370}
Trait Implementations§
Source§impl Clone for StashOptions
impl Clone for StashOptions
Source§fn clone(&self) -> StashOptions
fn clone(&self) -> StashOptions
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for StashOptions
impl Debug for StashOptions
Source§impl Default for StashOptions
impl Default for StashOptions
Source§fn default() -> StashOptions
fn default() -> StashOptions
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for StashOptions
impl RefUnwindSafe for StashOptions
impl Send for StashOptions
impl Sync for StashOptions
impl Unpin for StashOptions
impl UnwindSafe for StashOptions
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more