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 Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
EXECUTABLE := microgit
EXECUTABLE := mgit

build:
go build -o $(EXECUTABLE) cmd/cmd.go
go build -o $(EXECUTABLE) main.go
install: build
sudo mv $(EXECUTABLE) /usr/local/bin/
14 changes: 14 additions & 0 deletions cmd/abstract.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cmd

import (
"github.com/akamensky/argparse"
)

type ICommand interface {
Register(parser *argparse.Parser)
Handle() bool
}

type CommandMeta struct {
Command *argparse.Command
}
66 changes: 66 additions & 0 deletions cmd/cat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cmd

import (
"fmt"
"strconv"

"micro-git/object"

"github.com/akamensky/argparse"
)

type CatCommand struct {
CommandMeta
fileInput *string
shouldShowObjectType *bool
shouldShowSize *bool
}

func (c *CatCommand) Register(p *argparse.Parser) {
c.Command = p.NewCommand("cat-file", "Provide content or type and size information for repository objects")
c.fileInput = c.Command.StringPositional(&argparse.Options{
Required: true,
Help: "The name of the object to show",
})
c.shouldShowObjectType = c.Command.Flag("t", "type", &argparse.Options{
Default: false,
Help: "Instead of the content, show the object type",
})
c.shouldShowSize = c.Command.Flag("s", "size", &argparse.Options{
Default: false,
Help: "Instead of the content, show the object size",
})
}

func (c *CatCommand) Handle() bool {
if c.Command.Happened() {
info, err := catFile(*c.fileInput, *c.shouldShowObjectType, *c.shouldShowSize)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(info)
}
return true
}
return false
}

func catFile(oid string, shouldShowType, shouldShowSize bool) (string, error) {
objectInfo, err := object.Read(oid)
if err != nil {
return "", err
}

if shouldShowType && shouldShowSize {
err = fmt.Errorf("-t and -s cannot be used altogether")
return "", err
}

if shouldShowType {
return objectInfo.Type, nil
}
if shouldShowSize {
return strconv.Itoa(objectInfo.Size), nil
}
return string(objectInfo.Content), nil
}
99 changes: 99 additions & 0 deletions cmd/cat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package cmd

import (
"crypto/sha1"
"encoding/hex"
"os"
"path/filepath"
"testing"

"micro-git/root"
"micro-git/testutil"

"github.com/stretchr/testify/suite"
)

type CatTestSuite struct {
suite.Suite
currWd string
tmpDir string
sampleOid string
}

func (suite *CatTestSuite) SetupSuite() {
currWd, err := os.Getwd()
if err != nil {
suite.FailNow("Failed to get current working directory", "Error: %v", err)
}

suite.currWd = currWd

tmpDir := testutil.CreateTestDir(suite.T())
suite.tmpDir = tmpDir

err = os.WriteFile("test.txt", []byte("Hello"), 0o664)
if err != nil {
suite.FailNow("Failed to create test.txt file for testing", "Error: %v", err)
}

// create the .microgit folder
err = root.InitDB()
if err != nil {
suite.FailNow("Failed to execute Init command", "Error: %v", err)
}

// hash the test.txt file
combined := append([]byte("blob"), []byte(" 5")...)
combined = append(combined, '\x00')
combined = append(combined, []byte("Hello")...)
shaSum := sha1.Sum(combined)
hexSum := hex.EncodeToString(shaSum[:])

suite.sampleOid = hexSum

folderPath := filepath.Join(".microgit", "objects", hexSum[:2])
objectPath := filepath.Join(folderPath, hexSum[2:])

err = os.MkdirAll(folderPath, 0o774)
if err != nil {
suite.FailNow("Failed to create folder for hashed file of test.txt", "Error: %v", err)
}

err = os.WriteFile(objectPath, combined, 0o664)
if err != nil {
suite.FailNow("Failed to create hashed file of test.txt", "Error: %v", err)
}
}

func (suite *CatTestSuite) TearDownSuite() {
os.RemoveAll(suite.tmpDir)
os.Chdir(suite.currWd)
}

func (suite *CatTestSuite) TestCatFileContentReturnCorrectResult() {
content, err := catFile(suite.sampleOid, false, false)
if err != nil {
suite.FailNow("catFile failed", "Error: %v", err)
}
suite.Equal("Hello", string(content))
}

func (suite *CatTestSuite) TestCatFileTypeReturnCorrectResult() {
fileType, err := catFile(suite.sampleOid, true, false)
if err != nil {
suite.FailNow("catFile failed", "Error: %v", err)
}
suite.Equal("blob", fileType)
}

func (suite *CatTestSuite) TestCatFileSizeReturnCorrectResult() {
fileSize, err := catFile(suite.sampleOid, false, true)
if err != nil {
suite.FailNow("catFile failed", "Error: %v", err)
}
suite.Equal("5", fileSize)
}

func TestCatTestSuite(t *testing.T) {
suite.Run(t, new(CatTestSuite))
}
172 changes: 0 additions & 172 deletions cmd/cmd.go

This file was deleted.

Loading