Skip to content

Commit 7c06efc

Browse files
authored
Merge pull request #80 from jiaozifs/fix/many_fix
Fix/many fix
2 parents e4e812c + 3e60a3d commit 7c06efc

34 files changed

+2128
-834
lines changed

api/jiaozifs.gen.go

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

api/swagger.yml

Lines changed: 104 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ components:
202202
properties:
203203
description:
204204
type: string
205+
head:
206+
type: string
205207
RepositoryList:
206208
type: object
207209
required:
@@ -355,6 +357,31 @@ components:
355357
type: string
356358
is_dir:
357359
type: boolean
360+
FullTreeEntry:
361+
type: object
362+
required:
363+
- name
364+
- hash
365+
- is_dir
366+
- size
367+
- created_at
368+
- updated_at
369+
properties:
370+
name:
371+
type: string
372+
hash:
373+
type: string
374+
is_dir:
375+
type: boolean
376+
size:
377+
type: integer
378+
format: int64
379+
created_at:
380+
type: string
381+
format: date-time
382+
updated_at:
383+
type: string
384+
format: date-time
358385
TreeNode:
359386
type: object
360387
required:
@@ -1005,26 +1032,46 @@ paths:
10051032
description: Unauthorized
10061033
403:
10071034
description: Forbidden
1035+
1036+
/wip/{owner}/{repository}/revert:
1037+
parameters:
1038+
- in: path
1039+
name: repository
1040+
required: true
1041+
schema:
1042+
type: string
1043+
- in: path
1044+
name: owner
1045+
required: true
1046+
schema:
1047+
type: string
1048+
- in: query
1049+
name: refName
1050+
description: ref name
1051+
required: true
1052+
schema:
1053+
type: string
1054+
- in: query
1055+
name: pathPrefix
1056+
description: prefix of path
1057+
schema:
1058+
type: string
10081059
post:
10091060
tags:
10101061
- wip
1011-
operationId: createWip
1012-
summary: create working in process
1062+
operationId: revertWipChanges
1063+
summary: revert changes in working in process, empty path will revert all
10131064
responses:
1014-
201:
1015-
description: working in process created
1016-
content:
1017-
application/json:
1018-
schema:
1019-
$ref: "#/components/schemas/Wip"
1065+
200:
1066+
description: success to revert wip
10201067
400:
10211068
description: ValidationError
10221069
401:
10231070
description: Unauthorized
10241071
403:
10251072
description: Forbidden
1026-
502:
1027-
description: internal server error
1073+
500:
1074+
description: Server Internal Error
10281075

10291076
/wip/{owner}/{repository}/changes:
10301077
parameters:
@@ -1195,7 +1242,7 @@ paths:
11951242
schema:
11961243
type: array
11971244
items:
1198-
$ref: "#/components/schemas/TreeEntry"
1245+
$ref: "#/components/schemas/FullTreeEntry"
11991246
400:
12001247
description: ValidationError
12011248
401:
@@ -1225,8 +1272,8 @@ paths:
12251272
get:
12261273
tags:
12271274
- commit
1228-
operationId: getCommitDiff
1229-
summary: get commit differences
1275+
operationId: compareCommit
1276+
summary: compare two commit
12301277
parameters:
12311278
- in: query
12321279
name: path
@@ -1250,6 +1297,50 @@ paths:
12501297
503:
12511298
description: server internal error
12521299

1300+
/repos/{owner}/{repository}/changes/{commit_id}:
1301+
parameters:
1302+
- in: path
1303+
name: owner
1304+
required: true
1305+
schema:
1306+
type: string
1307+
- in: path
1308+
name: repository
1309+
required: true
1310+
schema:
1311+
type: string
1312+
- in: path
1313+
name: commit_id
1314+
required: true
1315+
schema:
1316+
type: string
1317+
get:
1318+
tags:
1319+
- commit
1320+
operationId: getCommitChanges
1321+
summary: get changes in commit
1322+
parameters:
1323+
- in: query
1324+
name: path
1325+
description: specific path, if not specific return entries in root
1326+
required: false
1327+
schema:
1328+
type: string
1329+
responses:
1330+
200:
1331+
description: commit diff
1332+
content:
1333+
application/json:
1334+
schema:
1335+
type: array
1336+
items:
1337+
$ref: "#/components/schemas/Change"
1338+
400:
1339+
description: ValidationError
1340+
401:
1341+
description: Unauthorized
1342+
503:
1343+
description: server internal error
12531344
/repos/{owner}/{repository}/commits:
12541345
parameters:
12551346
- in: path

controller/branch_ctl.go

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import (
77
"net/http"
88
"regexp"
99
"strings"
10-
"time"
10+
11+
"github.com/jiaozifs/jiaozifs/block/params"
12+
13+
"github.com/jiaozifs/jiaozifs/versionmgr"
1114

1215
"github.com/jiaozifs/jiaozifs/api"
1316
"github.com/jiaozifs/jiaozifs/auth"
1417
"github.com/jiaozifs/jiaozifs/models"
1518
"github.com/jiaozifs/jiaozifs/utils"
16-
"github.com/jiaozifs/jiaozifs/utils/hash"
1719
"go.uber.org/fx"
1820
)
1921

@@ -36,16 +38,22 @@ func CheckBranchName(name string) error {
3638
return fmt.Errorf("branch format must be <name> or <name>/<name>")
3739
}
3840

39-
if !branchNameRegex.Match([]byte(seg[0])) || !branchNameRegex.Match([]byte(seg[1])) {
41+
if !branchNameRegex.Match([]byte(seg[0])) {
4042
return fmt.Errorf("branch name must be combination of number and letter or combine with '/'")
4143
}
44+
if len(seg) > 2 {
45+
if !branchNameRegex.Match([]byte(seg[1])) {
46+
return fmt.Errorf("branch name must be combination of number and letter or combine with '/'")
47+
}
48+
}
4249
return nil
4350
}
4451

4552
type BranchController struct {
4653
fx.In
4754

48-
Repo models.IRepo
55+
Repo models.IRepo
56+
PublicStorageConfig params.AdapterConfig
4957
}
5058

5159
func (bct BranchController) ListBranches(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request, ownerName string, repositoryName string, params api.ListBranchesParams) {
@@ -148,42 +156,31 @@ func (bct BranchController) CreateBranch(ctx context.Context, w *api.JiaozifsRes
148156
return
149157
}
150158

151-
//check exit
152-
_, err = bct.Repo.BranchRepo().Get(ctx, models.NewGetBranchParams().SetName(body.Name).SetRepositoryID(repository.ID))
153-
if err == nil {
154-
w.BadRequest(fmt.Sprintf("%s already exit", body.Name))
155-
return
156-
}
157-
if err != nil && !errors.Is(err, models.ErrNotFound) {
158-
w.Error(err)
159-
return
160-
}
161159
//get source branch
162160
sourceBranch, err := bct.Repo.BranchRepo().Get(ctx, models.NewGetBranchParams().SetName(body.Source).SetRepositoryID(repository.ID))
163161
if err != nil && !errors.Is(err, models.ErrNotFound) {
164162
w.Error(err)
165163
return
166164
}
167165

168-
commitHash := hash.EmptyHash
169-
if sourceBranch != nil {
170-
commitHash = sourceBranch.CommitHash
166+
workRepo, err := versionmgr.NewWorkRepositoryFromConfig(ctx, operator, repository, bct.Repo, bct.PublicStorageConfig)
167+
if err != nil {
168+
w.Error(err)
169+
return
171170
}
172171

173-
// Create branch
174-
newBranch := &models.Branch{
175-
RepositoryID: repository.ID,
176-
CommitHash: commitHash,
177-
Name: body.Name,
178-
CreatorID: operator.ID,
179-
CreatedAt: time.Now(),
180-
UpdatedAt: time.Now(),
172+
err = workRepo.CheckOut(ctx, versionmgr.InCommit, sourceBranch.CommitHash.Hex())
173+
if err != nil {
174+
w.Error(err)
175+
return
181176
}
182-
newBranch, err = bct.Repo.BranchRepo().Insert(ctx, newBranch)
177+
178+
newBranch, err := workRepo.CreateBranch(ctx, body.Name)
183179
if err != nil {
184180
w.Error(err)
185181
return
186182
}
183+
187184
w.JSON(api.Branch{
188185
CommitHash: newBranch.CommitHash.Hex(),
189186
CreatedAt: newBranch.CreatedAt,
@@ -221,14 +218,21 @@ func (bct BranchController) DeleteBranch(ctx context.Context, w *api.JiaozifsRes
221218
return
222219
}
223220

224-
// Delete branch
225-
affectedRows, err := bct.Repo.BranchRepo().Delete(ctx, models.NewDeleteBranchParams().SetName(params.RefName).SetRepositoryID(repository.ID))
221+
workRepo, err := versionmgr.NewWorkRepositoryFromConfig(ctx, operator, repository, bct.Repo, bct.PublicStorageConfig)
226222
if err != nil {
227223
w.Error(err)
228224
return
229225
}
230-
if affectedRows == 0 {
231-
w.NotFound()
226+
227+
err = workRepo.CheckOut(ctx, versionmgr.InBranch, params.RefName)
228+
if err != nil {
229+
w.Error(err)
230+
return
231+
}
232+
233+
err = workRepo.DeleteBranch(ctx)
234+
if err != nil {
235+
w.Error(err)
232236
return
233237
}
234238
w.OK()

controller/commit_ctl.go

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (commitCtl CommitController) GetEntriesInRef(ctx context.Context, w *api.Ji
106106
w.JSON(treeEntry)
107107
}
108108

109-
func (commitCtl CommitController) GetCommitDiff(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request, ownerName string, repositoryName string, basehead string, params api.GetCommitDiffParams) {
109+
func (commitCtl CommitController) CompareCommit(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request, ownerName string, repositoryName string, basehead string, params api.CompareCommitParams) {
110110
operator, err := auth.GetOperator(ctx)
111111
if err != nil {
112112
w.Error(err)
@@ -154,35 +154,63 @@ func (commitCtl CommitController) GetCommitDiff(ctx context.Context, w *api.Jiao
154154
return
155155
}
156156

157-
changes, err := workRepo.DiffCommit(ctx, toCommitHash)
157+
changes, err := workRepo.DiffCommit(ctx, toCommitHash, utils.StringValue(params.Path))
158158
if err != nil {
159159
w.Error(err)
160160
return
161161
}
162162

163-
path := versionmgr.CleanPath(utils.StringValue(params.Path))
164-
var changesResp []api.Change
165-
err = changes.ForEach(func(change versionmgr.IChange) error {
166-
action, err := change.Action()
167-
if err != nil {
168-
return err
169-
}
170-
fullPath := change.Path()
171-
if strings.HasPrefix(fullPath, path) {
172-
apiChange := api.Change{
173-
Action: api.ChangeAction(action),
174-
Path: fullPath,
175-
}
176-
if change.From() != nil {
177-
apiChange.BaseHash = utils.String(hex.EncodeToString(change.From().Hash()))
178-
}
179-
if change.To() != nil {
180-
apiChange.ToHash = utils.String(hex.EncodeToString(change.To().Hash()))
181-
}
182-
changesResp = append(changesResp, apiChange)
183-
}
184-
return nil
185-
})
163+
changesResp, err := changesToDTO(changes)
164+
if err != nil {
165+
w.Error(err)
166+
return
167+
}
168+
w.JSON(changesResp)
169+
}
170+
171+
func (commitCtl CommitController) GetCommitChanges(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request, ownerName string, repositoryName string, commitID string, params api.GetCommitChangesParams) {
172+
operator, err := auth.GetOperator(ctx)
173+
if err != nil {
174+
w.Error(err)
175+
return
176+
}
177+
178+
owner, err := commitCtl.Repo.UserRepo().Get(ctx, models.NewGetUserParams().SetName(ownerName))
179+
if err != nil {
180+
w.Error(err)
181+
return
182+
}
183+
184+
repository, err := commitCtl.Repo.RepositoryRepo().Get(ctx, models.NewGetRepoParams().SetName(repositoryName).SetOwnerID(owner.ID))
185+
if err != nil {
186+
w.Error(err)
187+
return
188+
}
189+
190+
if operator.ID != owner.ID { //todo check permission
191+
w.Forbidden()
192+
return
193+
}
194+
195+
workRepo, err := versionmgr.NewWorkRepositoryFromConfig(ctx, operator, repository, commitCtl.Repo, commitCtl.PublicStorageConfig)
196+
if err != nil {
197+
w.Error(err)
198+
return
199+
}
200+
201+
err = workRepo.CheckOut(ctx, versionmgr.InCommit, commitID)
202+
if err != nil {
203+
w.Error(err)
204+
return
205+
}
206+
207+
changes, err := workRepo.GetCommitChanges(ctx, utils.StringValue(params.Path))
208+
if err != nil {
209+
w.Error(err)
210+
return
211+
}
212+
213+
changesResp, err := changesToDTO(changes)
186214
if err != nil {
187215
w.Error(err)
188216
return

0 commit comments

Comments
 (0)