|
1 | 1 | use commands::*;
|
2 | 2 | use error::Error;
|
3 | 3 | use options::*;
|
4 |
| -use std::io::{stdin, stdout, Read, Write}; |
| 4 | +use regex::Regex; |
| 5 | +use std::io::{stdin, stdout, Write}; |
5 | 6 |
|
6 | 7 | pub const COLUMN_SPACER_LENGTH: usize = 30;
|
7 | 8 |
|
@@ -42,84 +43,63 @@ impl Branches {
|
42 | 43 | println!("Updating remote {}", options.remote);
|
43 | 44 | run_command_with_no_output(&["git", "remote", "update", &options.remote, "--prune"]);
|
44 | 45 |
|
45 |
| - let merged_branches_regex = |
46 |
| - format!("\\*{branch}|\\s{branch}", branch = &options.base_branch); |
47 |
| - let merged_branches_filter = spawn_piped(&["grep", "-vE", &merged_branches_regex]); |
| 46 | + let merged_branches_regex = format!("^\\*?\\s*{}$", options.base_branch); |
| 47 | + let merged_branches_filter = Regex::new(&merged_branches_regex).unwrap(); |
48 | 48 | let merged_branches_cmd = run_command(&["git", "branch", "--merged"]);
|
| 49 | + let merged_branches_output = std::str::from_utf8(&merged_branches_cmd.stdout).unwrap(); |
49 | 50 |
|
50 |
| - { |
51 |
| - merged_branches_filter |
52 |
| - .stdin |
53 |
| - .unwrap() |
54 |
| - .write_all(&merged_branches_cmd.stdout) |
55 |
| - .unwrap(); |
56 |
| - } |
| 51 | + let merged_branches = merged_branches_output |
| 52 | + .lines() |
| 53 | + .fold(Vec::<String>::new(), |mut acc, line| { |
| 54 | + if !merged_branches_filter.is_match(line) { |
| 55 | + acc.push(line.trim().to_string()); |
| 56 | + } |
| 57 | + acc |
| 58 | + }); |
57 | 59 |
|
58 |
| - let mut merged_branches_output = String::new(); |
59 |
| - merged_branches_filter |
60 |
| - .stdout |
61 |
| - .unwrap() |
62 |
| - .read_to_string(&mut merged_branches_output) |
63 |
| - .unwrap(); |
64 |
| - let mut merged_branches = merged_branches_output.split('\n').map(|b| b.trim().into()); |
65 |
| - |
66 |
| - let local_branches_regex = |
67 |
| - format!("\\*{branch}|\\s{branch}", branch = &options.base_branch); |
68 |
| - let local_branches_filter = spawn_piped(&["grep", "-vE", &local_branches_regex]); |
| 60 | + let local_branches_regex = format!("^\\*?\\s*{}$", options.base_branch); |
| 61 | + let local_branches_filter = Regex::new(&local_branches_regex).unwrap(); |
69 | 62 | let local_branches_cmd = run_command(&["git", "branch"]);
|
70 |
| - |
71 |
| - { |
72 |
| - local_branches_filter |
73 |
| - .stdin |
74 |
| - .unwrap() |
75 |
| - .write_all(&local_branches_cmd.stdout) |
76 |
| - .unwrap(); |
77 |
| - } |
78 |
| - |
79 |
| - let mut local_branches_output = String::new(); |
80 |
| - local_branches_filter |
81 |
| - .stdout |
82 |
| - .unwrap() |
83 |
| - .read_to_string(&mut local_branches_output) |
84 |
| - .unwrap(); |
| 63 | + let local_branches_output = std::str::from_utf8(&local_branches_cmd.stdout).unwrap(); |
85 | 64 |
|
86 | 65 | let local_branches = local_branches_output
|
87 |
| - .split('\n') |
88 |
| - .map(|b| b.trim().into()) |
| 66 | + .lines() |
| 67 | + .fold(Vec::<String>::new(), |mut acc, line| { |
| 68 | + if !local_branches_filter.is_match(line) { |
| 69 | + acc.push(line.trim().to_string()); |
| 70 | + } |
| 71 | + acc |
| 72 | + }) |
| 73 | + .iter() |
89 | 74 | .filter(|branch| !options.ignored_branches.contains(branch))
|
| 75 | + .cloned() |
90 | 76 | .collect::<Vec<String>>();
|
91 | 77 |
|
92 |
| - let remote_branches_regex = format!("(HEAD|{})", &options.base_branch); |
93 |
| - let remote_branches_filter = spawn_piped(&["grep", "-vE", &remote_branches_regex]); |
| 78 | + let remote_branches_regex = format!("\\b(HEAD|{})\\b", &options.base_branch); |
| 79 | + let remote_branches_filter = Regex::new(&remote_branches_regex).unwrap(); |
94 | 80 | let remote_branches_cmd = run_command(&["git", "branch", "-r"]);
|
| 81 | + let remote_branches_output = std::str::from_utf8(&remote_branches_cmd.stdout).unwrap(); |
95 | 82 |
|
96 |
| - { |
97 |
| - remote_branches_filter |
98 |
| - .stdin |
99 |
| - .unwrap() |
100 |
| - .write_all(&remote_branches_cmd.stdout) |
101 |
| - .unwrap(); |
102 |
| - } |
103 |
| - |
104 |
| - let mut remote_branches_output = String::new(); |
105 |
| - remote_branches_filter |
106 |
| - .stdout |
107 |
| - .unwrap() |
108 |
| - .read_to_string(&mut remote_branches_output) |
109 |
| - .unwrap(); |
110 |
| - let mut remote_branches = remote_branches_output.split('\n').map(|b| b.trim().into()); |
| 83 | + let remote_branches = remote_branches_output |
| 84 | + .lines() |
| 85 | + .fold(Vec::<String>::new(), |mut acc, line| { |
| 86 | + if !remote_branches_filter.is_match(line) { |
| 87 | + acc.push(line.trim().to_string()); |
| 88 | + } |
| 89 | + acc |
| 90 | + }); |
111 | 91 |
|
112 | 92 | for branch in local_branches {
|
113 | 93 | // First check if the local branch doesn't exist in the remote, it's the cheapest and easiest
|
114 | 94 | // way to determine if we want to suggest to delete it.
|
115 |
| - if !remote_branches.any(|b: String| b == format!("{}/{}", &options.remote, branch)) { |
| 95 | + if !remote_branches.iter().any(|b: &String| *b == format!("{}/{}", &options.remote, branch)) { |
116 | 96 | branches.push(branch.to_owned());
|
117 | 97 | continue;
|
118 | 98 | }
|
119 | 99 |
|
120 | 100 | // If it does exist in the remote, check to see if it's listed in git branches --merged. If
|
121 | 101 | // it is, that means it wasn't merged using Github squashes, and we can suggest it.
|
122 |
| - if merged_branches.any(|b: String| b == branch) { |
| 102 | + if merged_branches.iter().any(|b: &String| *b == branch) { |
123 | 103 | branches.push(branch.to_owned());
|
124 | 104 | continue;
|
125 | 105 | }
|
|
0 commit comments