X Tutup
Skip to content

Commit e4b9f7c

Browse files
quiyesamcoe
authored andcommitted
Alert unpushed commits when merging a pull request
1 parent e6e9208 commit e4b9f7c

File tree

3 files changed

+182
-247
lines changed

3 files changed

+182
-247
lines changed

git/git.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,29 @@ func Commits(baseRef, headRef string) ([]*Commit, error) {
180180
return commits, nil
181181
}
182182

183+
func LastCommit() (*Commit, error) {
184+
logCmd := GitCommand("-c", "log.ShowSignature=false", "log", "--pretty=format:%H,%s", "-1")
185+
output, err := run.PrepareCmd(logCmd).Output()
186+
if err != nil {
187+
return nil, err
188+
}
189+
190+
lines := outputLines(output)
191+
if len(lines) != 1 {
192+
return nil, ErrNotOnAnyBranch
193+
}
194+
195+
split := strings.SplitN(lines[0], ",", 2)
196+
if len(split) != 2 {
197+
return nil, ErrNotOnAnyBranch
198+
}
199+
200+
return &Commit{
201+
Sha: split[0],
202+
Title: split[1],
203+
}, nil
204+
}
205+
183206
func CommitBody(sha string) (string, error) {
184207
showCmd, err := GitCommand("-c", "log.ShowSignature=false", "show", "-s", "--pretty=format:%b", sha)
185208
if err != nil {

pkg/cmd/pr/merge/merge.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,17 @@ func mergeRun(opts *MergeOptions) error {
131131
return err
132132
}
133133

134+
localBranchName, err := git.CurrentBranch()
135+
if err == nil {
136+
localBranchLastCommit, err := git.LastCommit()
137+
if err == nil {
138+
if localBranchName == pr.HeadRefName && localBranchLastCommit.Sha != pr.Commits.Nodes[0].Commit.Oid {
139+
fmt.Fprintf(opts.IO.ErrOut,
140+
"%s Pull request #%d (%s) may have a last commit that is different from local one\n", utils.Yellow("!"), pr.Number, pr.Title)
141+
}
142+
}
143+
}
144+
134145
if pr.Mergeable == "CONFLICTING" {
135146
fmt.Fprintf(opts.IO.ErrOut, "%s Pull request #%d (%s) has conflicts and isn't mergeable\n", cs.Red("!"), pr.Number, pr.Title)
136147
return cmdutil.SilentError

0 commit comments

Comments
 (0)
X Tutup