Skip to content

Commit

Permalink
commit with GIT_AUTHOR, GIT_COMMITTER env config, --message, -m (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardjennings authored Feb 14, 2024
1 parent e9ded3e commit 588b3aa
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 22 deletions.
16 changes: 12 additions & 4 deletions cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,31 @@ import (
"github.com/richardjennings/mygit/internal/mygit"
"github.com/spf13/cobra"
"log"
"os"
)

var commitMessage string

var commitCmd = &cobra.Command{
Use: "commit",
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
if err := configure(); err != nil {
log.Fatalln(err)
}
sha, err := mygit.Commit()
var msg []byte
if cmd.Flags().Changed("message") {
msg = []byte(commitMessage)
}
sha, err := mygit.Commit(msg)
if err != nil {
return err
fmt.Println(err)
os.Exit(1)
}
fmt.Println(hex.EncodeToString(sha))
return nil
},
}

func init() {
commitCmd.Flags().StringVarP(&commitMessage, "message", "m", "", "--message")
rootCmd.AddCommand(commitCmd)
}
46 changes: 45 additions & 1 deletion internal/mygit/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package config

import "path/filepath"
import (
"fmt"
"os"
"path/filepath"
)

const (
DefaultGitDirectory = ".git"
Expand All @@ -11,6 +15,7 @@ const (
DefaultRefsDirectory = "refs"
DefaultRefsHeadsDirectory = "heads"
DefaultBranch = "refs/heads/main"
DefaultEditor = "vim"
)

var Config Cnf
Expand All @@ -26,6 +31,8 @@ type (
RefsHeadsDirectory string
DefaultBranch string
GitIgnore []string
Editor string
EditorArgs []string
}
Opt func(m *Cnf) error
)
Expand Down Expand Up @@ -58,6 +65,7 @@ func Configure(opts ...Opt) error {
RefsDirectory: DefaultRefsDirectory,
RefsHeadsDirectory: DefaultRefsHeadsDirectory,
DefaultBranch: DefaultBranch,
Editor: DefaultEditor,
GitIgnore: []string{ //@todo read from .gitignore
".idea/",
},
Expand Down Expand Up @@ -113,3 +121,39 @@ func GitHeadPath() string {
func Pager() (string, []string) {
return "/usr/bin/less", []string{"-X", "-F"}
}

func Editor() (string, []string) {
return Config.Editor, Config.EditorArgs
}

func EditorFile() string {
return fmt.Sprintf("%s/COMMIT_EDITMSG", GitPath())
}

func AuthorName() string {
if v, ok := os.LookupEnv("GIT_AUTHOR_NAME"); ok {
return v
}
return "default"
}

func AuthorEmail() string {
if v, ok := os.LookupEnv("GIT_AUTHOR_EMAIL"); ok {
return v
}
return "[email protected]"
}

func CommitterName() string {
if v, ok := os.LookupEnv("GIT_COMMITTER_NAME"); ok {
return v
}
return AuthorName()
}

func CommitterEmail() string {
if v, ok := os.LookupEnv("GIT_COMMITTER_EMAIL"); ok {
return v
}
return AuthorEmail()
}
48 changes: 36 additions & 12 deletions internal/mygit/mygit.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"io"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
Expand Down Expand Up @@ -138,7 +139,7 @@ func LsFiles() ([]string, error) {
}

// Commit writes a git commit object from the files in the index
func Commit() ([]byte, error) {
func Commit(message []byte) ([]byte, error) {
idx, err := index.ReadIndex()
if err != nil {
return nil, err
Expand All @@ -155,17 +156,40 @@ func Commit() ([]byte, error) {
// @todo error types to check for e.g no previous commits as source of error
return nil, err
}
return objects.WriteCommit(
&objects.Commit{
Tree: tree,
Parents: previousCommits,
Author: "Richard Jennings <[email protected]>",
AuthoredTime: time.Now(),
Committer: "Richard Jennings <[email protected]>",
CommittedTime: time.Now(),
Message: []byte("test"),
},
)
commit := &objects.Commit{
Tree: tree,
Parents: previousCommits,
Author: fmt.Sprintf("%s <%s>", config.AuthorName(), config.AuthorEmail()),
AuthoredTime: time.Now(),
Committer: fmt.Sprintf("%s <%s>", config.CommitterName(), config.CommitterEmail()),
CommittedTime: time.Now(),
}
if message != nil {
commit.Message = message
} else {
// empty commit file
if err := os.WriteFile(config.EditorFile(), []byte{}, 0600); err != nil {
log.Fatalln(err)
}
ed, args := config.Editor()
args = append(args, config.EditorFile())
cmd := exec.Command(ed, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
err = cmd.Run()
if err != nil {
log.Fatalln(err)
}
msg, err := os.ReadFile(args[0])
if err != nil {
log.Fatalln(msg)
}
commit.Message = msg
}
if len(commit.Message) == 0 {
return nil, errors.New("Aborting commit due to empty commit message.")
}
return objects.WriteCommit(commit)
}

// Status currently displays the file statuses comparing the working directory
Expand Down
32 changes: 27 additions & 5 deletions internal/mygit/mygit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"fmt"
"github.com/richardjennings/mygit/internal/mygit/config"
"github.com/richardjennings/mygit/internal/mygit/gfs"
"github.com/richardjennings/mygit/internal/mygit/objects"
"github.com/richardjennings/mygit/internal/mygit/refs"
"github.com/stretchr/testify/assert"
"io/fs"
Expand Down Expand Up @@ -75,7 +77,7 @@ func Test_End_To_End(t *testing.T) {

// create commit
// git commit -m "test"
testCommit(t)
testCommit(t, []byte("78"))

// list branches - main should now show up as it has a commit
// git branch
Expand All @@ -101,7 +103,7 @@ func Test_End_To_End(t *testing.T) {
testStatus(t, "M hello\n")

// git commit
testCommit(t)
testCommit(t, []byte("104"))

// status should be empty
// git status --porcelain
Expand Down Expand Up @@ -140,7 +142,7 @@ func Test_End_To_End(t *testing.T) {
// git add world
testAdd(t, "world", 2)
// git commit
testCommit(t)
testCommit(t, []byte("143"))
// git status --porcelain
testStatus(t, "")

Expand Down Expand Up @@ -210,14 +212,34 @@ func testStatus(t *testing.T, expected string) {
assert.Equal(t, expected, buf.String())
}

func testCommit(t *testing.T) []byte {
sha, err := Commit()
func testCommit(t *testing.T, message []byte) []byte {
sha, err := Commit(message)
if err != nil {
t.Fatal(err)
}
if len(sha) != 20 {
t.Errorf("expected sha len 20 got %d", len(sha))
}
commitSha, err := gfs.NewSha(sha)
if err != nil {
t.Error(err)
return sha
}

// read object
c, err := objects.ReadCommit(commitSha.AsHexBytes())
if err != nil {
t.Error(err)
return sha
}
if c == nil {
t.Error("commit not found")
return sha
}
if string(c.Message) != string(message)+"\n" {
t.Errorf("expected commit message %s, got %s", string(message), string(c.Message))
return sha
}
return sha
}

Expand Down

0 comments on commit 588b3aa

Please sign in to comment.