-3

我需要帮助以使用 golang 将代码检查到特定 SHA 数的 git repo

4

1 回答 1

0

这实际上是一个 Go 问题,因为它指的是go-git library

因此,您可以执行以下操作:

package main

import (
    "fmt"

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

// Basic example of how to checkout a specific commit.
func main() {
    // Clone the given repository to the given directory
    r, err := git.PlainClone("your-local-repo-name", false, &git.CloneOptions{
        URL: "your-repo-clone-URL",
    })
    // handle error

    // ... retrieving the commit being pointed by HEAD
    ref, err := r.Head()
    // handle error

    w, err := r.Worktree()
    // handle error

    // ... checking out to commit
    err = w.Checkout(&git.CheckoutOptions{
        Hash: plumbing.NewHash("your-specific-commit-hash"),
    })
    // handle error

    // ... retrieving the commit being pointed by HEAD, it shows that the
    // repository is pointing to the giving commit in detached mode
    ref, err = r.Head()
    // handle error

    fmt.Println(ref.Hash())
}

注意: 大部分代码取自go-git示例目录,并通过将存储库克隆到新的本地目录来启动整个过程;如果不适用于您的用例,请跳过此步骤。

于 2019-06-28T17:42:55.430 回答