|
| 1 | +//! Basic Usage Example |
| 2 | +//! |
| 3 | +//! This example demonstrates the fundamental workflow of the rustic-git library: |
| 4 | +//! - Initialize a new repository |
| 5 | +//! - Create some files |
| 6 | +//! - Check repository status |
| 7 | +//! - Stage files |
| 8 | +//! - Create a commit |
| 9 | +//! |
| 10 | +//! Run with: cargo run --example basic_usage |
| 11 | +
|
| 12 | +use rustic_git::{Repository, Result}; |
| 13 | +use std::fs; |
| 14 | +use std::path::Path; |
| 15 | + |
| 16 | +fn main() -> Result<()> { |
| 17 | + println!("Rustic Git - Basic Usage Example\n"); |
| 18 | + |
| 19 | + // Use a temporary directory for this example |
| 20 | + let repo_path = "/tmp/rustic_git_basic_example"; |
| 21 | + |
| 22 | + // Clean up any previous run |
| 23 | + if Path::new(repo_path).exists() { |
| 24 | + fs::remove_dir_all(repo_path).expect("Failed to clean up previous example"); |
| 25 | + } |
| 26 | + |
| 27 | + println!("Initializing new repository at: {}", repo_path); |
| 28 | + |
| 29 | + // Initialize a new repository |
| 30 | + let repo = Repository::init(repo_path, false)?; |
| 31 | + println!("Repository initialized successfully\n"); |
| 32 | + |
| 33 | + // Create some example files |
| 34 | + println!("Creating example files..."); |
| 35 | + fs::create_dir_all(format!("{}/src", repo_path))?; |
| 36 | + |
| 37 | + fs::write( |
| 38 | + format!("{}/README.md", repo_path), |
| 39 | + "# My Awesome Project\n\nThis is a demo project for rustic-git!\n", |
| 40 | + )?; |
| 41 | + |
| 42 | + fs::write( |
| 43 | + format!("{}/src/main.rs", repo_path), |
| 44 | + r#"fn main() { |
| 45 | + println!("Hello from rustic-git example!"); |
| 46 | +} |
| 47 | +"#, |
| 48 | + )?; |
| 49 | + |
| 50 | + fs::write( |
| 51 | + format!("{}/src/lib.rs", repo_path), |
| 52 | + "// Library code goes here\npub fn hello() -> &'static str {\n \"Hello, World!\"\n}\n", |
| 53 | + )?; |
| 54 | + |
| 55 | + println!("Created 3 files: README.md, src/main.rs, src/lib.rs\n"); |
| 56 | + |
| 57 | + // Check repository status |
| 58 | + println!("Checking repository status..."); |
| 59 | + let status = repo.status()?; |
| 60 | + |
| 61 | + if status.is_clean() { |
| 62 | + println!(" Repository is clean (no changes)"); |
| 63 | + } else { |
| 64 | + println!(" Repository has changes:"); |
| 65 | + println!(" Modified files: {}", status.modified_files().len()); |
| 66 | + println!(" Untracked files: {}", status.untracked_files().len()); |
| 67 | + |
| 68 | + // Show untracked files |
| 69 | + for filename in status.untracked_files() { |
| 70 | + println!(" - {}", filename); |
| 71 | + } |
| 72 | + } |
| 73 | + println!(); |
| 74 | + |
| 75 | + // Stage specific files first |
| 76 | + println!("Staging files..."); |
| 77 | + |
| 78 | + // Stage README.md first |
| 79 | + repo.add(&["README.md"])?; |
| 80 | + println!("Staged README.md"); |
| 81 | + |
| 82 | + // Stage all remaining files |
| 83 | + repo.add_all()?; |
| 84 | + println!("Staged all remaining files"); |
| 85 | + |
| 86 | + // Check status after staging |
| 87 | + let status_after_staging = repo.status()?; |
| 88 | + println!("\nStatus after staging:"); |
| 89 | + if status_after_staging.is_clean() { |
| 90 | + println!(" Repository is clean (all changes staged)"); |
| 91 | + } else { |
| 92 | + println!( |
| 93 | + " Files staged for commit: {}", |
| 94 | + status_after_staging.files.len() |
| 95 | + ); |
| 96 | + for (file_status, filename) in &status_after_staging.files { |
| 97 | + println!(" {:?}: {}", file_status, filename); |
| 98 | + } |
| 99 | + } |
| 100 | + println!(); |
| 101 | + |
| 102 | + // Create a commit |
| 103 | + println!("Creating commit..."); |
| 104 | + let hash = repo.commit("Initial commit: Add project structure and basic files")?; |
| 105 | + |
| 106 | + println!("Commit created successfully!"); |
| 107 | + println!(" Full hash: {}", hash); |
| 108 | + println!(" Short hash: {}", hash.short()); |
| 109 | + println!(); |
| 110 | + |
| 111 | + // Verify final status |
| 112 | + println!("Final repository status:"); |
| 113 | + let final_status = repo.status()?; |
| 114 | + if final_status.is_clean() { |
| 115 | + println!(" Repository is clean - all changes committed!"); |
| 116 | + } else { |
| 117 | + println!(" Repository still has uncommitted changes"); |
| 118 | + } |
| 119 | + println!(); |
| 120 | + |
| 121 | + // Clean up |
| 122 | + println!("Cleaning up example repository..."); |
| 123 | + fs::remove_dir_all(repo_path)?; |
| 124 | + println!("Example completed successfully!"); |
| 125 | + |
| 126 | + Ok(()) |
| 127 | +} |
0 commit comments