Skip to content

Commit de06f9d

Browse files
Merge pull request #3 from jacobwgillespie/squash-merge
2 parents 659fe2a + be0e43c commit de06f9d

File tree

5 files changed

+54
-4
lines changed

5 files changed

+54
-4
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212

1313
- uses: actions/setup-go@v2
1414
with:
15-
go-version: 1.16
15+
go-version: 1.17
1616

1717
- run: go mod tidy
1818
- run: go build

.github/workflows/release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818

1919
- uses: actions/setup-go@v2
2020
with:
21-
go-version: 1.16
21+
go-version: 1.17
2222

2323
- uses: goreleaser/goreleaser-action@v2
2424
with:

git/git.go

+34
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,40 @@ func SymbolicRef(ref string) (string, error) {
209209
return firstLine(output), err
210210
}
211211

212+
func TreeRef(ref string) (string, error) {
213+
output, err := execGitQuiet("rev-parse", ref+"^{tree}")
214+
if err != nil {
215+
return "", err
216+
}
217+
return firstLine(output), err
218+
}
219+
220+
func MergeBase(a, b string) (string, error) {
221+
output, err := execGitQuiet("merge-base", a, b)
222+
if err != nil {
223+
return "", err
224+
}
225+
return firstLine(output), nil
226+
}
227+
228+
func CommitTree(args ...string) (string, error) {
229+
args = append([]string{"commit-tree"}, args...)
230+
output, err := execGitQuiet(args...)
231+
if err != nil {
232+
return "", err
233+
}
234+
return firstLine(output), nil
235+
}
236+
237+
func Cherry(args ...string) (string, error) {
238+
args = append([]string{"cherry"}, args...)
239+
output, err := execGitQuiet(args...)
240+
if err != nil {
241+
return "", err
242+
}
243+
return firstLine(output), nil
244+
}
245+
212246
func execGit(args ...string) (string, error) {
213247
cmd := exec.Command("git", args...)
214248
cmd.Stderr = os.Stderr

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/jacobwgillespie/git-sync
22

3-
go 1.16
3+
go 1.17

main.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,23 @@ func main() {
8181
diff, err := git.NewRange(fullBranch, fullDefaultBranch)
8282
check(err)
8383

84-
if diff.IsAncestor() {
84+
// Determine if branch has been merged with a merge
85+
shouldDelete := diff.IsAncestor()
86+
87+
// Otherwise, try to determine if branch has been squash-merged
88+
if !shouldDelete {
89+
ancestorHash, err := git.MergeBase(fullDefaultBranch, fullBranch)
90+
check(err)
91+
treeHash, err := git.TreeRef(fullBranch)
92+
check(err)
93+
danglingCommit, err := git.CommitTree(treeHash, "-p", ancestorHash, "-m", fmt.Sprintf("Dangling branch %s", branch))
94+
check(err)
95+
result, err := git.Cherry(fullDefaultBranch, danglingCommit)
96+
check(err)
97+
shouldDelete = strings.HasPrefix(result, "-")
98+
}
99+
100+
if shouldDelete {
85101
if branch == currentBranch {
86102
git.Quiet("checkout", "--quiet", defaultBranch)
87103
currentBranch = defaultBranch

0 commit comments

Comments
 (0)