diff --git a/.gitignore b/.gitignore index 54c30c3..16015f8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -./knit +knit diff --git a/git/git.go b/git/git.go index 3817bd7..9d5c865 100644 --- a/git/git.go +++ b/git/git.go @@ -9,6 +9,7 @@ import ( ) type Remote = struct { + Name string Host string Path string } @@ -32,10 +33,15 @@ outer: if len(fields) != 3 { continue } + if fields[2] != "(fetch)" { + continue + } + u, err := normalizeGitURL(fields[1]) if err != nil { continue } + u.Name = fields[0] for _, remote := range remotes { if remote.Host == u.Host && remote.Path == u.Path { @@ -44,7 +50,6 @@ outer: } remotes = append(remotes, u) - } return remotes, nil @@ -92,3 +97,27 @@ func FormatPatch(revRange string) (string, error) { return string(output), nil } + +func RemoteBranches(remote string) ([]string, error) { + cmd := exec.Command("git", "branch", "--remotes", "--list", remote+"*") + + output, err := cmd.Output() + if err != nil { + return nil, err + } + + branches := []string{} + scanner := bufio.NewScanner(strings.NewReader(string(output))) + + for scanner.Scan() { + line := strings.TrimSpace(scanner.Text()) + branch := strings.TrimPrefix(line, remote+"/") + if strings.HasPrefix(branch, "HEAD ->") { + continue + } + + branches = append(branches, branch) + + } + return branches, nil +} diff --git a/pr/create.go b/pr/create.go index 0aa3b96..4ed261b 100644 --- a/pr/create.go +++ b/pr/create.go @@ -65,23 +65,42 @@ func Create(cmd *cobra.Command, args []string) error { return err } - targetBranch := "main" - huh.NewInput(). + branches, err := git.RemoteBranches(remote.Name) + if err != nil { + return err + } + + options := make([]huh.Option[string], 0, len(branches)) + for _, branch := range branches { + options = append(options, huh.NewOption(branch, branch)) + } + + var ( + targetBranch string + title string + description string + ) + + if err := huh.NewSelect[string](). Title("Target branch"). - Placeholder("main"). - Value(&targetBranch).Run() + Options(options...). + Value(&targetBranch).Run(); err != nil { + return err + } - var title string - huh.NewInput(). - Title("Pull Request Title"). + if err := huh.NewInput(). + Title("Title"). Placeholder("(optional)"). - Value(&title).Run() + Value(&title).Run(); err != nil { + return err + } - var description string - huh.NewInput(). - Title("Pull Request Description"). + if err := huh.NewText(). + Title("Description"). Placeholder("(optional)"). - Value(&description).Run() + Value(&description).Run(); err != nil { + return err + } form := url.Values{} form.Add("title", title)