mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-29 00:59:39 +09:00
enhance: fall back to DEFAULT_TEMPLATE.md when style-specific template is missing (#38803)
Closes #38801 Introduce `DEFAULT_TEMPLATE.md` as the default message for all merge styles. https://gitea.com/gitea/docs/pulls/491 By the way, fix incorrect os.Expand usage for merge message & repo template --------- Co-authored-by: waterWang <waterWang@users.noreply.github.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
co-authored by
waterWang
wxiaoguang
parent
a34cc4cac4
commit
2657756cac
@@ -5,14 +5,17 @@ package vars
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// Expand replaces all variables like {var} by `vars` map, it always returns the expanded string regardless of errors
|
||||
// if error occurs, the error part doesn't change and is returned as it is.
|
||||
func Expand(template string, vars map[string]string) (string, error) {
|
||||
// ExpandCurlyBrace replaces all variables like {var} by `vars` map,
|
||||
// it always returns the expanded string regardless of errors.
|
||||
// if error occurs (wrong syntax, missing variable), the error part doesn't change and is returned as it is.
|
||||
func ExpandCurlyBrace(template string, vars map[string]string) (string, error) {
|
||||
// in the future, if necessary, we can introduce some escape-char,
|
||||
// for example: it will use `#' as a reversed char, templates will use `{#{}` to do escape and output char '{'.
|
||||
var buf strings.Builder
|
||||
@@ -71,3 +74,26 @@ func Expand(template string, vars map[string]string) (string, error) {
|
||||
|
||||
return buf.String(), err
|
||||
}
|
||||
|
||||
var globalVars = sync.OnceValue(func() (ret struct {
|
||||
regexpShellLike *regexp.Regexp
|
||||
},
|
||||
) {
|
||||
ret.regexpShellLike = regexp.MustCompile(`(\$\{[a-zA-Z_]\w*\}|\$[a-zA-Z_]\w*)`)
|
||||
return ret
|
||||
})
|
||||
|
||||
// ExpandShellLike works like os.Expand, the difference is that this function keeps the non-existing keys
|
||||
func ExpandShellLike(template string, vars map[string]string) string {
|
||||
re := globalVars().regexpShellLike
|
||||
return re.ReplaceAllStringFunc(template, func(s string) string {
|
||||
key := s[1:]
|
||||
if strings.HasPrefix(key, "{") && strings.HasSuffix(key, "}") {
|
||||
key = key[1 : len(key)-1]
|
||||
}
|
||||
if val, ok := vars[key]; ok {
|
||||
return val
|
||||
}
|
||||
return s
|
||||
})
|
||||
}
|
||||
|
||||
@@ -9,8 +9,8 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestExpandVars(t *testing.T) {
|
||||
kases := []struct {
|
||||
func TestExpandCurlyBrace(t *testing.T) {
|
||||
cases := []struct {
|
||||
tmpl string
|
||||
data map[string]string
|
||||
out string
|
||||
@@ -57,11 +57,11 @@ func TestExpandVars(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
for _, kase := range kases {
|
||||
t.Run(kase.tmpl, func(t *testing.T) {
|
||||
res, err := Expand(kase.tmpl, kase.data)
|
||||
assert.Equal(t, kase.out, res)
|
||||
if kase.error {
|
||||
for _, c := range cases {
|
||||
t.Run(c.tmpl, func(t *testing.T) {
|
||||
res, err := ExpandCurlyBrace(c.tmpl, c.data)
|
||||
assert.Equal(t, c.out, res)
|
||||
if c.error {
|
||||
assert.Error(t, err)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
@@ -69,3 +69,19 @@ func TestExpandVars(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExpandShellLike(t *testing.T) {
|
||||
cases := []struct {
|
||||
tmpl string
|
||||
data map[string]string
|
||||
out string
|
||||
}{
|
||||
{tmpl: "$key ${key} $other ${other}", data: map[string]string{"key": "val"}, out: "val val $other ${other}"},
|
||||
{tmpl: "$ key ${key }", data: map[string]string{"key": "val"}, out: "$ key ${key }"},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
out := ExpandShellLike(c.tmpl, c.data)
|
||||
assert.Equal(t, c.out, out, "tmpl: %s", c.tmpl)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user