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