gimmetool 0.1.3

A multi-repo manager that lets you jump between projects, pin favorites, clean stale branches, and alias repos — all from one CLI
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;

use crate::path;

const DEFAULT_SEARCH_FOLDER: &str = "~/";
const DEFAULT_GLOBAL_BRANCHES: &[&str] = &["main", "master"];

#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
  #[serde(rename = "search-folders", default = "default_search_folders")]
  pub search_folders: Vec<String>,

  #[serde(default)]
  pub pins: Pins,

  #[serde(default)]
  pub aliases: HashMap<String, String>,

  #[serde(skip)]
  file_path: Option<PathBuf>,
}

#[derive(Debug, Serialize, Deserialize, Default)]
pub struct Pins {
  #[serde(default)]
  pub repositories: Vec<String>,

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

#[derive(Debug, Serialize, Deserialize)]
pub struct PinnedBranches {
  #[serde(default = "default_global_branches")]
  pub global: Vec<String>,

  /// Viper (Go) writes dotted keys like "github.com/user/repo" as nested YAML
  /// maps. This custom deserializer flattens them back into dotted keys so
  /// existing configs are read correctly.
  #[serde(default, deserialize_with = "deserialize_repo_branches")]
  pub repositories: HashMap<String, Vec<String>>,
}

fn deserialize_repo_branches<'de, D>(
  deserializer: D,
) -> std::result::Result<HashMap<String, Vec<String>>, D::Error>
where
  D: serde::Deserializer<'de>,
{
  let value = serde_yaml::Value::deserialize(deserializer)?;
  match value {
    serde_yaml::Value::Mapping(map) => Ok(flatten_repo_map(&map, "")),
    _ => Ok(HashMap::new()),
  }
}

fn flatten_repo_map(map: &serde_yaml::Mapping, prefix: &str) -> HashMap<String, Vec<String>> {
  let mut result = HashMap::new();

  for (k, v) in map {
    let key_part = k.as_str().unwrap_or("");
    let full_key = if prefix.is_empty() {
      key_part.to_string()
    } else {
      format!("{prefix}.{key_part}")
    };

    match v {
      serde_yaml::Value::Sequence(seq) => {
        let branches: Vec<String> = seq
          .iter()
          .filter_map(|item| item.as_str().map(|s| s.to_string()))
          .collect();
        result.insert(full_key, branches);
      }
      serde_yaml::Value::Mapping(nested) => {
        result.extend(flatten_repo_map(nested, &full_key));
      }
      _ => {}
    }
  }

  result
}

impl Default for PinnedBranches {
  fn default() -> Self {
    Self {
      global: default_global_branches(),
      repositories: HashMap::new(),
    }
  }
}

impl Default for Config {
  fn default() -> Self {
    Self {
      search_folders: default_search_folders(),
      pins: Pins::default(),
      aliases: HashMap::new(),
      file_path: None,
    }
  }
}

fn default_search_folders() -> Vec<String> {
  vec![DEFAULT_SEARCH_FOLDER.to_string()]
}

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

// --- Load / Save ---

pub fn load() -> Config {
  let config_path = find_config_file();

  let Some(path) = config_path else {
    return Config::default();
  };

  let contents = match fs::read_to_string(&path) {
    Ok(c) => c,
    Err(e) => {
      eprintln!("ERROR Error reading gimme configuration: {e}");
      return Config::default();
    }
  };

  match serde_yaml::from_str::<Config>(&contents) {
    Ok(mut config) => {
      config.file_path = Some(path);
      config
    }
    Err(e) => {
      eprintln!("ERROR Error reading gimme configuration: {e}");
      Config {
        file_path: Some(path),
        ..Default::default()
      }
    }
  }
}

pub fn find_config_file() -> Option<PathBuf> {
  // 1. Local override in current directory
  for name in [".gimme.config.yaml", ".gimme.config.yml"] {
    let local = PathBuf::from(name);
    if local.exists() {
      return Some(local);
    }
  }

  // 2. XDG config path ($XDG_CONFIG_HOME/gimme/config.yaml)
  if let Some(gimme_dir) = crate::xdg::gimme_dir() {
    for name in ["config.yaml", "config.yml"] {
      let path = gimme_dir.join(name);
      if path.exists() {
        return Some(path);
      }
    }
  }

  // 3. Legacy home directory path
  let home = dirs::home_dir()?;
  for name in [".gimme.config.yaml", ".gimme.config.yml"] {
    let path = home.join(name);
    if path.exists() {
      return Some(path);
    }
  }

  None
}

fn save(config: &Config) -> Result<()> {
  let path = match &config.file_path {
    Some(p) => p.clone(),
    None => {
      // New config goes to XDG path
      let gimme_dir = crate::xdg::gimme_dir().context("Could not determine config directory")?;
      fs::create_dir_all(&gimme_dir)?;
      gimme_dir.join("config.yaml")
    }
  };

  let yaml = serde_yaml::to_string(config)?;
  fs::write(&path, yaml)?;
  Ok(())
}

fn is_default_search_folders(folders: &[String]) -> bool {
  folders.len() == 1 && folders[0] == DEFAULT_SEARCH_FOLDER
}

// --- Search Folders ---

pub fn get_search_folders(config: &Config) -> Vec<String> {
  config
    .search_folders
    .iter()
    .map(|raw| {
      path::normalize(raw).unwrap_or_else(|e| {
        eprintln!("ERROR Error parsing search folder \"{raw}\": {e}");
        raw.clone()
      })
    })
    .collect()
}

pub fn add_group(config: &mut Config, group_path: &str) {
  let normalized = match path::normalize(group_path) {
    Ok(n) => n,
    Err(e) => {
      eprintln!("ERROR Error parsing search folder \"{group_path}\": {e}");
      return;
    }
  };

  let already_exists = config
    .search_folders
    .iter()
    .any(|g| path::normalize(g).unwrap_or_default() == normalized);

  if already_exists {
    eprintln!("Group already exists: \"{group_path}\".");
    return;
  }

  if is_default_search_folders(&config.search_folders) {
    config.search_folders = vec![group_path.to_string()];
  } else {
    config.search_folders.push(group_path.to_string());
  }

  if let Err(e) = save(config) {
    eprintln!("ERROR Error saving config: {e}");
    return;
  }
  eprintln!("Added search group \"{group_path}\".");
}

pub fn delete_group(config: &mut Config, group_path: &str) {
  let normalized = path::normalize(group_path).unwrap_or_default();

  let original_len = config.search_folders.len();
  config
    .search_folders
    .retain(|g| path::normalize(g).unwrap_or_default() != normalized);

  if config.search_folders.len() == original_len {
    eprintln!("Group not found: \"{group_path}\".");
    return;
  }

  if let Err(e) = save(config) {
    eprintln!("ERROR Error saving config: {e}");
    return;
  }
  eprintln!("Deleted search group \"{group_path}\".");
}

pub fn delete_group_by_index(config: &mut Config, index: usize) {
  if index >= config.search_folders.len() {
    eprintln!(
      "Index out of range: {index} (have {} groups).",
      config.search_folders.len()
    );
    return;
  }

  let removed = config.search_folders.remove(index);

  if let Err(e) = save(config) {
    eprintln!("ERROR Error saving config: {e}");
    return;
  }
  eprintln!("Deleted search group \"{removed}\".");
}

// --- Pinned Repositories ---

pub fn get_pinned_repos(config: &Config) -> Vec<String> {
  config
    .pins
    .repositories
    .iter()
    .map(|raw| {
      path::normalize(raw).unwrap_or_else(|e| {
        eprintln!("ERROR Error parsing pinned path \"{raw}\": {e}");
        raw.clone()
      })
    })
    .collect()
}

pub fn add_pinned_repo(config: &mut Config, repo_path: &str) {
  let normalized = match path::normalize(repo_path) {
    Ok(n) => n,
    Err(e) => {
      eprintln!("ERROR Error parsing pinned path \"{repo_path}\": {e}");
      return;
    }
  };

  let already_exists = config
    .pins
    .repositories
    .iter()
    .any(|r| path::normalize(r).unwrap_or_default() == normalized);

  if already_exists {
    eprintln!("Pinned repo already exists: \"{repo_path}\".");
    return;
  }

  config.pins.repositories.push(repo_path.to_string());

  if let Err(e) = save(config) {
    eprintln!("ERROR Error saving config: {e}");
    return;
  }
  eprintln!("Added pinned repository \"{repo_path}\".");
}

pub fn delete_pinned_repo(config: &mut Config, repo_path: &str) {
  let normalized = path::normalize(repo_path).unwrap_or_default();

  let original_len = config.pins.repositories.len();
  config
    .pins
    .repositories
    .retain(|r| path::normalize(r).unwrap_or_default() != normalized);

  if config.pins.repositories.len() == original_len {
    eprintln!("Pinned repo not found: \"{repo_path}\".");
    return;
  }

  if let Err(e) = save(config) {
    eprintln!("ERROR Error saving config: {e}");
    return;
  }
  eprintln!("Deleted pinned repository \"{repo_path}\".");
}

// --- Global Pinned Branches ---

pub fn is_branch_globally_pinned(config: &Config, branch: &str) -> bool {
  config.pins.branches.global.contains(&branch.to_string())
}

pub fn add_global_pinned_branch(config: &mut Config, branch: &str) {
  if config.pins.branches.global.contains(&branch.to_string()) {
    eprintln!("Branch \"{branch}\" is already protected.");
    return;
  }

  config.pins.branches.global.push(branch.to_string());

  if let Err(e) = save(config) {
    eprintln!("ERROR Error saving config: {e}");
    return;
  }
  eprintln!("Added protected branch \"{branch}\".");
}

pub fn delete_global_pinned_branch(config: &mut Config, branch: &str) {
  let original_len = config.pins.branches.global.len();
  config.pins.branches.global.retain(|b| b != branch);

  if config.pins.branches.global.len() == original_len {
    eprintln!("Branch \"{branch}\" is not protected.");
    return;
  }

  if let Err(e) = save(config) {
    eprintln!("ERROR Error saving config: {e}");
    return;
  }
  eprintln!("Removed protected branch \"{branch}\".");
}

// --- Per-Repo Pinned Branches ---

pub fn is_branch_pinned_for_repo(config: &Config, repo_id: &str, branch: &str) -> bool {
  config
    .pins
    .branches
    .repositories
    .get(repo_id)
    .map(|branches| branches.contains(&branch.to_string()))
    .unwrap_or(false)
}

pub fn add_repo_pinned_branch(config: &mut Config, repo_id: &str, branch: &str) {
  let branches = config
    .pins
    .branches
    .repositories
    .entry(repo_id.to_string())
    .or_default();

  if branches.contains(&branch.to_string()) {
    eprintln!("Branch \"{branch}\" already pinned for repo \"{repo_id}\".");
    return;
  }

  branches.push(branch.to_string());

  if let Err(e) = save(config) {
    eprintln!("ERROR Error saving config: {e}");
    return;
  }
  eprintln!("Added pinned branch \"{branch}\" for repo \"{repo_id}\".");
}

pub fn delete_repo_pinned_branch(config: &mut Config, repo_id: &str, branch: &str) {
  let branches = match config.pins.branches.repositories.get_mut(repo_id) {
    Some(b) => b,
    None => {
      eprintln!("No pinned branches found for repo \"{repo_id}\".");
      return;
    }
  };

  let original_len = branches.len();
  branches.retain(|b| b != branch);

  if branches.len() == original_len {
    eprintln!("Branch \"{branch}\" not pinned for repo \"{repo_id}\".");
    return;
  }

  if branches.is_empty() {
    config.pins.branches.repositories.remove(repo_id);
  }

  if let Err(e) = save(config) {
    eprintln!("ERROR Error saving config: {e}");
    return;
  }
  eprintln!("Deleted pinned branch \"{branch}\" for repo \"{repo_id}\".");
}

// --- Aliases ---

pub fn add_alias(config: &mut Config, short: &str, expanded: &str) {
  config
    .aliases
    .insert(short.to_string(), expanded.to_string());

  if let Err(e) = save(config) {
    eprintln!("ERROR Error saving config: {e}");
    return;
  }
  eprintln!("Added alias \"{short}\" -> \"{expanded}\".");
}

pub fn delete_alias(config: &mut Config, short: &str) {
  if config.aliases.remove(short).is_none() {
    eprintln!("Alias not found: \"{short}\".");
    return;
  }

  if let Err(e) = save(config) {
    eprintln!("ERROR Error saving config: {e}");
    return;
  }
  eprintln!("Deleted alias \"{short}\".");
}