“exit status 128” 尝试从 Golang 创建 Git 分支

huangapple go评论87阅读模式
英文:

"exit status 128" trying to create git branches from golang

问题

我正在尝试从Golang创建一个“特性分支”,以下代码无法正常工作:

reader := bufio.NewReader(os.Stdin)
fmt.Print(color.RedString("New feature description: "))
featureName, _ := reader.ReadString('\n')
featureName = strings.ReplaceAll(featureName, " ", "-")
featureBranchName := "feature/" + featureName
cmdStartBranch := "git"
arguments := []string{"checkout", "-b", featureBranchName}
if _, err := exec.Command(cmdStartBranch, arguments...).Output(); err != nil {
    fmt.Println(color.RedString(err.Error()))
    os.Exit(1)
}

但是,如果硬编码featureBranchName,它可以正常工作:

cmdStartBranch := "git"
arguments := []string{"checkout", "-b", "feature/ciaone-proprio"}
if _, err := exec.Command(cmdStartBranch, arguments...).Output(); err != nil {
    fmt.Println(color.RedString(err.Error()))
    os.Exit(1)
}

请注意,这是一段Golang代码,用于从标准输入读取新特性描述,并根据描述创建一个特性分支。第一个代码段中的问题可能是featureName中包含了换行符\n,导致分支名称无效。

英文:

I am trying to create a "feature branch" from golang. The following code is not working:

reader := bufio.NewReader(os.Stdin)
fmt.Print(color.RedString("New feature description: "))
featureName, _ := reader.ReadString('\n')
featureName = strings.ReplaceAll(featureName, " ", "-")
featureBranchName := "feature/" + featureName
cmdStartBranch := "git"
arguments := []string{"checkout", "-b", featureBranchName}
if _, err := exec.Command(cmdStartBranch, arguments...).Output(); err != nil {
	fmt.Println(color.RedString(err.Error()))
	os.Exit(1)
}

But hardcoding featureBranchName it works:

cmdStartBranch := "git"
arguments := []string{"checkout", "-b", "feature/ciaone-proprio"}
if _, err := exec.Command(cmdStartBranch, arguments...).Output(); err != nil {
	fmt.Println(color.RedString(err.Error()))
	os.Exit(1)
}

答案1

得分: 1

[ReadString('\n')]返回分隔符'\n',你需要将其移除。

featureName, err := reader.ReadString('\n')
if err == nil {
    n := len(featureName)
    featureName = featureName[:n-1]
}

...

英文:

[ReadString('\n')][1] returns the delimiter '\n', you need to remove it.

featureName, err := reader.ReadString('\n')
if err == nil {
    n := len(featureName)
    featureName = featureName[:n-1]
}

...
[1]: https://golang.org/pkg/bufio/#Reader.ReadString

huangapple
  • 本文由 发表于 2019年3月25日 09:27:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/55330242.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定