Skip to content

Commit 98c2c35

Browse files
committed
fix(config): support jj extended revset-alias config
1 parent 4b59a3f commit 98c2c35

2 files changed

Lines changed: 75 additions & 2 deletions

File tree

internal/config/jj_config.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package config
22

33
import (
4+
"fmt"
5+
46
"github.com/BurntSushi/toml"
57
)
68

9+
type RevsetAliasMap map[string]string
10+
711
type JJConfig struct {
8-
Colors map[string]Color `toml:"colors"`
9-
RevsetAliases map[string]string `toml:"revset-aliases"`
12+
Colors map[string]Color `toml:"colors"`
13+
RevsetAliases RevsetAliasMap `toml:"revset-aliases"`
1014
Revsets struct {
1115
Log string `toml:"log"`
1216
} `toml:"revsets"`
@@ -51,3 +55,28 @@ func parseConfig(configContent string) (*JJConfig, error) {
5155
func DefaultConfig(output []byte) (*JJConfig, error) {
5256
return parseConfig(string(output))
5357
}
58+
59+
func (m *RevsetAliasMap) UnmarshalTOML(data any) error {
60+
rawMap, ok := data.(map[string]any)
61+
if !ok {
62+
return fmt.Errorf("expected a table for revset-aliases, got %T", data)
63+
}
64+
65+
*m = make(RevsetAliasMap)
66+
67+
for key, value := range rawMap {
68+
switch v := value.(type) {
69+
case string:
70+
(*m)[key] = v
71+
case map[string]any:
72+
if def, ok := v["definition"].(string); ok {
73+
(*m)[key] = def
74+
} else {
75+
return fmt.Errorf("missing or invalid 'definition' field in revset-alias object %q, %T", key, value)
76+
}
77+
default:
78+
return fmt.Errorf("expected string or object for revset-alias %q, got %T", key, value)
79+
}
80+
}
81+
return nil
82+
}

internal/config/jj_config_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package config
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestParseConfigRevsetAliases(t *testing.T) {
11+
t.Run("valid config parsing", func(t *testing.T) {
12+
tomlData := `
13+
[revsets]
14+
log = "all()"
15+
16+
[templates]
17+
log = "builtin_log_comfortable"
18+
19+
[revset-aliases]
20+
"@" = "HEAD"
21+
"my_alias" = { definition = "trunk()..", doc = "my_alias doc string" }
22+
23+
[revset-aliases."another_alias"]
24+
definition = "mine()"
25+
doc = "another_alias doc"
26+
27+
[revset-aliases."alias_func(to)"]
28+
definition = "heads(::to)"
29+
doc = "alias_func(to) doc"
30+
`
31+
config, err := parseConfig(tomlData)
32+
require.NoError(t, err)
33+
require.NotNil(t, config)
34+
35+
assert.Equal(t, "all()", config.Revsets.Log)
36+
assert.Equal(t, "builtin_log_comfortable", config.Templates.Log)
37+
38+
assert.Len(t, config.RevsetAliases, 4)
39+
assert.Equal(t, "HEAD", config.RevsetAliases["@"])
40+
assert.Equal(t, "trunk()..", config.RevsetAliases["my_alias"])
41+
assert.Equal(t, "mine()", config.RevsetAliases["another_alias"])
42+
assert.Equal(t, "heads(::to)", config.RevsetAliases["alias_func(to)"])
43+
})
44+
}

0 commit comments

Comments
 (0)