Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

init git log #4

Merged
merged 6 commits into from
Jan 11, 2024
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
23 changes: 23 additions & 0 deletions cmd/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cmd

import (
"github.com/richardjennings/mygit/internal/mygit"
"github.com/spf13/cobra"
"log"
"os"
)

var logCmd = &cobra.Command{
Use: "log",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
if err := configure(); err != nil {
log.Fatalln(err)
}
return mygit.Log(os.Stdout)
},
}

func init() {
rootCmd.AddCommand(logCmd)
}
31 changes: 0 additions & 31 deletions internal/mygit/commits/commit.go

This file was deleted.

36 changes: 0 additions & 36 deletions internal/mygit/commits/writer.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/mygit/index/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (idx *Index) CommitStatus(sha []byte) ([]*fs.File, error) {
}
return files, nil
}
obj, err := objects.ReadObject(sha)
obj, err := objects.ReadObjectTree(sha)
if err != nil {
return nil, err
}
Expand Down
32 changes: 27 additions & 5 deletions internal/mygit/mygit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package mygit
import (
"errors"
"fmt"
"github.com/richardjennings/mygit/internal/mygit/commits"
"github.com/richardjennings/mygit/internal/mygit/config"
"github.com/richardjennings/mygit/internal/mygit/fs"
"github.com/richardjennings/mygit/internal/mygit/index"
Expand Down Expand Up @@ -34,6 +33,29 @@ func Init() error {
return os.WriteFile(config.GitHeadPath(), []byte(fmt.Sprintf("ref: %s\n", config.Config.DefaultBranch)), 0644)
}

// Log prints out the commit log for the current branch
func Log(o io.Writer) error {
branch, err := refs.CurrentBranch()
if err != nil {
return err
}
commitSha, err := refs.HeadSHA(branch)
if err != nil {
return err
}
limit := 3
count := 0
for c, err := objects.ReadCommit(commitSha); c != nil && err == nil; c, err = objects.ReadCommit(c.Parents[0]) {
_, _ = fmt.Fprintf(o, "commit %s\nAuthor: %s <%s>\nDate: %s\n\n%8s\n", c.Sha, c.Author, c.AuthorEmail, c.AuthoredTime.String(), c.Message)
if count >= limit || len(c.Parents) == 0 {
break
}
count++
}

return nil
}

// Add adds one or more file paths to the Index.
func Add(paths ...string) error {
idx, err := index.ReadIndex()
Expand Down Expand Up @@ -104,19 +126,19 @@ func Commit() ([]byte, error) {
}
// git has the --allow-empty flag which here defaults to true currently
// @todo check for changes to be committed.
previousCommits, err := commits.PreviousCommits()
previousCommits, err := refs.PreviousCommits()
if err != nil {
return nil, err
}
return commits.Write(
&commits.Commit{
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: "test",
Message: []byte("test"),
},
)
}
Expand Down
11 changes: 11 additions & 0 deletions internal/mygit/mygit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ func Test_AddFile_Status_Commit(t *testing.T) {

// status should be empty
testStatus(t, "")

_ = testLog(t)
}

func testDir(t *testing.T) string {
Expand Down Expand Up @@ -151,3 +153,12 @@ func testCommit(t *testing.T) []byte {
}
return sha
}

func testLog(t *testing.T) []byte {
buf := bytes.NewBuffer(nil)
err := Log(buf)
if err != nil {
t.Fatal(err)
}
return buf.Bytes()
}
54 changes: 50 additions & 4 deletions internal/mygit/objects/object.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,47 @@
package objects

import (
"fmt"
"io"
"time"
)

type (
Object struct {
Path string
Typ objectType
Sha []byte
Objects []*Object
Path string
Typ objectType
Sha []byte
Objects []*Object
Length int
HeaderLength int
ReadCloser func() (io.ReadCloser, error)
//mode string
}
objectType int
Commit struct {
Sha []byte
Tree []byte
Parents [][]byte
Author string
AuthorEmail string
AuthoredTime time.Time
Committer string
CommitterEmail string
CommittedTime time.Time
Sig []byte
Message []byte
}
Tree struct {
Sha []byte
Typ objectType
Path string
Items []*TreeItem
}
TreeItem struct {
Sha []byte
Typ objectType
Path string
}
)

const (
Expand All @@ -17,3 +50,16 @@ const (
ObjectTree
ObjectCommit
)

func (c Commit) String() string {
var o string
o += fmt.Sprintf("commit: %s\n", string(c.Sha))
o += fmt.Sprintf("tree: %s\n", string(c.Tree))
for _, v := range c.Parents {
o += fmt.Sprintf("parent: %s\n", string(v))
}
o += fmt.Sprintf("%s <%s> %s\n", c.Author, c.AuthorEmail, c.AuthoredTime.String())
o += fmt.Sprintf("%s <%s> %s\n", c.Committer, c.CommitterEmail, c.CommittedTime.String())
o += fmt.Sprintf("message: \n%s\n", c.Message)
return o
}
Loading