fork of go-git with some jj specific features
at v5.11.0 3.8 kB view raw
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_CreateRemote() { 141 r, _ := git.Init(memory.NewStorage(), nil) 142 143 // Add a new remote, with the default fetch refspec 144 _, err := r.CreateRemote(&config.RemoteConfig{ 145 Name: "example", 146 URLs: []string{"https://github.com/git-fixtures/basic.git"}, 147 }) 148 149 if err != nil { 150 log.Fatal(err) 151 } 152 153 list, err := r.Remotes() 154 if err != nil { 155 log.Fatal(err) 156 } 157 158 for _, r := range list { 159 fmt.Println(r) 160 } 161 162 // Example Output: 163 // example https://github.com/git-fixtures/basic.git (fetch) 164 // example https://github.com/git-fixtures/basic.git (push) 165}