Skip to content

Commit

Permalink
Merge pull request #3 from jacobwgillespie/squash-merge
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwgillespie authored Mar 15, 2022
2 parents 659fe2a + be0e43c commit de06f9d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

- uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: 1.17

- run: go mod tidy
- run: go build
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:

- uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: 1.17

- uses: goreleaser/goreleaser-action@v2
with:
Expand Down
34 changes: 34 additions & 0 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,40 @@ func SymbolicRef(ref string) (string, error) {
return firstLine(output), err
}

func TreeRef(ref string) (string, error) {
output, err := execGitQuiet("rev-parse", ref+"^{tree}")
if err != nil {
return "", err
}
return firstLine(output), err
}

func MergeBase(a, b string) (string, error) {
output, err := execGitQuiet("merge-base", a, b)
if err != nil {
return "", err
}
return firstLine(output), nil
}

func CommitTree(args ...string) (string, error) {
args = append([]string{"commit-tree"}, args...)
output, err := execGitQuiet(args...)
if err != nil {
return "", err
}
return firstLine(output), nil
}

func Cherry(args ...string) (string, error) {
args = append([]string{"cherry"}, args...)
output, err := execGitQuiet(args...)
if err != nil {
return "", err
}
return firstLine(output), nil
}

func execGit(args ...string) (string, error) {
cmd := exec.Command("git", args...)
cmd.Stderr = os.Stderr
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/jacobwgillespie/git-sync

go 1.16
go 1.17
18 changes: 17 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,23 @@ func main() {
diff, err := git.NewRange(fullBranch, fullDefaultBranch)
check(err)

if diff.IsAncestor() {
// Determine if branch has been merged with a merge
shouldDelete := diff.IsAncestor()

// Otherwise, try to determine if branch has been squash-merged
if !shouldDelete {
ancestorHash, err := git.MergeBase(fullDefaultBranch, fullBranch)
check(err)
treeHash, err := git.TreeRef(fullBranch)
check(err)
danglingCommit, err := git.CommitTree(treeHash, "-p", ancestorHash, "-m", fmt.Sprintf("Dangling branch %s", branch))
check(err)
result, err := git.Cherry(fullDefaultBranch, danglingCommit)
check(err)
shouldDelete = strings.HasPrefix(result, "-")
}

if shouldDelete {
if branch == currentBranch {
git.Quiet("checkout", "--quiet", defaultBranch)
currentBranch = defaultBranch
Expand Down

0 comments on commit de06f9d

Please sign in to comment.