Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .commit.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"issueRegex": "#[0-9]+"
}
"issueRegex": "[0-9]+"
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*.so
*.dylib
commit
!cmd/commit

# Test binary, built with `go test -c`
*.test
Expand Down
21 changes: 21 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.PHONY: build
build:
@go build -o bin/ ./cmd/commit

.PHONY: clean
clean:
@if [ -f bin/commit ]; then \
rm bin/commit ; \
fi;
@go clean

.PHONY: run
run:
@go run ./cmd/commit "$(filter-out $@,$(MAKECMDGOALS))"

.PHONY: test
test:
@go test -v ./tests/

%:
@:
31 changes: 15 additions & 16 deletions main.go → cmd/commit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
"strings"
"time"

"github.com/artem-y/commit/internal/config"
"github.com/artem-y/commit/internal/helpers"
"github.com/artem-y/commit/internal/user"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
Expand All @@ -15,9 +19,9 @@ import (
func main() {
commitMessage := getCommitMessage()

issueRegex := default_issue_regex
issueRegex := helpers.DEFAULT_ISSUE_REGEX

commitCfg := readCommitConfig()
commitCfg := config.ReadCommitConfig()
if commitCfg.IssueRegex != "" {
issueRegex = commitCfg.IssueRegex
}
Expand Down Expand Up @@ -51,7 +55,7 @@ func main() {
func getCommitMessage() string {
args := os.Args[1:]
if len(args) < 1 {
fmt.Fprintln(os.Stderr, red("Commit message cannot be empty"))
fmt.Fprintln(os.Stderr, helpers.Red("Commit message cannot be empty"))
os.Exit(1)
}

Expand All @@ -62,7 +66,7 @@ func getCommitMessage() string {
func openRepo() *git.Repository {
repo, err := git.PlainOpen(".")
if err != nil {
fmt.Fprintf(os.Stderr, red("Failed to open repository: %v\n"), err)
fmt.Fprintf(os.Stderr, helpers.Red("Failed to open repository: %v\n"), err)
os.Exit(1)
}
return repo
Expand All @@ -72,7 +76,7 @@ func openRepo() *git.Repository {
func getCurrentHead(repo *git.Repository) *plumbing.Reference {
headRef, err := repo.Head()
if err != nil {
fmt.Fprintf(os.Stderr, red("Failed to read current HEAD: %v\n"), err)
fmt.Fprintf(os.Stderr, helpers.Red("Failed to read current HEAD: %v\n"), err)
os.Exit(1)
}
return headRef
Expand All @@ -82,17 +86,12 @@ func getCurrentHead(repo *git.Repository) *plumbing.Reference {
func openWorktree(repo *git.Repository) *git.Worktree {
worktree, err := repo.Worktree()
if err != nil {
fmt.Fprintf(os.Stderr, red("Failed to open worktree: %v\n"), err)
fmt.Fprintf(os.Stderr, helpers.Red("Failed to open worktree: %v\n"), err)
os.Exit(1)
}
return worktree
}

// Wraps the message string in red color
func red(msg string) string {
return fmt.Sprintf("\033[31m%s\033[0m", msg)
}

// Searches the branch name for issue numbers matching the given regex
func findIssueMatchesInBranch(rgxRaw string, branchName string) []string {
rgx := regexp.MustCompile(rgxRaw)
Expand All @@ -108,11 +107,11 @@ func findIssueMatchesInBranch(rgxRaw string, branchName string) []string {
}

// Creates commit options with the author information
func makeCommitOptions(usr user) git.CommitOptions {
func makeCommitOptions(usr user.User) git.CommitOptions {
return git.CommitOptions{
Author: &object.Signature{
Name: usr.name,
Email: usr.email,
Name: usr.Name,
Email: usr.Email,
When: time.Now(),
},
AllowEmptyCommits: false,
Expand All @@ -123,12 +122,12 @@ func makeCommitOptions(usr user) git.CommitOptions {
// Commits changes with provided message
func commitChanges(repo *git.Repository, commitMessage string) {
worktree := openWorktree(repo)
usr := getUser(*repo)
usr := user.GetUser(*repo)
commitOptions := makeCommitOptions(usr)

_, err := worktree.Commit(commitMessage, &commitOptions)
if err != nil {
fmt.Fprintf(os.Stderr, red("Failed to commit: %v\n"), err)
fmt.Fprintf(os.Stderr, helpers.Red("Failed to commit: %v\n"), err)
os.Exit(1)
}
}
6 changes: 0 additions & 6 deletions constants.go

This file was deleted.

12 changes: 7 additions & 5 deletions config.go → internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package main
package config

import (
"encoding/json"
"fmt"
"os"

"github.com/artem-y/commit/internal/helpers"
)

type commitConfig struct {
IssueRegex string `json:"issueRegex"`
}

// Reads .commit.json file from current directory and unmarshals it into commitConfig struct
func readCommitConfig() commitConfig {
func ReadCommitConfig() commitConfig {

configFilePath := default_config_file_path
configFilePath := helpers.DEFAULT_CONFIG_FILE_PATH

_, err := os.Stat(configFilePath)
if os.IsNotExist(err) {
Expand All @@ -22,14 +24,14 @@ func readCommitConfig() commitConfig {

file, err := os.ReadFile(configFilePath)
if err != nil {
fmt.Fprintf(os.Stderr, red("Error reading %s file: %v\n"), err, configFilePath)
fmt.Fprintf(os.Stderr, helpers.Red("Error reading %s file: %v\n"), err, configFilePath)
os.Exit(1)
}

var cfg commitConfig
err = json.Unmarshal(file, &cfg)
if err != nil {
fmt.Fprintf(os.Stderr, red("Error unmarshalling %s file: %v\n"), err, configFilePath)
fmt.Fprintf(os.Stderr, helpers.Red("Error unmarshalling %s file: %v\n"), err, configFilePath)
os.Exit(1)
}

Expand Down
6 changes: 6 additions & 0 deletions internal/helpers/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package helpers

const (
DEFAULT_CONFIG_FILE_PATH = ".commit.json"
DEFAULT_ISSUE_REGEX = "#[0-9]+"
)
9 changes: 9 additions & 0 deletions internal/helpers/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package helpers

import "fmt"

// Wraps the message string in red color
func Red(msg string) string {
return fmt.Sprintf("\033[31m%s\033[0m", msg)
}

47 changes: 47 additions & 0 deletions internal/user/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package user

import (
"fmt"
"os"

"github.com/artem-y/commit/internal/helpers"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
)

// Simple representation of a git user
type User struct {
Name string
Email string
}

// Returns the user name and email from the local repository or the global git config
func GetUser(repo git.Repository) User {
cfg, err := repo.Config()
if err != nil {
fmt.Fprintf(os.Stderr, helpers.Red("Error loading local config: %v\n"), err)
os.Exit(1)
}

usr := User{
Name: cfg.User.Name,
Email: cfg.User.Email,
}

globalCfg, err := config.LoadConfig(config.GlobalScope)
if err != nil {
fmt.Fprintf(os.Stderr, helpers.Red("Error loading global config: %v\n"), err)
os.Exit(1)
}

if usr.Email == "" {
usr.Email = globalCfg.User.Email
}

if usr.Name == "" {
usr.Name = globalCfg.User.Name
}

return usr
}
18 changes: 18 additions & 0 deletions tests/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package tests

import (
"fmt"
"testing"

"github.com/artem-y/commit/internal/helpers"
)

func Test_Red_WrapsCommitMessage_InRedColor(t *testing.T) {
errorMessage := "Error: Something went wrong"
actualOutput := helpers.Red(errorMessage)
expectedOutput := fmt.Sprintf("\033[31m%s\033[0m", errorMessage)

if actualOutput != expectedOutput {
t.Errorf("got %q, want %q", actualOutput, expectedOutput)
}
}
45 changes: 0 additions & 45 deletions user.go

This file was deleted.