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 569116b
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 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 All @@ -15,12 +16,12 @@ import (
func ActionLocalBranches() carapace.Action {
return carapace.ActionExecCommand("jj", "branch", "list")(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>.*)$`)

vals := make([]string, 0)
for _, line := range lines[:len(lines)-1] {
if matches := r.FindStringSubmatch(line); matches != nil {
vals = append(vals, matches[1], matches[2])
if matches := rLocal.FindStringSubmatch(line); matches != nil {
vals = append(vals, matches[1], matches[4])
}
}
return carapace.ActionValuesDescribed(vals...).Style(styles.Git.Branch)
Expand All @@ -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 569116b

Please sign in to comment.