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
428
429
430
431
432
433
434
435
436
437
/*
 * Copyright 2020 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::env;
use std::fmt;
use std::fs::File;
use std::path::{Path, PathBuf};
use std::process::Command;

// Define this environment variable to print all executed git commands to stderr
const GIT_BONSAI_DEBUG: &str = "GB_DEBUG";

// If a branch is checked out in a separate worktree, then `git branch` prefixes it with this
// string
const WORKTREE_BRANCH_PREFIX: &str = "+ ";

#[derive(Debug, PartialEq, Eq)]
pub enum GitError {
    FailedToRunGit,
    CommandFailed { exit_code: i32 },
    TerminatedBySignal,
    UnexpectedOutput(String),
}

impl fmt::Display for GitError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            GitError::FailedToRunGit => {
                write!(f, "Failed to run git")
            }
            GitError::CommandFailed { exit_code: e } => {
                write!(f, "Command exited with code {}", e)
            }
            GitError::TerminatedBySignal => {
                write!(f, "Terminated by signal")
            }
            GitError::UnexpectedOutput(message) => {
                write!(f, "UnexpectedOutput: {}", message)
            }
        }
    }
}

/**
 * Restores the current git branch when dropped
 * Assumes we are on a real branch
 */
pub struct BranchRestorer<'a> {
    repository: &'a Repository,
    branch: String,
}

impl BranchRestorer<'_> {
    pub fn new(repo: &Repository) -> BranchRestorer {
        let current_branch = repo.get_current_branch().expect("Can't get current branch");
        BranchRestorer {
            repository: repo,
            branch: current_branch,
        }
    }
}

impl Drop for BranchRestorer<'_> {
    fn drop(&mut self) {
        if let Err(_x) = self.repository.checkout(&self.branch) {
            println!("Failed to restore original branch {}", self.branch);
        }
    }
}

pub struct Repository {
    pub path: PathBuf,
}

impl Repository {
    pub fn new(path: &Path) -> Repository {
        Repository {
            path: path.to_path_buf(),
        }
    }

    #[allow(dead_code)]
    pub fn clone(path: &Path, url: &str) -> Result<Repository, GitError> {
        let repo = Repository::new(path);
        repo.git("clone", &[url, path.to_str().unwrap()])?;
        Ok(repo)
    }

    pub fn git(&self, subcommand: &str, args: &[&str]) -> Result<String, GitError> {
        let mut cmd = Command::new("git");
        cmd.current_dir(&self.path);
        cmd.env("LANG", "C");
        cmd.arg(subcommand);
        for arg in args {
            cmd.arg(arg);
        }
        if env::var(GIT_BONSAI_DEBUG).is_ok() {
            eprintln!(
                "DEBUG: pwd={}: git {} {}",
                self.path.to_str().unwrap(),
                subcommand,
                args.join(" ")
            );
        }
        let output = match cmd.output() {
            Ok(x) => x,
            Err(_x) => {
                println!("Failed to execute process");
                return Err(GitError::FailedToRunGit);
            }
        };
        if !output.status.success() {
            // TODO: store error message in GitError
            println!(
                "{}",
                String::from_utf8(output.stderr).expect("Failed to decode command stderr")
            );
            return match output.status.code() {
                Some(code) => Err(GitError::CommandFailed { exit_code: code }),
                None => Err(GitError::TerminatedBySignal),
            };
        }
        let out = String::from_utf8(output.stdout).expect("Failed to decode command stdout");
        Ok(out)
    }

    pub fn fetch(&self) -> Result<(), GitError> {
        self.git("fetch", &["--prune"])?;
        Ok(())
    }

    /// Reads config keys defined with `git config --add <key> <value>`
    pub fn get_config_keys(&self, key: &str) -> Result<Vec<String>, GitError> {
        let stdout = match self.git("config", &["--get-all", key]) {
            Ok(x) => x,
            Err(x) => match x {
                GitError::CommandFailed { exit_code: 1 } => {
                    // Happens when reading a non-existing key
                    return Ok([].to_vec());
                }
                x => {
                    return Err(x);
                }
            },
        };

        let values: Vec<String> = stdout.lines().map(|x| x.into()).collect();
        Ok(values)
    }

    pub fn set_config_key(&self, key: &str, value: &str) -> Result<(), GitError> {
        self.git("config", &[key, value])?;
        Ok(())
    }

    pub fn find_default_branch(&self) -> Result<String, GitError> {
        let stdout = self.git("ls-remote", &["--symref", "origin", "HEAD"])?;
        /* Output looks like this:
         *
         * ref: refs/heads/master\tHEAD
         * 960389f1c69e8b9c3fe06d29866d0d193375a6cb\tHEAD
         *
         * We want to extra "master" from the first line
         */
        let line = stdout.lines().next().ok_or_else(|| {
            GitError::UnexpectedOutput("ls-remote returned an empty string".to_string())
        })?;

        let line = line
            .strip_prefix("ref: refs/heads/")
            .ok_or_else(|| GitError::UnexpectedOutput("missing prefix".to_string()))?;

        let line = line
            .strip_suffix("\tHEAD")
            .ok_or_else(|| GitError::UnexpectedOutput("missing suffix".to_string()))?;

        Ok(line.to_string())
    }

    pub fn list_branches(&self) -> Result<Vec<String>, GitError> {
        self.list_branches_internal(&[])
    }

    pub fn list_branches_with_sha1s(&self) -> Result<Vec<(String, String)>, GitError> {
        let mut list: Vec<(String, String)> = Vec::new();

        let lines = self.list_branches_internal(&["-v"])?;

        for line in lines {
            let mut it = line.split_whitespace();
            let branch = it.next().unwrap().to_string();
            let sha1 = it.next().unwrap().to_string();
            list.push((branch, sha1));
        }
        Ok(list)
    }

    fn list_branches_internal(&self, args: &[&str]) -> Result<Vec<String>, GitError> {
        let mut branches: Vec<String> = Vec::new();

        let stdout = self.git("branch", args)?;

        for line in stdout.lines() {
            if line.starts_with(WORKTREE_BRANCH_PREFIX) {
                continue;
            }
            let branch = line.get(2..).expect("Invalid branch name");
            branches.push(branch.to_string());
        }
        Ok(branches)
    }

    pub fn list_branches_containing(&self, commit: &str) -> Result<Vec<String>, GitError> {
        self.list_branches_internal(&["--contains", commit])
    }

    pub fn list_tracking_branches(&self) -> Result<Vec<String>, GitError> {
        let mut branches: Vec<String> = Vec::new();

        let lines = self.list_branches_internal(&["-vv"])?;

        for line in lines {
            if line.contains("[origin/") && !line.contains(": gone]") {
                let branch = line.split(' ').next();
                branches.push(branch.unwrap().to_string());
            }
        }
        Ok(branches)
    }

    pub fn checkout(&self, branch: &str) -> Result<(), GitError> {
        self.git("checkout", &[branch])?;
        Ok(())
    }

    pub fn delete_branch(&self, branch: &str) -> Result<(), GitError> {
        self.git("branch", &["-D", branch])?;
        Ok(())
    }

    pub fn get_current_branch(&self) -> Option<String> {
        let stdout = self.git("branch", &[]);
        if stdout.is_err() {
            return None;
        }
        for line in stdout.unwrap().lines() {
            if line.starts_with('*') {
                return Some(line[2..].to_string());
            }
        }
        None
    }

    pub fn update_branch(&self) -> Result<(), GitError> {
        let out = self.git("merge", &["--ff-only"])?;
        println!("{}", out);
        Ok(())
    }

    pub fn has_changes(&self) -> Result<bool, GitError> {
        let out = self.git("status", &["--short"])?;
        Ok(!out.is_empty())
    }

    #[allow(dead_code)]
    pub fn get_current_sha1(&self) -> Result<String, GitError> {
        let out = self.git("show", &["--no-patch", "--oneline"])?;
        let sha1 = out.split(' ').next().unwrap().to_string();
        Ok(sha1)
    }
}

// Used by test code
#[allow(dead_code)]
pub fn create_test_repository(path: &Path) -> Repository {
    let repo = Repository::new(path);

    repo.git("init", &[]).expect("init failed");
    repo.git("config", &["user.name", "test"])
        .expect("setting username failed");
    repo.git("config", &["user.email", "test@example.com"])
        .expect("setting email failed");

    // Create a file so that we have more than the start commit
    File::create(path.join("f")).unwrap();
    repo.git("add", &["."]).expect("add failed");
    repo.git("commit", &["-m", "init"]).expect("commit failed");

    repo
}

#[cfg(test)]
mod tests {
    extern crate assert_fs;

    use super::*;
    use std::fs;

    #[test]
    fn get_current_branch() {
        let dir = assert_fs::TempDir::new().unwrap();
        let repo = create_test_repository(dir.path());
        assert_eq!(repo.get_current_branch().unwrap(), "master");

        repo.git("checkout", &["-b", "test"])
            .expect("create branch failed");
        assert_eq!(repo.get_current_branch().unwrap(), "test");
    }

    #[test]
    fn delete_branch() {
        // GIVEN a repository with a test branch containing unique content
        let dir = assert_fs::TempDir::new().unwrap();
        let repo = create_test_repository(dir.path());
        assert_eq!(repo.get_current_branch().unwrap(), "master");

        repo.git("checkout", &["-b", "test"]).unwrap();
        File::create(dir.path().join("test")).unwrap();
        repo.git("add", &["test"]).unwrap();
        repo.git("commit", &["-m", &format!("Create file")])
            .unwrap();

        repo.checkout("master").unwrap();

        // WHEN I call delete_branch
        let result = repo.delete_branch("test");

        // THEN the branch is deleted
        assert_eq!(result, Ok(()));

        // AND only the master branch remains
        assert_eq!(repo.list_branches().unwrap(), &["master"]);
    }

    #[test]
    fn list_branches_with_sha1s() {
        // GIVEN a repository with two branches
        let dir = assert_fs::TempDir::new().unwrap();
        let repo = create_test_repository(dir.path());

        repo.git("checkout", &["-b", "test"]).unwrap();
        File::create(dir.path().join("test")).unwrap();
        repo.git("add", &["test"]).unwrap();
        repo.git("commit", &["-m", &format!("Create file")])
            .unwrap();

        // WHEN I list branches with sha1
        let branches_with_sha1 = repo.list_branches_with_sha1s().unwrap();

        // THEN the list contains two entries
        assert_eq!(branches_with_sha1.len(), 2);

        // AND when switching to each branch, the current sha1 is the expected one
        for (branch, sha1) in branches_with_sha1 {
            repo.git("checkout", &[&branch]).unwrap();
            assert_eq!(repo.get_current_sha1().unwrap(), sha1);
        }
    }

    #[test]
    fn list_branches_skip_worktree_branches() {
        // GIVEN a source repository with two branches
        let tmp_dir = assert_fs::TempDir::new().unwrap();

        let source_path = tmp_dir.path().join("source");
        fs::create_dir_all(&source_path).unwrap();
        let source_repo = create_test_repository(&source_path);
        source_repo.git("branch", &["topic1"]).unwrap();

        // AND a clone of this repository
        let clone_path = tmp_dir.path().join("clone");
        fs::create_dir_all(&clone_path).unwrap();
        let clone_repo = Repository::clone(&clone_path, &source_path.to_str().unwrap()).unwrap();

        // with the topic1 branch checked-out in a separate worktree
        let worktree_dir = assert_fs::TempDir::new().unwrap();
        let worktree_path_str = worktree_dir.path().to_str().unwrap();
        clone_repo
            .git("worktree", &["add", worktree_path_str, "topic1"])
            .unwrap();

        // WHEN I list branches
        let branches = clone_repo.list_branches().unwrap();

        // THEN it does not list worktree branches
        assert_eq!(branches.len(), 1);
        assert_eq!(branches, &["master"]);
    }

    #[test]
    fn find_default_branch_happy_path() {
        // GIVEN a source repository
        let tmp_dir = assert_fs::TempDir::new().unwrap();
        let source_path = tmp_dir.path().join("source");
        fs::create_dir_all(&source_path).unwrap();
        create_test_repository(&source_path);

        // AND a clone of this repository
        let clone_path = tmp_dir.path().join("clone");
        fs::create_dir_all(&clone_path).unwrap();
        let clone_repo = Repository::clone(&clone_path, &source_path.to_str().unwrap()).unwrap();

        // WHEN I call find_default_branch() on the clone
        let branch = clone_repo.find_default_branch();

        // THEN it finds the default branch name
        assert_eq!(branch, Ok("master".to_string()));
    }

    #[test]
    fn find_default_branch_no_remote() {
        // GIVEN a repository without a remote
        let tmp_dir = assert_fs::TempDir::new().unwrap();
        let repo = create_test_repository(&tmp_dir.path());

        // WHEN I call find_default_branch()
        let branch = repo.find_default_branch();

        // THEN it fails
        assert_eq!(branch, Err(GitError::CommandFailed { exit_code: 128 }));
    }
}