1package git_test
2
3import (
4 "fmt"
5 "io"
6 "log"
7 "os"
8 "path/filepath"
9
10 "github.com/go-git/go-git/v5"
11 "github.com/go-git/go-git/v5/config"
12 "github.com/go-git/go-git/v5/plumbing"
13 "github.com/go-git/go-git/v5/plumbing/transport/http"
14 "github.com/go-git/go-git/v5/storage/memory"
15
16 "github.com/go-git/go-billy/v5/memfs"
17)
18
19func ExampleClone() {
20 // Filesystem abstraction based on memory
21 fs := memfs.New()
22 // Git objects storer based on memory
23 storer := memory.NewStorage()
24
25 // Clones the repository into the worktree (fs) and stores all the .git
26 // content into the storer
27 _, err := git.Clone(storer, fs, &git.CloneOptions{
28 URL: "https://github.com/git-fixtures/basic.git",
29 })
30 if err != nil {
31 log.Fatal(err)
32 }
33
34 // Prints the content of the CHANGELOG file from the cloned repository
35 changelog, err := fs.Open("CHANGELOG")
36 if err != nil {
37 log.Fatal(err)
38 }
39
40 io.Copy(os.Stdout, changelog)
41 // Output: Initial changelog
42}
43
44func ExamplePlainClone() {
45 // Tempdir to clone the repository
46 dir, err := os.MkdirTemp("", "clone-example")
47 if err != nil {
48 log.Fatal(err)
49 }
50
51 defer os.RemoveAll(dir) // clean up
52
53 // Clones the repository into the given dir, just as a normal git clone does
54 _, err = git.PlainClone(dir, false, &git.CloneOptions{
55 URL: "https://github.com/git-fixtures/basic.git",
56 })
57
58 if err != nil {
59 log.Fatal(err)
60 }
61
62 // Prints the content of the CHANGELOG file from the cloned repository
63 changelog, err := os.Open(filepath.Join(dir, "CHANGELOG"))
64 if err != nil {
65 log.Fatal(err)
66 }
67
68 io.Copy(os.Stdout, changelog)
69 // Output: Initial changelog
70}
71
72func ExamplePlainClone_usernamePassword() {
73 // Tempdir to clone the repository
74 dir, err := os.MkdirTemp("", "clone-example")
75 if err != nil {
76 log.Fatal(err)
77 }
78
79 defer os.RemoveAll(dir) // clean up
80
81 // Clones the repository into the given dir, just as a normal git clone does
82 _, err = git.PlainClone(dir, false, &git.CloneOptions{
83 URL: "https://github.com/git-fixtures/basic.git",
84 Auth: &http.BasicAuth{
85 Username: "username",
86 Password: "password",
87 },
88 })
89
90 if err != nil {
91 log.Fatal(err)
92 }
93}
94
95func ExamplePlainClone_accessToken() {
96 // Tempdir to clone the repository
97 dir, err := os.MkdirTemp("", "clone-example")
98 if err != nil {
99 log.Fatal(err)
100 }
101
102 defer os.RemoveAll(dir) // clean up
103
104 // Clones the repository into the given dir, just as a normal git clone does
105 _, err = git.PlainClone(dir, false, &git.CloneOptions{
106 URL: "https://github.com/git-fixtures/basic.git",
107 Auth: &http.BasicAuth{
108 Username: "abc123", // anything except an empty string
109 Password: "github_access_token",
110 },
111 })
112
113 if err != nil {
114 log.Fatal(err)
115 }
116}
117
118func ExampleRepository_References() {
119 r, _ := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
120 URL: "https://github.com/git-fixtures/basic.git",
121 })
122
123 // simulating a git show-ref
124 refs, _ := r.References()
125 refs.ForEach(func(ref *plumbing.Reference) error {
126 if ref.Type() == plumbing.HashReference {
127 fmt.Println(ref)
128 }
129
130 return nil
131 })
132
133 // Example Output:
134 // 6ecf0ef2c2dffb796033e5a02219af86ec6584e5 refs/remotes/origin/master
135 // e8d3ffab552895c19b9fcf7aa264d277cde33881 refs/remotes/origin/branch
136 // 6ecf0ef2c2dffb796033e5a02219af86ec6584e5 refs/heads/master
137
138}
139
140func ExampleRepository_Branches() {
141 r, _ := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
142 URL: "https://github.com/git-fixtures/basic.git",
143 })
144
145 branches, _ := r.Branches()
146 branches.ForEach(func(branch *plumbing.Reference) error {
147 fmt.Println(branch.Hash().String(), branch.Name())
148 return nil
149 })
150
151 // Example Output:
152 // 6ecf0ef2c2dffb796033e5a02219af86ec6584e5 refs/heads/master
153}
154
155func ExampleRepository_CreateRemote() {
156 r, _ := git.Init(memory.NewStorage(), nil)
157
158 // Add a new remote, with the default fetch refspec
159 _, err := r.CreateRemote(&config.RemoteConfig{
160 Name: "example",
161 URLs: []string{"https://github.com/git-fixtures/basic.git"},
162 })
163
164 if err != nil {
165 log.Fatal(err)
166 }
167
168 list, err := r.Remotes()
169 if err != nil {
170 log.Fatal(err)
171 }
172
173 for _, r := range list {
174 fmt.Println(r)
175 }
176
177 // Example Output:
178 // example https://github.com/git-fixtures/basic.git (fetch)
179 // example https://github.com/git-fixtures/basic.git (push)
180}