如何使用go-git找到origin/master的sha?

How to use go-git to find sha of origin/master?

我正在尝试使用 go-git 来查找 origin/master 的 SHA1,在我已经完成了 git fetch --all 的等效操作之后。但是,go-git 似乎也不支持:

  1. git ls-remote git@github.com:StevenACoffman/toolbox.git
  2. git rev-parse origin/master

是否有其他方法可以使用 go-git 确定 origin/master 的 SHA1?

糟糕! git rev-parse支持!使用 ./main.go $PWD origin/master 执行以下操作:

///usr/bin/env go run "[=10=]" "$@" ; exit "$?"
package main

import (
    "fmt"
    "os"

    "gopkg.in/src-d/go-git.v4"
    . "gopkg.in/src-d/go-git.v4/_examples"
    "gopkg.in/src-d/go-git.v4/plumbing"
)

// Example how to resolve a revision into its commit counterpart
func main() {
    CheckArgs("<path>", "<revision>")

    path := os.Args[1]
    revision := os.Args[2]

    // We instantiate a new repository targeting the given path (the .git     folder)
    r, err := git.PlainOpen(path)
    CheckIfError(err)

    // Resolve revision into a sha1 commit, only some revisions are resolved
    // look at the doc to get more details
    Info("git rev-parse %s", revision)

    h, err := r.ResolveRevision(plumbing.Revision(revision))

    CheckIfError(err)

    fmt.Println(h.String())
}