|
| 1 | +//! Repository Configuration Operations Example |
| 2 | +//! |
| 3 | +//! This example demonstrates the repository configuration management features: |
| 4 | +//! - Setting and getting user configuration |
| 5 | +//! - Managing arbitrary git configuration values |
| 6 | +//! - Repository-scoped configuration operations |
| 7 | +//! - Configuration integration with commit operations |
| 8 | +//! |
| 9 | +//! Run with: cargo run --example config_operations |
| 10 | +
|
| 11 | +use rustic_git::{Repository, Result}; |
| 12 | +use std::fs; |
| 13 | +use std::path::Path; |
| 14 | + |
| 15 | +fn main() -> Result<()> { |
| 16 | + println!("Rustic Git - Repository Configuration Operations Example\n"); |
| 17 | + |
| 18 | + // Use a temporary directory for this example |
| 19 | + let repo_path = "/tmp/rustic_git_config_example"; |
| 20 | + |
| 21 | + // Clean up any previous run |
| 22 | + if Path::new(repo_path).exists() { |
| 23 | + fs::remove_dir_all(repo_path).expect("Failed to clean up previous example"); |
| 24 | + } |
| 25 | + |
| 26 | + println!("Initializing new repository at: {}", repo_path); |
| 27 | + |
| 28 | + // Initialize a new repository |
| 29 | + let repo = Repository::init(repo_path, false)?; |
| 30 | + |
| 31 | + // ==================== USER CONFIGURATION ==================== |
| 32 | + |
| 33 | + println!("\n[CONFIG] Configuring git user settings..."); |
| 34 | + |
| 35 | + // Set user configuration (convenience method) |
| 36 | + repo.config() |
| 37 | + .set_user("Alice Developer", "[email protected]")? ; |
| 38 | + println!("Set user configuration"); |
| 39 | + |
| 40 | + // Verify user configuration |
| 41 | + let (name, email) = repo.config().get_user()?; |
| 42 | + println!("Current user: {} <{}>", name, email); |
| 43 | + |
| 44 | + // ==================== GENERAL CONFIGURATION ==================== |
| 45 | + |
| 46 | + println!("\n[CONFIG] Setting repository configuration values..."); |
| 47 | + |
| 48 | + // Set various git configuration values |
| 49 | + repo.config().set("core.autocrlf", "false")?; |
| 50 | + repo.config().set("core.ignorecase", "true")?; |
| 51 | + repo.config().set("pull.rebase", "true")?; |
| 52 | + repo.config().set("push.default", "simple")?; |
| 53 | + repo.config().set("branch.autosetupmerge", "always")?; |
| 54 | + |
| 55 | + println!("Set core configuration values"); |
| 56 | + |
| 57 | + // Get and display configuration values |
| 58 | + println!("\n[CONFIG] Current repository configuration:"); |
| 59 | + |
| 60 | + let configs = [ |
| 61 | + "core.autocrlf", |
| 62 | + "core.ignorecase", |
| 63 | + "pull.rebase", |
| 64 | + "push.default", |
| 65 | + "branch.autosetupmerge", |
| 66 | + ]; |
| 67 | + |
| 68 | + for config_key in &configs { |
| 69 | + match repo.config().get(config_key) { |
| 70 | + Ok(value) => println!(" {} = {}", config_key, value), |
| 71 | + Err(_) => println!(" {} = <not set>", config_key), |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // ==================== CONFIGURATION WITH COMMITS ==================== |
| 76 | + |
| 77 | + println!("\n[COMMIT] Testing configuration with commit operations..."); |
| 78 | + |
| 79 | + // Create a test file |
| 80 | + let test_file_path = format!("{}/test.txt", repo_path); |
| 81 | + fs::write( |
| 82 | + &test_file_path, |
| 83 | + "Hello from rustic-git configuration example!", |
| 84 | + )?; |
| 85 | + println!("Created test file: test.txt"); |
| 86 | + |
| 87 | + // Stage the file |
| 88 | + repo.add(&["test.txt"])?; |
| 89 | + println!("Staged test.txt"); |
| 90 | + |
| 91 | + // Create a commit (this will use our configured user) |
| 92 | + let commit_hash = repo.commit("Add test file with configuration example")?; |
| 93 | + println!("Created commit: {}", commit_hash.short()); |
| 94 | + |
| 95 | + // ==================== CONFIGURATION MODIFICATION ==================== |
| 96 | + |
| 97 | + println!("\n[UPDATE] Modifying configuration values..."); |
| 98 | + |
| 99 | + // Change some configuration values |
| 100 | + repo.config().set("core.autocrlf", "true")?; |
| 101 | + repo.config() |
| 102 | + .set("user.email", "[email protected]")? ; |
| 103 | + |
| 104 | + println!("Updated configuration values"); |
| 105 | + |
| 106 | + // Display updated values |
| 107 | + let autocrlf = repo.config().get("core.autocrlf")?; |
| 108 | + let (updated_name, updated_email) = repo.config().get_user()?; |
| 109 | + |
| 110 | + println!("Updated configuration:"); |
| 111 | + println!(" core.autocrlf = {}", autocrlf); |
| 112 | + println!(" user: {} <{}>", updated_name, updated_email); |
| 113 | + |
| 114 | + // ==================== CONFIGURATION REMOVAL ==================== |
| 115 | + |
| 116 | + println!("\n[REMOVE] Removing configuration values..."); |
| 117 | + |
| 118 | + // Remove a configuration value |
| 119 | + repo.config().unset("branch.autosetupmerge")?; |
| 120 | + println!("Removed branch.autosetupmerge"); |
| 121 | + |
| 122 | + // Try to get the removed value (should fail) |
| 123 | + match repo.config().get("branch.autosetupmerge") { |
| 124 | + Ok(value) => println!("Unexpected: branch.autosetupmerge = {}", value), |
| 125 | + Err(_) => println!("Confirmed: branch.autosetupmerge is not set"), |
| 126 | + } |
| 127 | + |
| 128 | + // ==================== ADVANCED CONFIGURATION ==================== |
| 129 | + |
| 130 | + println!("\n[ADVANCED] Setting advanced configuration..."); |
| 131 | + |
| 132 | + // Set some advanced git configuration |
| 133 | + repo.config().set("diff.tool", "vimdiff")?; |
| 134 | + repo.config().set("merge.tool", "vimdiff")?; |
| 135 | + repo.config().set("alias.st", "status")?; |
| 136 | + repo.config().set("alias.co", "checkout")?; |
| 137 | + repo.config().set("alias.br", "branch")?; |
| 138 | + repo.config().set("alias.ci", "commit")?; |
| 139 | + |
| 140 | + println!("Set advanced configuration (diff/merge tools and aliases)"); |
| 141 | + |
| 142 | + // Display all custom configuration |
| 143 | + println!("\n[SUMMARY] Complete repository configuration summary:"); |
| 144 | + |
| 145 | + let all_configs = [ |
| 146 | + ("User", vec![("user.name", ""), ("user.email", "")]), |
| 147 | + ("Core", vec![("core.autocrlf", ""), ("core.ignorecase", "")]), |
| 148 | + ("Workflow", vec![("pull.rebase", ""), ("push.default", "")]), |
| 149 | + ("Tools", vec![("diff.tool", ""), ("merge.tool", "")]), |
| 150 | + ( |
| 151 | + "Aliases", |
| 152 | + vec![ |
| 153 | + ("alias.st", ""), |
| 154 | + ("alias.co", ""), |
| 155 | + ("alias.br", ""), |
| 156 | + ("alias.ci", ""), |
| 157 | + ], |
| 158 | + ), |
| 159 | + ]; |
| 160 | + |
| 161 | + for (category, configs) in &all_configs { |
| 162 | + println!("\n {}:", category); |
| 163 | + for (key, _) in configs { |
| 164 | + match repo.config().get(key) { |
| 165 | + Ok(value) => println!(" {} = {}", key, value), |
| 166 | + Err(_) => println!(" {} = <not set>", key), |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + // ==================== PRACTICAL EXAMPLE ==================== |
| 172 | + |
| 173 | + println!("\n[TEAM] Practical example: Setting up repository for a team..."); |
| 174 | + |
| 175 | + // Configure repository for team development |
| 176 | + repo.config().set("user.name", "Team Member")?; |
| 177 | + repo .config().set("user.email", "[email protected]")? ; |
| 178 | + repo.config().set("core.autocrlf", "input")?; |
| 179 | + repo.config().set("core.safecrlf", "true")?; |
| 180 | + repo.config().set("pull.rebase", "true")?; |
| 181 | + repo.config().set("push.default", "current")?; |
| 182 | + repo.config().set("init.defaultBranch", "main")?; |
| 183 | + |
| 184 | + println!("Configured repository for team development"); |
| 185 | + |
| 186 | + // Create another commit with the team configuration |
| 187 | + fs::write( |
| 188 | + format!("{}/team.md", repo_path), |
| 189 | + "# Team Development\n\nThis repository is configured for team development.", |
| 190 | + )?; |
| 191 | + repo.add(&["team.md"])?; |
| 192 | + let team_commit = repo.commit("Add team development documentation")?; |
| 193 | + |
| 194 | + println!("Created team commit: {}", team_commit.short()); |
| 195 | + |
| 196 | + // Final verification |
| 197 | + let (final_name, final_email) = repo.config().get_user()?; |
| 198 | + println!("\n[FINAL] Final repository configuration:"); |
| 199 | + println!(" User: {} <{}>", final_name, final_email); |
| 200 | + println!(" Repository configured for team development workflow"); |
| 201 | + |
| 202 | + // ==================== CLEANUP ==================== |
| 203 | + |
| 204 | + println!("\n[CLEANUP] Cleaning up..."); |
| 205 | + fs::remove_dir_all(repo_path).expect("Failed to clean up example"); |
| 206 | + println!("Example completed successfully!"); |
| 207 | + |
| 208 | + Ok(()) |
| 209 | +} |
0 commit comments