新增:README文件
This commit is contained in:
parent
e0938f66dd
commit
f59fd170d1
|
|
@ -0,0 +1,57 @@
|
|||
## go-git-example
|
||||
|
||||
### 运行前准备
|
||||
|
||||
本地搭建gitea环境,注册一个账号
|
||||
|
||||
这里我注册了一个账号用户名为yystopf,密码为12345678
|
||||
|
||||
### 创建文件
|
||||
|
||||
```shell
|
||||
cd _example/operate/create
|
||||
# 示例
|
||||
go run main.go http://127.0.0.1:10061/yystopf/go-git.git yystopf 12345678
|
||||
```
|
||||
|
||||
### 删除文件
|
||||
|
||||
```shell
|
||||
cd _example/operate/delete
|
||||
# 示例
|
||||
go run main.go http://127.0.0.1:10061/yystopf/go-git.git yystopf 12345678
|
||||
```
|
||||
|
||||
### 查看文件
|
||||
|
||||
```shell
|
||||
cd _example/operate/read
|
||||
# 示例
|
||||
go run main.go http://127.0.0.1:10061/yystopf/go-git.git yystopf 12345678
|
||||
```
|
||||
|
||||
### 创建文件夹
|
||||
|
||||
```shell
|
||||
cd _example/operate/create/folder
|
||||
# 示例
|
||||
go run main.go http://127.0.0.1:10061/yystopf/go-git.git yystopf 12345678
|
||||
```
|
||||
|
||||
### 删除文件夹
|
||||
|
||||
```shell
|
||||
cd _example/operate/delete/folder
|
||||
# 示例
|
||||
go run main.go http://127.0.0.1:10061/yystopf/go-git.git yystopf 12345678
|
||||
```
|
||||
|
||||
### 查看文件夹
|
||||
|
||||
```shell
|
||||
cd _example/operate/read/folder
|
||||
# 示例
|
||||
go run main.go http://127.0.0.1:10061/yystopf/go-git.git yystopf 12345678 _example
|
||||
```
|
||||
|
||||
###
|
||||
|
|
@ -1,58 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
. "github.com/go-git/go-git/v5/_examples"
|
||||
"github.com/go-git/go-git/v5/plumbing"
|
||||
"github.com/go-git/go-git/v5/plumbing/object"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 裸仓库的路径
|
||||
CheckArgs("<path>")
|
||||
bareRepoPath := os.Args[1]
|
||||
|
||||
// 打开裸仓库
|
||||
r, err := git.PlainOpen(bareRepoPath)
|
||||
CheckIfError(err)
|
||||
|
||||
// 获取 HEAD 引用
|
||||
ref, err := r.Reference(plumbing.HEAD, true)
|
||||
CheckIfError(err)
|
||||
|
||||
// 解析 HEAD 引用到提交对象
|
||||
commit, err := r.CommitObject(ref.Hash())
|
||||
CheckIfError(err)
|
||||
|
||||
// 获取提交对象的树对象
|
||||
tree, err := commit.Tree()
|
||||
CheckIfError(err)
|
||||
|
||||
// 遍历树对象中的文件
|
||||
tree.Files().ForEach(func(f *object.File) error {
|
||||
fmt.Printf("Found file: %s\n", f.Name)
|
||||
|
||||
// 获取 blob 对象
|
||||
blob, err := r.BlobObject(f.Blob.Hash)
|
||||
CheckIfError(err)
|
||||
|
||||
// 获取 blob 的内容
|
||||
blobReader, err := blob.Reader()
|
||||
CheckIfError(err)
|
||||
|
||||
defer blobReader.Close()
|
||||
|
||||
// 读取 blob 的内容
|
||||
fileContent, err := ioutil.ReadAll(blobReader)
|
||||
CheckIfError(err)
|
||||
|
||||
// 打印文件内容c
|
||||
fmt.Printf("%s:\n%s\n", f.Name, fileContent)
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
|
@ -0,0 +1,80 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
. "github.com/go-git/go-git/v5/_examples"
|
||||
"github.com/go-git/go-git/v5/plumbing"
|
||||
"github.com/go-git/go-git/v5/plumbing/object"
|
||||
"github.com/go-git/go-git/v5/plumbing/transport/http"
|
||||
)
|
||||
|
||||
func main() {
|
||||
CheckArgs("<url>", "<github_username>", "<github_password>", "<directory_path>")
|
||||
url, username, password, find_directory := os.Args[1], os.Args[2], os.Args[3], os.Args[4]
|
||||
|
||||
directory := "./local-repo"
|
||||
|
||||
// 克隆裸仓库到本地
|
||||
Info("git clone %s %s", url, directory)
|
||||
|
||||
_, err := git.PlainClone(directory, false, &git.CloneOptions{
|
||||
Auth: &http.BasicAuth{
|
||||
Username: username,
|
||||
Password: password,
|
||||
},
|
||||
URL: url,
|
||||
Progress: os.Stdout,
|
||||
})
|
||||
CheckIfError(err)
|
||||
|
||||
// 打开本地仓库
|
||||
r, err := git.PlainOpen(directory)
|
||||
|
||||
// 获取 HEAD 引用
|
||||
ref, err := r.Reference(plumbing.HEAD, true)
|
||||
CheckIfError(err)
|
||||
|
||||
// 解析 HEAD 引用到提交对象
|
||||
commit, err := r.CommitObject(ref.Hash())
|
||||
CheckIfError(err)
|
||||
|
||||
// 获取提交对象的树对象
|
||||
tree, err := commit.Tree()
|
||||
CheckIfError(err)
|
||||
|
||||
// 遍历树对象中的文件
|
||||
tree.Files().ForEach(func(f *object.File) error {
|
||||
if strings.HasPrefix(f.Name, find_directory+"/") {
|
||||
fmt.Printf("Found file: %s\n", f.Name)
|
||||
|
||||
// 获取 blob 对象
|
||||
blob, err := r.BlobObject(f.Blob.Hash)
|
||||
CheckIfError(err)
|
||||
|
||||
// 获取 blob 的内容
|
||||
blobReader, err := blob.Reader()
|
||||
CheckIfError(err)
|
||||
|
||||
defer blobReader.Close()
|
||||
|
||||
// 读取 blob 的内容
|
||||
fileContent, err := ioutil.ReadAll(blobReader)
|
||||
CheckIfError(err)
|
||||
|
||||
// 打印文件内容
|
||||
fmt.Printf("%s:\n%s\n", f.Name, fileContent)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
// 删除本地仓库
|
||||
Info("Removing the local repository")
|
||||
err = os.RemoveAll(directory)
|
||||
CheckIfError(err)
|
||||
}
|
||||
|
|
@ -4,25 +4,25 @@ import (
|
|||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/go-git/go-git/v5/plumbing/transport/http"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
. "github.com/go-git/go-git/v5/_examples"
|
||||
"github.com/go-git/go-git/v5/plumbing"
|
||||
"github.com/go-git/go-git/v5/plumbing/object"
|
||||
"github.com/go-git/go-git/v5/plumbing/transport/http"
|
||||
)
|
||||
|
||||
func main() {
|
||||
CheckArgs("<url>", "<github_username>", "<github_password>", "<directory_path>")
|
||||
url, username, password, directory := os.Args[1], os.Args[2], os.Args[3], os.Args[4]
|
||||
CheckArgs("<url>", "<github_username>", "<github_password>")
|
||||
url, username, password := os.Args[1], os.Args[2], os.Args[3]
|
||||
|
||||
local_repo := "./local-repo"
|
||||
directory := "./local-repo"
|
||||
|
||||
// 克隆裸仓库到本地
|
||||
Info("git clone %s %s", url, local_repo)
|
||||
Info("git clone %s %s", url, directory)
|
||||
|
||||
_, err := git.PlainClone(local_repo, false, &git.CloneOptions{
|
||||
_, err := git.PlainClone(directory, false, &git.CloneOptions{
|
||||
Auth: &http.BasicAuth{
|
||||
Username: username,
|
||||
Password: password,
|
||||
|
|
@ -33,7 +33,8 @@ func main() {
|
|||
CheckIfError(err)
|
||||
|
||||
// 打开本地仓库
|
||||
r, err := git.PlainOpen(local_repo)
|
||||
r, err := git.PlainOpen(directory)
|
||||
CheckIfError(err)
|
||||
|
||||
// 获取 HEAD 引用
|
||||
ref, err := r.Reference(plumbing.HEAD, true)
|
||||
|
|
@ -49,32 +50,30 @@ func main() {
|
|||
|
||||
// 遍历树对象中的文件
|
||||
tree.Files().ForEach(func(f *object.File) error {
|
||||
if strings.HasPrefix(f.Name, directory+"/") {
|
||||
fmt.Printf("Found file: %s\n", f.Name)
|
||||
fmt.Printf("Found file: %s\n", f.Name)
|
||||
|
||||
// 获取 blob 对象
|
||||
blob, err := r.BlobObject(f.Blob.Hash)
|
||||
CheckIfError(err)
|
||||
// 获取 blob 对象
|
||||
blob, err := r.BlobObject(f.Blob.Hash)
|
||||
CheckIfError(err)
|
||||
|
||||
// 获取 blob 的内容
|
||||
blobReader, err := blob.Reader()
|
||||
CheckIfError(err)
|
||||
// 获取 blob 的内容
|
||||
blobReader, err := blob.Reader()
|
||||
CheckIfError(err)
|
||||
|
||||
defer blobReader.Close()
|
||||
defer blobReader.Close()
|
||||
|
||||
// 读取 blob 的内容
|
||||
fileContent, err := ioutil.ReadAll(blobReader)
|
||||
CheckIfError(err)
|
||||
// 读取 blob 的内容
|
||||
fileContent, err := ioutil.ReadAll(blobReader)
|
||||
CheckIfError(err)
|
||||
|
||||
// 打印文件内容c
|
||||
fmt.Printf("%s:\n%s\n", f.Name, fileContent)
|
||||
}
|
||||
// 打印文件内容c
|
||||
fmt.Printf("%s:\n%s\n", f.Name, fileContent)
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
// 删除本地仓库
|
||||
Info("Removing the local repository")
|
||||
err = os.RemoveAll(local_repo)
|
||||
err = os.RemoveAll(directory)
|
||||
CheckIfError(err)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue