Skip to content

Commit

Permalink
init git log
Browse files Browse the repository at this point in the history
  • Loading branch information
richardjennings committed Jan 10, 2024
1 parent c54b545 commit 8a8a53d
Show file tree
Hide file tree
Showing 9 changed files with 284 additions and 134 deletions.
22 changes: 22 additions & 0 deletions cmd/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cmd

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

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()
},
}

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
28 changes: 24 additions & 4 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,27 @@ 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() error {
branch, err := refs.CurrentBranch()
if err != nil {
return err
}
commitSha, err := refs.HeadSHA(branch)
if err != nil {
return err
}
commit, err := objects.ReadCommit(commitSha)
if err != nil {
return err
}
fmt.Printf("tree: %s\n", string(commit.Tree))
for _, v := range commit.Parents {
fmt.Printf("parent: %s\n", string(v))
}
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,12 +124,12 @@ 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]>",
Expand Down
37 changes: 33 additions & 4 deletions internal/mygit/objects/object.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
package objects

import (
"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
AuthoredTime time.Time
Committer string
CommittedTime time.Time
Message string
}
Tree struct {
Sha []byte
Typ objectType
Path string
Items []*TreeItem
}
TreeItem struct {
Sha []byte
Typ objectType
Path string
}
)

const (
Expand Down
Loading

0 comments on commit 8a8a53d

Please sign in to comment.