git-bonsai 0.3.0

Command-line tool to clean the branches of your git garden
Documentation
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
/*
 * Copyright 2021 Aurélien Gâteau <mail@agateau.com>
 *
 * This file is part of git-bonsai.
 *
 * Git-bonsai is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */
use std::collections::{HashMap, HashSet};
use std::convert::From;
use std::fmt;
use std::path::PathBuf;

use crate::appui::{AppUi, BranchToDeleteInfo};
use crate::batchappui::BatchAppUi;
use crate::cliargs::CliArgs;
use crate::git::{BranchRestorer, GitError, Repository};
use crate::interactiveappui::InteractiveAppUi;

pub static DEFAULT_BRANCH_CONFIG_KEY: &str = "git-bonsai.default-branch";

#[derive(Debug, PartialEq, Eq)]
pub enum AppError {
    Git(GitError),
    UnsafeDelete,
    InterruptedByUser,
}

impl From<GitError> for AppError {
    fn from(error: GitError) -> Self {
        AppError::Git(error)
    }
}

impl fmt::Display for AppError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            AppError::Git(error) => error.fmt(f),
            AppError::UnsafeDelete => {
                write!(f, "This branch cannot be deleted safely")
            }
            AppError::InterruptedByUser => {
                write!(f, "Interrupted")
            }
        }
    }
}

pub struct App {
    repo: Repository,
    protected_branches: HashSet<String>,
    ui: Box<dyn AppUi>,
    fetch: bool,
}

impl App {
    pub fn new(args: &CliArgs, ui: Box<dyn AppUi>, repo_dir: &str) -> App {
        let repo = Repository::new(&PathBuf::from(repo_dir));

        let mut branches: HashSet<String> = HashSet::new();
        for branch in repo
            .get_config_keys("git-bonsai.protected-branches")
            .unwrap()
        {
            branches.insert(branch.to_string());
        }
        for branch in &args.excluded {
            branches.insert(branch.to_string());
        }
        App {
            repo,
            protected_branches: branches,
            ui,
            fetch: !args.no_fetch,
        }
    }

    // Used by test code
    #[allow(dead_code)]
    pub fn get_protected_branches(&self) -> HashSet<String> {
        self.protected_branches.clone()
    }

    pub fn is_working_tree_clean(&self) -> bool {
        if self.repo.get_current_branch().is_none() {
            self.ui.log_error("No current branch");
            return false;
        }
        match self.repo.has_changes() {
            Ok(has_changes) => {
                if has_changes {
                    self.ui
                        .log_error("Can't work in a tree with uncommitted changes");
                    return false;
                }
                true
            }
            Err(_) => {
                self.ui.log_error("Failed to get working tree status");
                false
            }
        }
    }

    /// Ask git the name of the default branch, and store the result in git config. If we can't
    /// find it using git, fallback to asking the user.
    pub fn find_default_branch_from_git(&self) -> Result<String, AppError> {
        self.ui.log_info("Determining repository default branch");
        let branch = match self.repo.find_default_branch() {
            Ok(x) => x,
            Err(err) => {
                self.ui.log_error(&format!(
                    "Can't determine default branch: {}",
                    &err.to_string()
                ));
                return self.find_default_branch_from_user();
            }
        };
        self.repo
            .set_config_key(DEFAULT_BRANCH_CONFIG_KEY, &branch)?;
        self.ui.log_info(&format!("Default branch is {}", branch));
        Ok(branch)
    }

    /// Ask the user the name of the default branch, and store the result in git config
    pub fn find_default_branch_from_user(&self) -> Result<String, AppError> {
        let branch = match self.ui.select_default_branch(&self.repo.list_branches()?) {
            Some(x) => x,
            None => {
                return Err(AppError::InterruptedByUser);
            }
        };
        self.repo
            .set_config_key(DEFAULT_BRANCH_CONFIG_KEY, &branch)?;
        Ok(branch)
    }

    /// Return the default branch stored in git config, if any
    pub fn get_default_branch(&self) -> Result<Option<String>, AppError> {
        match self.repo.get_config_keys(DEFAULT_BRANCH_CONFIG_KEY) {
            Ok(values) => Ok(if values.len() != 1 {
                None
            } else {
                Some(values[0].clone())
            }),
            Err(x) => Err(AppError::Git(x)),
        }
    }

    pub fn fetch_changes(&self) -> Result<(), AppError> {
        self.ui.log_info("Fetching changes");
        self.repo.fetch()?;
        Ok(())
    }

    pub fn update_tracking_branches(&self) -> Result<(), AppError> {
        let branches = match self.repo.list_tracking_branches() {
            Ok(x) => x,
            Err(x) => {
                self.ui.log_error("Failed to list tracking branches");
                return Err(AppError::Git(x));
            }
        };

        let _restorer = BranchRestorer::new(&self.repo);
        for branch in branches {
            self.ui.log_info(&format!("Updating {}", branch));
            if let Err(x) = self.repo.checkout(&branch) {
                self.ui.log_error("Failed to checkout branch");
                return Err(AppError::Git(x));
            }
            if let Err(_x) = self.repo.update_branch() {
                self.ui.log_warning("Failed to update branch");
                // This is not wrong, it can happen if the branches have diverged
                // let's continue
            }
        }
        Ok(())
    }
    pub fn remove_merged_branches(&self) -> Result<(), AppError> {
        let to_delete = self.get_deletable_branches()?;

        if to_delete.is_empty() {
            self.ui.log_info("No deletable branches");
            return Ok(());
        }

        let selected_branches = self.ui.select_branches_to_delete(&to_delete);
        if selected_branches.is_empty() {
            return Ok(());
        }

        let branch_names: Vec<String> = selected_branches
            .iter()
            .map(|x| x.name.to_string())
            .collect();
        self.delete_branches(&branch_names[..])?;
        Ok(())
    }

    /// Delete the specified branches, takes care of checking out another branch if we are deleting
    /// the current one
    fn delete_branches(&self, branches: &[String]) -> Result<(), AppError> {
        let current_branch = self.repo.get_current_branch().unwrap();

        let mut current_branch_deleted = false;
        let default_branch = self.get_default_branch().unwrap().unwrap();

        match self.repo.checkout(&default_branch) {
            Ok(()) => (),
            Err(x) => {
                let msg = format!("Failed to switch to default branch '{}'", default_branch);
                self.ui.log_error(&msg);
                return Err(AppError::Git(x));
            }
        }

        for branch in branches {
            self.ui.log_info(&format!("Deleting {}", branch));

            if self.safe_delete_branch(branch).is_err() {
                self.ui.log_warning("Failed to delete branch");
            } else if *branch == current_branch {
                current_branch_deleted = true;
            }
        }

        if !current_branch_deleted {
            self.repo.checkout(&current_branch)?;
        }
        Ok(())
    }

    fn get_deletable_branches(&self) -> Result<Vec<BranchToDeleteInfo>, AppError> {
        let deletable_branches: Vec<BranchToDeleteInfo> = match self.repo.list_branches() {
            Ok(x) => x,
            Err(x) => {
                self.ui.log_error("Failed to list branches");
                return Err(AppError::Git(x));
            }
        }
        .iter()
        .filter(|&x| !self.protected_branches.contains(x))
        .map(|branch| {
            let contained_in: HashSet<String> = match self.repo.list_branches_containing(branch) {
                Ok(x) => x,
                Err(_x) => {
                    self.ui
                        .log_error(&format!("Failed to list branches containing {}", branch));
                    [].to_vec()
                }
            }
            .iter()
            .filter(|&x| x != branch)
            .cloned()
            .collect();

            BranchToDeleteInfo {
                name: branch.to_string(),
                contained_in,
            }
        })
        .filter(|x| !x.contained_in.is_empty())
        .collect();

        Ok(deletable_branches)
    }

    fn is_sha1_contained_in_another_branch(
        &self,
        sha1: &str,
        branches: &HashSet<String>,
    ) -> Result<bool, GitError> {
        for branch in self.repo.list_branches_containing(sha1).unwrap() {
            if !branches.contains(&branch) {
                return Ok(true);
            }
        }
        Ok(false)
    }

    pub fn do_delete_identical_branches(
        &self,
        sha1: &str,
        branch_set: &HashSet<String>,
    ) -> Result<(), AppError> {
        let unprotected_branch_set: HashSet<_> =
            branch_set.difference(&self.protected_branches).collect();
        if !self
            .is_sha1_contained_in_another_branch(sha1, branch_set)
            .unwrap()
        {
            let contains_protected_branches = unprotected_branch_set.len() < branch_set.len();
            let branches: Vec<String> = unprotected_branch_set
                .iter()
                .map(|x| x.to_string())
                .collect();
            if !contains_protected_branches {
                let selected_branches: Vec<String> = self
                    .ui
                    .select_identical_branches_to_delete_keep_one(&branches)
                    .iter()
                    .map(|x| x.to_string())
                    .collect();
                self.delete_branches(&selected_branches)?;
                return Ok(());
            }
        }
        if unprotected_branch_set.is_empty() {
            // Aliases are only protected branches, do nothing
            return Ok(());
        }
        let branches: Vec<String> = unprotected_branch_set
            .iter()
            .map(|x| x.to_string())
            .collect();
        let selected_branches: Vec<_> = self
            .ui
            .select_identical_branches_to_delete(&branches)
            .iter()
            .map(|x| x.to_string())
            .collect();
        self.delete_branches(&selected_branches)?;
        Ok(())
    }

    pub fn delete_identical_branches(&self) -> Result<(), AppError> {
        // Create a hashmap sha1 => set(branches)
        let mut branches_for_sha1: HashMap<String, HashSet<String>> = HashMap::new();

        match self.repo.list_branches_with_sha1s() {
            Ok(x) => x,
            Err(x) => {
                self.ui.log_error("Failed to list branches");
                return Err(AppError::Git(x));
            }
        }
        .iter()
        .for_each(|(branch, sha1)| {
            let branch_set = branches_for_sha1
                .entry(sha1.to_string())
                .or_insert_with(HashSet::<String>::new);
            branch_set.insert(branch.to_string());
        });

        // Delete identical branches if there are more than one for the same sha1
        for (sha1, branch_set) in branches_for_sha1 {
            if branch_set.len() == 1 {
                continue;
            }
            if let Err(x) = self.do_delete_identical_branches(&sha1, &branch_set) {
                self.ui.log_error("Failed to list branches");
                return Err(x);
            }
        }

        Ok(())
    }

    pub fn safe_delete_branch(&self, branch: &str) -> Result<(), AppError> {
        // A branch is only safe to delete if at least another branch contains it
        let contained_in = self.repo.list_branches_containing(branch).unwrap();
        if contained_in.len() < 2 {
            self.ui.log_error(&format!(
                "Not deleting {}, no other branches contain it",
                branch
            ));
            return Err(AppError::UnsafeDelete);
        }
        self.repo.delete_branch(branch)?;
        Ok(())
    }

    pub fn add_default_branch_to_protected_branches(&mut self) -> Result<(), AppError> {
        let default_branch = match self.get_default_branch()? {
            Some(x) => x,
            None => {
                if self.fetch {
                    self.find_default_branch_from_git()?
                } else {
                    self.find_default_branch_from_user()?
                }
            }
        };
        self.protected_branches.insert(default_branch);
        Ok(())
    }

    pub fn run(&mut self) -> Result<(), AppError> {
        self.add_default_branch_to_protected_branches()?;
        if self.fetch {
            self.fetch_changes()?;
        }

        self.update_tracking_branches()?;
        self.delete_identical_branches()?;
        self.remove_merged_branches()?;
        Ok(())
    }
}

pub fn run(args: CliArgs, dir: &str) -> i32 {
    let ui: Box<dyn AppUi> = match args.yes {
        false => Box::new(InteractiveAppUi {}),
        true => Box::new(BatchAppUi {}),
    };
    let mut app = App::new(&args, ui, dir);

    if !app.is_working_tree_clean() {
        return 1;
    }

    match app.run() {
        Ok(()) => 0,
        Err(_) => 1,
    }
}