Skip to content

Commit e9a55b0

Browse files
committed
test: add diff test
1 parent 9a019be commit e9a55b0

File tree

17 files changed

+678
-186
lines changed

17 files changed

+678
-186
lines changed

api/jiaozifs.gen.go

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

api/swagger.yml

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,17 @@ components:
213213
- Hash
214214
- Type
215215
- CheckSum
216+
- RepositoryID
216217
- Properties
217218
- Size
218219
- CreatedAt
219220
- UpdatedAt
220221
properties:
221222
Hash:
222223
type: string
224+
RepositoryID:
225+
type: string
226+
format: uuid
223227
CheckSum:
224228
type: string
225229
Type:
@@ -258,6 +262,7 @@ components:
258262
required:
259263
- Hash
260264
- Type
265+
- RepositoryID
261266
- Author
262267
- Committer
263268
- MergeTag
@@ -273,6 +278,9 @@ components:
273278
$ref: "#/components/schemas/Signature"
274279
Committer:
275280
$ref: "#/components/schemas/Signature"
281+
RepositoryID:
282+
type: string
283+
format: uuid
276284
MergeTag:
277285
type: string
278286
Message:
@@ -303,6 +311,7 @@ components:
303311
required:
304312
- Hash
305313
- Type
314+
- RepositoryID
306315
- Properties
307316
- SubObjects
308317
- CreatedAt
@@ -313,6 +322,9 @@ components:
313322
Type:
314323
type: integer
315324
format: int8
325+
RepositoryID:
326+
type: string
327+
format: uuid
316328
Properties:
317329
type: object
318330
additionalProperties:
@@ -364,7 +376,7 @@ components:
364376
type: string
365377
Action:
366378
type: integer
367-
format: int
379+
enum: [1,2,3]
368380
BaseHash:
369381
type: string
370382
ToHash:
@@ -889,7 +901,9 @@ paths:
889901
content:
890902
application/json:
891903
schema:
892-
$ref: "#/components/schemas/Change"
904+
type: array
905+
items:
906+
$ref: "#/components/schemas/Change"
893907
400:
894908
description: ValidationError
895909
401:
@@ -924,7 +938,7 @@ paths:
924938
- in: query
925939
name: msg
926940
description: commit message
927-
required: false
941+
required: true
928942
schema:
929943
type: string
930944
responses:
@@ -1002,10 +1016,15 @@ paths:
10021016
type: string
10031017
- in: query
10041018
name: ref
1005-
description: specific ref default to main branch
1019+
description: specific branch, default to repostiory default branch(HEAD)
10061020
required: false
10071021
schema:
10081022
type: string
1023+
- in: query
1024+
name: isWip
1025+
description: isWip indicate to retrieve from working in progress, default false
1026+
schema:
1027+
type: boolean
10091028
responses:
10101029
200:
10111030
description: commit

controller/branch_ctl.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"go.uber.org/fx"
1919
)
2020

21-
var maxBranchNameLength = 20
21+
var MaxBranchNameLength = 40
2222
var branchNameRegex = regexp.MustCompile("^[a-zA-Z0-9_]*$")
2323

2424
func CheckBranchName(name string) error {
@@ -28,7 +28,7 @@ func CheckBranchName(name string) error {
2828
}
2929
}
3030

31-
if len(name) > maxBranchNameLength {
31+
if len(name) > MaxBranchNameLength {
3232
return fmt.Errorf("branch name is too long")
3333
}
3434

@@ -81,8 +81,14 @@ func (bct BranchController) ListBranches(ctx context.Context, w *api.JiaozifsRes
8181
var refs []api.Ref
8282
for _, branch := range branches {
8383
ref := api.Ref{
84-
CommitHash: branch.Name,
85-
Name: branch.CommitHash.Hex(),
84+
CommitHash: branch.CommitHash.Hex(),
85+
CreatedAt: branch.CreatedAt,
86+
CreatorID: branch.CreatorID,
87+
Description: branch.Description,
88+
ID: branch.ID,
89+
Name: branch.Name,
90+
RepositoryID: branch.RepositoryID,
91+
UpdatedAt: branch.UpdatedAt,
8692
}
8793
refs = append(refs, ref)
8894
}
@@ -237,7 +243,13 @@ func (bct BranchController) GetBranch(ctx context.Context, w *api.JiaozifsRespon
237243
return
238244
}
239245
w.JSON(api.Ref{
240-
CommitHash: ref.CommitHash.Hex(),
241-
Name: ref.Name,
246+
CommitHash: ref.CommitHash.Hex(),
247+
CreatedAt: ref.CreatedAt,
248+
CreatorID: ref.CreatorID,
249+
Description: ref.Description,
250+
ID: ref.ID,
251+
Name: ref.Name,
252+
RepositoryID: ref.RepositoryID,
253+
UpdatedAt: ref.UpdatedAt,
242254
})
243255
}

controller/commit_ctl.go

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ package controller
33
import (
44
"context"
55
"encoding/hex"
6+
"errors"
67
"net/http"
78
"strings"
89

10+
"github.com/jiaozifs/jiaozifs/utils/hash"
11+
912
"github.com/jiaozifs/jiaozifs/auth"
1013

1114
"github.com/jiaozifs/jiaozifs/utils"
@@ -42,12 +45,7 @@ func (commitCtl CommitController) GetEntriesInRef(ctx context.Context, w *api.Ji
4245
return
4346
}
4447

45-
if operator.Name == ownerName { //todo check permission
46-
w.Forbidden()
47-
return
48-
}
49-
50-
refName := "main"
48+
refName := repository.HEAD
5149
if params.Path != nil {
5250
refName = *params.Ref
5351
}
@@ -58,24 +56,43 @@ func (commitCtl CommitController) GetEntriesInRef(ctx context.Context, w *api.Ji
5856
return
5957
}
6058

61-
commit, err := commitCtl.Repo.CommitRepo(repository.ID).Commit(ctx, ref.CommitHash)
62-
if err != nil {
63-
w.Error(err)
59+
if operator.Name != ownerName { //todo check permission
60+
w.Forbidden()
6461
return
6562
}
6663

67-
workTree, err := versionmgr.NewWorkTree(ctx, commitCtl.Repo.FileTreeRepo(repository.ID), models.NewRootTreeEntry(commit.TreeHash))
64+
treeHash := hash.EmptyHash
65+
if utils.BoolValue(params.IsWip) {
66+
wip, err := commitCtl.Repo.WipRepo().Get(ctx, models.NewGetWipParams().SetCreatorID(operator.ID).SetRepositoryID(repository.ID).SetRefID(ref.ID))
67+
if err != nil {
68+
w.Error(err)
69+
return
70+
}
71+
treeHash = wip.CurrentTree
72+
} else {
73+
if !ref.CommitHash.IsEmpty() {
74+
commit, err := commitCtl.Repo.CommitRepo(repository.ID).Commit(ctx, ref.CommitHash)
75+
if err != nil {
76+
w.Error(err)
77+
return
78+
}
79+
treeHash = commit.TreeHash
80+
}
81+
}
82+
83+
workTree, err := versionmgr.NewWorkTree(ctx, commitCtl.Repo.FileTreeRepo(repository.ID), models.NewRootTreeEntry(treeHash))
6884
if err != nil {
6985
w.Error(err)
7086
return
7187
}
7288

73-
path := ""
74-
if params.Path != nil {
75-
path = *params.Path
76-
}
89+
path := versionmgr.CleanPath(utils.StringValue(params.Path))
7790
treeEntry, err := workTree.Ls(ctx, path)
7891
if err != nil {
92+
if errors.Is(err, versionmgr.ErrPathNotFound) {
93+
w.NotFound()
94+
return
95+
}
7996
w.Error(err)
8097
return
8198
}
@@ -101,7 +118,7 @@ func (commitCtl CommitController) GetCommitDiff(ctx context.Context, w *api.Jiao
101118
return
102119
}
103120

104-
if operator.ID == owner.ID { //todo check permission
121+
if operator.ID != owner.ID { //todo check permission
105122
w.Forbidden()
106123
return
107124
}
@@ -129,11 +146,7 @@ func (commitCtl CommitController) GetCommitDiff(ctx context.Context, w *api.Jiao
129146
return
130147
}
131148

132-
path := ""
133-
if params.Path != nil {
134-
path = *params.Path
135-
}
136-
149+
path := versionmgr.CleanPath(utils.StringValue(params.Path))
137150
commitOp := versionmgr.NewCommitOp(commitCtl.Repo, repository.ID, bashCommit)
138151
changes, err := commitOp.DiffCommit(ctx, toCommitHash)
139152
if err != nil {
@@ -150,7 +163,7 @@ func (commitCtl CommitController) GetCommitDiff(ctx context.Context, w *api.Jiao
150163
fullPath := change.Path()
151164
if strings.HasPrefix(fullPath, path) {
152165
apiChange := api.Change{
153-
Action: int(action),
166+
Action: api.ChangeAction(action),
154167
Path: fullPath,
155168
}
156169
if change.From() != nil {

controller/object_ctl.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (oct ObjectController) DeleteObject(ctx context.Context, w *api.JiaozifsRes
8787
return
8888
}
8989

90-
err = workTree.RemoveEntry(ctx, params.Path)
90+
err = workTree.RemoveEntry(ctx, versionmgr.CleanPath(params.Path))
9191
if errors.Is(err, versionmgr.ErrPathNotFound) {
9292
w.BadRequest(fmt.Sprintf("path %s not found", params.Path))
9393
return
@@ -157,7 +157,7 @@ func (oct ObjectController) GetObject(ctx context.Context, w *api.JiaozifsRespon
157157
return
158158
}
159159

160-
blob, name, err := workTree.FindBlob(ctx, params.Path)
160+
blob, name, err := workTree.FindBlob(ctx, versionmgr.CleanPath(params.Path))
161161
if err != nil {
162162
if errors.Is(err, versionmgr.ErrPathNotFound) {
163163
w.BadRequest(fmt.Sprintf("path %s not found", params.Path))
@@ -264,7 +264,7 @@ func (oct ObjectController) HeadObject(ctx context.Context, w *api.JiaozifsRespo
264264
return
265265
}
266266

267-
blob, name, err := workTree.FindBlob(ctx, params.Path)
267+
blob, name, err := workTree.FindBlob(ctx, versionmgr.CleanPath(params.Path))
268268
if err != nil {
269269
if errors.Is(err, versionmgr.ErrPathNotFound) {
270270
w.BadRequest(fmt.Sprintf("path %s not found", params.Path))
@@ -387,6 +387,7 @@ func (oct ObjectController) UploadObject(ctx context.Context, w *api.JiaozifsRes
387387
return
388388
}
389389

390+
path := versionmgr.CleanPath(params.Path)
390391
var response api.ObjectStats
391392
err = oct.Repo.Transaction(ctx, func(dRepo models.IRepo) error {
392393
workingTree, err := versionmgr.NewWorkTree(ctx, dRepo.FileTreeRepo(repository.ID), models.NewRootTreeEntry(stash.CurrentTree))
@@ -400,14 +401,14 @@ func (oct ObjectController) UploadObject(ctx context.Context, w *api.JiaozifsRes
400401
return err
401402
}
402403

403-
err = workingTree.AddLeaf(ctx, params.Path, blob)
404+
err = workingTree.AddLeaf(ctx, path, blob)
404405
if err != nil {
405406
return err
406407
}
407408
response = api.ObjectStats{
408409
Checksum: blob.CheckSum.Hex(),
409410
Mtime: time.Now().Unix(),
410-
Path: params.Path,
411+
Path: path,
411412
PathMode: utils.Uint32(uint32(filemode.Regular)),
412413
SizeBytes: swag.Int64(blob.Size),
413414
ContentType: &contentType,

controller/repository_ctl.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,13 @@ func (repositoryCtl RepositoryController) GetCommitsInRepository(ctx context.Con
304304
if err == nil {
305305
modelCommit := commit.Commit()
306306
commits = append(commits, api.Commit{
307+
RepositoryID: modelCommit.RepositoryID,
307308
Author: api.Signature{
308309
Email: openapi_types.Email(modelCommit.Author.Email),
309310
Name: modelCommit.Author.Name,
310311
When: modelCommit.Author.When,
311312
},
313+
312314
Committer: api.Signature{
313315
Email: openapi_types.Email(modelCommit.Committer.Email),
314316
Name: modelCommit.Committer.Name,

controller/wip_ctl.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func (wipCtl WipController) CommitWip(ctx context.Context, w *api.JiaozifsRespon
196196
return
197197
}
198198

199-
ref, err := wipCtl.Repo.RefRepo().Get(ctx, models.NewGetRefParams().SetName(params.RefName))
199+
ref, err := wipCtl.Repo.RefRepo().Get(ctx, models.NewGetRefParams().SetName(params.RefName).SetRepositoryID(repository.ID))
200200
if err != nil {
201201
w.Error(err)
202202
return
@@ -222,15 +222,10 @@ func (wipCtl WipController) CommitWip(ctx context.Context, w *api.JiaozifsRespon
222222
}
223223
}
224224

225-
var msg string
226-
if params.Msg != nil {
227-
msg = *params.Msg
228-
}
229-
230225
//add commit
231226
err = wipCtl.Repo.Transaction(ctx, func(repo models.IRepo) error {
232227
commitOp := versionmgr.NewCommitOp(repo, repository.ID, commit)
233-
commit, err := commitOp.AddCommit(ctx, operator, wip.ID, msg)
228+
commit, err := commitOp.AddCommit(ctx, operator, wip.ID, params.Msg)
234229
if err != nil {
235230
return err
236231
}
@@ -362,10 +357,7 @@ func (wipCtl WipController) GetWipChanges(ctx context.Context, w *api.JiaozifsRe
362357
return
363358
}
364359

365-
var path string
366-
if params.Path != nil {
367-
path = *params.Path
368-
}
360+
path := versionmgr.CleanPath(utils.StringValue(params.Path))
369361

370362
var changesResp []api.Change
371363
err = changes.ForEach(func(change versionmgr.IChange) error {
@@ -376,7 +368,7 @@ func (wipCtl WipController) GetWipChanges(ctx context.Context, w *api.JiaozifsRe
376368
fullPath := change.Path()
377369
if strings.HasPrefix(fullPath, path) {
378370
apiChange := api.Change{
379-
Action: int(action),
371+
Action: api.ChangeAction(action),
380372
Path: fullPath,
381373
}
382374
if change.From() != nil {
@@ -394,5 +386,5 @@ func (wipCtl WipController) GetWipChanges(ctx context.Context, w *api.JiaozifsRe
394386
return
395387
}
396388

397-
w.JSON(changes)
389+
w.JSON(changesResp)
398390
}

integrationtest/branch_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package integrationtest
33
import (
44
"context"
55
"net/http"
6+
"strings"
7+
8+
"github.com/jiaozifs/jiaozifs/controller"
69

710
"github.com/jiaozifs/jiaozifs/api"
811
apiimpl "github.com/jiaozifs/jiaozifs/api/api_impl"
@@ -53,7 +56,7 @@ func BranchSpec(ctx context.Context, urlStr string) func(c convey.C) {
5356

5457
c.Convey("too long name", func() {
5558
resp, err := client.CreateBranch(ctx, userName, repoName, api.CreateBranchJSONRequestBody{
56-
Name: "feat/aaaaaaaaaaaaaaaaa",
59+
Name: "feat/" + strings.Repeat("a", controller.MaxBranchNameLength),
5760
Source: "main",
5861
})
5962
convey.So(err, convey.ShouldBeNil)

0 commit comments

Comments
 (0)