Clever / microplane

A CLI tool to make git changes across many repos, especially useful with Microservices.

Home Page:https://medium.com/always-a-student/mo-repos-mo-problems-how-we-make-changes-across-many-git-repositories-293ad7d418f0

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Squash instead of merge commit

simenandre opened this issue Β· comments

Hello πŸ‘‹

Thank you for this awesome project! I'd love to contribute to this project, and one thing that has bugged me a little is that I have no way to choose which kind of merge I'd like.

I would rather squash and merge than add a merge commit. I'm not sure if it's possible, but I'd love to have a go at it if that is a feature you'd like to see in Microplane!

Thanks again!

@cobraz

Thanks so much for the suggestion! Yes, I think that is possible, and it would be a nice feature if you'd like to add it.


I imagine usage would look something like

$ mp merge --merge-method squash ...

Here's how I'd go about adding it...

The backend logic for merging happens here

microplane/merge/merge.go

Lines 97 to 102 in 9487433

// Merge the PR
options := &github.PullRequestOptions{}
commitMsg := ""
<-mergeLimiter.C
<-repoLimiter.C
result, _, err := client.PullRequests.Merge(ctx, input.Repo.Owner, input.Repo.Name, input.PRNumber, commitMsg, options)

You'd want to set mergeMethod: "squash" within the PullRequestOptions (see `go-github: https://github.com/google/go-github/blob/a0bec87ef51b4d7f14371d355986c7ca8cd0d84c/github/pulls.go#L440-L441)

Beyond that, we'd want to make sure it's configurable from the CLI command. We can set the merge method via a flag, and default to the existing behavior.

microplane/cmd/root.go

Lines 33 to 36 in 9487433

rootCmd.AddCommand(mergeCmd)
mergeCmd.Flags().StringVarP(&mergeFlagThrottle, "throttle", "t", "30s", "Throttle number of merges, e.g. '30s' means 1 merge per 30 seconds")
mergeCmd.Flags().BoolVar(&mergeFlagIgnoreReviewApproval, "ignore-review-approval", false, "Ignore whether or not the review has been approved")
mergeCmd.Flags().BoolVar(&mergeFlagIgnoreBuildStatus, "ignore-build-status", false, "Ignore whether or not builds are passing")

microplane/cmd/merge.go

Lines 18 to 21 in da8630b

// CLI flags
var mergeFlagThrottle string
var mergeFlagIgnoreReviewApproval bool
var mergeFlagIgnoreBuildStatus bool

@nathanleiby thank you for the quick reply. I've opened a pull request, hopefully, that works out? :)

See #105

@cobraz Thanks for adding support for this feature!

I realize that we could also add this feature for Gitlab. Sharing some relevant info below, in case you or someone else would like to add it.

During merge, it seems that Gitlab supports merge (default) or squash. rebase isn't handled as a 3rd merge option, but instead must be done separately from the merge.

Today in Microplane's Gitlab logic...

If a rebase is possible, we will always do it before merging:

// Try to rebase master if Diverged Commits greates that zero
if mr.DivergedCommitsCount > 0 {
_, err := client.MergeRequests.RebaseMergeRequest(pid, input.PRNumber, ctxFunc)
if err != nil {
return Output{Success: false}, fmt.Errorf("Failed to rebase from master")
}
}

We do the merge here (this is where we could pass squash: true as an option):

result, _, err := client.MergeRequests.AcceptMergeRequest(pid, input.PRNumber, &gitlab.AcceptMergeRequestOptions{
ShouldRemoveSourceBranch: &truePointer,
}, ctxFunc)


Suggested Gitlab approach, to give squash support without breaking anything existing:

  • always rebase for Gitlab (keep existing behavior)
  • if mergeOption == squash, then squash: true, otherwise squash: false

Re-opening to track the remaining Gitlab work