Creates a tar archive from a directory
(source_path: &Path, output_path: &Path)
| 41 | |
| 42 | /// Creates a tar archive from a directory |
| 43 | fn create_archive(source_path: &Path, output_path: &Path) -> std::io::Result<()> { |
| 44 | info!("📦 Crafting your stack bundle: {:?}", output_path); |
| 45 | let file = File::create(output_path)?; |
| 46 | |
| 47 | // Create gzip encoder with maximum compression |
| 48 | let gz = GzBuilder::new() |
| 49 | .filename(output_path.to_str().unwrap_or("stack.stack")) |
| 50 | .comment("Stack bundle") |
| 51 | .write(file, Compression::best()); |
| 52 | |
| 53 | let mut builder = tar::Builder::new(gz); |
| 54 | |
| 55 | // Configure git-ignore based walker with explicit git directory exclusion |
| 56 | let walker = WalkBuilder::new(source_path) |
| 57 | .hidden(false) // Include hidden files |
| 58 | .git_ignore(true) // Use .gitignore files |
| 59 | .git_global(true) // Use global gitignore |
| 60 | .git_exclude(true) // Use .git/info/exclude |
| 61 | .require_git(false) // Don't require it to be a git repo |
| 62 | .ignore(true) // Use .ignore files |
| 63 | .filter_entry(|entry| !entry.path().starts_with(".git/")) // Explicitly filter out .git directory |
| 64 | .build(); |
| 65 | |
| 66 | // Custom ignore for .stack files and .git directory |
| 67 | let should_include = |path: &Path| { |
| 68 | path.extension().map(|ext| ext != "stack").unwrap_or(true) |
| 69 | && !path.starts_with(".git") |
| 70 | && !path.components().any(|c| c.as_os_str() == ".git") |
| 71 | }; |
| 72 | |
| 73 | // Walk the directory and add files to archive |
| 74 | for entry in walker { |
| 75 | let entry = entry.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?; |
| 76 | let path = entry.path(); |
| 77 | |
| 78 | // Skip if it's not a file or if it's a .stack file |
| 79 | if !path.is_file() || !should_include(path) { |
| 80 | continue; |
| 81 | } |
| 82 | |
| 83 | debug!("Adding: {:?}", path); |
| 84 | |
| 85 | let rel_path = path |
| 86 | .strip_prefix(source_path) |
| 87 | .expect("Failed to strip prefix"); |
| 88 | |
| 89 | builder.append_path_with_name(path, rel_path)?; |
| 90 | } |
| 91 | |
| 92 | builder.finish()?; |
| 93 | Ok(()) |
| 94 | } |
| 95 | |
| 96 | /// Packs a software stack into a stack bundle. |
| 97 | /// |