在Go-Git中如何获取origin/master的SHA值?
时间:2024-03-20
浏览:170
作者:佚名
Go-Git 库提供了与 Git 命令交互的功能。本文介绍了如何使用 Go-Git 查找 origin/master 分支的 SHA1 值。通过使用 git rev-parse 命令,Go-Git 可以将给定的修订解析为相应的 SHA1 提交。本文提供了示例代码,展示了如何使用 Go-Git 执行此操作。
问题内容
在我已经完成了与 git fetch --all 等效的操作之后,我正在尝试使用 go-git 查找 origin/master 的 SHA1。然而,go-git 似乎不支持:
git ls-remote [电子邮件受保护]:StevenACoffman/toolbox.gitgit rev-parse origin/master
是否有其他方法可以使用 go-git 确定 origin/master 的 SHA1?
解决方案
哎呀!支持 git rev-parse!使用 ./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())
}今天关于《在Go-Git中如何获取origin/master的SHA值?》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.20
11.20
11.20
11.20
热门专题
