diff --git a/.github/workflows/internal-ci.yml b/.github/workflows/internal-ci.yml index 400d247..12b32db 100644 --- a/.github/workflows/internal-ci.yml +++ b/.github/workflows/internal-ci.yml @@ -39,7 +39,11 @@ jobs: if [[ -n $(git status -s) ]]; then git add . git commit -m "docs: update docs with PTerm-CI" - git push origin HEAD:${GITHUB_REF} + if [ "${{ github.event_name }}" == "pull_request" ]; then + git push origin HEAD:${{ github.head_ref }} + else + git push origin HEAD:${GITHUB_REF} + fi else echo "No changes to commit" fi diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..5d31963 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-05-06 - [Hoist regexp.MustCompile to package level variables] +**Learning:** In Go, the `regexp` package parses regular expressions and builds execution machines at compile time. Repeating `regexp.MustCompile` inside frequently executed functions introduces significant CPU and memory overhead. Moving them to global variables ensures they are compiled exactly once at application startup. +**Action:** When finding `regexp.MustCompile` inside functions, hoist them out to package-level variables, especially within loops, hot paths, or frequently called agent methods like `Execute`. diff --git a/docs/docs.md b/docs/docs.md index 392b31f..dc000b8 100755 --- a/docs/docs.md +++ b/docs/docs.md @@ -1043,4 +1043,4 @@ Run 'magi version --help' for more information on a specific command. --- -> **Documentation automatically generated with [PTerm](https://github.com/pterm/cli-template) on 06 February 2026** +> **Documentation automatically generated with [PTerm](https://github.com/pterm/cli-template) on 06 May 2026** diff --git a/internal/cli/i18n/agents.go b/internal/cli/i18n/agents.go index 8bd0a4f..3a73610 100644 --- a/internal/cli/i18n/agents.go +++ b/internal/cli/i18n/agents.go @@ -43,25 +43,23 @@ func (a *KeyExtractor) WaitForResults() []string { return []string{} // No dependencies, runs first } +var i18nPatterns = []*regexp.Regexp{ + // t('key') or t("key") + regexp.MustCompile(`(?:^|[^a-zA-Z0-9_])t\((?:'([^']+)'|"([^"]+)")\)`), + // i18n.t('key') or i18n.t("key") + regexp.MustCompile(`i18n\.t\((?:'([^']+)'|"([^"]+)")\)`), + // $t('key') or $t("key") + regexp.MustCompile(`\$t\((?:'([^']+)'|"([^"]+)")\)`), + // + regexp.MustCompile(`]+key=(?:'([^']+)'|"([^"]+)")`), + // + regexp.MustCompile(`]+keyName=(?:'([^']+)'|"([^"]+)")`), +} + func (a *KeyExtractor) Execute(input map[string]string) (string, error) { var keys []I18nKey lines := strings.Split(a.diff, "\n") - // Regex patterns for different i18n usage - // We use two capturing groups: one for single quotes, one for double quotes - patterns := []*regexp.Regexp{ - // t('key') or t("key") - regexp.MustCompile(`(?:^|[^a-zA-Z0-9_])t\((?:'([^']+)'|"([^"]+)")\)`), - // i18n.t('key') or i18n.t("key") - regexp.MustCompile(`i18n\.t\((?:'([^']+)'|"([^"]+)")\)`), - // $t('key') or $t("key") - regexp.MustCompile(`\$t\((?:'([^']+)'|"([^"]+)")\)`), - // - regexp.MustCompile(`]+key=(?:'([^']+)'|"([^"]+)")`), - // - regexp.MustCompile(`]+keyName=(?:'([^']+)'|"([^"]+)")`), - } - for _, line := range lines { // We only care about added lines if !strings.HasPrefix(line, "+") { @@ -71,7 +69,7 @@ func (a *KeyExtractor) Execute(input map[string]string) (string, error) { // Remove the "+" prefix content := line[1:] - for _, pattern := range patterns { + for _, pattern := range i18nPatterns { matches := pattern.FindAllStringSubmatch(content, -1) for _, match := range matches { // match[0] is full match diff --git a/internal/cli/ssh/add.go b/internal/cli/ssh/add.go index b309949..b4b5e90 100644 --- a/internal/cli/ssh/add.go +++ b/internal/cli/ssh/add.go @@ -73,14 +73,14 @@ func addConnection() { pterm.Success.Printf("Connection '%s' saved successfully!\n", alias) } +var sshAliasRegex = regexp.MustCompile("^[a-zA-Z0-9_-]+$") + func promptForAlias() (string, error) { var alias string var err error existing := viper.GetStringMap(ConfigSSHConnections) - aliasRegex := regexp.MustCompile("^[a-zA-Z0-9_-]+$") - for { alias, err = pterm.DefaultInteractiveTextInput.WithDefaultText("Connection Alias").Show() if err != nil { @@ -94,7 +94,7 @@ func promptForAlias() (string, error) { } // Check for valid characters (alphanumeric, -, _) - if !aliasRegex.MatchString(alias) { + if !sshAliasRegex.MatchString(alias) { pterm.Warning.Println("Alias can only contain letters, numbers, hyphens, and underscores") continue } diff --git a/pkg/utils/parsing.go b/pkg/utils/parsing.go index 5f18b7f..63b517f 100644 --- a/pkg/utils/parsing.go +++ b/pkg/utils/parsing.go @@ -2,12 +2,14 @@ package utils import "regexp" +// codeBlockRegex is used to extract content from markdown code blocks. +// It is non-greedy to extract only the first block. +var codeBlockRegex = regexp.MustCompile(`(\` + "`" + "`" + "`" + `[\w-]*)\n([\s\S]*?)(\` + "`" + "`" + "`" + `)`) + // RemoveCodeBlock removes code block tags from a string. // If no code block tags are found, it returns the original string. func RemoveCodeBlock(input string) string { - re := regexp.MustCompile(`(\` + "`" + "`" + "`" + `[\w-]*)\n([\s\S]*)(\` + "`" + "`" + "`" + `)`) - - matches := re.FindStringSubmatch(input) + matches := codeBlockRegex.FindStringSubmatch(input) if len(matches) == 0 { return input }