diff --git a/LGPL-2.1 b/LGPL-2 similarity index 100% rename from LGPL-2.1 rename to LGPL-2 diff --git a/ac/ac.go b/ac/ac.go new file mode 100644 index 0000000..ff860d8 --- /dev/null +++ b/ac/ac.go @@ -0,0 +1,36 @@ +package ac + +import ( + "fmt" + "git-auto-commit/achelper" + "git-auto-commit/achelper/logger" + "git-auto-commit/diff" + "git-auto-commit/git" + "git-auto-commit/parser" +) + +func AutoCommit() { + achelper.GetVersion(false) + + files, err := diff.GetStagedFiles() + if err != nil { + logger.ErrorLogger(fmt.Errorf("error getting staged files: %s", err.Error())) + return + } + + if len(files) == 0 { + logger.InfoLogger("No files staged for commit.") + return + } + + parserMsg, err := parser.Parser(files) + if err != nil { + logger.ErrorLogger(err) + return + } + + if err := git.Commit(parserMsg); err != nil { + logger.ErrorLogger(fmt.Errorf("error committing: %s", err.Error())) + return + } +} diff --git a/detected.go b/achelper/code/detected.go similarity index 88% rename from detected.go rename to achelper/code/detected.go index 67158b8..e3a7072 100644 --- a/detected.go +++ b/achelper/code/detected.go @@ -1,11 +1,11 @@ -package main +package code import ( "path/filepath" "strings" ) -func DetectLanguage(filename string) string { +var DetectLanguage = func(filename string) string { ext := strings.ToLower(filepath.Ext(filename)) switch ext { diff --git a/logger.go b/achelper/logger/logger.go similarity index 82% rename from logger.go rename to achelper/logger/logger.go index 73a9efe..d4b380a 100644 --- a/logger.go +++ b/achelper/logger/logger.go @@ -1,11 +1,11 @@ -package main +package logger import ( "fmt" "strings" ) -func InfoLogger(msg string) { +var InfoLogger = func(msg string) { var builder strings.Builder builder.Reset() builder.WriteString("[git auto-commit] ") @@ -13,7 +13,7 @@ func InfoLogger(msg string) { fmt.Println(builder.String()) } -func GitLogger(msg string) { +var GitLogger = func(msg string) { var builder strings.Builder builder.Reset() builder.WriteString("\033[0;34m[git auto-commit] ") @@ -22,7 +22,7 @@ func GitLogger(msg string) { fmt.Print(builder.String()) } -func ErrorLogger(err error) { +var ErrorLogger = func(err error) { var builder strings.Builder builder.Reset() builder.WriteString("\033[0;31m[git auto-commit] ") diff --git a/update.go b/achelper/update.go similarity index 68% rename from update.go rename to achelper/update.go index 66526e9..90074fd 100644 --- a/update.go +++ b/achelper/update.go @@ -1,7 +1,11 @@ -package main +package achelper import ( "fmt" + "git-auto-commit/achelper/logger" + "git-auto-commit/config" + "git-auto-commit/constants" + "git-auto-commit/git" "io" "net/http" "os" @@ -12,23 +16,23 @@ import ( ) func AutoCommitUpdate() { - root, err := GetGitRoot() + root, err := git.GetGitRoot() if err != nil { - ErrorLogger(err) + logger.ErrorLogger(err) return } - versionFile := filepath.Join(root, ".git", "hooks", VERSION_FILE) + versionFile := filepath.Join(root, ".git", "hooks", constants.VERSION_FILE) version, err := os.ReadFile(versionFile) if err != nil { - ErrorLogger(fmt.Errorf("unknown version for auto-commit, please re-install: %w", err)) + logger.ErrorLogger(fmt.Errorf("unknown version for auto-commit, please re-install: %w", err)) return } - resp, err := http.Get(GITHUB_API_REPO_URL + "/releases/latest") + resp, err := http.Get(constants.GITHUB_API_REPO_URL + "/releases/latest") if err != nil { - ErrorLogger(fmt.Errorf("could not check latest version: %w", err)) + logger.ErrorLogger(fmt.Errorf("could not check latest version: %w", err)) return } defer resp.Body.Close() @@ -36,8 +40,8 @@ func AutoCommitUpdate() { var data struct { TagName string `json:"tag_name"` } - if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { - ErrorLogger(fmt.Errorf("could not parse version info: %w", err)) + if err := config.JSON.NewDecoder(resp.Body).Decode(&data); err != nil { + logger.ErrorLogger(fmt.Errorf("could not parse version info: %w", err)) return } @@ -61,7 +65,7 @@ func AutoCommitUpdate() { tmpFile := filepath.Join(os.TempDir(), "auto-commit-update"+scriptUpdateExt) err = downloadFile(scriptUpdate, tmpFile) if err != nil { - ErrorLogger(fmt.Errorf("failed to download update script: %v", err)) + logger.ErrorLogger(fmt.Errorf("failed to download update script: %v", err)) return } defer os.Remove(tmpFile) @@ -77,7 +81,7 @@ func AutoCommitUpdate() { cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil { - ErrorLogger(fmt.Errorf("failed to run update script: %v", err)) + logger.ErrorLogger(fmt.Errorf("failed to run update script: %v", err)) return } } diff --git a/achelper/version.go b/achelper/version.go new file mode 100644 index 0000000..9c87b59 --- /dev/null +++ b/achelper/version.go @@ -0,0 +1,53 @@ +package achelper + +import ( + "fmt" + "git-auto-commit/achelper/logger" + "git-auto-commit/config" + "git-auto-commit/constants" + "git-auto-commit/git" + "net/http" + "os" + "path/filepath" + "strings" +) + +var GetVersion = func(isCurrent bool) { + root, err := git.GetGitRoot() + if err != nil { + logger.ErrorLogger(fmt.Errorf("could not get git root: %w", err)) + return + } + + versionFile := filepath.Join(root, ".git", "hooks", constants.VERSION_FILE) + + version, err := os.ReadFile(versionFile) + if err != nil { + logger.ErrorLogger(fmt.Errorf("unknown version for auto-commit, please re-install: %w", err)) + return + } + + if isCurrent { + fmt.Println("[git auto-commit] current version:", strings.TrimSpace(string(version))) + } + + resp, err := http.Get(constants.GITHUB_API_REPO_URL + "/releases/latest") + if err != nil { + logger.ErrorLogger(fmt.Errorf("could not check latest version: %w", err)) + return + } + defer resp.Body.Close() + + var data struct { + TagName string `json:"tag_name"` + } + if err := config.JSON.NewDecoder(resp.Body).Decode(&data); err != nil { + logger.ErrorLogger(fmt.Errorf("could not parse version info: %w", err)) + return + } + + if strings.TrimSpace(string(version)) != strings.TrimSpace(data.TagName) { + fmt.Printf("\033[33m[!] a new version is available: %s\033[0m\n", strings.TrimSpace(data.TagName)) + fmt.Printf("\033[33m[!] please update! 'git auto -u'\033[0m\n") + } +} diff --git a/watcher.go b/achelper/watcher.go similarity index 61% rename from watcher.go rename to achelper/watcher.go index 1dd86a1..b7dbd51 100644 --- a/watcher.go +++ b/achelper/watcher.go @@ -1,7 +1,12 @@ -package main +package achelper import ( "fmt" + "git-auto-commit/achelper/logger" + "git-auto-commit/constants" + "git-auto-commit/diff" + "git-auto-commit/git" + "git-auto-commit/parser" "os" "os/exec" "os/signal" @@ -18,12 +23,12 @@ func WatchCommit(path string) { watcher, err := fsnotify.NewWatcher() if err != nil { - ErrorLogger(err) + logger.ErrorLogger(err) return } defer watcher.Close() - InfoLogger("Started commit watcher...") + logger.InfoLogger("Started commit watcher...") if err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error { if info.IsDir() && !strings.HasPrefix(path, ".git") { @@ -34,7 +39,7 @@ func WatchCommit(path string) { return nil }); err != nil { - ErrorLogger(err) + logger.ErrorLogger(err) return } @@ -42,7 +47,7 @@ func WatchCommit(path string) { signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) go func() { <-sigs - InfoLogger("shutdown work watcher...") + logger.InfoLogger("shutdown work watcher...") os.Exit(0) }() @@ -61,36 +66,36 @@ func WatchCommit(path string) { } if err := exec.Command("git", "add", ".").Run(); err != nil { - ErrorLogger(err) + logger.ErrorLogger(err) return } - files, err := GetStagedFiles() + files, err := diff.GetStagedFiles() if err != nil { - ErrorLogger(fmt.Errorf("error getting staged files: %s", err.Error())) + logger.ErrorLogger(fmt.Errorf("error getting staged files: %s", err.Error())) return } if len(files) == 0 { - InfoLogger("No files staged for commit.") + logger.InfoLogger("No files staged for commit.") } - parser, err := Parser(files) + parser, err := parser.Parser(files) if err != nil { - ErrorLogger(err) + logger.ErrorLogger(err) return } - if uint16(len(parser)) >= MAX_COMMIT_LENGTH_WATCHER { - if err := Commit(parser); err != nil { - ErrorLogger(err) + if uint16(len(parser)) >= constants.MAX_COMMIT_LENGTH_WATCHER { + if err := git.Commit(parser); err != nil { + logger.ErrorLogger(err) } } } case err := <-watcher.Errors: - ErrorLogger(err) + logger.ErrorLogger(err) } - time.Sleep(COMMIT_TIME) + time.Sleep(constants.COMMIT_TIME) } } diff --git a/acpkg/acpkg-comment.go b/acpkg/acpkg-comment.go new file mode 100644 index 0000000..f3537b5 --- /dev/null +++ b/acpkg/acpkg-comment.go @@ -0,0 +1 @@ +package acpkg diff --git a/auto-function.go b/acpkg/acpkg-function.go similarity index 96% rename from auto-function.go rename to acpkg/acpkg-function.go index 28ae597..64fff20 100644 --- a/auto-function.go +++ b/acpkg/acpkg-function.go @@ -1,6 +1,7 @@ -package main +package acpkg import ( + "git-auto-commit/constants" "git-auto-commit/types" "regexp" "strings" @@ -121,13 +122,13 @@ func FormattedFunction(diff, lang string) string { } parser := strings.Join(results, " | ") - for len(parser) > int(MAX_COMMIT_LENGTH) && len(results) > 1 { + for len(parser) > int(constants.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)] + if len(parser) > int(constants.MAX_COMMIT_LENGTH) && len(results) == 1 { + parser = parser[:int(constants.MAX_COMMIT_LENGTH)] } return parser diff --git a/auto-import.go b/acpkg/acpkg-import.go similarity index 90% rename from auto-import.go rename to acpkg/acpkg-import.go index f4664a9..26f8413 100644 --- a/auto-import.go +++ b/acpkg/acpkg-import.go @@ -1,6 +1,7 @@ -package main +package acpkg import ( + "git-auto-commit/constants" "regexp" "strings" ) @@ -86,13 +87,13 @@ func FormattedImport(diff, lang, filename string) string { } result := "included " + strings.Join(quoted, ", ") + " in " + filename - for len(result) > int(MAX_COMMIT_LENGTH) && len(quoted) > 1 { + for len(result) > int(constants.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)] + if len(result) > int(constants.MAX_COMMIT_LENGTH) && len(quoted) == 1 { + result = result[:int(constants.MAX_COMMIT_LENGTH)] } return result diff --git a/auto-logic.go b/acpkg/acpkg-logic.go similarity index 87% rename from auto-logic.go rename to acpkg/acpkg-logic.go index 2e13794..a190409 100644 --- a/auto-logic.go +++ b/acpkg/acpkg-logic.go @@ -1,6 +1,7 @@ -package main +package acpkg import ( + "git-auto-commit/constants" "git-auto-commit/types" "regexp" "strings" @@ -154,7 +155,7 @@ func FormattedLogic(line, lang, filename string) string { } result := builder.String() - for len(result) > int(MAX_COMMIT_LENGTH) && len(newIfs) > 1 { + for len(result) > int(constants.MAX_COMMIT_LENGTH) && len(newIfs) > 1 { newIfs = newIfs[:len(newIfs)-1] builder.Reset() @@ -169,8 +170,8 @@ func FormattedLogic(line, lang, filename string) string { result = builder.String() } - if len(result) > int(MAX_COMMIT_LENGTH) && len(newIfs) == 1 { - result = result[:int(MAX_COMMIT_LENGTH)] + if len(result) > int(constants.MAX_COMMIT_LENGTH) && len(newIfs) == 1 { + result = result[:int(constants.MAX_COMMIT_LENGTH)] } return result @@ -195,13 +196,13 @@ func FormattedLogic(line, lang, filename string) string { } result := makeResult(oldSwitches) - for len(result) > int(MAX_COMMIT_LENGTH) && len(oldSwitches) > 1 { + for len(result) > int(constants.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)] + if len(result) > int(constants.MAX_COMMIT_LENGTH) && len(oldSwitches) == 1 { + result = result[:int(constants.MAX_COMMIT_LENGTH)] } return result @@ -226,13 +227,13 @@ func FormattedLogic(line, lang, filename string) string { } result := makeResult(newSwitches) - for len(result) > int(MAX_COMMIT_LENGTH) && len(newSwitches) > 1 { + for len(result) > int(constants.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)] + if len(result) > int(constants.MAX_COMMIT_LENGTH) && len(newSwitches) == 1 { + result = result[:int(constants.MAX_COMMIT_LENGTH)] } return result @@ -258,7 +259,7 @@ func FormattedLogic(line, lang, filename string) string { } result := makeResult(osw, nsw) - for len(result) > int(MAX_COMMIT_LENGTH) && (len(osw.Cases) > 1 || len(nsw.Cases) > 1) { + for len(result) > int(constants.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 { @@ -268,8 +269,8 @@ func FormattedLogic(line, lang, filename string) string { 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)] + if len(result) > int(constants.MAX_COMMIT_LENGTH) && len(osw.Cases) == 1 && len(nsw.Cases) == 1 { + result = result[:int(constants.MAX_COMMIT_LENGTH)] } return result diff --git a/auto-oop.go b/acpkg/acpkg-oop.go similarity index 99% rename from auto-oop.go rename to acpkg/acpkg-oop.go index 59c2690..9cd7536 100644 --- a/auto-oop.go +++ b/acpkg/acpkg-oop.go @@ -1,4 +1,4 @@ -package main +package acpkg import ( "git-auto-commit/types" diff --git a/auto-remote.go b/acpkg/acpkg-remote.go similarity index 73% rename from auto-remote.go rename to acpkg/acpkg-remote.go index cdde711..f481fac 100644 --- a/auto-remote.go +++ b/acpkg/acpkg-remote.go @@ -1,6 +1,7 @@ -package main +package acpkg import ( + "git-auto-commit/git" "strconv" "strings" ) @@ -9,22 +10,22 @@ func FormattedByRemote(token string) (string, error) { var builder strings.Builder builder.Reset() - branch, err := GetCurrentBranch() + branch, err := git.GetCurrentBranch() if err != nil { return "", err } - issue := ExtractIssueNumber(branch) + issue := git.ExtractIssueNumber(branch) if issue == "" { return "", nil } - owner, repo, err := GetOwnerRepository() + owner, repo, err := git.GetOwnerRepository() if err != nil { return "", err } - issueName, issueNumber, err := GetIssueData(owner, repo, issue, token) + issueName, issueNumber, err := git.GetIssueData(owner, repo, issue, token) if err != nil { return "", err } @@ -41,7 +42,7 @@ func FormattedByBranch() (string, error) { var builder strings.Builder builder.Reset() - branch, err := GetCurrentBranch() + branch, err := git.GetCurrentBranch() if err != nil { return "", err } diff --git a/auto-structure.go b/acpkg/acpkg-structure.go similarity index 99% rename from auto-structure.go rename to acpkg/acpkg-structure.go index d1826d8..7a170fc 100644 --- a/auto-structure.go +++ b/acpkg/acpkg-structure.go @@ -1,4 +1,4 @@ -package main +package acpkg import ( "git-auto-commit/types" diff --git a/auto-variables.go b/acpkg/acpkg-variables.go similarity index 94% rename from auto-variables.go rename to acpkg/acpkg-variables.go index 06354e1..46e9d04 100644 --- a/auto-variables.go +++ b/acpkg/acpkg-variables.go @@ -1,6 +1,7 @@ -package main +package acpkg import ( + "git-auto-commit/constants" "git-auto-commit/types" "regexp" "strings" @@ -129,13 +130,13 @@ func FormattedVariables(diff, lang string) string { } parser := strings.Join(results, " | ") - for len(parser) > int(MAX_COMMIT_LENGTH) && len(results) > 1 { + for len(parser) > int(constants.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)] + if len(parser) > int(constants.MAX_COMMIT_LENGTH) && len(results) == 1 { + parser = parser[:int(constants.MAX_COMMIT_LENGTH)] } return parser diff --git a/auto-comment.go b/auto-comment.go deleted file mode 100644 index 06ab7d0..0000000 --- a/auto-comment.go +++ /dev/null @@ -1 +0,0 @@ -package main diff --git a/config/json.go b/config/json.go new file mode 100644 index 0000000..e981a89 --- /dev/null +++ b/config/json.go @@ -0,0 +1,5 @@ +package config + +import jsoniter "github.com/json-iterator/go" + +var JSON = jsoniter.ConfigCompatibleWithStandardLibrary diff --git a/define.go b/constants/define.go similarity index 96% rename from define.go rename to constants/define.go index 8da364a..ec6ec58 100644 --- a/define.go +++ b/constants/define.go @@ -1,4 +1,4 @@ -package main +package constants import "time" diff --git a/diff.go b/diff/diff.go similarity index 86% rename from diff.go rename to diff/diff.go index ba71fba..c5fcec1 100644 --- a/diff.go +++ b/diff/diff.go @@ -1,4 +1,4 @@ -package main +package diff import ( "bufio" @@ -6,6 +6,8 @@ import ( "os/exec" "strings" "sync" + + "git-auto-commit/git" ) var diffBufferPool = sync.Pool{ @@ -14,11 +16,11 @@ var diffBufferPool = sync.Pool{ }, } -func GetDiff(file string) (string, error) { +var GetDiff = func(file string) (string, error) { var builder strings.Builder builder.Reset() - root, err := GetGitRoot() + root, err := git.GetGitRoot() if err != nil { return "", err } @@ -41,7 +43,7 @@ func GetDiff(file string) (string, error) { return buf.String(), nil } -func GetStagedFiles() ([]string, error) { +var GetStagedFiles = func() ([]string, error) { cmd := exec.Command("git", "diff", "--cached", "--name-only") stdout, err := cmd.StdoutPipe() diff --git a/commit.go b/git/commit.go similarity index 51% rename from commit.go rename to git/commit.go index 74d3b7b..995974f 100644 --- a/commit.go +++ b/git/commit.go @@ -1,13 +1,14 @@ -package main +package git import ( "fmt" + "git-auto-commit/achelper/logger" "os" "os/exec" ) -func Commit(commitMsg string) error { - GitLogger(fmt.Sprintf("commit is: %s", commitMsg)) +var Commit = func(commitMsg string) error { + logger.GitLogger(fmt.Sprintf("commit is: %s", commitMsg)) cmd := exec.Command("git", "commit", "-m", commitMsg) cmd.Stdout = os.Stdout diff --git a/git.go b/git/git.go similarity index 97% rename from git.go rename to git/git.go index ecc4f2b..c736261 100644 --- a/git.go +++ b/git/git.go @@ -1,4 +1,4 @@ -package main +package git import ( "bytes" diff --git a/issues.go b/git/issues.go similarity index 93% rename from issues.go rename to git/issues.go index 111710b..d010753 100644 --- a/issues.go +++ b/git/issues.go @@ -1,8 +1,9 @@ -package main +package git import ( "bytes" "fmt" + "git-auto-commit/config" "git-auto-commit/types" "io" "net/http" @@ -79,7 +80,7 @@ func GetIssueData(owner, repo, issue, token string) (string, uint32, error) { } var githubIssue types.GithubIssue - if err := json.Unmarshal(buf.Bytes(), &githubIssue); err != nil { + if err := config.JSON.Unmarshal(buf.Bytes(), &githubIssue); err != nil { return "", 0, err } diff --git a/json.go b/json.go deleted file mode 100644 index b406a6a..0000000 --- a/json.go +++ /dev/null @@ -1,5 +0,0 @@ -package main - -import jsoniter "github.com/json-iterator/go" - -var json = jsoniter.ConfigCompatibleWithStandardLibrary diff --git a/main.go b/main.go index 2f9478a..0db4f56 100644 --- a/main.go +++ b/main.go @@ -2,14 +2,18 @@ package main import ( "fmt" + "git-auto-commit/ac" + "git-auto-commit/achelper" + "git-auto-commit/achelper/logger" + "git-auto-commit/git" "os" ) func main() { if len(os.Args) > 1 && (os.Args[1] == "-w" || os.Args[1] == "--watch") { - path, err := GetGitRoot() + path, err := git.GetGitRoot() if err != nil { - ErrorLogger(err) + logger.ErrorLogger(err) return } @@ -17,38 +21,12 @@ func main() { path = fmt.Sprintf("%s/%s", path, os.Args[2]) } - WatchCommit(path) + achelper.WatchCommit(path) } else if len(os.Args) > 1 && (os.Args[1] == "-v" || os.Args[1] == "--version") { - GetVersion(true) + achelper.GetVersion(true) } else if len(os.Args) > 1 && (os.Args[1] == "-u" || os.Args[1] == "--update") { - AutoCommitUpdate() + achelper.AutoCommitUpdate() } else { - AutoCommit() - } -} - -func AutoCommit() { - GetVersion(false) - - files, err := GetStagedFiles() - if err != nil { - ErrorLogger(fmt.Errorf("error getting staged files: %s", err.Error())) - return - } - - if len(files) == 0 { - InfoLogger("No files staged for commit.") - return - } - - parserMsg, err := Parser(files) - if err != nil { - ErrorLogger(err) - return - } - - if err := Commit(parserMsg); err != nil { - ErrorLogger(fmt.Errorf("error committing: %s", err.Error())) - return + ac.AutoCommit() } } diff --git a/parser.go b/parser/parser.go similarity index 56% rename from parser.go rename to parser/parser.go index b070413..d53ca52 100644 --- a/parser.go +++ b/parser/parser.go @@ -1,13 +1,17 @@ -package main +package parser import ( "fmt" + "git-auto-commit/achelper/code" + "git-auto-commit/acpkg" + "git-auto-commit/constants" + "git-auto-commit/diff" "path/filepath" "strings" "sync" ) -func appendMsg(commitMsg, addition string) string { +func AppendMsg(commitMsg, addition string) string { var builder strings.Builder builder.Reset() @@ -21,7 +25,7 @@ func appendMsg(commitMsg, addition string) string { return builder.String() } -func Parser(files []string) (string, error) { +var Parser = func(files []string) (string, error) { var ( payloadMsg string mu sync.Mutex @@ -39,37 +43,37 @@ func Parser(files []string) (string, error) { for file := range jobs { mu.Lock() - if uint16(len(payloadMsg)) > MAX_COMMIT_LENGTH { + if uint16(len(payloadMsg)) > constants.MAX_COMMIT_LENGTH { mu.Unlock() continue } mu.Unlock() - diff, err := GetDiff(file) + diff, err := diff.GetDiff(file) if err != nil { errChan <- fmt.Errorf("error getting diff for %s: %w", file, err) continue } - lang := DetectLanguage(file) + lang := code.DetectLanguage(file) if lang == "" { mu.Lock() - payloadMsg = appendMsg(payloadMsg, fmt.Sprintf("the '%s' file has been changed", filepath.Base(file))) + payloadMsg = AppendMsg(payloadMsg, fmt.Sprintf("the '%s' file has been changed", filepath.Base(file))) mu.Unlock() continue // README.md, etc. } var fileChanges []string for _, formatted := range []string{ - FormattedVariables(diff, lang), - FormattedFunction(diff, lang), - FormattedClass(diff, lang), - FormattedLogic(diff, lang, filepath.Base(file)), - FormattedImport(diff, lang, filepath.Base(file)), - FormattedStruct(diff, lang), - FormattedType(diff, lang), - FormattedInterface(diff, lang), - FormattedEnum(diff, lang), + acpkg.FormattedVariables(diff, lang), + acpkg.FormattedFunction(diff, lang), + acpkg.FormattedClass(diff, lang), + acpkg.FormattedLogic(diff, lang, filepath.Base(file)), + acpkg.FormattedImport(diff, lang, filepath.Base(file)), + acpkg.FormattedStruct(diff, lang), + acpkg.FormattedType(diff, lang), + acpkg.FormattedInterface(diff, lang), + acpkg.FormattedEnum(diff, lang), } { if formatted != "" { fileChanges = append(fileChanges, formatted) @@ -79,11 +83,11 @@ func Parser(files []string) (string, error) { if len(fileChanges) > 0 { mu.Lock() for _, change := range fileChanges { - nextMsg := appendMsg(payloadMsg, change) - if len(nextMsg) > int(MAX_COMMIT_LENGTH) { + nextMsg := AppendMsg(payloadMsg, change) + if len(nextMsg) > int(constants.MAX_COMMIT_LENGTH) { if len(payloadMsg) == 0 { - if len(change) > int(MAX_COMMIT_LENGTH) { - change = change[:int(MAX_COMMIT_LENGTH)] + if len(change) > int(constants.MAX_COMMIT_LENGTH) { + change = change[:int(constants.MAX_COMMIT_LENGTH)] } payloadMsg = change @@ -115,20 +119,20 @@ func Parser(files []string) (string, error) { } if len(payloadMsg) == 0 { - formattedByRemote, err := FormattedByRemote("") + formattedByRemote, err := acpkg.FormattedByRemote("") if err != nil { return "", err } - formattedByBranch, err := FormattedByBranch() + formattedByBranch, err := acpkg.FormattedByBranch() if err != nil { return "", err } if formattedByRemote != "" { - payloadMsg = appendMsg(payloadMsg, formattedByRemote) + payloadMsg = AppendMsg(payloadMsg, formattedByRemote) } else { - payloadMsg = appendMsg(payloadMsg, formattedByBranch) + payloadMsg = AppendMsg(payloadMsg, formattedByBranch) } } diff --git a/tests/ac_test.go b/tests/ac_test.go new file mode 100644 index 0000000..6b8bc98 --- /dev/null +++ b/tests/ac_test.go @@ -0,0 +1,54 @@ +package tests + +import ( + "errors" + "git-auto-commit/ac" + "git-auto-commit/achelper" + "git-auto-commit/achelper/logger" + "git-auto-commit/diff" + "git-auto-commit/git" + "git-auto-commit/parser" + "testing" +) + +func TestAutoCommit_NoStagedFiles(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + calledInfo := "" + diff.GetStagedFiles = func() ([]string, error) { return []string{}, nil } + parser.Parser = func(files []string) (string, error) { return "", nil } + git.Commit = func(msg string) error { return nil } + logger.ErrorLogger = func(err error) { t.Errorf("unexpected error: %v", err) } + logger.InfoLogger = func(msg string) { calledInfo = msg } + achelper.GetVersion = func(show bool) {} + + ac.AutoCommit() + + if calledInfo != "No files staged for commit." { + t.Errorf("expected info log 'No files staged for commit.', got '%s'", calledInfo) + } +} + +func TestAutoCommit_ErrorGettingFiles(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetStagedFiles = func() ([]string, error) { return nil, errors.New("fail") } + parser.Parser = func(files []string) (string, error) { return "", nil } + git.Commit = func(msg string) error { return nil } + logger.InfoLogger = func(msg string) {} + achelper.GetVersion = func(show bool) {} + + var calledErr string + logger.ErrorLogger = func(err error) { calledErr = err.Error() } + + logger.InfoLogger = func(msg string) { calledErr = msg } + + ac.AutoCommit() + + expected := "error getting staged files: fail" + if calledErr != expected { + t.Errorf("expected error log '%s', got '%s'", expected, calledErr) + } +} diff --git a/tests/af_go_test.go b/tests/af_go_test.go new file mode 100644 index 0000000..980ec34 --- /dev/null +++ b/tests/af_go_test.go @@ -0,0 +1,169 @@ +package tests + +import ( + "git-auto-commit/achelper/code" + "git-auto-commit/diff" + "git-auto-commit/parser" + "testing" +) + +func TestFormattedFunction_AddedGoFunction(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetDiff = func(file string) (string, error) { + return "+func AddedGoFunction() {}", nil + } + + code.DetectLanguage = func(filename string) string { + return "go" + } + + msg, err := parser.Parser([]string{"auto-commit-parser-test.go"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := "added function AddedGoFunction" + if msg != expected { + t.Errorf("expected '%s', got '%s'", expected, msg) + } +} + +func TestFormattedFunction_AddedGoFunctions(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetDiff = func(file string) (string, error) { + return "+func AddedGoFunction1()\n+func AddedGoFunction2()\n+func AddedGoFunction3()", nil + } + + code.DetectLanguage = func(filename string) string { + return "go" + } + + msg, err := parser.Parser([]string{"auto-commit-parser-test.go"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := "added functions: AddedGoFunction1, AddedGoFunction2, AddedGoFunction3" + if msg != expected { + t.Errorf("expected '%s', got '%s'", expected, msg) + } +} + +func TestFormattedFunction_DeletedGoFunction(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetDiff = func(file string) (string, error) { + return "-func DeletedGoFunction() {}", nil + } + + code.DetectLanguage = func(filename string) string { + return "go" + } + + msg, err := parser.Parser([]string{"auto-commit-parser-test.go"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := "deleted function DeletedGoFunction" + if msg != expected { + t.Errorf("expected '%s', got '%s'", expected, msg) + } +} + +func TestFormattedFunction_DeletedGoFunctions(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetDiff = func(file string) (string, error) { + return "-func DeletedGoFunction1()\n-func DeletedGoFunction2()\n-func DeletedGoFunction3()", nil + } + + code.DetectLanguage = func(filename string) string { + return "go" + } + + msg, err := parser.Parser([]string{"auto-commit-parser-test.go"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := "deleted functions: DeletedGoFunction1, DeletedGoFunction2, DeletedGoFunction3" + if msg != expected { + t.Errorf("expected '%s', got '%s'", expected, msg) + } +} + +func TestFormattedFunction_ChangedParamNameGoFunction(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetDiff = func(file string) (string, error) { + return "-func ParamTest(a int)\n+func ParamTest(b int)", nil + } + + code.DetectLanguage = func(filename string) string { + return "go" + } + + msg, err := parser.Parser([]string{"auto-commit-parser-test.go"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := "changed parameter in ParamTest function" + if msg != expected { + t.Errorf("expected '%s', got '%s'", expected, msg) + } +} + +func TestFormattedFunction_ChangedParamNameGoFunctions(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetDiff = func(file string) (string, error) { + return "-func ParamTest1(a int)\n+func ParamTest1(b int)\n-func ParamTest2(x string)\n+func ParamTest2(y string)", nil + } + + code.DetectLanguage = func(filename string) string { + return "go" + } + + msg, err := parser.Parser([]string{"auto-commit-parser-test.go"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := "changed parameters in functions: ParamTest1 function, ParamTest2 function" + if msg != expected { + t.Errorf("expected '%s', got '%s'", expected, msg) + } +} + +func TestFormattedFunction_ChangedParamTypeGoFunction(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetDiff = func(file string) (string, error) { + return "-func TypeTest(a int)\n+func TypeTest(a string)", nil + } + + code.DetectLanguage = func(filename string) string { + return "go" + } + + msg, err := parser.Parser([]string{"auto-commit-parser-test.go"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := "changed parameter type a in TypeTest function" + if msg != expected { + t.Errorf("expected '%s', got '%s'", expected, msg) + } +} diff --git a/tests/af_ts_test.go b/tests/af_ts_test.go new file mode 100644 index 0000000..44ee13f --- /dev/null +++ b/tests/af_ts_test.go @@ -0,0 +1,169 @@ +package tests + +import ( + "git-auto-commit/achelper/code" + "git-auto-commit/diff" + "git-auto-commit/parser" + "testing" +) + +func TestFormattedFunction_AddedTSFunction(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetDiff = func(file string) (string, error) { + return "+function AddedTSFunction() {}", nil + } + + code.DetectLanguage = func(filename string) string { + return "typescript" + } + + msg, err := parser.Parser([]string{"auto-commit-parser-test.ts"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := "added function AddedTSFunction" + if msg != expected { + t.Errorf("expected '%s', got '%s'", expected, msg) + } +} + +func TestFormattedFunction_AddedTSFunctions(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetDiff = func(file string) (string, error) { + return "+function AddedTSFunction1() {}\n+function AddedTSFunction2() {}\n+function AddedTSFunction3() {}", nil + } + + code.DetectLanguage = func(filename string) string { + return "typescript" + } + + msg, err := parser.Parser([]string{"auto-commit-parser-test.ts"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := "added functions: AddedTSFunction1, AddedTSFunction2, AddedTSFunction3" + if msg != expected { + t.Errorf("expected '%s', got '%s'", expected, msg) + } +} + +func TestFormattedFunction_DeletedTSFunction(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetDiff = func(file string) (string, error) { + return "-function DeletedTSFunction() {}", nil + } + + code.DetectLanguage = func(filename string) string { + return "typescript" + } + + msg, err := parser.Parser([]string{"auto-commit-parser-test.ts"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := "deleted function DeletedTSFunction" + if msg != expected { + t.Errorf("expected '%s', got '%s'", expected, msg) + } +} + +func TestFormattedFunction_DeletedTSFunctions(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetDiff = func(file string) (string, error) { + return "-function DeletedTSFunction1() {}\n-function DeletedTSFunction2() {}\n-function DeletedTSFunction3() {}", nil + } + + code.DetectLanguage = func(filename string) string { + return "typescript" + } + + msg, err := parser.Parser([]string{"auto-commit-parser-test.ts"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := "deleted functions: DeletedTSFunction1, DeletedTSFunction2, DeletedTSFunction3" + if msg != expected { + t.Errorf("expected '%s', got '%s'", expected, msg) + } +} + +func TestFormattedFunction_ChangedParamNameTSFunction(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetDiff = func(file string) (string, error) { + return "-function ParamTest(a: number) {}\n+function ParamTest(b: number) {}", nil + } + + code.DetectLanguage = func(filename string) string { + return "typescript" + } + + msg, err := parser.Parser([]string{"auto-commit-parser-test.ts"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := "changed parameter in ParamTest function" + if msg != expected { + t.Errorf("expected '%s', got '%s'", expected, msg) + } +} + +func TestFormattedFunction_ChangedParamNameTSFunctions(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetDiff = func(file string) (string, error) { + return "-function ParamTest1(a: number) {}\n+function ParamTest1(b: number) {}\n-function ParamTest2(x: string) {}\n+function ParamTest2(y: string) {}", nil + } + + code.DetectLanguage = func(filename string) string { + return "typescript" + } + + msg, err := parser.Parser([]string{"auto-commit-parser-test.ts"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := "changed parameters in functions: ParamTest1 function, ParamTest2 function" + if msg != expected { + t.Errorf("expected '%s', got '%s'", expected, msg) + } +} + +func TestFormattedFunction_ChangedParamTypeTSFunction(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetDiff = func(file string) (string, error) { + return "-function TypeTest(a: number) {}\n+function TypeTest(a: string) {}", nil + } + + code.DetectLanguage = func(filename string) string { + return "typescript" + } + + msg, err := parser.Parser([]string{"auto-commit-parser-test.ts"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := "changed parameter type a in TypeTest function" + if msg != expected { + t.Errorf("expected '%s', got '%s'", expected, msg) + } +} diff --git a/tests/ai_go_test.go b/tests/ai_go_test.go new file mode 100644 index 0000000..b1cac60 --- /dev/null +++ b/tests/ai_go_test.go @@ -0,0 +1,100 @@ +package tests + +import ( + "git-auto-commit/achelper/code" + "git-auto-commit/diff" + "git-auto-commit/parser" + "testing" +) + +func TestFormattedImport_AddedGoImport(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetDiff = func(file string) (string, error) { + return "+import \"fmt\"", nil + } + + code.DetectLanguage = func(filename string) string { + return "go" + } + + msg, err := parser.Parser([]string{"main.go"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := "included 'fmt' in main.go" + if msg != expected { + t.Errorf("expected '%s', got '%s'", expected, msg) + } +} + +func TestFormattedImport_AddedGoImports(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetDiff = func(file string) (string, error) { + return "+import \"fmt\"\n+import \"os\"", nil + } + + code.DetectLanguage = func(filename string) string { + return "go" + } + + msg, err := parser.Parser([]string{"main.go"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := "included 'fmt', 'os' in main.go" + if msg != expected { + t.Errorf("expected '%s', got '%s'", expected, msg) + } +} + +func TestFormattedImport_AddedImportsBlockGo(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetDiff = func(file string) (string, error) { + return "+import (\n+\"fmt\"\n+\"os\"\n+)", nil + } + + code.DetectLanguage = func(filename string) string { + return "go" + } + + msg, err := parser.Parser([]string{"main.go"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := "included 'fmt', 'os' in main.go" + if msg != expected { + t.Errorf("expected '%s', got '%s'", expected, msg) + } +} + +func TestFormattedImport_AddedImportBlockGo(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetDiff = func(file string) (string, error) { + return "+import (\n+\"fmt\"\n+)", nil + } + + code.DetectLanguage = func(filename string) string { + return "go" + } + + msg, err := parser.Parser([]string{"main.go"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := "included 'fmt' in main.go" + if msg != expected { + t.Errorf("expected '%s', got '%s'", expected, msg) + } +} diff --git a/tests/av_go_test.go b/tests/av_go_test.go new file mode 100644 index 0000000..b5822f8 --- /dev/null +++ b/tests/av_go_test.go @@ -0,0 +1,146 @@ +package tests + +import ( + "git-auto-commit/achelper/code" + "git-auto-commit/diff" + "git-auto-commit/parser" + "testing" +) + +func TestFormattedVariables_AddedGoVar(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetDiff = func(file string) (string, error) { + return "+var testVar int = 5", nil + } + + code.DetectLanguage = func(filename string) string { + return "go" + } + + msg, err := parser.Parser([]string{"auto-commit-parser-test.go"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := "added variable testVar" + if msg != expected { + t.Errorf("expected '%s', got '%s'", expected, msg) + } +} + +func TestFormattedVariables_AddedGoVarEQ(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetDiff = func(file string) (string, error) { + return "+testVar := 5", nil + } + + code.DetectLanguage = func(filename string) string { + return "go" + } + + msg, err := parser.Parser([]string{"auto-commit-parser-test.go"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := "added variable testVar" + if msg != expected { + t.Errorf("expected '%s', got '%s'", expected, msg) + } +} + +func TestFormattedVariables_AddedGoVars(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetDiff = func(file string) (string, error) { + return "+var testVar1 int = 5\n+var testVar2 string\n+testVar3 := 0", nil + } + + code.DetectLanguage = func(filename string) string { + return "go" + } + + msg, err := parser.Parser([]string{"auto-commit-parser-test.go"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := "added variables: testVar1, testVar2, testVar3" + if msg != expected { + t.Errorf("expected '%s', got '%s'", expected, msg) + } +} + +func TestFormattedVariables_RenamedGoVar(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetDiff = func(file string) (string, error) { + return "-var testVar1 int = 5\n+var testVar int = 5", nil + } + + code.DetectLanguage = func(filename string) string { + return "go" + } + + msg, err := parser.Parser([]string{"auto-commit-parser-test.go"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := "renamed variable testVar1 -> testVar" + if msg != expected { + t.Errorf("expected '%s', got '%s'", expected, msg) + } +} + +func TestFormattedVariables_ChangedTypeGoVar(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetDiff = func(file string) (string, error) { + return "-var testVar int = 5\n+var testVar uint = 5", nil + } + + code.DetectLanguage = func(filename string) string { + return "go" + } + + msg, err := parser.Parser([]string{"auto-commit-parser-test.go"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := "changed type of variable testVar (int -> uint)" + if msg != expected { + t.Errorf("expected '%s', got '%s'", expected, msg) + } +} + +func TestFormattedVariables_ChangedTypeGoVars(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetDiff = func(file string) (string, error) { + return "-var testVar uint8 = 5\n+var testVar uint16 = 5\n-var testVar2 uint16 = 5\n+var testVar2 int32 = 5", nil + } + + code.DetectLanguage = func(filename string) string { + return "go" + } + + msg, err := parser.Parser([]string{"auto-commit-parser-test.go"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := "changed types of variables: testVar (uint8 -> uint16), testVar2 (uint16 -> int32)" + if msg != expected { + t.Errorf("expected '%s', got '%s'", expected, msg) + } +} diff --git a/tests/av_ts_test.go b/tests/av_ts_test.go new file mode 100644 index 0000000..a79ef98 --- /dev/null +++ b/tests/av_ts_test.go @@ -0,0 +1,123 @@ +package tests + +import ( + "git-auto-commit/achelper/code" + "git-auto-commit/diff" + "git-auto-commit/parser" + "testing" +) + +func TestFormattedVariables_AddedTSVar(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetDiff = func(file string) (string, error) { + return "+let testVar: number = 5;", nil + } + + code.DetectLanguage = func(filename string) string { + return "typescript" + } + + msg, err := parser.Parser([]string{"auto-commit-parser-test.ts"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := "added variable testVar" + if msg != expected { + t.Errorf("expected '%s', got '%s'", expected, msg) + } +} + +func TestFormattedVariables_AddedTSVars(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetDiff = func(file string) (string, error) { + return "+let testVar1: number = 5;\n+const testVar2: string = 'hi';", nil + } + + code.DetectLanguage = func(filename string) string { + return "typescript" + } + + msg, err := parser.Parser([]string{"auto-commit-parser-test.ts"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := "added variables: testVar1, testVar2" + if msg != expected { + t.Errorf("expected '%s', got '%s'", expected, msg) + } +} + +func TestFormattedVariables_RenamedTSVar(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetDiff = func(file string) (string, error) { + return "-let testVar1: number = 5;\n+let testVar: number = 5;", nil + } + + code.DetectLanguage = func(filename string) string { + return "typescript" + } + + msg, err := parser.Parser([]string{"auto-commit-parser-test.ts"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := "renamed variable testVar1 -> testVar" + if msg != expected { + t.Errorf("expected '%s', got '%s'", expected, msg) + } +} + +func TestFormattedVariables_ChangedTypeTSVar(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetDiff = func(file string) (string, error) { + return "-let testVar: number = 5;\n+let testVar: string = 5;", nil + } + + code.DetectLanguage = func(filename string) string { + return "typescript" + } + + msg, err := parser.Parser([]string{"auto-commit-parser-test.ts"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := "changed type of variable testVar (number -> string)" + if msg != expected { + t.Errorf("expected '%s', got '%s'", expected, msg) + } +} + +func TestFormattedVariables_ChangedTypesTSVars(t *testing.T) { + mocks := SaveMocks() + defer mocks.Apply() + + diff.GetDiff = func(file string) (string, error) { + return "-let testVar: number = 5;\n+let testVar: string = 5;\n-var testVar2: boolean = true;\n+var testVar2: number = true;", nil + } + + code.DetectLanguage = func(filename string) string { + return "typescript" + } + + msg, err := parser.Parser([]string{"auto-commit-parser-test.ts"}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := "changed types of variables: testVar (number -> string), testVar2 (boolean -> number)" + if msg != expected { + t.Errorf("expected '%s', got '%s'", expected, msg) + } +} diff --git a/tests/test.g.go b/tests/test.g.go new file mode 100644 index 0000000..3b75aad --- /dev/null +++ b/tests/test.g.go @@ -0,0 +1,38 @@ +package tests + +import ( + "git-auto-commit/achelper" + "git-auto-commit/achelper/logger" + "git-auto-commit/diff" + "git-auto-commit/git" + "git-auto-commit/parser" +) + +type Mocks struct { + GetStagedFiles func() ([]string, error) + Parser func([]string) (string, error) + Commit func(string) error + ErrorLogger func(error) + InfoLogger func(string) + GetVersion func(bool) +} + +func SaveMocks() *Mocks { + return &Mocks{ + GetStagedFiles: diff.GetStagedFiles, + Parser: parser.Parser, + Commit: git.Commit, + ErrorLogger: logger.ErrorLogger, + InfoLogger: logger.InfoLogger, + GetVersion: achelper.GetVersion, + } +} + +func (m *Mocks) Apply() { + diff.GetStagedFiles = m.GetStagedFiles + parser.Parser = m.Parser + git.Commit = m.Commit + logger.ErrorLogger = m.ErrorLogger + logger.InfoLogger = m.InfoLogger + achelper.GetVersion = m.GetVersion +} diff --git a/vercel.json b/vercel.json new file mode 100644 index 0000000..f8f5ec0 --- /dev/null +++ b/vercel.json @@ -0,0 +1,14 @@ +{ + "builds": [ + { + "src": "www/**/*", + "use": "@vercel/static" + } + ], + "routes": [ + { + "src": "/(.*)", + "dest": "/www/$1" + } + ] +} diff --git a/version.go b/version.go deleted file mode 100644 index f13c0d5..0000000 --- a/version.go +++ /dev/null @@ -1,49 +0,0 @@ -package main - -import ( - "fmt" - "net/http" - "os" - "path/filepath" - "strings" -) - -func GetVersion(isCurrent bool) { - root, err := GetGitRoot() - if err != nil { - ErrorLogger(fmt.Errorf("could not get git root: %w", err)) - return - } - - versionFile := filepath.Join(root, ".git", "hooks", VERSION_FILE) - - version, err := os.ReadFile(versionFile) - if err != nil { - ErrorLogger(fmt.Errorf("unknown version for auto-commit, please re-install: %w", err)) - return - } - - if isCurrent { - fmt.Println("[git auto-commit] current version:", strings.TrimSpace(string(version))) - } - - resp, err := http.Get(GITHUB_API_REPO_URL + "/releases/latest") - if err != nil { - ErrorLogger(fmt.Errorf("could not check latest version: %w", err)) - return - } - defer resp.Body.Close() - - var data struct { - TagName string `json:"tag_name"` - } - if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { - ErrorLogger(fmt.Errorf("could not parse version info: %w", err)) - return - } - - if strings.TrimSpace(string(version)) != strings.TrimSpace(data.TagName) { - fmt.Printf("\033[33m[!] a new version is available: %s\033[0m\n", strings.TrimSpace(data.TagName)) - fmt.Printf("\033[33m[!] please update! 'git auto -u'\033[0m\n") - } -} diff --git a/www/index.html b/www/index.html new file mode 100644 index 0000000..46c006b --- /dev/null +++ b/www/index.html @@ -0,0 +1,174 @@ + + +
+ + ++ Git Auto-Commit is an extension for the Git version control + system designed to automatically generate meaningful and + context-sensitive commit messages based on changes made to the + codebase. The tool simplifies developers' workflows by allowing + them to focus on the content of edits rather than on the + formulation of descriptions for commits. +
+ ++ The development is conducted as an open source project and is + distributed under the MIT license (or other compatible + licensing, depending on the implementation). Git Auto-Commit can + be integrated into CI/CD pipelines, hook scripts, or used + manually via the command line. +
+ +git auto sub-command or configuration of user
+ aliases.
+
+ If you're on Windows
+ Go to the root of the project and run the command:
+
+iex ((New-Object Net.WebClient).DownloadString('https://github.com/thefuture-industries/git-auto-commit/blob/main/scripts/install-windows-auto-commit.ps1?raw=true'))
+
+
+ If you're on Linux
+ Go to the root of the project and run the command:
+
+echo Y | bash <(curl -fsSL https://github.com/thefuture-industries/git-auto-commit/blob/main/scripts/install-linux-auto-commit.sh?raw=true)+ +
+ Everything is ready now, after making changes to the code, just + run: +
+ +1 Option
++git add . +git auto +git push+ +
2 Commands
++git auto -w # Commit observer: you don't have to think and write anymore, `git auto -w` will figure it out and commit for you! +git auto -v # Viewing the current version of auto-commit +git auto -u # Upgrade to the new auto-commit version+ + +