diff --git a/auto-function.go b/auto-function.go index 112b97f..28ae597 100644 --- a/auto-function.go +++ b/auto-function.go @@ -120,7 +120,17 @@ func FormattedFunction(diff, lang string) string { return "" } - return strings.Join(results, " | ") + parser := strings.Join(results, " | ") + for len(parser) > int(MAX_COMMIT_LENGTH) && len(results) > 1 { + results = results[:len(results)-1] + parser = strings.Join(results, " | ") + } + + if len(parser) > int(MAX_COMMIT_LENGTH) && len(results) == 1 { + parser = parser[:int(MAX_COMMIT_LENGTH)] + } + + return parser } func parseGoFunction(line string) *types.FunctionSignature { diff --git a/auto-import.go b/auto-import.go index 5870b18..f4664a9 100644 --- a/auto-import.go +++ b/auto-import.go @@ -85,7 +85,17 @@ func FormattedImport(diff, lang, filename string) string { quoted[i] = "'" + imp + "'" } - return "included " + strings.Join(quoted, ", ") + " in " + filename + result := "included " + strings.Join(quoted, ", ") + " in " + filename + for len(result) > int(MAX_COMMIT_LENGTH) && len(quoted) > 1 { + quoted = quoted[:len(quoted)-1] + result = "included " + strings.Join(quoted, ", ") + " in " + filename + } + + if len(result) > int(MAX_COMMIT_LENGTH) && len(quoted) == 1 { + result = result[:int(MAX_COMMIT_LENGTH)] + } + + return result } return "" diff --git a/auto-logic.go b/auto-logic.go index c321232..2e13794 100644 --- a/auto-logic.go +++ b/auto-logic.go @@ -75,7 +75,7 @@ func extractIfBlocks(lines []string, lang string, isNew bool) []string { case "python": ifRegex = regexp.MustCompile(`if\s+([^:]+):`) case "go": - ifRegex = regexp.MustCompile(`^\s*if\b`) + ifRegex = regexp.MustCompile(`^\s*if\s+(.*)`) case "c", "cpp", "java", "csharp", "typescript", "javascript": ifRegex = regexp.MustCompile(`if\s*\(([^)]+)\)`) default: @@ -99,8 +99,11 @@ func extractIfBlocks(lines []string, lang string, isNew bool) []string { return blocks } -func _(expr string) string { // describeCondition +func describeCondition(expr string) string { expr = strings.TrimSpace(expr) + if idx := strings.Index(expr, "{"); idx != -1 { + expr = strings.TrimSpace(expr[:idx]) + } replacements := []struct { pattern *regexp.Regexp @@ -112,6 +115,11 @@ func _(expr string) string { // describeCondition {regexp.MustCompile(`(\w+)\s*>\s*(\d+)`), "if $1 is greater than $2"}, } + // {regexp.MustCompile(`(.+?)\s*==\s*"?(.+?)"?$`), "if $1 is equal to $2"}, + // {regexp.MustCompile(`(.+?)\s*!=\s*"?(.+?)"?$`), "if $1 is not equal to $2"}, + // {regexp.MustCompile(`(.+?)\s*<\s*(.+?)$`), "if $1 is less than $2"}, + // {regexp.MustCompile(`(.+?)\s*>\s*(.+?)$`), "if $1 is greater than $2"}, + for _, r := range replacements { if r.pattern.MatchString(expr) { return r.pattern.ReplaceAllString(expr, r.replace) @@ -136,65 +144,135 @@ func FormattedLogic(line, lang, filename string) string { if len(newIfs) > 0 && len(oldIfs) == 0 { builder.Reset() - builder.WriteString("added logic to the ") - builder.WriteString("'") - builder.WriteString(filename) - builder.WriteString("'") - builder.WriteString(" file") - return builder.String() - } - if len(oldSwitches) > 0 && len(newSwitches) == 0 { - builder.WriteString("deleted switch: ") - for i, sw := range oldSwitches { + for i, cond := range newIfs { if i > 0 { builder.WriteString("; ") } - builder.WriteString(sw.Expr) - if len(sw.Cases) > 0 { - builder.WriteString(" (cases: ") - builder.WriteString(strings.ReplaceAll(strings.Join(sw.Cases, ", "), "\"", "'")) - builder.WriteString(")") + builder.WriteString(describeCondition(cond)) + } + + result := builder.String() + for len(result) > int(MAX_COMMIT_LENGTH) && len(newIfs) > 1 { + newIfs = newIfs[:len(newIfs)-1] + builder.Reset() + + for i, cond := range newIfs { + if i > 0 { + builder.WriteString("; ") + } + + builder.WriteString(describeCondition(cond)) } + + result = builder.String() + } + + if len(result) > int(MAX_COMMIT_LENGTH) && len(newIfs) == 1 { + result = result[:int(MAX_COMMIT_LENGTH)] } - return builder.String() + return result } - if len(newSwitches) > 0 && len(oldSwitches) == 0 { - builder.WriteString("added switch: ") - for i, sw := range newSwitches { - if i > 0 { - builder.WriteString("; ") + if len(oldSwitches) > 0 && len(newSwitches) == 0 { + makeResult := func(switches []types.SwitchSignature) string { + var b strings.Builder + b.WriteString("removed switch on ") + for i, sw := range switches { + if i > 0 { + b.WriteString("; ") + } + b.WriteString("'" + sw.Expr + "'") + if len(sw.Cases) > 0 { + b.WriteString(" with cases: ") + b.WriteString(strings.ReplaceAll(strings.Join(sw.Cases, ", "), "\"", "'")) + } } - builder.WriteString(sw.Expr) - if len(sw.Cases) > 0 { - builder.WriteString(" (cases: ") - builder.WriteString(strings.ReplaceAll(strings.Join(sw.Cases, ", "), "\"", "'")) - builder.WriteString(")") + return b.String() + } + + result := makeResult(oldSwitches) + for len(result) > int(MAX_COMMIT_LENGTH) && len(oldSwitches) > 1 { + oldSwitches = oldSwitches[:len(oldSwitches)-1] + result = makeResult(oldSwitches) + } + + if len(result) > int(MAX_COMMIT_LENGTH) && len(oldSwitches) == 1 { + result = result[:int(MAX_COMMIT_LENGTH)] + } + + return result + } + + if len(newSwitches) > 0 && len(oldSwitches) == 0 { + makeResult := func(switches []types.SwitchSignature) string { + var b strings.Builder + b.WriteString("added switch on ") + for i, sw := range switches { + if i > 0 { + b.WriteString("; ") + } + b.WriteString("'" + sw.Expr + "'") + if len(sw.Cases) > 0 { + b.WriteString(" with cases: ") + b.WriteString(strings.ReplaceAll(strings.Join(sw.Cases, ", "), "\"", "'")) + } } + + return b.String() } - return builder.String() + result := makeResult(newSwitches) + for len(result) > int(MAX_COMMIT_LENGTH) && len(newSwitches) > 1 { + newSwitches = newSwitches[:len(newSwitches)-1] + result = makeResult(newSwitches) + } + + if len(result) > int(MAX_COMMIT_LENGTH) && len(newSwitches) == 1 { + result = result[:int(MAX_COMMIT_LENGTH)] + } + + return result } if len(oldSwitches) > 0 && len(newSwitches) > 0 { osw := oldSwitches[0] nsw := newSwitches[0] - if osw.Expr != nsw.Expr || strings.Join(osw.Cases, ",") != strings.Join(nsw.Cases, ",") { - builder.WriteString("changed logic switch '") - builder.WriteString(osw.Expr) - builder.WriteString(" (cases: ") - builder.WriteString(strings.Join(osw.Cases, ", ")) - builder.WriteString(")' -> '") - builder.WriteString(nsw.Expr) - builder.WriteString(" (cases: ") - builder.WriteString(strings.ReplaceAll(strings.Join(nsw.Cases, ", "), "\"", "'")) - builder.WriteString(")'") - return builder.String() + + makeResult := func(osw, nsw types.SwitchSignature) string { + var b strings.Builder + b.WriteString("changed switch from '") + b.WriteString(osw.Expr) + b.WriteString("' (cases: ") + b.WriteString(strings.Join(osw.Cases, ", ")) + b.WriteString(") to '") + b.WriteString(nsw.Expr) + b.WriteString("' (cases: ") + b.WriteString(strings.ReplaceAll(strings.Join(nsw.Cases, ", "), "\"", "'")) + b.WriteString(")") + + return b.String() + } + + result := makeResult(osw, nsw) + for len(result) > int(MAX_COMMIT_LENGTH) && (len(osw.Cases) > 1 || len(nsw.Cases) > 1) { + if len(osw.Cases) > len(nsw.Cases) { + osw.Cases = osw.Cases[:len(osw.Cases)-1] + } else { + nsw.Cases = nsw.Cases[:len(nsw.Cases)-1] + } + + result = makeResult(osw, nsw) } + + if len(result) > int(MAX_COMMIT_LENGTH) && len(osw.Cases) == 1 && len(nsw.Cases) == 1 { + result = result[:int(MAX_COMMIT_LENGTH)] + } + + return result } return "" diff --git a/auto-variables.go b/auto-variables.go index 5f26fef..06354e1 100644 --- a/auto-variables.go +++ b/auto-variables.go @@ -128,5 +128,15 @@ func FormattedVariables(diff, lang string) string { return "" } - return strings.Join(results, " | ") + parser := strings.Join(results, " | ") + for len(parser) > int(MAX_COMMIT_LENGTH) && len(results) > 1 { + results = results[:len(results)-1] + parser = strings.Join(results, " | ") + } + + if len(parser) > int(MAX_COMMIT_LENGTH) && len(results) == 1 { + parser = parser[:int(MAX_COMMIT_LENGTH)] + } + + return parser } diff --git a/bin/auto-commit b/bin/auto-commit index f59e4ea..2ef2f7d 100644 Binary files a/bin/auto-commit and b/bin/auto-commit differ diff --git a/define.go b/define.go index cc247b6..023e1f1 100644 --- a/define.go +++ b/define.go @@ -3,7 +3,8 @@ package main import "time" const ( - MAX_LINE_LENGTH uint16 = 1024 - MAX_COMMIT_LENGTH uint16 = 300 - COMMIT_TIME time.Duration = 27 * time.Second + MAX_LINE_LENGTH uint16 = 1024 + MAX_COMMIT_LENGTH uint16 = 300 + MAX_COMMIT_LENGTH_WATCHER uint16 = 60 + COMMIT_TIME time.Duration = 17 * time.Second ) diff --git a/parser.go b/parser.go index 5b48648..b070413 100644 --- a/parser.go +++ b/parser.go @@ -39,7 +39,7 @@ func Parser(files []string) (string, error) { for file := range jobs { mu.Lock() - if uint16(len(strings.Fields(payloadMsg))) > MAX_COMMIT_LENGTH { + if uint16(len(payloadMsg)) > MAX_COMMIT_LENGTH { mu.Unlock() continue } @@ -79,7 +79,20 @@ func Parser(files []string) (string, error) { if len(fileChanges) > 0 { mu.Lock() for _, change := range fileChanges { - payloadMsg = appendMsg(payloadMsg, change) + nextMsg := appendMsg(payloadMsg, change) + if len(nextMsg) > int(MAX_COMMIT_LENGTH) { + if len(payloadMsg) == 0 { + if len(change) > int(MAX_COMMIT_LENGTH) { + change = change[:int(MAX_COMMIT_LENGTH)] + } + + payloadMsg = change + } + + break + } + + payloadMsg = nextMsg } mu.Unlock() } diff --git a/watcher.go b/watcher.go index 5a4112c..13b6bd3 100644 --- a/watcher.go +++ b/watcher.go @@ -79,7 +79,7 @@ func WatchCommit(path string) { return } - if len(strings.Fields(parser)) >= 255 { + if uint16(len(parser)) >= MAX_COMMIT_LENGTH_WATCHER { if err := Commit(parser); err != nil { ErrorLogger(err) }