{"contents":"package git\n\nimport (\n\t\"strings\"\n\n\t\"github.com/go-git/go-git/v5/plumbing\"\n)\n\n// Branch represents a git branch.\ntype Branch struct {\n\tName string `json:\"name\"`\n\tHash string `json:\"hash\"`\n\tIsHead bool `json:\"isHead\"`\n}\n\n// Branches returns all branches in the repo.\nfunc (repo *Repo) Branches() ([]Branch, error) {\n\titer, err := repo.r.References()\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tdefer iter.Close()\n\n\tvar headHash plumbing.Hash\n\tif repo.head != nil {\n\t\theadHash = repo.head.Hash()\n\t}\n\tvar branches []Branch\n\n\terr = iter.ForEach(func(ref *plumbing.Reference) error {\n\t\tif !ref.Name().IsBranch() {\n\t\t\treturn nil\n\t\t}\n\t\tname := strings.TrimPrefix(string(ref.Name()), \"refs/heads/\")\n\t\tbranches = append(branches, Branch{\n\t\t\tName: name,\n\t\t\tHash: ref.Hash().String(),\n\t\t\tIsHead: ref.Hash() == headHash,\n\t\t})\n\t\treturn nil\n\t})\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\treturn branches, nil\n}\n\n// DefaultBranch returns the name of the default branch (from HEAD symbolic ref).\nfunc (repo *Repo) DefaultBranch() (string, error) {\n\thead, err := repo.s.Reference(plumbing.HEAD)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tif head.Type() == plumbing.SymbolicReference {\n\t\treturn strings.TrimPrefix(string(head.Target()), \"refs/heads/\"), nil\n\t}\n\treturn \"\", nil\n}\n\n// SetDefaultBranch updates the HEAD symbolic ref.\nfunc (repo *Repo) SetDefaultBranch(branch string) error {\n\tref := plumbing.NewSymbolicReference(plumbing.HEAD, plumbing.NewBranchReferenceName(branch))\n\treturn repo.s.SetReference(ref)\n}\n","path":"git/branch.go","ref":"main"}