Skip to content

Commit

Permalink
jj: fix ActionRemoteBranches
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Apr 30, 2024
1 parent a33692f commit 2a76b17
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions pkg/actions/tools/jj/branch.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jj

import (
"fmt"
"regexp"
"strings"

Expand Down Expand Up @@ -34,24 +35,22 @@ func ActionLocalBranches() carapace.Action {
func ActionRemoteBranches(remote string) carapace.Action {
return carapace.ActionExecCommand("jj", "branch", "list", "--all")(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
r := regexp.MustCompile(`^(?P<branch>[^: ]+)[: ]+(?P<description>.*)$`)
rLocal := regexp.MustCompile(`^(?P<branch>[^@: ]+): (?P<changeid>[^ ]+) (?P<commitid>[^ ]+) (?P<description>.*)$`)
rRemote := regexp.MustCompile(`^(?P<branch>[^@: ]+)@(?P<remote>[^: ]+): (?P<changeid>[^ ]+) (?P<commitid>[^ ]+) (?P<description>.*)$`)
rTracking := regexp.MustCompile(`^ @(?P<remote>[^: ]+): (?P<changeid>[^ ]+) (?P<commitid>[^ ]+) (?P<description>.*)$`)

vals := make([]string, 0)
branch := ""
for _, line := range lines[:len(lines)-1] {
switch {
case strings.HasPrefix(line, " @"):
splitted := strings.SplitN(line, ": ", 2)
switch remote {
case "", strings.TrimPrefix(splitted[0], " @"):
splitted := strings.SplitN(line, ": ", 2)
description := strings.SplitN(splitted[1], " ", 3)[2]
remote := strings.SplitN(strings.TrimSpace(splitted[0]), " ", 2)[0]
vals = append(vals, branch+remote, description)
if matches := rTracking.FindStringSubmatch(line); matches != nil {
vals = append(vals, fmt.Sprintf("%v@%v", branch, matches[1]), matches[4])
}
case strings.HasPrefix(line, " "):
default:
if matches := r.FindStringSubmatch(line); matches != nil {
if matches := rRemote.FindStringSubmatch(line); matches != nil {
vals = append(vals, fmt.Sprintf("%v@%v", matches[1], matches[2]), matches[5])
} else if matches := rLocal.FindStringSubmatch(line); matches != nil {
branch = matches[1]
}
}
Expand Down

0 comments on commit 2a76b17

Please sign in to comment.