A CLI for tangled.sh

pr(create): get remote branches, use select

authored by rockorager.dev and committed by Tangled 10085aae 401b830b

Changed files
+62 -14
git
pr
+1 -1
.gitignore
··· 1 - ./knit 1 + knit
+30 -1
git/git.go
··· 9 9 ) 10 10 11 11 type Remote = struct { 12 + Name string 12 13 Host string 13 14 Path string 14 15 } ··· 32 33 if len(fields) != 3 { 33 34 continue 34 35 } 36 + if fields[2] != "(fetch)" { 37 + continue 38 + } 39 + 35 40 u, err := normalizeGitURL(fields[1]) 36 41 if err != nil { 37 42 continue 38 43 } 44 + u.Name = fields[0] 39 45 40 46 for _, remote := range remotes { 41 47 if remote.Host == u.Host && remote.Path == u.Path { ··· 44 50 } 45 51 46 52 remotes = append(remotes, u) 47 - 48 53 } 49 54 50 55 return remotes, nil ··· 92 97 93 98 return string(output), nil 94 99 } 100 + 101 + func RemoteBranches(remote string) ([]string, error) { 102 + cmd := exec.Command("git", "branch", "--remotes", "--list", remote+"*") 103 + 104 + output, err := cmd.Output() 105 + if err != nil { 106 + return nil, err 107 + } 108 + 109 + branches := []string{} 110 + scanner := bufio.NewScanner(strings.NewReader(string(output))) 111 + 112 + for scanner.Scan() { 113 + line := strings.TrimSpace(scanner.Text()) 114 + branch := strings.TrimPrefix(line, remote+"/") 115 + if strings.HasPrefix(branch, "HEAD ->") { 116 + continue 117 + } 118 + 119 + branches = append(branches, branch) 120 + 121 + } 122 + return branches, nil 123 + }
+31 -12
pr/create.go
··· 65 65 return err 66 66 } 67 67 68 - targetBranch := "main" 69 - huh.NewInput(). 68 + branches, err := git.RemoteBranches(remote.Name) 69 + if err != nil { 70 + return err 71 + } 72 + 73 + options := make([]huh.Option[string], 0, len(branches)) 74 + for _, branch := range branches { 75 + options = append(options, huh.NewOption(branch, branch)) 76 + } 77 + 78 + var ( 79 + targetBranch string 80 + title string 81 + description string 82 + ) 83 + 84 + if err := huh.NewSelect[string](). 70 85 Title("Target branch"). 71 - Placeholder("main"). 72 - Value(&targetBranch).Run() 86 + Options(options...). 87 + Value(&targetBranch).Run(); err != nil { 88 + return err 89 + } 73 90 74 - var title string 75 - huh.NewInput(). 76 - Title("Pull Request Title"). 91 + if err := huh.NewInput(). 92 + Title("Title"). 77 93 Placeholder("(optional)"). 78 - Value(&title).Run() 94 + Value(&title).Run(); err != nil { 95 + return err 96 + } 79 97 80 - var description string 81 - huh.NewInput(). 82 - Title("Pull Request Description"). 98 + if err := huh.NewText(). 99 + Title("Description"). 83 100 Placeholder("(optional)"). 84 - Value(&description).Run() 101 + Value(&description).Run(); err != nil { 102 + return err 103 + } 85 104 86 105 form := url.Values{} 87 106 form.Add("title", title)