Skip to content

Commit 11ae3cc

Browse files
author
mritd
committed
feat(push): add git ps command
add git ps command Signed-off-by: mritd <[email protected]>
1 parent d34b9e9 commit 11ae3cc

File tree

5 files changed

+52
-16
lines changed

5 files changed

+52
-16
lines changed

cmd/ps.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright © 2018 mritd <[email protected]>
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
21+
package cmd
22+
23+
import (
24+
"github.com/mritd/gitflow-toolkit/pkg/util"
25+
"github.com/spf13/cobra"
26+
)
27+
28+
func NewPs() *cobra.Command {
29+
return &cobra.Command{
30+
Use: "ps",
31+
Short: "推送本地分支",
32+
Long: `
33+
将本地分支推送到远程`,
34+
Aliases: []string{"git-ps"},
35+
Run: func(cmd *cobra.Command, args []string) {
36+
util.Push()
37+
},
38+
}
39+
}

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ func init() {
6262
RootCmd.AddCommand(NewChore())
6363
RootCmd.AddCommand(NewInstall())
6464
RootCmd.AddCommand(NewUninstall())
65+
RootCmd.AddCommand(NewPs())
6566
}
6667

6768
func initConfig() {

main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ func commandFor(basename string, rootCommand *cobra.Command) *cobra.Command {
4141
}
4242

4343
func main() {
44-
4544
basename := filepath.Base(os.Args[0])
4645
util.CheckAndExit(commandFor(basename, cmd.RootCmd).Execute())
4746
}

pkg/util/common.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func BinPaths() *[]string {
5959
InstallBaseDir + "/git-hotfix",
6060
InstallBaseDir + "/git-xmr",
6161
InstallBaseDir + "/git-xpr",
62+
InstallBaseDir + "/git-ps",
6263
}
6364
}
6465

@@ -78,18 +79,17 @@ func CheckAndExit(err error) {
7879

7980
func MustExec(name string, arg ...string) {
8081
cmd := exec.Command(name, arg...)
81-
b, err := cmd.CombinedOutput()
82-
if err != nil {
83-
fmt.Println(string(b))
84-
os.Exit(1)
85-
}
82+
cmd.Stdin = os.Stdin
83+
cmd.Stdout = os.Stdout
84+
cmd.Stderr = os.Stderr
85+
CheckAndExit(cmd.Run())
8686
}
8787

8888
func MustExecRtOut(name string, arg ...string) string {
8989
cmd := exec.Command(name, arg...)
90-
b, err := cmd.CombinedOutput()
90+
b, err := cmd.Output()
9191
if err != nil {
92-
fmt.Println(string(b))
92+
fmt.Print(err)
9393
os.Exit(1)
9494
}
9595
return string(b)
@@ -102,9 +102,6 @@ func MustExecNoOut(name string, arg ...string) {
102102

103103
func TryExec(name string, arg ...string) error {
104104
cmd := exec.Command(name, arg...)
105-
cmd.Stdin = os.Stdin
106-
cmd.Stdout = os.Stdout
107-
cmd.Stderr = os.Stderr
108105
return cmd.Run()
109106
}
110107

@@ -144,11 +141,7 @@ func OSEditInput() string {
144141
}
145142

146143
// 执行编辑文件
147-
cmd := exec.Command(editor, f.Name())
148-
cmd.Stdin = os.Stdin
149-
cmd.Stdout = os.Stdout
150-
cmd.Stderr = os.Stderr
151-
CheckAndExit(cmd.Run())
144+
MustExec(editor, f.Name())
152145
raw, err := ioutil.ReadFile(f.Name())
153146
CheckAndExit(err)
154147
input := string(bytes.TrimPrefix(raw, bom))

pkg/util/git.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,7 @@ func Rebase(sourceBranch string, targetBranch string) {
4545
func Checkout(prefix consts.CommitType, branch string) {
4646
MustExec(consts.GitCmd, "checkout", "-b", string(prefix)+"/"+branch)
4747
}
48+
49+
func Push() {
50+
MustExec(consts.GitCmd, "push", "origin", strings.TrimSpace(GetCurrentBranch()))
51+
}

0 commit comments

Comments
 (0)