从不同的目录中使用Go获取Git状态。

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

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(&quot;Starting Site map&quot;)
	dir := &quot;/media/my_path/ubuntu/&quot;

	git0 := &quot;git --git-dir=&quot; + dir + &quot;.git --work-tree=&quot; + dir + &quot; status&quot;
	log.Println(git0) //&lt;-- works when pasted in console
	cmd0 := exec.Command(&quot;git&quot;, git0)
	status0, err := cmd0.Output()
	if err != nil {
		log.Println(whereami.WhereAmI(), err)
	}
	log.Println(status0)

	git := &quot;git --git-dir=&quot; + dir + &quot;.git --work-tree=&quot; + dir
	log.Println(git)
	cmd := exec.Command(&quot;git&quot;, &quot;status&quot;, git)
	status, err := cmd.Output()
	if err != nil {
		log.Println(whereami.WhereAmI(), err)
	}
	log.Println(status)
	log.Println(&quot;End Site map&quot;)
}

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 &quot;git --git-dir …&quot;

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(&quot;/path/to/git&quot;, &quot;--git-dir=&quot; + dir + &quot;.git&quot;, &quot;--work-tree=&quot; + dir, &quot;status&quot;)

答案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 (
   &quot;os&quot;
   &quot;os/exec&quot;
)

func main() {
   c := exec.Command(&quot;git&quot;, &quot;status&quot;)
   c.Dir = &quot;/media/my_path/ubuntu&quot;
   c.Stdout = os.Stdout
   c.Run()
}

https://golang.org/pkg/os/exec#Cmd.Dir

huangapple
  • 本文由 发表于 2021年5月25日 05:36:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/67679220.html
匿名

发表评论

匿名网友

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

确定