config_operations/
config_operations.rs

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