英文:
Get Git status from Go from a different directory
问题
我需要从另一个目录获取修改过的文件列表。
func main() {
log.Println("Starting Site map")
dir := "/media/my_path/ubuntu/"
git0 := "git --git-dir=" + dir + ".git --work-tree=" + dir + " status"
log.Println(git0) // <-- 在控制台中粘贴时有效
cmd0 := exec.Command("git", git0)
status0, err := cmd0.Output()
if err != nil {
log.Println(whereami.WhereAmI(), err)
}
log.Println(status0)
git := "git --git-dir=" + dir + ".git --work-tree=" + dir
log.Println(git)
cmd := exec.Command("git", "status", git)
status, err := cmd.Output()
if err != nil {
log.Println(whereami.WhereAmI(), err)
}
log.Println(status)
log.Println("End Site map")
}
我得到了以下输出:
2021/05/24 11:38:07 Starting Site map
2021/05/24 11:38:07 git --git-dir=/media/my_path/ubuntu/.git --work-tree=/media/my_path/ubuntu/ status
2021/05/24 11:38:07 文件:main.go 函数:main.main 行:19 退出状态 1
2021/05/24 11:38:07 []
2021/05/24 11:38:07 git --git-dir=/media/my_path/ubuntu/.git --work-tree=/media/my_path/ubuntu/
2021/05/24 11:38:07 文件:main.go 函数:main.main 行:28 退出状态 128
2021/05/24 11:38:07 []
2021/05/24 11:38:07 End Site map
这个命令:git --git-dir=/media/my_path/ubuntu/.git --work-tree=/media/my_path/ubuntu/ status
在控制台中粘贴时可以正常工作。
预期结果应该是一个包含修改过的文件的数组。
英文:
I need to get a list of modified files from a different directory.
func main() {
log.Println("Starting Site map")
dir := "/media/my_path/ubuntu/"
git0 := "git --git-dir=" + dir + ".git --work-tree=" + dir + " status"
log.Println(git0) //<-- works when pasted in console
cmd0 := exec.Command("git", git0)
status0, err := cmd0.Output()
if err != nil {
log.Println(whereami.WhereAmI(), err)
}
log.Println(status0)
git := "git --git-dir=" + dir + ".git --work-tree=" + dir
log.Println(git)
cmd := exec.Command("git", "status", git)
status, err := cmd.Output()
if err != nil {
log.Println(whereami.WhereAmI(), err)
}
log.Println(status)
log.Println("End Site map")
}
The I have the following output:
2021/05/24 11:38:07 Starting Site map
2021/05/24 11:38:07 git --git-dir=/media/my_path/ubuntu/.git --work-tree=/media/my_path/ubuntu/ status
2021/05/24 11:38:07 File: main.go Function: main.main Line: 19 exit status 1
2021/05/24 11:38:07 []
2021/05/24 11:38:07 git --git-dir=/media/my_path/ubuntu/.git --work-tree=/media/my_path/ubuntu/
2021/05/24 11:38:07 File: main.go Function: main.main Line: 28 exit status 128
2021/05/24 11:38:07 []
2021/05/24 11:38:07 End Site map
This command: git --git-dir=/media/my_path/ubuntu/.git --work-tree=/media/my_path/ubuntu/ status
It works fine when pasted into the console.
The expected result would be array containing modified files.
答案1
得分: 1
exec.Command
接受一个命令和一些参数。
你正在使用'git'作为命令,并将'git ...'作为参数调用它,这将被执行为
git "git --git-dir ..."
这样是行不通的。
- 不要在参数中重复命令。
- 使用单独的参数。
- 提供git命令的路径。
每个参数应该是一个单独的字符串,用逗号分隔。
exec.Command("/path/to/git", "--git-dir="+dir+".git", "--work-tree="+dir, "status")
英文:
exec.Command
takes a command and some arguments.
You’re calling it with ‘git’, then ‘git …’ as an argument, which is executed like
git "git --git-dir …"
which isn’t going to work.
- Don’t repeat the command in the argument.
- Use individual arguments.
- Provide the path to the git command.
Each argument should be a separate string comma separated.
exec.Command("/path/to/git", "--git-dir=" + dir + ".git", "--work-tree=" + dir, "status")
答案2
得分: 1
这是我为您翻译的内容:
这对我有效:
package main
import (
"os"
"os/exec"
)
func main() {
c := exec.Command("git", "status")
c.Dir = "/media/my_path/ubuntu"
c.Stdout = os.Stdout
c.Run()
}
https://golang.org/pkg/os/exec#Cmd.Dir
英文:
This works for me:
package main
import (
"os"
"os/exec"
)
func main() {
c := exec.Command("git", "status")
c.Dir = "/media/my_path/ubuntu"
c.Stdout = os.Stdout
c.Run()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论