Skip to content

Commit c380057

Browse files
committed
feat: ensure subobjects exist
1 parent 0b24f26 commit c380057

File tree

9 files changed

+81
-52
lines changed

9 files changed

+81
-52
lines changed

api/jiaozifs.gen.go

Lines changed: 36 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/swagger.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,10 +1563,10 @@ paths:
15631563
schema:
15641564
type: object
15651565
required:
1566-
- username
1566+
- name
15671567
- password
15681568
properties:
1569-
username:
1569+
name:
15701570
type: string
15711571
password:
15721572
type: string

controller/commit_ctl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (commitCtl CommitController) GetEntriesInRef(ctx context.Context, w *api.Ji
4444
}
4545

4646
refName := repository.HEAD
47-
if params.Path != nil {
47+
if params.Ref != nil {
4848
refName = *params.Ref
4949
}
5050

controller/user_ctl.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type UserController struct {
3535
func (userCtl UserController) Login(ctx context.Context, w *api.JiaozifsResponse, r *http.Request, body api.LoginJSONRequestBody) {
3636

3737
// get user encryptedPassword by username
38-
ep, err := userCtl.Repo.UserRepo().GetEPByName(ctx, body.Username)
38+
ep, err := userCtl.Repo.UserRepo().GetEPByName(ctx, body.Name)
3939
if err != nil {
4040
w.Code(http.StatusUnauthorized)
4141
return
@@ -56,13 +56,13 @@ func (userCtl UserController) Login(ctx context.Context, w *api.JiaozifsResponse
5656
return
5757
}
5858

59-
tokenString, err := auth.GenerateJWTLogin(secretKey, body.Username, loginTime, expires)
59+
tokenString, err := auth.GenerateJWTLogin(secretKey, body.Name, loginTime, expires)
6060
if err != nil {
6161
w.Error(err)
6262
return
6363
}
6464

65-
userCtlLog.Infof("usert %s login successful", body.Username)
65+
userCtlLog.Infof("user %s login successful", body.Name)
6666

6767
internalAuthSession, _ := userCtl.SessionStore.Get(r, auth.InternalAuthSessionName)
6868
internalAuthSession.Values[auth.TokenSessionKeyName] = tokenString

integrationtest/helper_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func createUser(ctx context.Context, c convey.C, client *api.Client, userName st
117117
func loginAndSwitch(ctx context.Context, c convey.C, client *api.Client, userName string) {
118118
c.Convey("login "+userName, func() {
119119
resp, err := client.Login(ctx, api.LoginJSONRequestBody{
120-
Username: userName,
120+
Name: userName,
121121
Password: "12345678",
122122
})
123123
convey.So(err, convey.ShouldBeNil)

integrationtest/user_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func UserSpec(ctx context.Context, urlStr string) func(c convey.C) {
2323

2424
c.Convey("login fail", func() {
2525
resp, err := client.Login(ctx, api.LoginJSONRequestBody{
26-
Username: "admin",
26+
Name: "admin",
2727
Password: " vvvvvvvv",
2828
})
2929
convey.So(err, convey.ShouldBeNil)

models/tree.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ func SortSubObjects(subObjects []TreeEntry) []TreeEntry {
4343

4444
func NewRootTreeEntry(hash hash.Hash) TreeEntry {
4545
return TreeEntry{
46-
Name: "",
47-
Hash: hash,
46+
Name: "",
47+
Hash: hash,
48+
IsDir: true,
4849
}
4950
}
5051
func (treeEntry TreeEntry) Equal(other TreeEntry) bool {
@@ -158,6 +159,9 @@ type TreeNode struct {
158159
}
159160

160161
func NewTreeNode(props Property, repoID uuid.UUID, subObjects ...TreeEntry) (*TreeNode, error) {
162+
if subObjects == nil {
163+
subObjects = make([]TreeEntry, 0) //to ensure tree entry not null
164+
}
161165
newTree := &TreeNode{
162166
Type: TreeObject,
163167
RepositoryID: repoID,
@@ -226,7 +230,7 @@ type FileTree struct {
226230
Size int64 `bun:"size"`
227231
Properties Property `bun:"properties,type:jsonb,notnull"`
228232
//tree
229-
SubObjects []TreeEntry `bun:"sub_objects,type:jsonb" json:"sub_objects"`
233+
SubObjects []TreeEntry `bun:"sub_objects,type:jsonb,notnull" json:"sub_objects"`
230234

231235
CreatedAt time.Time `bun:"created_at,notnull" json:"created_at"`
232236
UpdatedAt time.Time `bun:"updated_at,notnull" json:"updated_at"`

models/tree_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"testing"
66

7+
"github.com/jiaozifs/jiaozifs/utils/hash"
8+
79
"github.com/brianvoe/gofakeit/v6"
810
"github.com/google/go-cmp/cmp"
911
"github.com/google/uuid"
@@ -72,3 +74,25 @@ func TestObjectRepo_Insert(t *testing.T) {
7274
require.ErrorIs(t, err, models.ErrRepoIDMisMatch)
7375
})
7476
}
77+
78+
func TestNewTreeNode(t *testing.T) {
79+
id, err := uuid.Parse("a91ef678-1980-4b26-9bb9-eadc9f366429")
80+
require.NoError(t, err)
81+
82+
t.Run("no subobjects", func(t *testing.T) {
83+
node, err := models.NewTreeNode(models.Property{Mode: filemode.Dir}, id)
84+
require.NoError(t, err)
85+
require.NotNil(t, node.SubObjects)
86+
require.Equal(t, "03c2737fb833f979f2bb5398248e8e64", node.Hash.Hex())
87+
})
88+
89+
t.Run("no subobjects", func(t *testing.T) {
90+
node, err := models.NewTreeNode(models.Property{Mode: filemode.Dir}, id, models.TreeEntry{
91+
Name: "a.txt",
92+
Hash: hash.Hash("aaa"),
93+
})
94+
require.NoError(t, err)
95+
require.NotNil(t, node.SubObjects)
96+
require.Equal(t, "27d9fbf6d43195f34404a94c0de707a2", node.Hash.Hex())
97+
})
98+
}

versionmgr/worktree.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ import (
1717
)
1818

1919
var EmptyRoot = &models.TreeNode{
20-
Hash: hash.Hash([]byte{}),
21-
Type: models.TreeObject,
20+
Hash: hash.Hash([]byte{}),
21+
Type: models.TreeObject,
22+
SubObjects: make([]models.TreeEntry, 0),
2223
}
2324

2425
var EmptyDirEntry = models.TreeEntry{
@@ -69,11 +70,11 @@ func (workTree *WorkTree) RepositoryID() uuid.UUID {
6970
}
7071

7172
func (workTree *WorkTree) AppendDirectEntry(ctx context.Context, treeEntry models.TreeEntry) (*models.TreeNode, error) {
72-
chilren, err := workTree.root.Children()
73+
children, err := workTree.root.Children()
7374
if err != nil {
7475
return nil, err
7576
}
76-
for _, node := range chilren {
77+
for _, node := range children {
7778
if node.Name() == treeEntry.Name {
7879
return nil, ErrEntryExit
7980
}
@@ -94,7 +95,7 @@ func (workTree *WorkTree) AppendDirectEntry(ctx context.Context, treeEntry model
9495
}
9596

9697
func (workTree *WorkTree) DeleteDirectEntry(ctx context.Context, name string) (*models.TreeNode, bool, error) {
97-
var subObjects []models.TreeEntry
98+
subObjects := []models.TreeEntry{} //ensure subobject not nul
9899
for _, sub := range workTree.root.SubObjects() {
99100
if sub.Name != name { //filter tree entry by name
100101
subObjects = append(subObjects, sub)

0 commit comments

Comments
 (0)