(
&self,
common_ancestor: &str,
parent_branch: &str,
current_branch: &str,
)
| 10 | |
| 11 | impl GitChain { |
| 12 | pub fn is_squashed_merged( |
| 13 | &self, |
| 14 | common_ancestor: &str, |
| 15 | parent_branch: &str, |
| 16 | current_branch: &str, |
| 17 | ) -> Result<bool, Error> { |
| 18 | // References: |
| 19 | // https://blog.takanabe.tokyo/en/2020/04/remove-squash-merged-local-git-branches/ |
| 20 | // https://github.com/not-an-aardvark/git-delete-squashed |
| 21 | |
| 22 | // common_ancestor should be pre-computed beforehand, ideally with self.merge_base_fork_point() |
| 23 | // common_ancestor is commit sha |
| 24 | |
| 25 | // tree_id = git rev-parse current_branch^{tree} |
| 26 | let tree_id = self.get_tree_id_from_branch_name(current_branch)?; |
| 27 | |
| 28 | // dangling_commit_id = git commit-tree tree_id -p common_ancestor -m "Temp commit for checking is_squashed_merged for branch current_branch" |
| 29 | let output = Command::new("git") |
| 30 | .arg("commit-tree") |
| 31 | .arg(&tree_id) |
| 32 | .arg("-p") |
| 33 | .arg(common_ancestor) |
| 34 | .arg("-m") |
| 35 | .arg(format!( |
| 36 | "Temp commit for checking is_squashed_merged for branch {}", |
| 37 | current_branch |
| 38 | )) |
| 39 | .output() |
| 40 | .unwrap_or_else(|_| { |
| 41 | panic!( |
| 42 | "Unable to generate commit-tree of branch {}", |
| 43 | current_branch.bold() |
| 44 | ) |
| 45 | }); |
| 46 | |
| 47 | let dangling_commit_id = if output.status.success() { |
| 48 | let raw_output = String::from_utf8(output.stdout).unwrap(); |
| 49 | let dangling_commit_id = raw_output.trim().to_string(); |
| 50 | dangling_commit_id |
| 51 | } else { |
| 52 | return Err(Error::from_str(&format!( |
| 53 | "Unable to generate commit-tree of branch {}", |
| 54 | current_branch.bold() |
| 55 | ))); |
| 56 | }; |
| 57 | |
| 58 | // output = git cherry parent_branch dangling_commit_id |
| 59 | let output = Command::new("git") |
| 60 | .arg("cherry") |
| 61 | .arg(parent_branch) |
| 62 | .arg(&dangling_commit_id) |
| 63 | .output() |
| 64 | .unwrap_or_else(|_| { |
| 65 | panic!( |
| 66 | "Unable to determine if branch {} was squashed and merged into {}", |
| 67 | current_branch.bold(), |
| 68 | parent_branch.bold() |
| 69 | ) |
no test coverage detected