deadbranch 0.1.3

Clean up stale git branches safely
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//! Configuration handling for deadbranch

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::PathBuf;

/// Default number of days before a branch is considered stale
const DEFAULT_DAYS: u32 = 30;

/// Default protected branches
const DEFAULT_PROTECTED: &[&str] = &["main", "master", "develop", "staging", "production"];

/// Default exclude patterns (WIP/draft branches)
const DEFAULT_EXCLUDE_PATTERNS: &[&str] = &["wip/*", "draft/*", "*/wip", "*/draft"];

/// General settings section
#[derive(Debug, Deserialize, Serialize)]
pub struct GeneralConfig {
    /// Default age threshold (days)
    #[serde(default = "default_days")]
    pub default_days: u32,
}

impl Default for GeneralConfig {
    fn default() -> Self {
        Self {
            default_days: default_days(),
        }
    }
}

/// Branch-related settings section
#[derive(Debug, Deserialize, Serialize)]
pub struct BranchesConfig {
    /// The default branch to check merges against (auto-detected if not set)
    #[serde(default)]
    pub default_branch: Option<String>,

    /// Protected branches (never deleted)
    #[serde(default = "default_protected_branches")]
    pub protected: Vec<String>,

    /// Branch name patterns to exclude (glob-style: wip/*, */draft, etc.)
    #[serde(default = "default_exclude_patterns")]
    pub exclude_patterns: Vec<String>,
}

impl Default for BranchesConfig {
    fn default() -> Self {
        Self {
            default_branch: None,
            protected: default_protected_branches(),
            exclude_patterns: default_exclude_patterns(),
        }
    }
}

/// Configuration for deadbranch
#[derive(Debug, Deserialize, Serialize, Default)]
pub struct Config {
    #[serde(default)]
    pub general: GeneralConfig,

    #[serde(default)]
    pub branches: BranchesConfig,
}

fn default_days() -> u32 {
    DEFAULT_DAYS
}

fn default_protected_branches() -> Vec<String> {
    DEFAULT_PROTECTED.iter().map(|s| s.to_string()).collect()
}

fn default_exclude_patterns() -> Vec<String> {
    DEFAULT_EXCLUDE_PATTERNS
        .iter()
        .map(|s| s.to_string())
        .collect()
}

impl Config {
    /// Get the main deadbranch directory (~/.deadbranch)
    pub fn deadbranch_dir() -> Result<PathBuf> {
        let home = dirs::home_dir().context("Could not determine home directory")?;
        Ok(home.join(".deadbranch"))
    }

    /// Get the path to the config file (~/.deadbranch/config.toml)
    pub fn config_path() -> Result<PathBuf> {
        Ok(Self::deadbranch_dir()?.join("config.toml"))
    }

    /// Get the backups directory (~/.deadbranch/backups)
    pub fn backups_dir() -> Result<PathBuf> {
        Ok(Self::deadbranch_dir()?.join("backups"))
    }

    /// Get the backup directory for a specific repository
    pub fn repo_backup_dir(repo_name: &str) -> Result<PathBuf> {
        Ok(Self::backups_dir()?.join(repo_name))
    }

    /// Get the current repository name (uses directory name)
    pub fn get_repo_name() -> String {
        std::env::current_dir()
            .ok()
            .and_then(|path| {
                path.file_name()
                    .and_then(|name| name.to_str())
                    .map(|s| s.to_string())
            })
            .unwrap_or_else(|| "unknown-repo".to_string())
    }

    /// Load config from file, or create default config if file doesn't exist
    pub fn load() -> Result<Self> {
        let path = Self::config_path()?;

        if path.exists() {
            let content = fs::read_to_string(&path)
                .with_context(|| format!("Failed to read config file: {}", path.display()))?;
            let config: Config = toml::from_str(&content)
                .with_context(|| format!("Failed to parse config file: {}", path.display()))?;
            Ok(config)
        } else {
            // Auto-create config file with defaults on first use
            let config = Config::default();
            config.save()?;
            Ok(config)
        }
    }

    /// Save config to file
    pub fn save(&self) -> Result<()> {
        let path = Self::config_path()?;

        // Create directory if it doesn't exist
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).with_context(|| {
                format!("Failed to create config directory: {}", parent.display())
            })?;
        }

        let content = toml::to_string_pretty(self).context("Failed to serialize config")?;
        fs::write(&path, content)
            .with_context(|| format!("Failed to write config file: {}", path.display()))?;

        Ok(())
    }

    /// Set a configuration value by key (accepts multiple values for list types)
    /// Supports both flat keys (default-days) and dotted keys (general.default-days)
    pub fn set(&mut self, key: &str, values: &[String]) -> Result<()> {
        match key {
            // General section
            "general.default-days" | "default-days" | "days" => {
                if values.len() != 1 {
                    anyhow::bail!("default-days expects a single value");
                }
                self.general.default_days = values[0]
                    .parse()
                    .with_context(|| format!("Invalid number: {}", values[0]))?;
            }

            // Branches section
            "branches.protected" | "protected-branches" => {
                // Filter out empty strings to allow clearing with ""
                self.branches.protected =
                    values.iter().filter(|s| !s.is_empty()).cloned().collect();
            }
            "branches.default-branch" | "default-branch" => {
                if values.len() != 1 {
                    anyhow::bail!("default-branch expects a single value");
                }
                self.branches.default_branch = if values[0].is_empty() {
                    None
                } else {
                    Some(values[0].clone())
                };
            }
            "branches.exclude-patterns" | "exclude-patterns" => {
                // Filter out empty strings to allow clearing with ""
                self.branches.exclude_patterns =
                    values.iter().filter(|s| !s.is_empty()).cloned().collect();
            }

            _ => {
                anyhow::bail!(
                    "Unknown config key: {}. Valid keys: general.default-days, branches.protected, branches.default-branch, branches.exclude-patterns",
                    key
                );
            }
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    /// Helper to create a temporary config in a temp directory
    fn with_temp_config<F>(test: F)
    where
        F: FnOnce(PathBuf),
    {
        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join("config.toml");
        test(config_path);
    }

    #[test]
    fn test_default_config() {
        let config = Config::default();
        assert_eq!(config.general.default_days, 30);
        assert_eq!(
            config.branches.protected,
            vec!["main", "master", "develop", "staging", "production"]
        );
        assert_eq!(
            config.branches.exclude_patterns,
            vec!["wip/*", "draft/*", "*/wip", "*/draft"]
        );
        assert_eq!(config.branches.default_branch, None);
    }

    #[test]
    fn test_config_serialization() {
        let config = Config::default();
        let toml_str = toml::to_string(&config).unwrap();
        assert!(toml_str.contains("[general]"));
        assert!(toml_str.contains("default_days = 30"));
        assert!(toml_str.contains("[branches]"));
        assert!(toml_str.contains("protected"));
        assert!(toml_str.contains("exclude_patterns"));
    }

    #[test]
    fn test_config_deserialization() {
        let toml_str = r#"
            [general]
            default_days = 45

            [branches]
            default_branch = "master"
            protected = ["main", "develop"]
            exclude_patterns = ["temp/*"]
        "#;
        let config: Config = toml::from_str(toml_str).unwrap();
        assert_eq!(config.general.default_days, 45);
        assert_eq!(config.branches.protected, vec!["main", "develop"]);
        assert_eq!(config.branches.exclude_patterns, vec!["temp/*"]);
        assert_eq!(config.branches.default_branch, Some("master".to_string()));
    }

    #[test]
    fn test_config_set_default_days() {
        let mut config = Config::default();
        config.set("default-days", &["45".to_string()]).unwrap();
        assert_eq!(config.general.default_days, 45);

        // Alternative key name
        config.set("days", &["60".to_string()]).unwrap();
        assert_eq!(config.general.default_days, 60);

        // Dotted key name
        config
            .set("general.default-days", &["75".to_string()])
            .unwrap();
        assert_eq!(config.general.default_days, 75);
    }

    #[test]
    fn test_config_set_default_days_invalid() {
        let mut config = Config::default();
        let result = config.set("default-days", &["not_a_number".to_string()]);
        assert!(result.is_err());

        // Multiple values should fail
        let result = config.set("default-days", &["30".to_string(), "45".to_string()]);
        assert!(result.is_err());
    }

    #[test]
    fn test_config_set_protected_branches() {
        let mut config = Config::default();
        config
            .set(
                "protected-branches",
                &["main".to_string(), "develop".to_string()],
            )
            .unwrap();
        assert_eq!(config.branches.protected, vec!["main", "develop"]);

        // Can set single value
        config
            .set("protected-branches", &["main".to_string()])
            .unwrap();
        assert_eq!(config.branches.protected, vec!["main"]);

        // Dotted key
        config
            .set("branches.protected", &["staging".to_string()])
            .unwrap();
        assert_eq!(config.branches.protected, vec!["staging"]);

        // Can clear with empty string
        config.set("protected-branches", &["".to_string()]).unwrap();
        assert!(config.branches.protected.is_empty());
    }

    #[test]
    fn test_config_set_default_branch() {
        let mut config = Config::default();
        config
            .set("default-branch", &["master".to_string()])
            .unwrap();
        assert_eq!(config.branches.default_branch, Some("master".to_string()));

        // Dotted key
        config
            .set("branches.default-branch", &["main".to_string()])
            .unwrap();
        assert_eq!(config.branches.default_branch, Some("main".to_string()));

        // Can clear with empty string
        config.set("default-branch", &["".to_string()]).unwrap();
        assert_eq!(config.branches.default_branch, None);
    }

    #[test]
    fn test_config_set_default_branch_invalid() {
        let mut config = Config::default();
        // Multiple values should fail
        let result = config.set(
            "default-branch",
            &["main".to_string(), "master".to_string()],
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_config_set_exclude_patterns() {
        let mut config = Config::default();
        config
            .set(
                "exclude-patterns",
                &["temp/*".to_string(), "*/old".to_string()],
            )
            .unwrap();
        assert_eq!(config.branches.exclude_patterns, vec!["temp/*", "*/old"]);

        // Dotted key
        config
            .set("branches.exclude-patterns", &["test/*".to_string()])
            .unwrap();
        assert_eq!(config.branches.exclude_patterns, vec!["test/*"]);

        // Can clear with empty string
        config.set("exclude-patterns", &["".to_string()]).unwrap();
        assert!(config.branches.exclude_patterns.is_empty());
    }

    #[test]
    fn test_config_set_unknown_key() {
        let mut config = Config::default();
        let result = config.set("unknown-key", &["value".to_string()]);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Unknown config key"));
    }

    #[test]
    fn test_config_save_and_load() {
        with_temp_config(|config_path| {
            let temp_dir = config_path.parent().unwrap();

            // Override the config path for this test
            let mut config = Config::default();
            config.general.default_days = 45;
            config.branches.protected = vec!["main".to_string()];

            // Save manually to temp path
            fs::create_dir_all(temp_dir).unwrap();
            let content = toml::to_string_pretty(&config).unwrap();
            fs::write(&config_path, content).unwrap();

            // Load and verify
            let loaded_content = fs::read_to_string(&config_path).unwrap();
            let loaded_config: Config = toml::from_str(&loaded_content).unwrap();
            assert_eq!(loaded_config.general.default_days, 45);
            assert_eq!(loaded_config.branches.protected, vec!["main"]);
        });
    }

    #[test]
    fn test_get_repo_name() {
        let repo_name = Config::get_repo_name();
        // Should be "deadbranch" when running in the deadbranch directory
        assert!(!repo_name.is_empty());
        assert_ne!(repo_name, "unknown-repo");
    }

    #[test]
    fn test_config_paths() {
        // These should not panic and should return valid paths
        let deadbranch_dir = Config::deadbranch_dir();
        assert!(deadbranch_dir.is_ok());

        let config_path = Config::config_path();
        assert!(config_path.is_ok());

        let backups_dir = Config::backups_dir();
        assert!(backups_dir.is_ok());

        let repo_backup = Config::repo_backup_dir("test-repo");
        assert!(repo_backup.is_ok());
        assert!(repo_backup.unwrap().to_string_lossy().contains("test-repo"));
    }
}