+
Skip to content

Add support for removing squash-merged branches #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载