Skip to content

Commit

Permalink
a test to get the ball rolling
Browse files Browse the repository at this point in the history
  • Loading branch information
richardjennings committed Oct 24, 2024
1 parent 6708c03 commit 473cda1
Show file tree
Hide file tree
Showing 33 changed files with 68 additions and 44 deletions.
18 changes: 1 addition & 17 deletions git/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,10 @@ import (
// Status currently displays the file statuses comparing the working directory
// to the index and the index to the last commit (if any).
func Status(o io.Writer) error {
var err error
// index
idx, err := g.ReadIndex()
files, err := g.CurrentStatus()
if err != nil {
return err
}

commitSha, err := g.LastCommit()
if err != nil {
// @todo error types to check for e.g no previous commits as source of error
return err
}

files, err := g.Status(idx, commitSha)

if err != nil {
return err
}

for _, v := range files.Files() {
if v.IdxStatus == g.IndexNotUpdated && v.WdStatus == g.WDIndexAndWorkingTreeMatch {
continue
Expand All @@ -36,6 +21,5 @@ func Status(o io.Writer) error {
return err
}
}

return nil
}
21 changes: 0 additions & 21 deletions index.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,27 +241,6 @@ func fromIndexItemP(p *indexItemP) *Finfo {
return f
}

// Status returns a FileSet containing all files from commit, index and working directory
// with the corresponding status.
func Status(idx *Index, commitSha Sha) (*FileSet, error) {
var commitFiles []*File
var err error
if commitSha.IsSet() {
commitFiles, err = CommittedFiles(commitSha)
if err != nil {
return nil, err
}
}
files := NewFileSet(commitFiles)
files.MergeFromIndex(NewFileSet(idx.Files()))
workingDirectoryFiles, err := Ls(Path())
if err != nil {
return nil, err
}
files.MergeFromWD(NewFileSet(workingDirectoryFiles))
return files, nil
}

// FsStatus returns a FileSet containing all files from the index and working directory
// with the corresponding status.
func FsStatus(path string) (*FileSet, error) {
Expand Down
3 changes: 3 additions & 0 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ func ReadObjectTree(sha Sha) (*Object, error) {
if err != nil {
return nil, err
}
if obj == nil {
return nil, fmt.Errorf("object %s not found", sha.AsHexString())
}
switch obj.Typ {
case ObjectTypeCommit:
commit, err := readCommit(obj)
Expand Down
31 changes: 25 additions & 6 deletions packfile_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
package g

import "testing"
import (
"testing"
)

func TestLookup(t *testing.T) {
Configure()
sha, err := ShaFromHexString("d208f34d505adb8914e5a5c577c6db8359173b4e")
//sha, err := ShaFromHexString("ebf679b4719cb6df2e6d9f8a471353abae08cf98")
func TestPackfile_lookupInPackfiles(t *testing.T) {
if err := Configure(
WithPath("./test_assets/repo/test-pack-file"),
WithGitDirectory(".gitg"),
); err != nil {
t.Fatal(err)
}
sha, err := ShaFromHexString("132fa1c9e9de4d67a0037f9ca1f143b09cccd704")
if err != nil {
t.Fatal(err)
}
obj, err := lookupInPackfiles(sha)
if err != nil {
t.Fatal(err)
}
lookupInPackfiles(sha)
if obj.Typ != ObjectTypeCommit {
t.Errorf("typ = %d, want %d", obj.Typ, ObjectTypeCommit)
}
files, err := CurrentStatus()
if err != nil {
t.Fatal(err)
}
if files.Files()[0].IdxStatus != IndexNotUpdated {
t.Errorf("idxStatus = %d, want %d", files.Files()[0].IdxStatus, IndexNotUpdated)
}
}
37 changes: 37 additions & 0 deletions status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package g

func CurrentStatus() (*FileSet, error) {
// index
idx, err := ReadIndex()
if err != nil {
return nil, err
}

commitSha, err := LastCommit()
if err != nil {
// @todo error types to check for e.g no previous commits as source of error
return nil, err
}
return Status(idx, commitSha)
}

// Status returns a FileSet containing all files from commit, index and working directory
// with the corresponding status.
func Status(idx *Index, commitSha Sha) (*FileSet, error) {
var commitFiles []*File
var err error
if commitSha.IsSet() {
commitFiles, err = CommittedFiles(commitSha)
if err != nil {
return nil, err
}
}
files := NewFileSet(commitFiles)
files.MergeFromIndex(NewFileSet(idx.Files()))
workingDirectoryFiles, err := Ls(Path())
if err != nil {
return nil, err
}
files.MergeFromWD(NewFileSet(workingDirectoryFiles))
return files, nil
}
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
2 changes: 2 additions & 0 deletions test_assets/repo/test-pack-file/.gitg/packed-refs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# pack-refs with: peeled fully-peeled sorted
132fa1c9e9de4d67a0037f9ca1f143b09cccd704 refs/heads/main
Empty file.

0 comments on commit 473cda1

Please sign in to comment.