-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
164 lines (135 loc) · 3.45 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"errors"
"fmt"
"os"
"strings"
"github.com/atotto/clipboard"
flags "github.com/jessevdk/go-flags"
"gitlab.com/jfaucherre/mergo/credentials"
"gitlab.com/jfaucherre/mergo/git"
"gitlab.com/jfaucherre/mergo/logger"
"gitlab.com/jfaucherre/mergo/models"
"gitlab.com/jfaucherre/mergo/tools"
)
func main() {
opts := &models.Opts{}
repo, err := git.FromPwd()
tools.CheckError(err)
err = repo.LoadConfig(&struct {
Mergo *models.Opts `ini:"mergo"`
}{Mergo: opts})
tools.CheckError(err)
_, err = flags.ParseArgs(opts, os.Args)
if err != nil {
return
}
models.SetDefaultOptions(opts)
logger.Verbosity = len(opts.Verbose)
logger.Info("verbosity = %d\n", logger.Verbosity)
logger.Debug("opts = %+v\n", opts)
err = setDefaultMessage(opts, repo)
tools.CheckError(err)
if opts.Delete != "" {
credentials.DeleteHostConfig(opts.Delete)
return
}
err = setHeadBranch(opts, repo)
tools.CheckError(err)
err = checkProblems(opts, repo)
tools.CheckError(err)
err = FormalizeMessage(opts, repo)
tools.CheckError(err)
mrParams := &models.MRParams{
Head: opts.Head,
Base: opts.Base,
Message: opts.Message,
}
rmts, err := getRemotes(opts, repo)
tools.CheckError(err)
mrUrls := SubmitPr(mrParams, rmts)
urls := strings.Join(mrUrls, "\n")
if opts.Clipboard {
clipboard.WriteAll(urls)
}
}
func setHeadBranch(opts *models.Opts, repo *git.Repo) error {
var err error
if len(opts.Head) != 0 {
return nil
}
opts.Head, err = repo.GetActualBranch()
if err == git.ErrNotABranch {
return fmt.Errorf("we can not take your branch as a source branch because you are not on a branch")
}
if err != nil {
return err
}
return nil
}
// checkProblems checks for different problems that might hold you back from
// making your pull request
func checkProblems(opts *models.Opts, repo *git.Repo) error {
if opts.Force {
return nil
}
problems := []string{}
// check if the repo has unstaged changes
if repo.HasChanges() {
problems = append(problems, "you have unstaged changes")
}
// check if head branch is up to date
isHdUp, err := repo.IsBranchUpToDate(opts.Head)
if err != nil {
return err
}
if !isHdUp {
problems = append(problems, "your head branch is not up to date")
}
// check if base branch is up to date
isBsUp, err := repo.IsBranchUpToDate(opts.Base)
if err != nil {
return err
}
if !isBsUp {
problems = append(problems, "your base branch is not up to date")
}
if len(problems) == 0 {
return nil
}
// ask you if you still wish to continue
problems = append([]string{"you have the following problems:"}, problems...)
msg := strings.Join(problems, "\n\t- ") + "\ndo you wish to ignore them and continue ?"
agreed, err := tools.AskYesNo(msg)
if err != nil {
return err
}
if !agreed {
return errors.New("cancelled due to user input")
}
return nil
}
func getRemotes(opts *models.Opts, repo *git.Repo) (remotes []string, err error) {
if len(opts.RemoteURLs) != 0 {
remotes = opts.RemoteURLs
return
}
remotes, err = repo.GetRemoteURLs(opts.Remote)
return
}
func setDefaultMessage(opts *models.Opts, repo *git.Repo) error {
head, err := repo.GetBranchCommit(opts.Head)
if err != nil {
return err
}
base, err := repo.GetBranchCommit(opts.Base)
if err != nil {
return err
}
// If there was only one commit on branch head from base, we set this commit
// message as the default's message of the pull request's
if git.IsDirectChild(base, head) {
opts.Message = head.Message
}
return nil
}