fork of go-git with some jj specific features
at main 1.3 kB view raw
1package main 2 3import ( 4 "os" 5 6 "github.com/go-git/go-git/v5" 7 . "github.com/go-git/go-git/v5/_examples" 8 "github.com/go-git/go-git/v5/plumbing" 9) 10 11// An example of how to create and remove branches or any other kind of reference. 12func main() { 13 CheckArgs("<url>", "<directory>") 14 url, directory := os.Args[1], os.Args[2] 15 16 // Clone the given repository to the given directory 17 Info("git clone %s %s", url, directory) 18 r, err := git.PlainClone(directory, false, &git.CloneOptions{ 19 URL: url, 20 }) 21 CheckIfError(err) 22 23 // Create a new branch to the current HEAD 24 Info("git branch my-branch") 25 26 headRef, err := r.Head() 27 CheckIfError(err) 28 29 // Create a new plumbing.HashReference object with the name of the branch 30 // and the hash from the HEAD. The reference name should be a full reference 31 // name and not an abbreviated one, as is used on the git cli. 32 // 33 // For tags we should use `refs/tags/%s` instead of `refs/heads/%s` used 34 // for branches. 35 ref := plumbing.NewHashReference("refs/heads/my-branch", headRef.Hash()) 36 37 // The created reference is saved in the storage. 38 err = r.Storer.SetReference(ref) 39 CheckIfError(err) 40 41 // Or deleted from it. 42 Info("git branch -D my-branch") 43 err = r.Storer.RemoveReference(ref.Name()) 44 CheckIfError(err) 45}