gogo-git

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


I am trying to use go-git to find the SHA1 of origin/master, after I have already done the equivalent of git fetch --all. However, go-git does not appear to support either:

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

Is there an alternative way to determine the SHA1 of origin/master using go-git?


Solution

  • Whoops! git rev-parse is supported! Execute the following with ./main.go $PWD origin/master:

    ///usr/bin/env go run "$0" "$@" ; 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())
    }