Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
github
hub
提交
a16a1216
H
hub
项目概览
github
/
hub
大约 1 年 前同步成功
通知
3
Star
22523
Fork
2406
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hub
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a16a1216
编写于
11月 30, 2014
作者:
J
Jingwen Owen Ou
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #714 from github/exec_cmd
Use `Exec` except Windows for the last command run in the commands chain
上级
34e6d1d2
4cb663a9
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
50 addition
and
20 deletion
+50
-20
cmd/cmd.go
cmd/cmd.go
+31
-8
commands/runner.go
commands/runner.go
+12
-5
fixtures/test_repo.go
fixtures/test_repo.go
+1
-1
git/git.go
git/git.go
+5
-5
github/editor.go
github/editor.go
+1
-1
未找到文件。
cmd/cmd.go
浏览文件 @
a16a1216
...
...
@@ -2,11 +2,14 @@ package cmd
import
(
"fmt"
"github.com/github/hub/utils"
"github.com/github/hub/Godeps/_workspace/src/github.com/kballard/go-shellquote"
"os"
"os/exec"
"runtime"
"strings"
"syscall"
"github.com/github/hub/Godeps/_workspace/src/github.com/kballard/go-shellquote"
"github.com/github/hub/utils"
)
type
Cmd
struct
{
...
...
@@ -34,19 +37,25 @@ func (cmd *Cmd) WithArgs(args ...string) *Cmd {
return
cmd
}
func
(
cmd
*
Cmd
)
Exec
Output
()
(
string
,
error
)
{
func
(
cmd
*
Cmd
)
Combined
Output
()
(
string
,
error
)
{
output
,
err
:=
exec
.
Command
(
cmd
.
Name
,
cmd
.
Args
...
)
.
CombinedOutput
()
return
string
(
output
),
err
}
func
(
cmd
*
Cmd
)
Exec
()
error
{
binary
,
lookErr
:=
exec
.
LookPath
(
cmd
.
Name
)
if
lookErr
!=
nil
{
return
fmt
.
Errorf
(
"command not found: %s"
,
cmd
.
Name
)
// Run runs command with `Exec` on platforms except Windows
// which only supports `Spawn`
func
(
cmd
*
Cmd
)
Run
()
error
{
if
runtime
.
GOOS
==
"windows"
{
return
cmd
.
Spawn
()
}
else
{
return
cmd
.
Exec
()
}
}
c
:=
exec
.
Command
(
binary
,
cmd
.
Args
...
)
// Spawn runs command with spawn(3)
func
(
cmd
*
Cmd
)
Spawn
()
error
{
c
:=
exec
.
Command
(
cmd
.
Name
,
cmd
.
Args
...
)
c
.
Stdin
=
os
.
Stdin
c
.
Stdout
=
os
.
Stdout
c
.
Stderr
=
os
.
Stderr
...
...
@@ -54,6 +63,20 @@ func (cmd *Cmd) Exec() error {
return
c
.
Run
()
}
// Exec runs command with exec(3)
// Note that Windows doesn't support exec(3): http://golang.org/src/pkg/syscall/exec_windows.go#L339
func
(
cmd
*
Cmd
)
Exec
()
error
{
binary
,
err
:=
exec
.
LookPath
(
cmd
.
Name
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"command not found: %s"
,
cmd
.
Name
)
}
args
:=
[]
string
{
binary
}
args
=
append
(
args
,
cmd
.
Args
...
)
return
syscall
.
Exec
(
binary
,
args
,
os
.
Environ
())
}
func
New
(
cmd
string
)
*
Cmd
{
cmds
,
err
:=
shellquote
.
Split
(
cmd
)
utils
.
Check
(
err
)
...
...
commands/runner.go
浏览文件 @
a16a1216
...
...
@@ -7,11 +7,11 @@ import (
"strings"
"syscall"
"github.com/github/hub/Godeps/_workspace/src/github.com/kballard/go-shellquote"
flag
"github.com/github/hub/Godeps/_workspace/src/github.com/ogier/pflag"
"github.com/github/hub/cmd"
"github.com/github/hub/git"
"github.com/github/hub/utils"
"github.com/github/hub/Godeps/_workspace/src/github.com/kballard/go-shellquote"
flag
"github.com/github/hub/Godeps/_workspace/src/github.com/ogier/pflag"
)
type
ExecError
struct
{
...
...
@@ -76,7 +76,7 @@ func (r *Runner) Execute() ExecError {
return
r
.
Call
(
cmd
,
args
)
}
err
=
git
.
Spaw
n
(
args
.
Command
,
args
.
Params
...
)
err
=
git
.
Ru
n
(
args
.
Command
,
args
.
Params
...
)
return
newExecError
(
err
)
}
...
...
@@ -106,8 +106,15 @@ func printCommands(cmds []*cmd.Cmd) {
}
func
executeCommands
(
cmds
[]
*
cmd
.
Cmd
)
error
{
for
_
,
c
:=
range
cmds
{
err
:=
c
.
Exec
()
for
i
,
c
:=
range
cmds
{
var
err
error
// Run with `Exec` for the last command in chain
if
i
==
len
(
cmds
)
-
1
{
err
=
c
.
Run
()
}
else
{
err
=
c
.
Spawn
()
}
if
err
!=
nil
{
return
err
}
...
...
fixtures/test_repo.go
浏览文件 @
a16a1216
...
...
@@ -48,7 +48,7 @@ func (r *TestRepo) Setup() {
func
(
r
*
TestRepo
)
clone
(
repo
,
dir
string
)
error
{
cmd
:=
cmd
.
New
(
"git"
)
.
WithArgs
(
"clone"
,
repo
,
dir
)
output
,
err
:=
cmd
.
Exec
Output
()
output
,
err
:=
cmd
.
Combined
Output
()
if
err
!=
nil
{
err
=
fmt
.
Errorf
(
"error cloning %s to %s: %s"
,
repo
,
dir
,
output
)
}
...
...
git/git.go
浏览文件 @
a16a1216
...
...
@@ -130,7 +130,7 @@ func Show(sha string) (string, error) {
cmd
:=
cmd
.
New
(
"git"
)
cmd
.
WithArg
(
"show"
)
.
WithArg
(
"-s"
)
.
WithArg
(
"--format=%s%n%+b"
)
.
WithArg
(
sha
)
output
,
err
:=
cmd
.
Exec
Output
()
output
,
err
:=
cmd
.
Combined
Output
()
output
=
strings
.
TrimSpace
(
output
)
return
output
,
err
...
...
@@ -144,7 +144,7 @@ func Log(sha1, sha2 string) (string, error) {
shaRange
:=
fmt
.
Sprintf
(
"%s...%s"
,
sha1
,
sha2
)
execCmd
.
WithArg
(
shaRange
)
outputs
,
err
:=
execCmd
.
Exec
Output
()
outputs
,
err
:=
execCmd
.
Combined
Output
()
if
err
!=
nil
{
return
""
,
fmt
.
Errorf
(
"Can't load git log %s..%s"
,
sha1
,
sha2
)
}
...
...
@@ -191,14 +191,14 @@ func Alias(name string) (string, error) {
return
Config
(
fmt
.
Sprintf
(
"alias.%s"
,
name
))
}
func
Spaw
n
(
command
string
,
args
...
string
)
error
{
func
Ru
n
(
command
string
,
args
...
string
)
error
{
cmd
:=
cmd
.
New
(
"git"
)
cmd
.
WithArg
(
command
)
for
_
,
a
:=
range
args
{
cmd
.
WithArg
(
a
)
}
return
cmd
.
Exec
()
return
cmd
.
Run
()
}
func
execGitCmd
(
input
...
string
)
(
outputs
[]
string
,
err
error
)
{
...
...
@@ -207,7 +207,7 @@ func execGitCmd(input ...string) (outputs []string, err error) {
cmd
.
WithArg
(
i
)
}
out
,
err
:=
cmd
.
Exec
Output
()
out
,
err
:=
cmd
.
Combined
Output
()
for
_
,
line
:=
range
strings
.
Split
(
out
,
"
\n
"
)
{
line
=
strings
.
TrimSpace
(
line
)
if
line
!=
""
{
...
...
github/editor.go
浏览文件 @
a16a1216
...
...
@@ -118,7 +118,7 @@ func openTextEditor(program, file string) error {
}
editCmd
.
WithArg
(
file
)
return
editCmd
.
Exec
()
return
editCmd
.
Spawn
()
}
func
readTitleAndBody
(
reader
io
.
Reader
,
cs
string
)
(
title
,
body
string
,
err
error
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录