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
pub mod args;
pub mod config;
pub mod remote_ref;
pub mod simple_glob;

use std::collections::{HashMap, HashSet};
use std::process::{Command, Stdio};

use anyhow::{Context, Result};
use git2::{BranchType, Config, Direction, Repository};
use log::*;

use crate::args::{Category, DeleteFilter};
use crate::config::ConfigValue;
use crate::remote_ref::{get_fetch_remote_ref, get_push_remote_ref};
use crate::simple_glob::{expand_refspec, ExpansionSide};

pub fn git(args: &[&str]) -> Result<()> {
    info!("> git {}", args.join(" "));
    let exit_status = Command::new("git").args(args).status()?;
    if !exit_status.success() {
        Err(std::io::Error::from_raw_os_error(exit_status.code().unwrap_or(-1)).into())
    } else {
        Ok(())
    }
}

fn git_output(args: &[&str]) -> Result<String> {
    info!("> git {}", args.join(" "));
    let output = Command::new("git")
        .args(args)
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .output()?;
    if !output.status.success() {
        return Err(std::io::Error::from_raw_os_error(output.status.code().unwrap_or(-1)).into());
    }

    let str = std::str::from_utf8(&output.stdout)?.trim();
    Ok(str.to_string())
}

fn is_merged(base: &str, branch: &str) -> Result<bool> {
    let range = format!("{}...{}", base, branch);
    // Is there any revs that are not applied to the base in the branch?
    let output = git_output(&[
        "rev-list",
        "--cherry-pick",
        "--right-only",
        "--no-merges",
        "-n1",
        &range,
    ])?;

    // empty output means there aren't any revs that are not applied to the base.
    if output.is_empty() {
        Ok(true)
    } else {
        Ok(false)
    }
}

/// Source: https://stackoverflow.com/a/56026209
fn is_squash_merged(base: &str, branch: &str) -> Result<bool> {
    let merge_base = git_output(&["merge-base", base, branch])?;
    let tree = git_output(&["rev-parse", &format!("{}^{{tree}}", branch)])?;
    let dangling_commit = git_output(&[
        "commit-tree",
        &tree,
        "-p",
        &merge_base,
        "-m",
        "git-trim: squash merge test",
    ])?;
    is_merged(base, &dangling_commit)
}

#[derive(Default, Eq, PartialEq, Debug)]
pub struct MergedOrGone {
    // local branches
    pub merged_locals: HashSet<String>,
    pub gone_locals: HashSet<String>,

    pub kept_back_locals: HashSet<String>,

    /// remote refs
    pub merged_remotes: HashSet<String>,
    pub gone_remotes: HashSet<String>,
}

impl MergedOrGone {
    pub fn adjust_not_to_detach(&mut self, repo: &Repository) -> Result<()> {
        if repo.head_detached()? {
            return Ok(());
        }
        let head = repo.head()?;
        let head_name = head.name().context("non-utf8 head ref name")?;
        assert!(head_name.starts_with("refs/heads/"));
        let head_name = &head_name["refs/heads/".len()..];

        if self.merged_locals.contains(head_name) {
            self.merged_locals.remove(head_name);
            self.kept_back_locals.insert(head_name.to_string());
        }
        if self.gone_locals.contains(head_name) {
            self.gone_locals.remove(head_name);
            self.kept_back_locals.insert(head_name.to_string());
        }
        Ok(())
    }

    pub fn print_summary(&self, filter: &DeleteFilter) {
        fn print(branches: &HashSet<String>, filter: &DeleteFilter, category: Category) {
            if filter.contains(&category) && !branches.is_empty() {
                println!("Delete {}:", category);
                for branch in branches {
                    println!("  {}", branch);
                }
            } else if !branches.is_empty() {
                println!("Skip {}:", category);
                for branch in branches {
                    println!("  {}", branch);
                }
            }
        }
        print(&self.merged_locals, filter, Category::MergedLocal);
        print(&self.merged_remotes, filter, Category::MergedRemote);

        if !self.kept_back_locals.is_empty() {
            println!("Kept back not to become detached HEAD:");
            for branch in &self.kept_back_locals {
                println!("  {}", branch);
            }
        }

        print(&self.gone_locals, filter, Category::GoneLocal);
        print(&self.gone_remotes, filter, Category::GoneRemote);
    }

    pub fn get_local_branches_to_delete(&self, filter: &DeleteFilter) -> Vec<&str> {
        let mut result = Vec::new();
        if filter.contains(&Category::MergedLocal) {
            result.extend(self.merged_locals.iter().map(String::as_str))
        }
        if filter.contains(&Category::GoneLocal) {
            result.extend(self.gone_locals.iter().map(String::as_str))
        }
        result
    }

    pub fn get_remote_refs_to_delete(&self, filter: &DeleteFilter) -> Vec<&str> {
        let mut result = Vec::new();
        if filter.contains(&Category::MergedRemote) {
            result.extend(self.merged_remotes.iter().map(String::as_str))
        }
        if filter.contains(&Category::GoneLocal) {
            result.extend(self.gone_remotes.iter().map(String::as_str))
        }
        result
    }
}

pub fn get_merged_or_gone(repo: &Repository, config: &Config, base: &str) -> Result<MergedOrGone> {
    let base_remote_ref = resolve_config_base_ref(repo, config, base)?;
    let mut result = MergedOrGone::default();
    for branch in repo.branches(Some(BranchType::Local))? {
        let (branch, _) = branch?;
        let branch_name = branch.name()?.context("non-utf8 branch name")?;
        debug!("Branch: {:?}", branch.name()?);
        if let ConfigValue::Implicit(_) = config::get_remote(config, branch_name)? {
            debug!(
                "Skip: the branch doesn't have a tracking remote: {:?}",
                branch_name
            );
            continue;
        }
        if let Some(remote_ref) = get_fetch_remote_ref(repo, config, branch_name)? {
            if Some(&remote_ref) == Some(&base_remote_ref) {
                debug!("Skip: the branch is the base: {:?}", branch_name);
                continue;
            }
        }
        let reference = branch.get();
        if reference.symbolic_target().is_some() {
            debug!("Skip: the branch is a symbolic ref: {:?}", branch_name);
            continue;
        }
        let merged = is_merged(&base_remote_ref, branch_name)?
            || is_squash_merged(&base_remote_ref, branch_name)?;
        let fetch = get_fetch_remote_ref(repo, config, branch_name)?;
        let push = get_push_remote_ref(repo, config, branch_name)?;
        trace!("merged: {}", merged);
        trace!("fetch: {:?}", fetch);
        trace!("push: {:?}", push);
        match (fetch, push) {
            (Some(_), Some(remote_ref)) if merged => {
                debug!("merged local, merged remote: the branch is merged, but forgot to delete");
                result.merged_locals.insert(branch_name.to_string());
                result.merged_remotes.insert(remote_ref);
            }
            (Some(_), Some(_)) => {
                debug!("skip: live branch. not merged, not gone");
            }

            // `git branch`'s shows `%(upstream)` as s `%(push)` fallback if there isn't a specified push remote.
            // But our `get_push_remote_ref` doesn't.
            (Some(fetch_ref), None) if merged => {
                debug!("merged local, merged remote: the branch is merged, but forgot to delete");
                // TODO: it might be a long running branch like 'develop' in a git-flow
                result.merged_locals.insert(branch_name.to_string());
                result.merged_remotes.insert(fetch_ref);
            }
            (Some(_), None) => {
                debug!("skip: it might be a long running branch like 'develop' in a git-flow");
            }

            (None, Some(remote_ref)) if merged => {
                debug!("merged remote: it might be a long running branch like 'develop' which is once pushed to the personal repo in the triangular workflow, but the branch is merged on the upstream");
                result.merged_remotes.insert(remote_ref);
            }
            (None, Some(remote_ref)) => {
                debug!("gone remote: it might be a long running branch like 'develop' which is once pushed to the personal repo in the triangular workflow, but the branch is gone on the upstream");
                result.gone_remotes.insert(remote_ref);
            }

            (None, None) if merged => {
                debug!("merged local: the branch is merged, and deleted");
                result.merged_locals.insert(branch_name.to_string());
            }
            (None, None) => {
                debug!("gone local: the branch is not merged but gone somehow");
                result.gone_locals.insert(branch_name.to_string());
            }
        }
    }

    Ok(result)
}

fn resolve_config_base_ref(repo: &Repository, config: &Config, base: &str) -> Result<String> {
    // find "master -> refs/remotes/origin/master"
    if let Some(remote_ref) = get_fetch_remote_ref(repo, config, base)? {
        trace!("Found fetch remote ref for: {}, {}", base, remote_ref);
        return Ok(remote_ref);
    }

    // match "origin/master -> refs/remotes/origin/master"
    if let Ok(remote_ref) = repo.find_reference(&format!("refs/remotes/{}", base)) {
        let refname = remote_ref.name().context("non-utf8 reference name")?;
        trace!("Found remote ref for: {}, {}", base, refname);
        return Ok(refname.to_string());
    }

    trace!("Not found remote refs. fallback: {}", base);
    Ok(repo
        .find_reference(base)?
        .name()
        .context("non-utf8 ref")?
        .to_string())
}

pub fn delete_local_branches(repo: &Repository, branches: &[&str], dry_run: bool) -> Result<()> {
    if branches.is_empty() {
        return Ok(());
    }
    let mut args = vec!["branch", "--delete", "--force"];
    args.extend(branches);

    let detach_to = if repo.head_detached()? {
        None
    } else {
        let head = repo.head()?;
        let head_refname = head.name().context("non-utf8 head ref name")?;
        assert!(head_refname.starts_with("refs/heads/"));
        let head_name = &head_refname["refs/heads/".len()..];
        if branches.contains(&head_name) {
            Some(head)
        } else {
            None
        }
    };

    if dry_run {
        if let Some(head) = detach_to {
            let head_refname = head.name().context("non-utf8 head ref name")?;
            info!("> git checkout {} (dry-run)", head_refname);

            println!("Note: switching to '{}' (dry run)", head_refname);
            println!("You are in 'detached HED' state... blah blah...");
            let commit = head.peel_to_commit()?;
            let message = commit.message().context("non-utf8 head ref name")?;
            println!(
                "HEAD is now at {} {} (dry run)",
                &commit.id().to_string()[..7],
                message.lines().next().unwrap_or_default()
            );
        }
        for branch in branches {
            info!("> git {} (dry-run)", args.join(" "));
            println!("Delete branch {} (dry run).", branch);
        }
    } else {
        if let Some(head) = detach_to {
            let head_refname = head.name().context("non-utf8 head ref name")?;
            git(&["checkout", head_refname])?;
        }
        git(&args)?;
    }
    Ok(())
}

pub fn delete_remote_branches(
    repo: &Repository,
    remote_refs: &[&str],
    dry_run: bool,
) -> Result<()> {
    if remote_refs.is_empty() {
        return Ok(());
    }
    let mut per_remote = HashMap::new();
    for remote_ref in remote_refs {
        let (remote_name, ref_on_remote) = get_remote_name_and_ref_on_remote(repo, remote_ref)?;
        let entry = per_remote.entry(remote_name).or_insert_with(Vec::new);
        entry.push(ref_on_remote);
    }
    let mut command = vec!["push", "--delete"];
    if dry_run {
        command.push("--dry-run");
    }
    for (remote_name, remote_refnames) in per_remote.iter() {
        let mut args = command.clone();
        args.push(remote_name);
        args.extend(remote_refnames.iter().map(String::as_str));
        git(&args)?;
    }
    Ok(())
}

fn get_remote_name_and_ref_on_remote(
    repo: &Repository,
    remote_ref: &str,
) -> Result<(String, String)> {
    assert!(remote_ref.starts_with("refs/remotes/"));
    for remote_name in repo.remotes()?.iter() {
        let remote_name = remote_name.context("non-utf8 remote name")?;
        let remote = repo.find_remote(&remote_name)?;
        if let Some(expanded) =
            expand_refspec(&remote, remote_ref, Direction::Fetch, ExpansionSide::Left)?
        {
            return Ok((
                remote.name().context("non-utf8 remote name")?.to_string(),
                expanded,
            ));
        }
    }
    unreachable!("matching refspec is not found");
}